0

I'm trying to replace RAILS_ENVenv variable defined in this Deployment file.

deploy.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myservice-web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myservice-web
  template:
    metadata:
      labels:
        app: myservice-web
    spec:
      serviceAccountName: myservice-web-sa
      nodeSelector:
        iam.gke.io/gke-metadata-server-enabled: "true"
      containers:
        - name: myservice-web
          image: us-central1-docker.pkg.dev/path/to/myimage
          args:
            - bundle 
            - exec 
            - bin/rails 
            - server 
            - -p 
            - "3000" 
            - -b 
            - "0.0.0.0"
          ports:
            - containerPort: 3000
          imagePullPolicy: Always
          resources:
            requests:
              memory: "2Gi"
              cpu: 1
            limits:
              memory: "2Gi"
              cpu: 1
          env:
            - name: RAILS_ENV
              value: "production"
            - name: RACK_ENV
              value: "production"
            - name: NODE_ENV
              value: "production"
            - name: SECRETS_PATH
              value: "/var/myservice-secrets"
            - name: SECRETS_FILE
              value: ".secrets"
          volumeMounts:
            - mountPath: "/var/myservice-secrets"
              name: gcp-myservice-secrets
      initContainers:
        - name: migrations
          image: us-central1-docker.pkg.dev/path/to/myservice:develop
          command:
          - bundle
          - exec
          - bin/rails
          - db:migrate
          env:
          - name: RAILS_ENV
            value: "production"
          - name: RACK_ENV
            value: "production"
          - name: NODE_ENV
            value: "production"
          - name: SECRETS_PATH
            value: "/var/myservice-secrets"
          - name: SECRETS_FILE
            value: ".secrets"
          volumeMounts:
          ...
      imagePullSecrets: 
      ...
      volumes:
      ...
---
apiVersion: v1
kind: Service
metadata:
  name: myservice-web
spec:
  ports:
    - port: 3000
      targetPort: 3000
      protocol: TCP
  selector:
    app: myservice-web

This is the kustomization file:

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

namespace: default

bases:
  - ../../base

images:
  - name: us-central1-docker.pkg.dev/path/to/myimage
    newTag: develop

resources:
  - sa.yaml
  - ing.yaml
  - ing-jukeboxhealth.yaml
  - cert.yaml
  - cert-jukeboxhealth.yaml

patches:
- patch: |-
    - op: replace
      path: /spec/template/spec/containers/0/env[name="RAILS_ENV"]/value
      value: "staging"
  target:
    kind: Deployment
    name: myservice-web

I get this error:

error: replace operation does not apply: doc is missing path: /spec/template/spec/containers/0/env[name="RAILS_ENV"]/value: missing value

I've tried defining the path by position instead of name and it works. But I'd like to do it by targeting the name. What is wrong with the path?

borjagvo
  • 1,802
  • 2
  • 20
  • 35
  • 1
    [Array element election by `key=value` is not supported](https://github.com/kubernetes-sigs/kustomize/issues/4509). See also the [relevant section of the corresponding RFC](https://www.rfc-editor.org/rfc/rfc6901#section-4). --- Out of curiosity: have you tried a strategic merge patch instead? – Turing85 Jan 03 '23 at 15:55
  • Does this answer your question? [kustomize patching a specific container other than by array (/containers/0)](https://stackoverflow.com/questions/63927916/kustomize-patching-a-specific-container-other-than-by-array-containers-0) – Turing85 Jan 03 '23 at 15:58
  • @Turing85 I haven't. I'm new to Kubernetes. But thanks for pointing it out! I'll dig more into strategic merge patch. – borjagvo Jan 03 '23 at 16:13

1 Answers1

0

The above error due to path.
Try this path:

/spec/template/spec/containers/0/env/0/value

in the kustomization file.

ryanwebjackson
  • 1,017
  • 6
  • 22
  • 36
Ajay A
  • 1
  • 2