kustomize icon indicating copy to clipboard operation
kustomize copied to clipboard

Env variable not added to multiple deployments with /- in the path if there is no other env variable defined before

Open e9169 opened this issue 10 months ago • 7 comments

What happened?

When trying to add env variables to several Kubernetes Deployments at the same time, it failed because one of the Deployments manifest didn't have environment variables defined before. Using '/-' don't add it if there are no envs defined in advance, instead it throws the error:

doc is missing path: "/spec/template/spec/containers/0/env/-": missing value

What did you expect to happen?

I expect that, if an item, an env variable in this case, doesn't exist and I end the path with '/-', that it's actually appended at the end. At the end of none, should be the first and only env variable in there.

How can we reproduce it (as minimally and precisely as possible)?

#kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: argocd
resources:
- github.com/argoproj-labs/argocd-autopilot/manifests/base?ref=v0.4.17

patches:
- patch: |-
    - op: add
      path: /spec/template/spec/containers/0/env/-
      value:
        name: test
        value: test

  target:
    group: apps
    version: v1
    kind: Deployment

Expected output

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: argocd
  name: argocd-deployment
spec:
  template:
    spec:
      containers:
      - name: nginx
         env:
         - name: test
            value: test

Actual output

doc is missing path: "/spec/template/spec/containers/0/env/-": missing value

Kustomize version

v5.0.4-0.20230601165947-6ce0bf390ce3

Operating system

MacOS

e9169 avatar Apr 08 '24 13:04 e9169

This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Apr 08 '24 13:04 k8s-ci-robot

Just to clarify the issue, this happens because the env key is not defined in your deployment. Which basically means that the deployment yaml looks like:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: argocd
  name: argocd-deployment
spec:
  template:
    spec:
      containers:
      - name: nginx

I don't see an issue in us appending the env variable directly if none is specified. But it needs investigation on whether it can be implemented in Kustomize.

varshaprasad96 avatar Apr 10 '24 16:04 varshaprasad96

/assign @garomonegro

varshaprasad96 avatar Apr 10 '24 16:04 varshaprasad96

@varshaprasad96: GitHub didn't allow me to assign the following users: garomonegro.

Note that only kubernetes-sigs members with read permissions, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time. For more information please see the contributor guide

In response to this:

/assign @garomonegro

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Apr 10 '24 16:04 k8s-ci-robot

/assign

garomonegro avatar Apr 10 '24 16:04 garomonegro

You can create env before it seems.

patches:
- patch: |-
    - op: add
      path: /spec/template/spec/containers/0/env
      value: []
    - op: add
      path: /spec/template/spec/containers/0/env/-
      value:
        name: KUBEVIP_ENABLE_LOADBALANCERCLASS
        value: "true"
  target:
    kind: Deployment
    name: kube-vip-cloud-provider

Raboo avatar Jul 04 '24 12:07 Raboo

You can create env before it seems.

Applying that patch would remove any pre-existing env, which is not desirable in cases you want to add an env to all targets marched, but not remove what is in the base.

pecigonzalo avatar Sep 20 '24 16:09 pecigonzalo