kubernetes-sigs / kustomize

Customization of kubernetes YAML configurations
Apache License 2.0
11k stars 2.25k forks source link

Patch to add label fails if label: {} is not in resources #3520

Closed rob8714 closed 3 years ago

rob8714 commented 3 years ago

Hi

Came across a problem adding a label with the following files:

service.yml

---
kind: Service
apiVersion: v1
metadata:
  name: app-svc
spec:
  ports:
    - name: app
      port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    app: app
  type: ClusterIP

kustomization.yaml

---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - service.yml

patches:
- patch: |-
    - op: add
      path: /metadata/labels/testLabel
      value: test
  target:
    kind: Service
    version: v1
    name: app-svc

Running kustomize build . gives an error:

Error: add operation does not apply: doc is missing path: "/metadata/labels/testLabel": missing value

Expected output

---
kind: Service
apiVersion: v1
metadata:
  name: app-svc
  labels:
    testLabel: test
spec:
  ports:
    - name: app
      port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    app: app
  type: ClusterIP

Actual output

Error: add operation does not apply: doc is missing path: "/metadata/labels/testLabel": missing value

Kustomize version

v3.8.9

Platform

macOS

Additional context

Adding labels: {} in service.yml solves the problem, however understanding is that this should not fail especially since we are adding adding objects.

Shell32-Natsu commented 3 years ago

That is how json patch works. To add label please use LabelTransformer

rob8714 commented 3 years ago

This assumes that I want to add the label to all the resources... I want to add the label to just one resource so using the commonLabel is not really an option, what I provided in the above was a simple example. It also assumes that the base layer needs to cater for possibilities that may be added in the overlays.

I will work around by adding label: {}, but it would be great if the documentation could be updated to clarify on this behavior since, in my opinion, this is not clear.

Shell32-Natsu commented 3 years ago

If you want to use json patch, I assume you have clear understanding of it. Please take a look at the specification of add operation: https://tools.ietf.org/html/rfc6902#section-4.1.

rob8714 commented 3 years ago

Thanks for clarifying, I wasn't looking at the RFC but now I read through the section and understand :)

jcpunk commented 1 year ago

For folks that find this bug and are looking for a quick solution, something like:

patches:
  - target:
      kind: ConfigMap
      name: example
    patch: |-
      - op: add
        path: /metadata/labels
        value: {"example": "value"}
jeffmccune commented 10 months ago

One gotcha with the quick solution above, it replaces the labels. If instead you do want to add a label and you're wondering why kustomize is gas-lighting you with op: add replacing instead of adding , then try this:

patches:
  - target:
      kind: Deployment
      name: httpbin
    patch: |-
      - op: add
        path: "/spec/template/metadata/labels/sidecar.istio.io~1inject"
        value: "true"