From 5513c60b543026641e72905bec44ce58b343702b Mon Sep 17 00:00:00 2001 From: Weiping Cai Date: Tue, 16 Jun 2020 09:50:06 +0800 Subject: [PATCH 1/5] add define interdependent environment variables page Signed-off-by: Weiping Cai --- ...ne-interdependent-environment-variables.md | 82 +++++++++++++++++++ .../pods/inject/dependent-envars.yaml | 26 ++++++ 2 files changed, 108 insertions(+) create mode 100644 content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md create mode 100644 content/en/examples/pods/inject/dependent-envars.yaml diff --git a/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md new file mode 100644 index 0000000000..3992cc46d7 --- /dev/null +++ b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md @@ -0,0 +1,82 @@ +--- +title: Define Dependent Environment Variables +content_type: task +weight: 20 +--- + + + +This page shows how to define dependent environment variables for a container +in a Kubernetes Pod. + + + + +## {{% heading "prerequisites" %}} + + +{{< include "task-tutorial-prereqs.md" >}} + + + + + + +## Define an environment dependent variable for a container + +When you create a Pod, you can set dependent environment variables for the containers that run in the Pod. To set dependent environment variables, you can use $(VAR_NAME) in the `value` of `env` in the configuration file. + +In this exercise, you create a Pod that runs one container. The configuration +file for the Pod defines an dependent environment variable with common usage defined. Here is the configuration manifest for the +Pod: + +{{< codenew file="pods/inject/dependent-envars.yaml" >}} + +1. Create a Pod based on that manifest: + + ```shell + kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml + ``` + ``` + pod/dependent-envars-demo created + ``` + +2. List the running Pods: + + ```shell + kubectl get pods dependent-envars-demo + ``` + ``` + NAME READY STATUS RESTARTS AGE + dependent-envars-demo 1/1 Running 0 9s + ``` + +3. Get log to the container running in your Pod: + + ```shell + kubectl logs pod/dependent-envars-demo + ``` + ``` + SERVICE_ADDRESS + https://172.17.0.1:80 + UNCHANGE_REFERENCE + $(PROTOCOL)://172.17.0.1:80 + ESCAPED_REFERENCE + $(PROTOCOL)://172.17.0.1:80 + ``` + +As shown above, we have defined the correct dependency reference of `SERVICE_ADDRESS`, bad dependency reference of `UNCHANGE_REFERENCE` and skip dependent references of `ESCAPED_REFERENCE`. + +When the environment variable is defined, you can use it directly. You can use it after the definition is completed, such as `SERVICE_ADDRESS`. + +When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as `UNCHANGE_REFERENCE`. A bad reference does not interfere with the operation of the container. + +The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).Escaped references will never be expanded, regardless of whether the variable exists or not. + +## {{% heading "whatsnext" %}} + + +* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/). +* See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core). + + diff --git a/content/en/examples/pods/inject/dependent-envars.yaml b/content/en/examples/pods/inject/dependent-envars.yaml new file mode 100644 index 0000000000..c274870cf3 --- /dev/null +++ b/content/en/examples/pods/inject/dependent-envars.yaml @@ -0,0 +1,26 @@ +apiVersion: v1 +kind: Pod +metadata: + name: dependent-envars-demo +spec: + containers: + - name: dependent-envars-demo + args: + - while true; do echo -en '\n'; echo SERVICE_ADDRESS;printenv SERVICE_ADDRESS;echo UNCHANGE_REFERENCE;printenv UNCHANGE_REFERENCE;echo ESCAPED_REFERENCE;printenv ESCAPED_REFERENCE; sleep 30; done; + command: + - sh + - -c + image: busybox + env: + - name: SERVICE_PORT + value: "80" + - name: SERVICE_IP + value: "172.17.0.1" + - name: UNCHANGE_REFERENCE + value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" + - name: PROTOCOL + value: "https" + - name: SERVICE_ADDRESS + value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" + - name: ESCAPED_REFERENCE + value: "$$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" \ No newline at end of file From 1253c3b39e632ea86d18b1d08534feb0192c0abf Mon Sep 17 00:00:00 2001 From: Weiping Cai Date: Wed, 17 Jun 2020 14:02:10 +0800 Subject: [PATCH 2/5] fix UNCHANGE_REFERENCE to UNCHANGED_REFERENCE,and use you to replace we Signed-off-by: Weiping Cai --- .../define-interdependent-environment-variables.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md index 3992cc46d7..a67b86ad19 100644 --- a/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md +++ b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md @@ -10,16 +10,12 @@ This page shows how to define dependent environment variables for a container in a Kubernetes Pod. - - ## {{% heading "prerequisites" %}} {{< include "task-tutorial-prereqs.md" >}} - - ## Define an environment dependent variable for a container @@ -51,7 +47,7 @@ Pod: dependent-envars-demo 1/1 Running 0 9s ``` -3. Get log to the container running in your Pod: +3. Check the logs for the container running in your Pod: ```shell kubectl logs pod/dependent-envars-demo @@ -59,17 +55,17 @@ Pod: ``` SERVICE_ADDRESS https://172.17.0.1:80 - UNCHANGE_REFERENCE + UNCHANGED_REFERENCE $(PROTOCOL)://172.17.0.1:80 ESCAPED_REFERENCE $(PROTOCOL)://172.17.0.1:80 ``` -As shown above, we have defined the correct dependency reference of `SERVICE_ADDRESS`, bad dependency reference of `UNCHANGE_REFERENCE` and skip dependent references of `ESCAPED_REFERENCE`. +As shown above, you have defined the correct dependency reference of `SERVICE_ADDRESS`, bad dependency reference of `UNCHANGED_REFERENCE` and skip dependent references of `ESCAPED_REFERENCE`. When the environment variable is defined, you can use it directly. You can use it after the definition is completed, such as `SERVICE_ADDRESS`. -When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as `UNCHANGE_REFERENCE`. A bad reference does not interfere with the operation of the container. +When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as `UNCHANGED_REFERENCE`. A bad reference does not interfere with the operation of the container. The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).Escaped references will never be expanded, regardless of whether the variable exists or not. From e3f6541d0a59dcf00b9be0f58e45c400af82d44a Mon Sep 17 00:00:00 2001 From: Cweiping Date: Sat, 20 Jun 2020 12:41:38 +0800 Subject: [PATCH 3/5] Apply suggestions from code review Signed-off-by: Weiping Cai --- .../define-interdependent-environment-variables.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md index a67b86ad19..cbeed9f846 100644 --- a/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md +++ b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md @@ -63,11 +63,14 @@ Pod: As shown above, you have defined the correct dependency reference of `SERVICE_ADDRESS`, bad dependency reference of `UNCHANGED_REFERENCE` and skip dependent references of `ESCAPED_REFERENCE`. -When the environment variable is defined, you can use it directly. You can use it after the definition is completed, such as `SERVICE_ADDRESS`. +When an environment variable is already defined when being referenced, +the reference can be correctly resolved, such as in the `SERVICE_ADDRESS` case. -When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as `UNCHANGED_REFERENCE`. A bad reference does not interfere with the operation of the container. +When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as `UNCHANGED_REFERENCE`. Note that incorrectly parsed environment variables, in general, will not block the container from starting. -The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).Escaped references will never be expanded, regardless of whether the variable exists or not. +The `$(VAR_NAME)` syntax can be escaped with a double `$`, ie: `$$(VAR_NAME)`. +Escaped references are never expanded, regardless of whether the referenced variable +is defined or not. This can be seen from the `ESCAPED_REFERENCE` case above. ## {{% heading "whatsnext" %}} @@ -75,4 +78,3 @@ The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).Escaped * Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/). * See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core). - From 506a57849b53342073690ea3ca84d29763436373 Mon Sep 17 00:00:00 2001 From: Weiping Cai Date: Tue, 23 Jun 2020 09:38:16 +0800 Subject: [PATCH 4/5] uograde dependent-envars.yaml leave out env name Signed-off-by: Weiping Cai --- content/en/examples/pods/inject/dependent-envars.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/en/examples/pods/inject/dependent-envars.yaml b/content/en/examples/pods/inject/dependent-envars.yaml index c274870cf3..c9f8c8b248 100644 --- a/content/en/examples/pods/inject/dependent-envars.yaml +++ b/content/en/examples/pods/inject/dependent-envars.yaml @@ -16,7 +16,7 @@ spec: value: "80" - name: SERVICE_IP value: "172.17.0.1" - - name: UNCHANGE_REFERENCE + - name: UNCHANGED_REFERENCE value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" - name: PROTOCOL value: "https" From 4490fe014fe36d53383dcd3f937f6581842c9f36 Mon Sep 17 00:00:00 2001 From: Weiping Cai Date: Wed, 1 Jul 2020 14:48:38 +0800 Subject: [PATCH 5/5] use printf to replace printenv Signed-off-by: Weiping Cai --- ...ne-interdependent-environment-variables.md | 10 +++--- .../pods/inject/dependent-envars.yaml | 36 +++++++++---------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md index cbeed9f846..74c5c245db 100644 --- a/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md +++ b/content/en/docs/tasks/inject-data-application/define-interdependent-environment-variables.md @@ -53,12 +53,10 @@ Pod: kubectl logs pod/dependent-envars-demo ``` ``` - SERVICE_ADDRESS - https://172.17.0.1:80 - UNCHANGED_REFERENCE - $(PROTOCOL)://172.17.0.1:80 - ESCAPED_REFERENCE - $(PROTOCOL)://172.17.0.1:80 + + UNCHANGED_REFERENCE=$(PROTOCOL)://172.17.0.1:80 + SERVICE_ADDRESS=https://172.17.0.1:80 + ESCAPED_REFERENCE=$(PROTOCOL)://172.17.0.1:80 ``` As shown above, you have defined the correct dependency reference of `SERVICE_ADDRESS`, bad dependency reference of `UNCHANGED_REFERENCE` and skip dependent references of `ESCAPED_REFERENCE`. diff --git a/content/en/examples/pods/inject/dependent-envars.yaml b/content/en/examples/pods/inject/dependent-envars.yaml index c9f8c8b248..2509c6f47b 100644 --- a/content/en/examples/pods/inject/dependent-envars.yaml +++ b/content/en/examples/pods/inject/dependent-envars.yaml @@ -4,23 +4,23 @@ metadata: name: dependent-envars-demo spec: containers: - - name: dependent-envars-demo - args: - - while true; do echo -en '\n'; echo SERVICE_ADDRESS;printenv SERVICE_ADDRESS;echo UNCHANGE_REFERENCE;printenv UNCHANGE_REFERENCE;echo ESCAPED_REFERENCE;printenv ESCAPED_REFERENCE; sleep 30; done; - command: + - name: dependent-envars-demo + args: + - while true; do echo -en '\n'; printf UNCHANGED_REFERENCE=$UNCHANGED_REFERENCE'\n'; printf SERVICE_ADDRESS=$SERVICE_ADDRESS'\n';printf ESCAPED_REFERENCE=$ESCAPED_REFERENCE'\n'; sleep 30; done; + command: - sh - -c - image: busybox - env: - - name: SERVICE_PORT - value: "80" - - name: SERVICE_IP - value: "172.17.0.1" - - name: UNCHANGED_REFERENCE - value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" - - name: PROTOCOL - value: "https" - - name: SERVICE_ADDRESS - value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" - - name: ESCAPED_REFERENCE - value: "$$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" \ No newline at end of file + image: busybox + env: + - name: SERVICE_PORT + value: "80" + - name: SERVICE_IP + value: "172.17.0.1" + - name: UNCHANGED_REFERENCE + value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" + - name: PROTOCOL + value: "https" + - name: SERVICE_ADDRESS + value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)" + - name: ESCAPED_REFERENCE + value: "$$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"