kubernetes Python API Client: execute full yaml file
Asked Answered
A

3

11

Kubernetes has a very nice official Python API client. The API client assumes that you will be creating individual resources (such as pods, or services) and assumes that you will be using Python objects to compose and create API requests.

However, I'd like to run arbitrary kubernetes YAML files (containing one or more k8s resources) via a Python interface. I was wondering if the Python kubernetes client can be leveraged to apply arbitrary YAML files?

I'm basically looking for the Python equivalent of:

kubectl apply -f some-file-containing-multiple-resources.yaml

I'm looking for something where I can basically load the kubeconfig and apply the yaml via Python in a fairly Pythonic way.

I know I can probably wrap the kubectl command with a Python subprocess call, but I was hoping for something more Pythonic than that and hoped that the core K8s Python client could do something like that. Or, if there is another Python package that does something similar.

Can the Python kubernetes client call arbitrary k8s yaml files and if not, is there something that can?

Thanks for reading - I appreciate any advice you have to offer.

Absorbance answered 19/6, 2019 at 18:36 Comment(0)
R
11

There appears to be examples of this in the examples directory. In particular https://github.com/kubernetes-client/python/blob/6709b753b4ad2e09aa472b6452bbad9f96e264e3/examples/create_deployment_from_yaml.py which does:

from os import path

import yaml

from kubernetes import client, config


def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
        dep = yaml.safe_load(f)
        k8s_beta = client.ExtensionsV1beta1Api()
        resp = k8s_beta.create_namespaced_deployment(
            body=dep, namespace="default")
        print("Deployment created. status='%s'" % str(resp.status))


if __name__ == '__main__':
    main()
Renvoi answered 19/6, 2019 at 22:15 Comment(4)
Thanks @Andy Shinn, good to know you can pass yaml as the api body. However, do you know if you can pass multiple types of objects in the same call (without necessarily knowing the resource type)? Say I want to create a pod and a service from the same yaml file...Absorbance
Digging slightly more reveals another example at github.com/kubernetes-client/python/blob/master/examples/… that uses utils.create_from_yaml() which looks like it handles multiple objects per github.com/kubernetes-client/python/blob/master/kubernetes/…Renvoi
Thank you again @Andy Shinn, I was playing around with this endpoint a bit and it looks like it achieves what I was trying to do. One note to others who may read this: you need to be sure to place the triple dashes at the top of your yaml file and in-between documents, if it contains more than one documentAbsorbance
The link provided by @AndyShinn has been removed from the master. Just pasting one from the history: github.com/kubernetes-client/python/blob/…Otherworld
L
16

You can try using create_from_yaml provided by kubernetes.utils in the following way. This is the multi-resource-definition file

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
    - name: ngnx-container
      image: nginx:latest
      ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
   - port: 8080
     targetPort: 80

Now you can try running the below code and check whether that works out for you or not.

from kubernetes import client, config, utils
config.load_kube_config()
k8s_client = client.ApiClient()
yaml_file = '<location to your multi-resource file>'
utils.create_from_yaml(k8s_client, yaml_file)
Leshalesher answered 6/5, 2020 at 18:28 Comment(2)
I don't get why the need to create a config, if it's not passed as parameter.Inveigle
config.load_kube_config( ) this Loads authentication and cluster information from kube-config file and stores them in kubernetes.client.configuration.Stilt
R
11

There appears to be examples of this in the examples directory. In particular https://github.com/kubernetes-client/python/blob/6709b753b4ad2e09aa472b6452bbad9f96e264e3/examples/create_deployment_from_yaml.py which does:

from os import path

import yaml

from kubernetes import client, config


def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
        dep = yaml.safe_load(f)
        k8s_beta = client.ExtensionsV1beta1Api()
        resp = k8s_beta.create_namespaced_deployment(
            body=dep, namespace="default")
        print("Deployment created. status='%s'" % str(resp.status))


if __name__ == '__main__':
    main()
Renvoi answered 19/6, 2019 at 22:15 Comment(4)
Thanks @Andy Shinn, good to know you can pass yaml as the api body. However, do you know if you can pass multiple types of objects in the same call (without necessarily knowing the resource type)? Say I want to create a pod and a service from the same yaml file...Absorbance
Digging slightly more reveals another example at github.com/kubernetes-client/python/blob/master/examples/… that uses utils.create_from_yaml() which looks like it handles multiple objects per github.com/kubernetes-client/python/blob/master/kubernetes/…Renvoi
Thank you again @Andy Shinn, I was playing around with this endpoint a bit and it looks like it achieves what I was trying to do. One note to others who may read this: you need to be sure to place the triple dashes at the top of your yaml file and in-between documents, if it contains more than one documentAbsorbance
The link provided by @AndyShinn has been removed from the master. Just pasting one from the history: github.com/kubernetes-client/python/blob/…Otherworld
I
2

In addition to what others have recommended, you can do this with Hikaru's load_full_yaml.

What's nice about Hikaru is that:

  1. It's object oriented and Pythonic with functions like Pod.create().
  2. It's autogenerated from the OpenAPI spec so nothing is missing

I evaluated at least five different Kubernetes libraries when building my own open source Python Kubernetes platform and Hikaru was by far the easiest to use.

Infancy answered 30/12, 2021 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.