Chapter 10. Accessing secrets and config maps from functions
After your functions have been deployed to the cluster, they 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, or by manually by editing the function configuration YAML file.
To access secrets and config maps, the function must be deployed on the cluster. This functionality is not available to a function running locally.
If a secret or config map value cannot be accessed, the deployment fails with an error message specifying the inaccessible values.
10.1. Modifying function access to secrets and config maps interactively Copy linkLink copied to clipboard!
You can manage the secrets and config maps accessed by your function by using the kn func config
interactive utility. The available operations include listing, adding, and removing values stored in config maps and secrets as environment variables, as well as listing, adding, and removing volumes. This functionality enables you to manage what data stored on the cluster is accessible by your function.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on the cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a function.
Procedure
Run the following command in the function project directory:
kn func config
$ kn func config
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alternatively, you can specify the function project directory using the
--path
or-p
option.Use the interactive interface to perform the necessary operation. For example, using the utility to list configured volumes produces an output similar to this:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This scheme shows all operations available in the interactive utility and how to navigate to them:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional. Deploy the function to make the changes take effect:
kn func deploy -p test
$ kn func deploy -p test
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2. Modifying function access to secrets and config maps interactively by using specialized commands Copy linkLink copied to clipboard!
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:
To list configured environment variables:
kn func config envs [-p <function-project-path>]
$ kn func config envs [-p <function-project-path>]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To add environment variables to the function configuration:
kn func config envs add [-p <function-project-path>]
$ kn func config envs add [-p <function-project-path>]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To remove environment variables from the function configuration:
kn func config envs remove [-p <function-project-path>]
$ kn func config envs remove [-p <function-project-path>]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To list configured volumes:
kn func config volumes [-p <function-project-path>]
$ kn func config volumes [-p <function-project-path>]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To add a volume to the function configuration:
kn func config volumes add [-p <function-project-path>]
$ kn func config volumes add [-p <function-project-path>]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To remove a volume from the function configuration:
kn func config volumes remove [-p <function-project-path>]
$ kn func config volumes remove [-p <function-project-path>]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.3. Adding function access to secrets and config maps manually Copy linkLink copied to clipboard!
You can manually add configuration for accessing secrets and config maps to your function. This might be preferable to using the kn func config
interactive utility and commands, for example when you have an existing configuration snippet.
10.3.1. Mounting a secret as a volume Copy linkLink copied to clipboard!
You can mount a secret as a volume. Once a secret is mounted, you can access it from the function as a regular file. This enables you to store on the cluster data needed by the function, for example, a list of URIs that need to be accessed by the function.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on the cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a function.
Procedure
-
Open the
func.yaml
file for your function. For each secret you want to mount as a volume, add the following YAML to the
volumes
section:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Substitute
mysecret
with the name of the target secret. Substitute
/workspace/secret
with the path where you want to mount the secret.For example, to mount the
addresses
secret, use the following YAML:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Substitute
- Save the configuration.
10.3.2. Mounting a config map as a volume Copy linkLink copied to clipboard!
You can mount a config map as a volume. Once a config map is mounted, you can access it from the function as a regular file. This enables you to store on the cluster data needed by the function, for example, a list of URIs that need to be accessed by the function.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on the cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a function.
Procedure
-
Open the
func.yaml
file for your function. For each config map you want to mount as a volume, add the following YAML to the
volumes
section:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Substitute
myconfigmap
with the name of the target config map. Substitute
/workspace/configmap
with the path where you want to mount the config map.For example, to mount the
addresses
config map, use the following YAML:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Substitute
- Save the configuration.
10.3.3. Setting environment variable from a key value defined in a secret Copy linkLink copied to clipboard!
You can set an environment variable from a key value defined as a secret. A value previously stored in a secret can then be accessed as an environment variable by the function at runtime. This can be useful for getting access to a value stored in a secret, such as the ID of a user.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on the cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a function.
Procedure
-
Open the
func.yaml
file for your function. 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Substitute
EXAMPLE
with the name of the environment variable. -
Substitute
mysecret
with the name of the target secret. Substitute
key
with the key mapped to the target value.For example, to access the user ID that is stored in
userdetailssecret
, use the following YAML:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Substitute
- Save the configuration.
10.3.4. Setting environment variable from a key value defined in a config map Copy linkLink copied to clipboard!
You can set an environment variable from a key value defined as a config map. A value previously stored in a config map can then be accessed as an environment variable by the function at runtime. This can be useful for getting access to a value stored in a config map, such as the ID of a user.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on the cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a function.
Procedure
-
Open the
func.yaml
file for your function. 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Substitute
EXAMPLE
with the name of the environment variable. -
Substitute
myconfigmap
with the name of the target config map. Substitute
key
with the key mapped to the target value.For example, to access the user ID that is stored in
userdetailsmap
, use the following YAML:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Substitute
- Save the configuration.
10.3.5. Setting environment variables from all values defined in a secret Copy linkLink copied to clipboard!
You can set an environment variable from all values defined in a secret. Values previously stored in a secret can then be accessed as environment variables by the function at runtime. This can be useful for simultaneously getting access to a collection of values stored in a secret, for example, a set of data pertaining to a user.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on the cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a function.
Procedure
-
Open the
func.yaml
file for your function. For every secret for which you want to import all key-value pairs as environment variables, add the following YAML to the
envs
section:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Substitute
mysecret
with the name of the target secret.
For example, to access all user data that is stored in
userdetailssecret
, use the following YAML:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Save the configuration.
10.3.6. Setting environment variables from all values defined in a config map Copy linkLink copied to clipboard!
You can set an environment variable from all values defined in a config map. Values previously stored in a config map can then be accessed as environment variables by the function at runtime. This can be useful for simultaneously getting access to a collection of values stored in a config map, for example, a set of data pertaining to a user.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on the cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a function.
Procedure
-
Open the
func.yaml
file for your function. 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Substitute
myconfigmap
with the name of the target config map.
For example, to access all user data that is stored in
userdetailsmap
, use the following YAML:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Save the file.