Skip to content

Commit bb9bdca

Browse files
committed
Add docs on accessing secrets and ConfigMaps from Srvls functions
Many improvements Fix a duplicate ID Add descriptions for code snippets, minor fixes Swap sections Restructure the manual configuration part, move it to the very end Fix bad inclusion Fix bad inclusion Remove a redundant note Update the environment variable assignment syntax Many small improvements Remove incorrect comma Remove used in these assemblies metadata from modules
1 parent 10e8049 commit bb9bdca

10 files changed

+262
-0
lines changed

_topic_map.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,6 +3057,8 @@ Topics:
30573057
File: serverless-developing-quarkus-functions
30583058
- Name: Using functions with Knative Eventing
30593059
File: serverless-functions-eventing
3060+
- Name: Accessing secrets and config maps from Serverless functions
3061+
File: serverless-functions-accessing-secrets-configmaps
30603062
- Name: Functions development reference guide
30613063
File: serverless-functions-reference-guide
30623064
#
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[id="serverless-functions-all-values-in-configmap-to-env-variables_{context}"]
2+
= Setting environment variables from all values defined in a config map
3+
4+
. Open the `func.yaml` file for your function.
5+
6+
. For every config map for which you want to import all key-value pairs as environment variables, add the following YAML to the `envs` section:
7+
+
8+
[source,yaml]
9+
----
10+
name: test
11+
namespace: ""
12+
runtime: go
13+
...
14+
envs:
15+
- value: '{{ configMap:myconfigmap }}' <1>
16+
----
17+
<1> Substitute `myconfigmap` with the name of the target config map.
18+
19+
. Save the file.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[id="serverless-functions-all-values-in-secret-to-env-variables_{context}"]
2+
= Setting environment variables from all values defined in a secret
3+
4+
. Open the `func.yaml` file for your function.
5+
6+
. For every secret for which you want to import all key-value pairs as environment variables, add the following YAML to the `envs` section:
7+
+
8+
[source,yaml]
9+
----
10+
name: test
11+
namespace: ""
12+
runtime: go
13+
...
14+
envs:
15+
- value: '{{ secret:mysecret }}' <1>
16+
----
17+
<1> Substitute `mysecret` with the name of the target secret.
18+
19+
. Save the configuration.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[id="serverless-functions-key-value-in-configmap-to-env-variable_{context}"]
2+
= Setting environment variable from a key value defined in a config map
3+
4+
. Open the `func.yaml` file for your function.
5+
6+
. For each value from a config map key-value pair that you want to assign to an environment variable, add the following YAML to the `envs` section:
7+
+
8+
[source,yaml]
9+
----
10+
name: test
11+
namespace: ""
12+
runtime: go
13+
...
14+
envs:
15+
- name: EXAMPLE
16+
value: '{{ configMap:myconfigmap:key }}'
17+
----
18+
+
19+
* Substitute `EXAMPLE` with the name of the environment variable.
20+
* Substitute `myconfigmap` with the name of the target config map.
21+
* Substitute `key` with the key mapped to the target value.
22+
23+
. Save the configuration.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[id="serverless-functions-key-value-in-secret-to-env-variable_{context}"]
2+
= Setting environment variable from a key value defined in a secret
3+
4+
. Open the `func.yaml` file for your function.
5+
6+
. For each value from a secret key-value pair that you want to assign to an environment variable, add the following YAML to the `envs` section:
7+
+
8+
[source,yaml]
9+
----
10+
name: test
11+
namespace: ""
12+
runtime: go
13+
...
14+
envs:
15+
- name: EXAMPLE
16+
value: '{{ secret:mysecret:key }}'
17+
----
18+
+
19+
* Substitute `EXAMPLE` with the name of the environment variable.
20+
* Substitute `mysecret` with the name of the target secret.
21+
* Substitute `key` with the key mapped to the target value.
22+
23+
. Save the configuration.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[id="serverless-functions-mounting-configmap-as-volume_{context}"]
2+
= Mounting a config map as a volume
3+
4+
. Open the `func.yaml` file for your function.
5+
6+
. For each config map you want to mount as a volume, add the following YAML to the `volumes` section:
7+
+
8+
[source,yaml]
9+
----
10+
name: test
11+
namespace: ""
12+
runtime: go
13+
...
14+
volumes:
15+
- configMap: myconfigmap
16+
path: /workspace/configmap
17+
----
18+
+
19+
* Substitute `myconfigmap` with the name of the target config map.
20+
* Substitute `/workspace/configmap` with the path where you want to mount the config map.
21+
22+
. Save the configuration.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[id="serverless-functions-mounting-secret-as-volume_{context}"]
2+
= Mounting a secret as a volume
3+
4+
. Open the `func.yaml` file for your function.
5+
6+
. For each secret you want to mount as a volume, add the following YAML to the `volumes` section:
7+
+
8+
[source,yaml]
9+
----
10+
name: test
11+
namespace: ""
12+
runtime: go
13+
...
14+
volumes:
15+
- secret: mysecret
16+
path: /workspace/secret
17+
----
18+
+
19+
* Substitute `mysecret` with the name of the target secret.
20+
* Substitute `/workspace/secret` with the path where you want to mount the secret.
21+
22+
. Save the configuration.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[id="serverless-functions-secrets-configmaps-interactively-specialized_{context}"]
2+
= Modifying function access to secrets and config maps interactively with specialized commands
3+
4+
Every time you run the `kn func config` utility, you need to navigate the entire dialogue to select the operation you need, as shown in the previous section. To save steps, you can directly execute a specific operation by running a more specific form of the `kn func config` command:
5+
6+
* To list configured environment variables:
7+
+
8+
[source,terminal]
9+
----
10+
$ kn func config envs [-p <function-project-path>]
11+
----
12+
13+
* To add environment variables to the function configuration:
14+
+
15+
[source,terminal]
16+
----
17+
$ kn func config envs add [-p <function-project-path>]
18+
----
19+
20+
* To remove environment variables from the function configuration:
21+
+
22+
[source,terminal]
23+
----
24+
$ kn func config envs remove [-p <function-project-path>]
25+
----
26+
27+
* To list configured volumes:
28+
+
29+
[source,terminal]
30+
----
31+
$ kn func config volumes [-p <function-project-path>]
32+
----
33+
34+
* To add a volume to the function configuration:
35+
+
36+
[source,terminal]
37+
----
38+
$ kn func config volumes add [-p <function-project-path>]
39+
----
40+
41+
* To remove a volume from the function configuration:
42+
+
43+
[source,terminal]
44+
----
45+
$ kn func config volumes remove [-p <function-project-path>]
46+
----
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[id="serverless-functions-secrets-configmaps-interactively_{context}"]
2+
= Modifying function access to secrets and config maps interactively
3+
4+
You can manage the secrets and config maps accessed by your function by using the `kn func config` interactive utility.
5+
6+
.Procedure
7+
8+
. Run the following command in the function project directory:
9+
+
10+
[source,terminal]
11+
----
12+
$ kn func config
13+
----
14+
+
15+
Alternatively, you can specify the function project directory using the `--path` or `-p` option.
16+
17+
. Use the interactive interface to perform the necessary operation. For example, using the utility to list configured volumes produces an output similar to this:
18+
+
19+
[source,terminal]
20+
----
21+
$ kn func config
22+
? What do you want to configure? Volumes
23+
? What operation do you want to perform? List
24+
Configured Volumes mounts:
25+
- Secret "mysecret" mounted at path: "/workspace/secret"
26+
- Secret "mysecret2" mounted at path: "/workspace/secret2"
27+
----
28+
+
29+
This scheme shows all operations available in the interactive utility and how to navigate to them:
30+
+
31+
[source]
32+
----
33+
kn func config
34+
├─> Environment variables
35+
│   ├─> Add
36+
│   │  ├─> ConfigMap: Add all key-value pairs from a config map
37+
│   │  ├─> ConfigMap: Add value from a key in a config map
38+
│ │  ├─> Secret: Add all key-value pairs from a secret
39+
│ │ └─> Secret: Add value from a key in a secret
40+
│   ├─> List: List all configured environment variables
41+
│   └─> Remove: Remove a configured environment variable
42+
└─> Volumes
43+
├─> Add
44+
│   ├─> ConfigMap: Mount a config map as a volume
45+
│   └─> Secret: Mount a secret as a volume
46+
├─> List: List all configured volumes
47+
└─> Remove: Remove a configured volume
48+
----
49+
50+
. Optional. Deploy the function to make the changes take effect:
51+
+
52+
[source,terminal]
53+
----
54+
$ kn func deploy -p test
55+
----
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
include::modules/serverless-document-attributes.adoc[]
2+
[id="serverless-functions-accessing-secrets-configmaps"]
3+
= Accessing secrets and config maps from Serverless functions
4+
include::modules/common-attributes.adoc[]
5+
:context: serverless-functions-secrets
6+
7+
toc::[]
8+
9+
Your functions, after deployed to the cluster, can access data stored in secrets and config maps. This data can be mounted as volumes, or assigned to environment variables. You can configure this access interactively by using the Knative CLI `kn func` commands or manually by editing the function configuration file.
10+
11+
[IMPORTANT]
12+
====
13+
To access secrets and config maps, the function needs to be deployed on the cluster. This functionality is not available to a function running locally.
14+
15+
If a secret or config map value cannot be accessed, the deployment fails with an error message specifying the inaccessible values.
16+
====
17+
18+
include::modules/serverless-functions-secrets-configmaps-interactively.adoc[leveloffset=+1]
19+
include::modules/serverless-functions-secrets-configmaps-interactively-specialized.adoc[leveloffset=+1]
20+
21+
[id="serverless-functions-secrets-configmaps-manually_{context}"]
22+
== Adding function access to secrets and config maps manually
23+
24+
You can manually add configuration for accessing secrets and config maps to your function.
25+
26+
include::modules/serverless-functions-mounting-secret-as-volume.adoc[leveloffset=+2]
27+
include::modules/serverless-functions-mounting-configmap-as-volume.adoc[leveloffset=+2]
28+
include::modules/serverless-functions-key-value-in-secret-to-env-variable.adoc[leveloffset=+2]
29+
include::modules/serverless-functions-key-value-in-configmap-to-env-variable.adoc[leveloffset=+2]
30+
include::modules/serverless-functions-all-values-in-secret-to-env-variables.adoc[leveloffset=+2]
31+
include::modules/serverless-functions-all-values-in-configmap-to-env-variables.adoc[leveloffset=+2]

0 commit comments

Comments
 (0)