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

5/1/2020

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?

-- u123
kubectl
kubernetes

2 Answers

5/1/2020

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.

-- Rico
Source: StackOverflow

5/1/2020

You can update above json array field with following

kubectl patch ing hello-world --type json -p '[{"op": "replace", "path": "/spec/tls/0/secretName", "value" : "updated"}]'

Here, you have to specify the index, in your case it is 0

-- hoque
Source: StackOverflow