Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the kubernetes-client for executing "kubectl apply"

I have a python script which basically runs the following three commands:

kubectl apply -f class.yaml
kubectl apply -f rbac.yaml
kubectl apply -f deployment-arm.yaml

I want to use the kubernetes-client written in python to replace it. My current code, loads the there yaml files (using pyyaml), edits them a bit, inserts into a file and use the command line kubectl to execute those three commands. Some of the code:

# load files, edit them and dump into new files, part ...
result = run(['kubectl', 'apply', '-f', class_file_path])
# status check part ...
result = run(['kubectl', 'apply', '-f', rbac_file_path])
# status check part ...
result = run(['kubectl', 'apply', '-f', deployment_file_path])
# status check part ...

What I want to do: Replace those three commands with the python kubernetes-client. Reading the docs and seeing the topic, I came across with the create_namespaced_deployment method which I think I need to use for the deployment_file_path file. But I can't seem to figure out what I need to do with the two other files.

Assuming that I already loaded the three yaml files (using pyyaml) and edited them (without dumping into new files) and now you have free yaml dicts deployment_dict, class_dict, and rbac_dict, How can I use the client to execute the three above methods?

EDIT: BTW if it's not possible to pass the three dicts, I could just dump them into files again but I want to use the python client instead of the kubectl. How to do it?

like image 640
vesii Avatar asked Dec 16 '20 13:12

vesii


1 Answers

There is a separate function for every object and action:

from kubernetes import client, config
import yaml

body = yaml.safe_load("my_deployment.yml")
config.load_kube_config()
apps_api = client.AppsV1Api()
apps_api.create_namespaced_deployment(body=body, namespace="default")
apps_api.replace_namespaced_deployment(body=body, namespace="default")
apps_api.patch_namespaced_deployment(body=body, namespace="default")
apps_api.delete_namespaced_deployment(body=body, namespace="default")

body = yaml.safe_load("my_cluster_role.yml")
rbac_api = client.RbacAuthorizationV1Api()
rbac_api.create_cluster_role(body=body)
rbac_api.patch_cluster_role(body=body)
rbac_api.replace_cluster_role(body=body)
rbac_api.delete_cluster_role(body=body)

# And so on

When you use kubectl apply you don't care if the object already exists, what API to use, which method to apply, but with the client you will have to. As you see from the example, you need to:

  1. Load kube-config.
  2. Select the right API to use (create an object only after you loaded config).
  3. Select the method you want to use. Note that create_something will not work if the something already exists.

Thanks to the strict naming pattern it's easy to get the required function from client using

getattr(some_k8s_api, f"{verb}_{namespaced_or_not}_{kind.lower()}")

I recommend you to go through the examples that the library provides, they really are great to learn the thing.

like image 102
anemyte Avatar answered Oct 11 '22 21:10

anemyte