Search code examples
kuberneteskubectl

Kubectl patch gives: Error from server: cannot restore slice from map


I have this ingress object where I am trying to patch the secretName:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: hello-world
...
spec:
  rules:
  - host: my.host
    http:
      paths:
      - backend:
          serviceName: hello-world
          servicePort: 8080
  tls:
  - hosts:
    - my.host
    secretName: my-secret

I would like to update the secret name using kubectl patch I have tried:

$ kubectl patch ing hello-world -p '{"spec":{"tls":{"secretName":"updated"}}}'
Error from server: cannot restore slice from map

and:

$ kubectl patch ing hello-world -p '[{"op": "replace", "path": "/spec/tls/secretName", "value" : "updated"}]'
Error from server (BadRequest): json: cannot unmarshal array into Go value of type map[string]interface {}

Any suggestions?


Solution

  • tls is an array/slice so you have to refer to it like that and include it in the original patch.

    $ kubectl patch ing hello-world -p '{"spec":{"tls":[{"hosts":["my.host"], "secretName": "updated"}]}}'
    

    A good way to get the -ps right (that works for me) is to convert them from YAML to JSON. You can try an online tool like this.