9

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?

vesii
  • 2,760
  • 4
  • 25
  • 71
  • https://stackoverflow.com/a/59977059/5525824 – Harsh Manvar Dec 16 '20 at 14:13
  • @HarshManvar Hi, thanks for the comment. If it's a problem that they are dicts and not jsons, I am opened for suggestion (like dumping into files again). My main focus of this question is how to imitate the three mentioned commands using the python client. – vesii Dec 16 '20 at 14:19
  • 1
    Isn't that what you are lookign for? https://github.com/kubernetes-client/python/blob/master/kubernetes/utils/create_from_yaml.py – Olesya Bolobova Dec 22 '20 at 23:21

1 Answers1

3

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, etc. With the client library, as you can see from the example above, you need to:

  1. Load kube-config.
  2. Select the right API to use.
  3. Select the method you want to use. Note that create_something will not work if that something already exists.

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

anemyte
  • 17,618
  • 1
  • 24
  • 45
  • How do i get the output of `kubectl get pods` using the above python client for k8s? or any kubectl command for that matter. Is there an object where i can directly pass the corresponding kubectl command and get its respective output? – Mahesh Oct 13 '22 at 11:22
  • @Mahesh I know no such object. However, you can execute `kubectl` binary from python and parse text output, if that's what you seek. – anemyte Oct 13 '22 at 12:11
  • When you say `kubectl binary`, are you taking about `python subprocess`? Because I have tried that and pod is not able to find kubectl command which is why I want to run kubectl commands using python client for k8s. I want to run the command `kubectl get taskruns -l triggers.tekton.dev/triggers-eventid=e54dd948-8ae0-490f-9544-5375252d2b89`. Please let me know if there is any way to get output for the given command? – Mahesh Oct 13 '22 at 12:15
  • 1
    @Mahesh yes, I meant subprocess. If you want to work with `kubectl` binary, you have to ship it along with your python core or otherwise ensure it presence in the environment (container or machine) where you run that python script. Please also note that in order yo use it like `kubectl get something`, the binary has to be on `$PATH`. Otherwise use full path like `/usr/bin/whatever/kubectl get something`. – anemyte Oct 13 '22 at 12:35
  • Update:- I directly got an API from tekton that gives the pipeline status. So wouldn;t need to apply kubectl to our image. Thanks for your help – Mahesh Oct 15 '22 at 11:12