Kubernetes Ansible Operators - Patch an Existing Kubernetes Resource fails

554 views
Skip to first unread message

George Tsoumplekas

unread,
Mar 25, 2021, 3:19:49 AM3/25/21
to Operator Framework
Hello,
I am newbie in Kubernetes, ansible and operators
I try to rollback a current deployment , but fails with invalid patch error.

I have the following kubernetes command

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

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

I defined the  following role  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


- 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

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\\\

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

I will appreciate your help


The question has been posted in stack overflow as well

https://stackoverflow.com/questions/66790966/kubernetes-ansible-operators-patch-an-existing-deployment-fails


Carlos Santana

unread,
Mar 25, 2021, 8:54:59 AM3/25/21
to Operator Framework

Hi George

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 

Melvin Hillsman

unread,
Mar 25, 2021, 8:57:53 AM3/25/21
to George Tsoumplekas, Operator Framework
Your playbook is YAML and here is the documentation on creating an array https://www.w3schools.io/file/yaml-arrays/

You need a - before my-operator

--
You received this message because you are subscribed to the Google Groups "Operator Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to operator-framew...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/operator-framework/5332f789-91dd-4f70-8638-bc7e352ef8a8n%40googlegroups.com.

George Tsoumplekas

unread,
Mar 25, 2021, 9:55:55 AM3/25/21
to Operator Framework
Thank you for the hint and for the tutorial. I have no experience in kubernetes , ansible e.t.c
I put the - before my operator .Seems that , array is recongized . However a new error was generated 
Deployment.apps \\\\\"my-operator\\\\\" is invalid: [spec.template.spec.containers[0].name: Required value, spec.template.spec.containers[0].image: Required value]\

Melvin Hillsman

unread,
Mar 25, 2021, 10:12:04 AM3/25/21
to George Tsoumplekas, Operator Framework
Your container needs an image to run. Familiarize yourself with Kubernetes via the upstream documentation or a book, even though it is quite outdated now, Kubernetes In Action is good; a second edition is being written https://www.manning.com/books/kubernetes-in-action-second-edition

Ansible has good documentation - https://docs.ansible.com - and review the K8s collection docs as well https://galaxy.ansible.com/community/kubernetes

tsoumplekas_giorgos

unread,
Mar 25, 2021, 10:23:02 AM3/25/21
to Melvin Hillsman, Operator Framework
Hello Melvin,
I am confused.  The playbook runs in a container.
I need to roll back the image .. I set this image to spec.template.spec.containers[0].image. The image name is stored in installed_software_image variable . I printed this value and it is not empty.. 
After the aforementioned changes the format is the following 

- 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 }}"    

Probably I am missing something.
Reply all
Reply to author
Forward
0 new messages