Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] how to add an env to all containers in Deployment? #4391

Closed
Lwxiang opened this issue Jan 18, 2022 · 6 comments
Closed

[Question] how to add an env to all containers in Deployment? #4391

Lwxiang opened this issue Jan 18, 2022 · 6 comments
Labels
kind/support Categorizes issue or PR as a support question. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@Lwxiang
Copy link

Lwxiang commented Jan 18, 2022

origin resource

apiVersion: apps/v1
kind: Deployment
...
    spec:
      containers:
        - name: nginx
           image: nginx
           env:
           - name: foo
             value: bar
        - name: sidecar
           image: sidecar
           env:
           - name: hello
             value: world

As the origin resource above, I have 2 container in 1 pod, and each has its own env.
Now I want to insert an env drink=coffee into the 2 container.

I can do this by json patch:

- op: add
  path: /spec/template/spec/containers/0/env/-
  value:
    name: drink
    value: coffee
- op: add
  path: /spec/template/spec/containers/1/env/-
  value:
    name: drink
    value: coffee

But if I don't want to do this with every index of containers or I just don't know how many the containers are. Can I have a batch method, to add env for all containers by one step?

@k8s-ci-robot k8s-ci-robot added needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 18, 2022
@natasha41575
Copy link
Contributor

natasha41575 commented Jan 21, 2022

I believe your use case will be supported by replacements after completion of this feature: #4053. Unfortunately, this is not available yet. However, when this done, you will be able to do the following:

source: 
  kind: Pod
  name: pod-with-desired-env
  fieldPath: spec.template.spec.containers.0.env
targets:
- select:
    kind: Deployment
  fieldPaths: 
  - spec.template.spec.containers.*.env
  options:
    create: true

Would this resolve your issue?

/kind support/
/triage accepted

@k8s-ci-robot
Copy link
Contributor

@natasha41575: The label(s) kind/support/ cannot be applied, because the repository doesn't have them.

In response to this:

I believe your use case will be supported by [replacements](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/replacements/) after completion of this feature: #4053. Unfortunately, this is not available yet. However, when this done, you will be able to do the following:

source: 
 kind: Pod
 name: pod-with-desired-env
 fieldPath: spec.template.spec.containers.0.env
targets:
- select:
   kind: Deployment
 fieldPaths: 
 - spec.template.spec.containers.*.env
 options:
   create: true

Would this resolve your issue?

/kind support/
/triage accepted

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 k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 21, 2022
@natasha41575
Copy link
Contributor

/kind support
/unlabel needs-kind

@k8s-ci-robot k8s-ci-robot added kind/support Categorizes issue or PR as a support question. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Jan 21, 2022
@natasha41575
Copy link
Contributor

/close

Please reopen if #4053 will not solve the issue for you. Otherwise please see that issue for updates.

@k8s-ci-robot
Copy link
Contributor

@natasha41575: Closing this issue.

In response to this:

/close

Please reopen if #4053 will not solve the issue for you. Otherwise please see that issue for updates.

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.

@jashandeep-sohi
Copy link

Wouldn't the proposed solution replace all env vars in all containers? Is there a way to append to the env list (or better yet to apply a patch to every item of a list)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

4 participants