Search code examples
kubernetesansibledevopskubernetes-custom-resourcesoperator-sdk

Kubernetes Ansible operators: Patch an existing deployment fails


I recently start studying for kubernetes and ansible.

I have the following kubernetes command in order to do rollback

kubectl patch deployment -n my-namespace mydeployment --type='json' -p='[ {"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"127.0.0.1:5050/mydeployment:image_version"} ]

Is any way to introduce a json array in kubernetes ansible command and patch my deployment?

That I tried is the following in my playbook

- name: Demo
  k8s:
    api_version: apps/v1
    kind: Deployment
    state: present
    namespace: '{{ meta.namespace }}'
    name: my-operator
    definition: |
      spec:
       template:
         spec:
          containers:
            my-operator:
              image: {{ installed_software_image }}    
  register: output

Due to the fact that containers is an array , the patch command fails . I get the following error

NewReplicaSetAvailable\\\\\\",\\\\\\"message\\\\\\":\\\\\\"ReplicaSet \\\\\\\\\\\\\\"my-operator-66ff64c9f4\\\\\\\\\\\\\\" has successfully progressed.\\\\\\"}]}}\\": v1.Deployment.Spec: v1.DeploymentSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: decode slice: expect [ or n, but found {, error found in #10 byte of ...|tainers\\":{\\"new-opera|..., bigger context ...|t\\":\\"2021-03-24T22:26:02Z\\"}},\\"spec\\":{\\"containers\\":{\\"my-operator\\":\\"image:\\\\\\"27.0.0.1:5050/my-ope|...","field":"patch"}]},"code":422}\n'", "reason": "Unprocessable Entity", "status": 422}

Is any way to do debug or to print the command that actually is sent to kubernetes server?


Solution

  • The error is indicating that “containers:” is an array.

    Try adding “- “ in front of “my-operaror:” to indicate that it's the first item in the array