Update subset of properties of a kubernetes manifest file (eg. like nodeselector) programmatically

12/13/2021

I have a few daemonSets running on my k8s cluster. I want to add an additional property in all of the daemonSets' pod template spec programmatically, using powershell.

In specific, I want to "add" the following to to a few daemon sets:

spec:
  template:
    spec:
      tolerations:
      - key: node-role.kubernetes.io/master
        operator: Exists
        effect: NoSchedule

The existing things like containers, volumes, label selectors, etc should be as it already exists. How can I do this programmatically?

-- Jerald Baker
kubernetes
powershell

1 Answer

12/13/2021

You can update the YAML file first and apply the YAML file changes using the Code or Program with the K8s client.

Python client

Example

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__), "daemon-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()
-- Harsh Manvar
Source: StackOverflow