kubernetes: kubectl patch cannot patch a json list of items, only a single item
What happened: I am applying a patch to an existing pod. This patch is an add operation of type json with includes a list of items to add. Example:
kubectl patch pod alpine-correct --type='json' -p='[{"op":"add","path":"/metadata/annotations", "value":{"testone.annotation.io":"test-one"}},{"op":"add","path":"/metadata/annotations","value":{"testtwo.annotation.io":"test-two"}},{"op":"add","path":"/metadata/annotations","value":{"testthree.annotation.io":"test-three"}}]'
Results in: pod/alpine-correct patched
I ran kubectl describe pod alpine-correct
Before the patch there were 0 annotations, now there was 1: testthree.annotation.io":"test-three"
What you expected to happen: I expected all three annotations to get added.
How to reproduce it (as minimally and precisely as possible):
- Create an alpine pod (
kubectl apply -f alpine-correct.yaml
) - Run the patch above
- Kubectl describe the pod and observe
The alpine pod spec:
apiVersion: v1
kind: Pod
metadata:
name: alpine-correct
spec:
containers:
- image: alpine:3.2
command:
- /bin/sh
- "-c"
- "sleep 60m"
imagePullPolicy: IfNotPresent
name: alpine
restartPolicy: Always
Anything else we need to know?:
Environment:
- Kubernetes version (use
kubectl version
): 1.13 - Cloud provider or hardware configuration: GCP
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (9 by maintainers)
@natalysheinin I suppose it is working as expected according to this RFC 6902 If you want to add 3 annotations to your pod you can run the following command:
This command would patch the
/metadata/annotations
object with that value you provide.In your prior execution,
each operation was overriding the
metadata/annotations
object with the value in that operation, the final state of the pod’s annotations was the result of the last override in your patch operations array.this is an example of patching multiple paths that works as expected:
/close
see https://datatracker.ietf.org/doc/html/rfc6901#section-3
slash in keys is expressed as
~1
API documentation is at https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/, and each object has a list of fields with links to the schema for that field
For example, pods: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#pod-v1-core