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

Update an array containing a parameterized value (and conditionally adding env to deployments) #2500

Closed
rrichardson opened this issue May 19, 2020 · 4 comments

Comments

@rrichardson
Copy link

I have a collection of manifests that I am mutating for a specific set of environments.
Specifically, a subset of these manifests are Deployments. I want to add an environment variable if it doesn't already exist. This poses 3 problems:

  1. For each Deployment I want to update the spec/template/spec/containers/N where N.name == the metadata.name
  2. In the Containers object in the deployment mentioned in update readme w/ travis #1, I want to add env if it doesn't already exist.
  3. If env does exist, and the variable I want is present, I want to alter its value, if it's not present, I want to add a new element to the array that contains my intended name and value.

I thought I might be able to accomplish part one using vars, but I keep running into
Error: var '{DEPLOYMENT_NAME apps_v1_Deployment {metadata.name}}' cannot be mapped to a field in the set of known resources

The relevant part of my kustomize is:

vars:
  - name: DEPLOYMENT_NAME
    objref:
      kind: Deployment
      apiVersion: apps/v1
patches:
- path: setenv.yaml
  target:
    kind: Deployment

Then the setenv.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: placeholder
spec:
  template:
    spec:
      containers:
      - name: $(DEPLOYMENT_NAME)
        env:
        - name: NODE_ENV
          value: development

I am not sure what is causing the cannot be mapped to a field... error, but even if I got past that, I am assuming that I can't use $(DEPLOYMENT_NAME) as a selector.
Is there an alternate approach for this that might work?

After I accomplish that, how can I "upsert" both the env object and its contents?
In the approaches that I've tried, I can't seem to get kustomize to not just create another array entry so that the result ends up looking like

      containers:  
      - env: 
          name: NODE_ENV
          value: development
      - name: myintendedcontainer
         image: ...

Am I taking the wrong approach here? I would really like to avoid having to spell out a kustomization patch for every named deployment I am using.

@rrichardson
Copy link
Author

Thinking about this more, I think that there is no good way in most merge languages to describe such a conditional inclusion into an array. Not with a single step, at least.

@monopole
Copy link
Contributor

There's no notion of branching in kustomize - no way to check to see if a value exists and then do different things as a result.

this example has a description of merging configmap values as one goes up the overlay stack:
https://github.com/kubernetes-sigs/kustomize/blob/master/examples/combineConfigs.md

it should be easier to manage this you don't mind having more env vars defined for a particular container than the container actually needs

also, this might give you some ideas:
https://github.com/kubernetes-sigs/kustomize/blob/master/examples/valueAdd.md

@monopole
Copy link
Contributor

monopole commented Jun 1, 2020

please reopen if you have anything to add
there's also a notion of testing in IETF RFC 6902 (json)

@monopole monopole closed this as completed Jun 1, 2020
@rrichardson
Copy link
Author

@monopole - Thanks for your response. Do you think there is anything actionable that could be done after a test? Can it be used as conditional branches? It would be nice to have a boolean grouping in jsonpatch: e.g. { op: "and" : [ { op: "test" ...}, { op: "replace" ... }] }
I don't know if that would ever make it off the editing room floor in an RFC :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants