MCPcopy Index your code
hub / github.com/kubernetes-client/python / main

Function main

examples/dynamic-client/service.py:25–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23
24
25def main():
26 # Creating a dynamic client
27 client = dynamic.DynamicClient(
28 api_client.ApiClient(configuration=config.load_kube_config())
29 )
30
31 # fetching the service api
32 api = client.resources.get(api_version="v1", kind="Service")
33
34 name = "frontend-service"
35
36 service_manifest = {
37 "apiVersion": "v1",
38 "kind": "Service",
39 "metadata": {"labels": {"name": name}, "name": name, "resourceversion": "v1"},
40 "spec": {
41 "ports": [
42 {"name": "port", "port": 80, "protocol": "TCP", "targetPort": 80}
43 ],
44 "selector": {"name": name},
45 },
46 }
47
48 # Creating service `frontend-service` in the `default` namespace
49
50 service = api.create(body=service_manifest, namespace="default")
51
52 print("\n[INFO] service `frontend-service` created\n")
53
54 # Listing service `frontend-service` in the `default` namespace
55 service_created = api.get(name=name, namespace="default")
56
57 print("%s\t%s" % ("NAMESPACE", "NAME"))
58 print(
59 "%s\t\t%s\n"
60 % (service_created.metadata.namespace, service_created.metadata.name)
61 )
62
63 # Patching the `spec` section of the `frontend-service`
64
65 service_manifest["spec"]["ports"] = [
66 {"name": "new", "port": 8080, "protocol": "TCP", "targetPort": 8080}
67 ]
68
69 service_patched = api.patch(body=service_manifest, name=name, namespace="default")
70
71 print("\n[INFO] service `frontend-service` patched\n")
72 print("%s\t%s\t\t\t%s" % ("NAMESPACE", "NAME", "PORTS"))
73 print(
74 "%s\t\t%s\t%s\n"
75 % (
76 service_patched.metadata.namespace,
77 service_patched.metadata.name,
78 service_patched.spec.ports,
79 )
80 )
81
82 # Deleting service `frontend-service` from the `default` namespace

Callers 1

service.pyFile · 0.70

Calls 4

getMethod · 0.45
createMethod · 0.45
patchMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected