Skip to content

Commit fa40565

Browse files
correct CRD's apiVersion, manifest & naming typos
1 parent 553f6ae commit fa40565

File tree

2 files changed

+182
-60
lines changed

2 files changed

+182
-60
lines changed

examples/dynamic-client/cluster_scoped_custom_resource.py

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
"""
1616
This example demonstrates the following:
17-
- Creation of a cluster scoped custom resource definition (CRD) using dynamic-client
18-
- Creation of custom resources (CR) using the above created CRD
17+
- Creation of a custom resource definition (CRD) using dynamic-client
18+
- Creation of cluster scoped custom resources (CR) using the above created CRD
1919
- List, patch(update), delete the custom resources
20-
- Delete the custom resource defintion
20+
- Delete the custom resource defintion (CRD)
2121
"""
2222

2323
from kubernetes import config, dynamic
@@ -34,40 +34,71 @@ def main():
3434

3535
# fetching the custom resource definition (CRD) api
3636
crd_api = client.resources.get(
37-
api_version="apiextensions.k8s.io/v1beta1", kind="CustomResourceDefinition"
37+
api_version="apiextensions.k8s.io/v1", kind="CustomResourceDefinition"
3838
)
3939

4040
# Creating a Namespaced CRD named "ingressroutes.apps.example.com"
4141
name = "ingressroutes.apps.example.com"
4242

4343
crd_manifest = {
44-
"apiVersion": "apiextensions.k8s.io/v1beta1",
44+
"apiVersion": "apiextensions.k8s.io/v1",
4545
"kind": "CustomResourceDefinition",
4646
"metadata": {
4747
"name": name,
4848
},
4949
"spec": {
5050
"group": "apps.example.com",
51+
"versions": [
52+
{
53+
"name": "v1",
54+
"schema": {
55+
"openAPIV3Schema": {
56+
"properties": {
57+
"spec": {
58+
"properties": {
59+
"strategy": {"type": "string"},
60+
"virtualhost": {
61+
"properties": {
62+
"fqdn": {"type": "string"},
63+
"tls": {
64+
"properties": {
65+
"secretName": {"type": "string"}
66+
},
67+
"type": "object",
68+
},
69+
},
70+
"type": "object",
71+
},
72+
},
73+
"type": "object",
74+
}
75+
},
76+
"type": "object",
77+
}
78+
},
79+
"served": True,
80+
"storage": True,
81+
}
82+
],
83+
"scope": "Cluster",
5184
"names": {
52-
"kind": "IngressRoute",
53-
"listKind": "IngressRouteList",
5485
"plural": "ingressroutes",
86+
"listKind": "IngressRouteList",
5587
"singular": "ingressroute",
88+
"kind": "IngressRoute",
89+
"shortNames": ["ir"],
5690
},
57-
"scope": "Cluster",
58-
"version": "v1",
59-
"subresources": {"status": {}},
6091
},
6192
}
6293

63-
crd_creation_respone = crd_api.create(crd_manifest)
94+
crd_creation_response = crd_api.create(crd_manifest)
6495
print(
6596
"\n[INFO] custom resource definition `ingressroutes.apps.example.com` created\n"
6697
)
6798
print("%s\t\t%s" % ("SCOPE", "NAME"))
6899
print(
69100
"%s\t\t%s\n"
70-
% (crd_creation_respone.spec.scope, crd_creation_respone.metadata.name)
101+
% (crd_creation_response.spec.scope, crd_creation_response.metadata.name)
71102
)
72103

73104
# Fetching the "ingressroutes" CRD api
@@ -86,13 +117,19 @@ def main():
86117

87118
# Creating a custom resource (CR) `ingress-route-*`, using the above CRD `ingressroutes.apps.example.com`
88119

