Kubernetes python client: delete missing variable

11/26/2019

I'm trying to use kubernetes python client to manage my cluster, but I can't find how to update it without breaking things. My input is a generic list of micro services that I translate into kubernetes deployments and services.
At this point I generated a list of V1Deployment and V1Service. How to do the equivalent of kubectl apply with those objects ?

I tried this :

def apply(self, api, cur_deploy_names: List[str]):
        from .kubernetes_impl import KubernetesImpl

        if self.deployment.metadata.name in cur_deploy_names:
            api.patch_namespaced_deployment(name=self.deployment.metadata.name,
                                            namespace=KubernetesImpl.namespace,
                                            body=self.deployment)
            logging.info(f"KUBERNETES: Deployment '{self.deployment.metadata.name}' updated")
        else:
            api.create_namespaced_deployment(namespace=KubernetesImpl.namespace, body=self.deployment)
            logging.info(f"KUBERNETES: Deployment '{self.deployment.metadata.name}' created")

The main problem here, is that if in my new configuration, there is just one environment variable deleted, for example, patch_namespaced_deployment don't update the deployment. Is there an efficient way to know if there is a difference between new and old config, and if so, use replace_namespaced_deployment if needed ?

I'm open to all answer because I'm really new to kubernetes api.

-- Borhink
kubernetes
python

0 Answers