Skip to content

Instantly share code, notes, and snippets.

@djkonro
Last active April 16, 2020 22:41
Show Gist options
  • Save djkonro/1b46f583107311ef3a144759ec26180f to your computer and use it in GitHub Desktop.
Save djkonro/1b46f583107311ef3a144759ec26180f to your computer and use it in GitHub Desktop.
'''
intro.py
Managing kubernetes objects using common resource operations with the python client
-----------------------------------------------------------------------------------------------
Some of this operations include;
- **`create_xxxx`** : create a resource object. Ex **`create_namespaced_pod`** and **`create_namespaced_deployment`**, for creation of pods and deployments respectively. This performs operations similar to `kubectl create`.
- **`read_xxxx`** : read the specified resource object. Ex **`read_namespaced_pod`** and **`read_namespaced_deployment`**, to read pods and deployments respectively. This performs operations similar to **`kubectl describe`**.
- **`list_xxxx`** : retrieve all resource objects of a specific type. **`list_namespaced_pod`** and **`list_namespaced_deployment`**, to list pods and deployments respectively. This performs operations similar to **`kubectl get`**.
- **`patch_xxxx`** : apply a change to a specific field. Ex **`patch_namespaced_pod`** and **`patch_namespaced_deployment`**, to update pods and deployments respectively. This performs operations similar to **`kubectl patch`**, **`kubectl label`**, **`kubectl annotate`** etc.
- **`replace_xxxx`** : replacing a resource object will update the resource by replacing the existing spec with the provided one. Ex **`replace_namespaced_pod`** and **`replace_namespaced_deployment`**, to update pods and deployments respectively, by creating new replacements of the entire object. This performs operations similar to **`kubectl rolling-update`**, **`kubectl apply`** and **`kubectl replace`**.
- **`delete_xxxx`** : delete a resource. This performs operations similar to **`kubectl delete`**.
For Futher information see the in Documentation for API Endpoints section in https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/README.md
'''
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
pod = client.V1Pod()
spec = client.V1PodSpec()
name = "busybox"
pod.metadata=client.V1ObjectMeta(name=name)
container=client.V1Container()
container.image="busybox:1.26.1"
container.name = name
spec.containers = [container]
pod.spec = spec
#Use list_xxxx command for pods, to list preexisting pods.
ret = v1.list_namespaced_pod(namespace="default")
for i in ret.items:
print("%s %s %s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
#Create pod using create_xxxx command for pods .
v1.create_namespaced_pod(namespace="default",body=pod)
#Use read_xxxx command for pods, to display the detailed state of the created pod resource.
v1.read_namespaced_pod(namespace="default",name=name)
#Use patch_xxxx command for pods, to make specific update to the pod.
pod.metadata.labels = {"key": "value"}
v1.patch_namespaced_pod(name=name, namespace="default", body=pod)
#Use replace_xxxx command for pods, to update pod with a completely new version of the object.
pod.spec.containers[0].image = 'busybox:1.26.2'
v1.replace_namespaced_pod(name=name, namespace="default", body=pod)
#Use delete_xxxx command for pods, to delete created pod
v1.delete_namespaced_pod(name=name, namespace="default", body=client.V1DeleteOptions())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment