Skip to content

Commit

Permalink
Rename example scripts, reformat with Black (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomplus authored Mar 1, 2024
1 parent a09b62e commit 272076b
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 79 deletions.
5 changes: 2 additions & 3 deletions examples/create_deployment_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ async def main():
k8s_client = client.ApiClient()
await utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
k8s_api = client.ExtensionsV1beta1Api(k8s_client)
deps = await k8s_api.read_namespaced_deployment("nginx-deployment",
"default")
deps = await k8s_api.read_namespaced_deployment("nginx-deployment", "default")
print("Deployment {0} created".format(deps.metadata.name))


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
6 changes: 5 additions & 1 deletion examples/dynamic-client/accept_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ async def main():
api = await client.resources.get(api_version="v1", kind="Node")

# Creating a custom header
params = {'header_params': {'Accept': 'application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io'}}
params = {
"header_params": {
"Accept": "application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io"
}
}

resp = await api.get(**params)

Expand Down
30 changes: 22 additions & 8 deletions examples/dynamic-client/cluster_scoped_custom_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ async def main():
"fqdn": {"type": "string"},
"tls": {
"properties": {
"secretName": {"type": "string"}
"secretName": {
"type": "string"
}
},
"type": "object",
},
Expand Down Expand Up @@ -100,7 +102,9 @@ async def main():
"\n[INFO] custom resource definition `ingressroutes.apps.example.com` created\n"
)
print("SCOPE\t\tNAME")
print(f"{crd_creation_response.spec.scope}\t\t{crd_creation_response.metadata.name}")
print(
f"{crd_creation_response.spec.scope}\t\t{crd_creation_response.metadata.name}"
)

# Fetching the "ingressroutes" CRD api

Expand Down Expand Up @@ -157,25 +161,35 @@ async def main():
ingress_routes_list = await ingressroute_api.get()
print("NAME\t\t\t\tFQDN\t\tTLS\t\t\t\tSTRATEGY")
for item in ingress_routes_list.items:
print(f"{item.metadata.name}\t{item.spec.virtualhost.fqdn}\t{item.spec.virtualhost.tls}\t"
f"{item.spec.strategy}")
print(
f"{item.metadata.name}\t{item.spec.virtualhost.fqdn}\t{item.spec.virtualhost.tls}\t"
f"{item.spec.strategy}"
)

# Patching the ingressroutes custom resources

ingressroute_manifest_first["spec"]["strategy"] = "Random"
ingressroute_manifest_second["spec"]["strategy"] = "WeightedLeastRequest"

await ingressroute_api.patch(body=ingressroute_manifest_first, content_type="application/merge-patch+json")
await ingressroute_api.patch(body=ingressroute_manifest_second, content_type="application/merge-patch+json")
await ingressroute_api.patch(
body=ingressroute_manifest_first,
content_type="application/merge-patch+json",
)
await ingressroute_api.patch(
body=ingressroute_manifest_second,
content_type="application/merge-patch+json",
)

print(
"\n[INFO] custom resources `ingress-route-*` patched to update the strategy\n"
)
patched_ingress_routes_list = await ingressroute_api.get()
print("NAME\t\t\t\tFQDN\t\t\tTLS\t\t\tSTRATEGY")
for item in patched_ingress_routes_list.items:
print(f"{item.metadata.name}\t{item.spec.virtualhost.fqdn}\t{item.spec.virtualhost.tls}\t"
f"{item.spec.strategy}")
print(
f"{item.metadata.name}\t{item.spec.virtualhost.fqdn}\t{item.spec.virtualhost.tls}\t"
f"{item.spec.strategy}"
)

# Deleting the ingressroutes custom resources

Expand Down
12 changes: 8 additions & 4 deletions examples/dynamic-client/deployment_rolling_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ async def main():
deployment_created = await api.get(name=name, namespace="default")

print("NAMESPACE\tNAME\t\t\t\tREVISION\t\t\t\t\t\t\t\t\t\tRESTARTED-AT")
print(f"{deployment_created.metadata.namespace}\t\t{deployment_created.metadata.name}\t"
f"{deployment_created.metadata.annotations}\t\t{deployment_created.spec.template.metadata.annotations}")
print(
f"{deployment_created.metadata.namespace}\t\t{deployment_created.metadata.name}\t"
f"{deployment_created.metadata.annotations}\t\t{deployment_created.spec.template.metadata.annotations}"
)

# Patching the `spec.template.metadata` section to add `kubectl.kubernetes.io/restartedAt` annotation
# In order to perform a rolling restart on the deployment `nginx-deployment`
Expand All @@ -94,8 +96,10 @@ async def main():

print("\n[INFO] deployment `nginx-deployment` restarted\n")
print("NAMESPACE\tNAME\t\t\t\tREVISION\t\t\t\t\t\t\t\t\tRESTARTED-AT")
print(f"{deployment_patched.metadata.namespace}\t\t{deployment_patched.metadata.name}\t"
f"{deployment_patched.metadata.annotations}\t{deployment_patched.spec.template.metadata.annotations}")
print(
f"{deployment_patched.metadata.namespace}\t\t{deployment_patched.metadata.name}\t"
f"{deployment_patched.metadata.annotations}\t{deployment_patched.spec.template.metadata.annotations}"
)

# Deleting deployment `nginx-deployment` from the `default` namespace

Expand Down
22 changes: 15 additions & 7 deletions examples/dynamic-client/namespaced_custom_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ async def main():
"fqdn": {"type": "string"},
"tls": {
"properties": {
"secretName": {"type": "string"}
"secretName": {
"type": "string"
}
},
"type": "object",
},
Expand Down Expand Up @@ -172,8 +174,12 @@ async def main():
},
}

await ingressroute_api.create(body=ingressroute_manifest_first, namespace=namespace_first)
await ingressroute_api.create(body=ingressroute_manifest_second, namespace=namespace_second)
await ingressroute_api.create(
body=ingressroute_manifest_first, namespace=namespace_first
)
await ingressroute_api.create(
body=ingressroute_manifest_second, namespace=namespace_second
)
print("\n[INFO] custom resources `ingress-route-*` created\n")

# Listing the `ingress-route-*` custom resources
Expand All @@ -192,7 +198,7 @@ async def main():
item["metadata"]["namespace"],
item["spec"]["virtualhost"]["fqdn"],
item["spec"]["virtualhost"]["tls"],
item["spec"]["strategy"]
item["spec"]["strategy"],
)
)

Expand All @@ -202,10 +208,12 @@ async def main():
ingressroute_manifest_second["spec"]["strategy"] = "WeightedLeastRequest"

await ingressroute_api.patch(
body=ingressroute_manifest_first, content_type="application/merge-patch+json"
body=ingressroute_manifest_first,
content_type="application/merge-patch+json",
)
await ingressroute_api.patch(
body=ingressroute_manifest_second, content_type="application/merge-patch+json"
body=ingressroute_manifest_second,
content_type="application/merge-patch+json",
)

print(
Expand All @@ -227,7 +235,7 @@ async def main():
item["metadata"]["namespace"],
item["spec"]["virtualhost"]["fqdn"],
item["spec"]["virtualhost"]["tls"],
item["spec"]["strategy"]
item["spec"]["strategy"],
)
)

Expand Down
12 changes: 7 additions & 5 deletions examples/dynamic-client/replication_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ async def main():
}

# Creating replication-controller `frontend-replication-controller` in the `default` namespace
await api.create(
body=replication_controller_manifest, namespace="default"
)
await api.create(body=replication_controller_manifest, namespace="default")

print("\n[INFO] replication-controller `frontend-replication-controller` created\n")
print(
"\n[INFO] replication-controller `frontend-replication-controller` created\n"
)

# Listing replication-controllers in the `default` namespace
replication_controller_created = await api.get(name=name, namespace="default")
Expand All @@ -81,7 +81,9 @@ async def main():
# Deleting replication-controller `frontend-service` from the `default` namespace
await api.delete(name=name, body={}, namespace="default")

print("[INFO] replication-controller `frontend-replication-controller` deleted\n")
print(
"[INFO] replication-controller `frontend-replication-controller` deleted\n"
)


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion examples/dynamic-client/request_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ async def main():
# Client-side timeout to 60 seconds

configmap_list = await api.get(
name=configmap_name, namespace="default", label_selector="foo=bar", _request_time=60
name=configmap_name,
namespace="default",
label_selector="foo=bar",
_request_time=60,
)

print(f"NAME:\n{configmap_list.metadata.name}\n")
Expand Down
10 changes: 8 additions & 2 deletions examples/dynamic-client/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ async def main():
service_manifest = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"labels": {"name": name}, "name": name, "resourceversion": "v1"},
"metadata": {
"labels": {"name": name},
"name": name,
"resourceversion": "v1",
},
"spec": {
"ports": [
{"name": "port", "port": 80, "protocol": "TCP", "targetPort": 80}
Expand Down Expand Up @@ -71,7 +75,9 @@ async def main():
{"name": "new", "port": 8080, "protocol": "TCP", "targetPort": 8080}
]

service_patched = await api.patch(body=service_manifest, name=name, namespace="default")
service_patched = await api.patch(
body=service_manifest, name=name, namespace="default"
)

print("\n[INFO] service `frontend-service` patched\n")
print("%s\t%s\t\t\t%s" % ("NAMESPACE", "NAME", "PORTS"))
Expand Down
2 changes: 1 addition & 1 deletion examples/in_cluster_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def main():
await asyncio.sleep(10)


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
2 changes: 1 addition & 1 deletion examples/example1.py → examples/list_pods.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def main():
print(i.status.pod_ip, i.metadata.namespace, i.metadata.name)


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
30 changes: 18 additions & 12 deletions examples/example3.py → examples/pod_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,39 @@ async def main():
ret = await v1.list_pod_for_all_namespaces()

for i in ret.items:
if i.metadata.name.startswith('busybox'):
if i.metadata.name.startswith("busybox"):
pod = i.metadata.name
namespace = i.metadata.namespace
print('Buxy box', pod, 'namespace', namespace)
print("Buxy box", pod, "namespace", namespace)
break
else:
print('Busybox not found !')
print("Busybox not found !")
return

v1_ws = client.CoreV1Api(api_client=WsApiClient())

exec_command = [
'/bin/sh',
'-c',
'echo This message goes to stderr >&2; echo This message goes to stdout']

resp = v1_ws.connect_get_namespaced_pod_exec(pod, namespace,
command=exec_command,
stderr=True, stdin=False,
stdout=True, tty=False)
"/bin/sh",
"-c",
"echo This message goes to stderr >&2; echo This message goes to stdout",
]

resp = v1_ws.connect_get_namespaced_pod_exec(
pod,
namespace,
command=exec_command,
stderr=True,
stdin=False,
stdout=True,
tty=False,
)

ret = await resp

print("Response: ", ret)


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Loading

0 comments on commit 272076b

Please sign in to comment.