89-
ingressroute_manifest_one = {
120+
ingressroute_manifest_first = {
90121
"apiVersion": "apps.example.com/v1",
91122
"kind": "IngressRoute",
92123
"metadata": {
93-
"name": "ingress-route-one",
124+
"name": "ingress-route-first",
125+
},
126+
"spec": {
127+
"virtualhost": {
128+
"fqdn": "www.google.com",
129+
"tls": {"secretName": "google-tls"},
130+
},
131+
"strategy": "RoundRobin",
94132
},
95-
"spec": {},
96133
}
97134

98135
ingressroute_manifest_second = {
@@ -101,41 +138,65 @@ def main():
101138
"metadata": {
102139
"name": "ingress-route-second",
103140
},
104-
"spec": {},
141+
"spec": {
142+
"virtualhost": {
143+
"fqdn": "www.yahoo.com",
144+
"tls": {"secretName": "yahoo-tls"},
145+
},
146+
"strategy": "RoundRobin",
147+
},
105148
}
106149

107-
ingressroute_api.create(body=ingressroute_manifest_one)
150+
ingressroute_api.create(body=ingressroute_manifest_first)
108151
ingressroute_api.create(body=ingressroute_manifest_second)
109152
print("\n[INFO] custom resources `ingress-route-*` created\n")
110153

111154
# Listing the `ingress-route-*` custom resources
112155

113156
ingress_routes_list = ingressroute_api.get()
114-
print("%s\t\t\t\t%s" % ("NAME", "SPEC"))
157+
print("%s\t\t\t%s\t\t%s\t\t\t\t%s" % ("NAME", "FQDN", "TLS", "STRATEGY"))
115158
for item in ingress_routes_list.items:
116-
print("%s\t\t%s" % (item.metadata.name, item.spec))
159+
print(
160+
"%s\t%s\t%s\t%s"
161+
% (
162+
item.metadata.name,
163+
item.spec.virtualhost.fqdn,
164+
item.spec.virtualhost.tls,
165+
item.spec.strategy,
166+
)
167+
)
117168

118169
# Patching the ingressroutes custom resources
119170

120-
ingressroute_manifest_one["spec"]["entrypoints"] = ["websecure"]
121-
ingressroute_manifest_second["spec"]["entrypoints"] = ["web"]
171+
ingressroute_manifest_first["spec"]["strategy"] = "Random"
172+
ingressroute_manifest_second["spec"]["strategy"] = "WeightedLeastRequest"
122173

123-
patch_ingressroute_one = ingressroute_api.patch(
124-
body=ingressroute_manifest_one, content_type="application/merge-patch+json"
174+
patch_ingressroute_first = ingressroute_api.patch(
175+
body=ingressroute_manifest_first, content_type="application/merge-patch+json"
125176
)
126177
patch_ingressroute_second = ingressroute_api.patch(
127178
body=ingressroute_manifest_second, content_type="application/merge-patch+json"
128179
)
129180

130-
print("\n[INFO] custom resources `ingress-route-*` patched\n")
181+
print(
182+
"\n[INFO] custom resources `ingress-route-*` patched to update the strategy\n"
183+
)
131184
patched_ingress_routes_list = ingressroute_api.get()
132-
print("%s\t\t\t\t%s" % ("NAME", "SPEC"))
185+
print("%s\t\t\t%s\t\t%s\t\t\t\t%s" % ("NAME", "FQDN", "TLS", "STRATEGY"))
133186
for item in patched_ingress_routes_list.items:
134-
print("%s\t\t%s" % (item.metadata.name, item.spec))
187+
print(
188+
"%s\t%s\t%s\t%s"
189+
% (
190+
item.metadata.name,
191+
item.spec.virtualhost.fqdn,
192+
item.spec.virtualhost.tls,
193+
item.spec.strategy,
194+
)
195+
)
135196

136197
# Deleting the ingressroutes custom resources
137198

138-
delete_ingressroute_one = ingressroute_api.delete(name="ingress-route-one")
199+
delete_ingressroute_first = ingressroute_api.delete(name="ingress-route-first")
139200
delete_ingressroute_second = ingressroute_api.delete(name="ingress-route-second")
140201

141202
print("\n[INFO] custom resources `ingress-route-*` deleted")

examples/dynamic-client/namespaced_custom_resource.py

Lines changed: 94 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
"""
1616
This example demonstrates the following:
17-
- Creation of a namespaced scoped custom resource definition (CRD) using dynamic-client
18-
- Creation of custom resources (CR) using the above created CRD
17+
- Creation of a custom resource definition (CRD) using dynamic-client
18+
- Creation of namespaced custom resources (CR) using the above created CRD
1919
- List, patch(update), delete the custom resources
20-
- Delete the custom resource defintion
20+
- Delete the custom resource defintion (CRD)
2121
"""
2222

2323
from kubernetes import config, dynamic
@@ -34,30 +34,58 @@ def main():
3434

3535
# fetching the custom resource definition (CRD) api
3636
crd_api = client.resources.get(
37-
api_version="apiextensions.k8s.io/v1beta1", kind="CustomResourceDefinition"
37+
api_version="apiextensions.k8s.io/v1", kind="CustomResourceDefinition"
3838
)
3939

4040
# Creating a Namespaced CRD named "ingressroutes.apps.example.com"
4141
name = "ingressroutes.apps.example.com"
4242

4343
crd_manifest = {
44-
"apiVersion": "apiextensions.k8s.io/v1beta1",
44+
"apiVersion": "apiextensions.k8s.io/v1",
4545
"kind": "CustomResourceDefinition",
46-
"metadata": {
47-
"name": name,
48-
"namespace": "default",
49-
},
46+
"metadata": {"name": name, "namespace": "default"},
5047
"spec": {
5148
"group": "apps.example.com",
49+
"versions": [
50+
{
51+
"name": "v1",
52+
"schema": {
53+
"openAPIV3Schema": {
54+
"properties": {
55+
"spec": {
56+
"properties": {
57+
"strategy": {"type": "string"},
58+
"virtualhost": {
59+
"properties": {
60+
"fqdn": {"type": "string"},
61+
"tls": {
62+
"properties": {
63+
"secretName": {"type": "string"}
64+
},
65+
"type": "object",
66+
},
67+
},
68+
"type": "object",
69+
},
70+
},
71+
"type": "object",
72+
}
73+
},
74+
"type": "object",
75+
}
76+
},
77+
"served": True,
78+
"storage": True,
79+
}
80+
],
81+
"scope": "Namespaced",
5282
"names": {
53-
"kind": "IngressRoute",
54-
"listKind": "IngressRouteList",
5583
"plural": "ingressroutes",
84+
"listKind": "IngressRouteList",
5685
"singular": "ingressroute",
86+
"kind": "IngressRoute",
87+
"shortNames": ["ir"],
5788
},
58-
"scope": "Namespaced",
59-
"version": "v1",
60-
"subresources": {"status": {}},
6189
},
6290
}
6391

@@ -87,14 +115,20 @@ def main():
87115

88116
# Creating a custom resource (CR) `ingress-route-*`, using the above CRD `ingressroutes.apps.example.com`
89117

90-
ingressroute_manifest_one = {
118+
ingressroute_manifest_first = {
91119
"apiVersion": "apps.example.com/v1",
92120
"kind": "IngressRoute",
93121
"metadata": {
94-
"name": "ingress-route-one",
122+
"name": "ingress-route-first",
95123
"namespace": "default",
96124
},
97-
"spec": {},
125+
"spec": {
126+
"virtualhost": {
127+
"fqdn": "www.google.com",
128+
"tls": {"secretName": "google-tls"},
129+
},
130+
"strategy": "RoundRobin",
131+
},
98132
}
99133

100134
ingressroute_manifest_second = {
@@ -104,47 +138,74 @@ def main():
104138
"name": "ingress-route-second",
105139
"namespace": "default",
106140
},
107-
"spec": {},
141+
"spec": {
142+
"virtualhost": {
143+
"fqdn": "www.yahoo.com",
144+
"tls": {"secretName": "yahoo-tls"},
145+
},
146+
"strategy": "RoundRobin",
147+
},
108148
}
109149

110-
ingressroute_api.create(body=ingressroute_manifest_one, namespace="default")
150+
ingressroute_api.create(body=ingressroute_manifest_first, namespace="default")
111151
ingressroute_api.create(body=ingressroute_manifest_second, namespace="default")
112152
print("\n[INFO] custom resources `ingress-route-*` created\n")
113153

114154
# Listing the `ingress-route-*` custom resources
115155

116156
ingress_routes_list = ingressroute_api.get()
117-
print("%s\t\t\t\t%s\t%s" % ("NAME", "NAMESPACE", "SPEC"))
157+
print(
158+
"%s\t\t\t%s\t%s\t\t%s\t\t\t\t%s"
159+
% ("NAME", "NAMESPACE", "FQDN", "TLS", "STRATEGY")
160+
)
118161
for item in ingress_routes_list.items:
119162
print(
120-
"%s\t\t%s\t\t%s" % (item.metadata.name, item.metadata.namespace, item.spec)
163+
"%s\t%s\t\t%s\t%s\t%s"
164+
% (
165+
item.metadata.name,
166+
item.metadata.namespace,
167+
item.spec.virtualhost.fqdn,
168+
item.spec.virtualhost.tls,
169+
item.spec.strategy,
170+
)
121171
)
122172

123173
# Patching the ingressroutes custom resources
124174

125-
ingressroute_manifest_one["spec"]["entrypoints"] = ["websecure"]
126-
ingressroute_manifest_second["spec"]["entrypoints"] = ["web"]
175+
ingressroute_manifest_first["spec"]["strategy"] = "Random"
176+
ingressroute_manifest_second["spec"]["strategy"] = "WeightedLeastRequest"
127177

128-
patch_ingressroute_one = ingressroute_api.patch(
129-
body=ingressroute_manifest_one, content_type="application/merge-patch+json"
178+
patch_ingressroute_first = ingressroute_api.patch(
179+
body=ingressroute_manifest_first, content_type="application/merge-patch+json"
130180
)
131181
patch_ingressroute_second = ingressroute_api.patch(
132182
body=ingressroute_manifest_second, content_type="application/merge-patch+json"
133183
)
134184

135-
print("\n[INFO] custom resources `ingress-route-*` patched\n")
136-
patched_ingress_routes_list = ingressroute_api.get()
137-
print("%s\t\t\t\t%s\t%s" % ("NAME", "NAMESPACE", "SPEC"))
138-
for item in patched_ingress_routes_list.items:
185+
print(
186+
"\n[INFO] custom resources `ingress-route-*` patched to update the strategy\n"
187+
)
188+
ingress_routes_list = ingressroute_api.get()
189+
print(
190+
"%s\t\t\t%s\t%s\t\t%s\t\t\t\t%s"
191+
% ("NAME", "NAMESPACE", "FQDN", "TLS", "STRATEGY")
192+
)
193+
for item in ingress_routes_list.items:
139194
print(
140-
"%s\t\t%s\t\t%s"
141-
% (item.metadata.name, item.metadata.namespace, str(item.spec))
195+
"%s\t%s\t\t%s\t%s\t%s"
196+
% (
197+
item.metadata.name,
198+
item.metadata.namespace,
199+
item.spec.virtualhost.fqdn,
200+
item.spec.virtualhost.tls,
201+
item.spec.strategy,
202+
)
142203
)
143204

144205
# Deleting the ingressroutes custom resources
145206

146-
delete_ingressroute_one = ingressroute_api.delete(
147-
name="ingress-route-one", namespace="default"
207+
delete_ingressroute_first = ingressroute_api.delete(
208+
name="ingress-route-first", namespace="default"
148209
)
149210
delete_ingressroute_second = ingressroute_api.delete(
150211
name="ingress-route-second", namespace="default"

0 commit comments

Comments
 (0)