Search code examples
pythonapikubernetesclientyaml

kubernetes Python API Client: execute full yaml file


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.


Solution

  • 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()