Can I use env in preStop command

3/9/2019

Can I use environment variable in lifecycle.preStop.exec.command? I have a script that has to be run in preStop command. The answer here states that it's possible to use env variables in postStart Can I use env in postStart command. It doesn't work with preStop though. Is it a bug or am I doing something wrong?


apiVersion: apps/v1beta1
kind: Deployment
metadata: 
  name: loap
spec: 
  replicas: 1
  template: 
    metadata: 
      labels: 
        app: loap
    spec: 
      containers: 
        - 
          command: 
            - sh
            - "-c"
            - "echo $(date +%s): START >> /loap/timing; sleep 10; echo $(date +%s): END >> /loap/timing;"
          image: busybox
          env:
          - name: secretThing
            valueFrom:
              secretKeyRef:
                name: supersecret
                key: password
          lifecycle: 
            preStop: 
              exec: 
                command: 
                  - sh
                  - "-c"
                  - "echo ${secretThing} $(date +%s): PRE-HOOK >> /loap/timing"
          livenessProbe: 
            exec: 
              command: 
                - sh
                - "-c"
                - "echo $(date +%s): LIVENESS >> /loap/timing"
          name: main
          readinessProbe: 
            exec: 
              command: 
                - sh
                - "-c"
                - "echo $(date +%s): READINESS >> /loap/timing"
          volumeMounts: 
            - 
              mountPath: /loap
              name: timing
      initContainers: 
        - 
          command: 
            - sh
            - "-c"
            - "echo $(date +%s): INIT >> /loap/timing"
          image: busybox
          name: init
          volumeMounts: 
            - 
              mountPath: /loap
              name: timing
      volumes: 
        - 
          hostPath: 
            path: /tmp/loap
          name: timing
-- medvedev1088
kubernetes

1 Answer

4/1/2019

This is explained in the Kubernetes docs Working with objects - Names.

A client-provided string that refers to an object in a resource URL, such as /api/v1/pods/some-name.

Only one object of a given kind can have a given name at a time. However, if you delete the object, you can make a new object with the same name.

By convention, the names of Kubernetes resources should be up to maximum length of 253 characters and consist of lower case alphanumeric characters, -, and ., but certain resources have more specific restrictions.

-- Crou
Source: StackOverflow