pythonkuberneteskubernetes-deploymentkubernetes-python-client

Remove volume from deployment using patch_namespaced_deployment not working


I'm trying to patch a deployment and remove its volumes using patch_namespaced_deployment (https://github.com/kubernetes-client/python) with the following arguments, but it's not working.

patch_namespaced_deployment(
            name=deployment_name,
            namespace='default',
            body={"spec": {"template": {
                "spec": {"volumes": None,
                "containers": [{'name': container_name, 'volumeMounts': None}]
                }
            }
            }
            },
            pretty='true'
        )

How to reproduce it:

Create this file app.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  annotations:
    pv.kubernetes.io/bound-by-controller: "yes"
  finalizers:
  - kubernetes.io/pv-protection
  labels:
    volume: pv0001
  name: pv0001
  resourceVersion: "227035"
  selfLink: /api/v1/persistentvolumes/pv0001
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 5Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: myclaim
    namespace: default
    resourceVersion: "227033"
  hostPath:
    path: /mnt/pv-data/pv0001
    type: ""
  persistentVolumeReclaimPolicy: Recycle
  volumeMode: Filesystem
status:
  phase: Bound
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pv-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mypv
  template:
    metadata:
      labels:
        app: mypv
    spec:
      containers:
      - name: shell
        image: centos:7
        command:
        - "bin/bash"
        - "-c"
        - "sleep 10000"
        volumeMounts:
        - name: mypd
          mountPath: "/tmp/persistent"
      volumes:
      - name: mypd
        persistentVolumeClaim:
          claimName: myclaim
- kubectl apply -f app.yaml

- kubectl describe deployment.apps/pv-deploy (to check the volumeMounts and Volumes)

- kubectl patch deployment.apps/pv-deploy --patch '{"spec": {"template": {"spec": {"volumes": null, "containers": [{"name": "shell", "volumeMounts": null}]}}}}'

- kubectl describe deployment.apps/pv-deploy (to check the volumeMounts and Volumes)

- Delete the application now: kubectl delete -f app.yaml

- kubectl create -f app.yaml

- Patch the deployment using the python library function as stated above. The *VolumeMounts* section is removed but the Volumes still exist.

** EDIT ** Running the kubectl patch command works as expected. But after executing the Python script and running a describe deployment command, the persistentVolumeClaim is replaced with an emptyDir like this

  Volumes:
   mypd:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:     
    SizeLimit:  <unset>

Solution

  • What you're trying to do is called a strategic merge patch (https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/). As you can see in the documentation, With a strategic merge patch, a list is either replaced or merged depending on its patch strategy so this may be why you're seeing this behavior.

    I think you should go with replace https://jamesdefabia.github.io/docs/user-guide/kubectl/kubectl_replace/ and instead of trying to manage a part of your deployment object, replace it with a new one.