Mount Secrets
Mount Kubernetes Secrets into workspace containers to provide sensitive configuration data such as credentials, API keys, and certificates.
-
You have an active
kubectlsession with your namespace. See Overview of kubectl.By default, applying a
Secretwith thecontroller.devfile.io/mount-to-devworkspace: 'true'label restarts all running workspaces in the namespace. To mount the Secret only at workspace start and prevent automatic restarts, add thecontroller.devfile.io/mount-on-start: 'true'annotation.
-
Create a Secret with the required labels and annotations:
kind: Secret apiVersion: v1 metadata: name: my-credentials labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/mount-as: file controller.devfile.io/mount-path: /etc/my-credentials type: Opaque data: api-key: <base64_encoded_api_key>where:
controller.devfile.io/mount-to-devworkspace-
Required label to mount the Secret to all workspaces.
controller.devfile.io/watch-secret-
Watch the Secret for changes and update mounted files.
controller.devfile.io/mount-as-
Mount type:
file,subpath, orenv. controller.devfile.io/mount-path-
Path where the Secret data is mounted.
-
Apply the Secret to your namespace:
$ kubectl apply -f my-credentials.yaml -n <your_namespace> -
Optional: Add annotations to the Secret to customize the mounting behavior.
Table 1. Secret mounting annotations Annotation Description controller.devfile.io/mount-path: <path>Overrides the default mount path. The default mount path is
/etc/secret/<Secret_name>.controller.devfile.io/mount-as: fileEach key in the Secret data becomes a file in the mount path directory.
controller.devfile.io/mount-as: subpathSimilar to
file, but uses subPath volumes for better compatibility.controller.devfile.io/mount-as: envEach key-value pair becomes an environment variable in all workspace containers.
controller.devfile.io/mount-on-start: 'true'When set to
true, the Secret is mounted only when a workspace starts, not while it is already running. This prevents workspace restarts when the Secret is created.controller.devfile.io/mount-to-devworkspace-include:Specifies a comma-separated list of
DevWorkspacename patterns. When set, the Secret is mounted only to workspaces whose names match at least one pattern.controller.devfile.io/mount-to-devworkspace-exclude:Specifies a comma-separated list of
DevWorkspacename patterns. When set, the Secret is mounted to all workspaces except those whose names match a pattern.When both controller.devfile.io/mount-to-devworkspace-includeandcontroller.devfile.io/mount-to-devworkspace-excludeannotations are set, the Secret is mounted only to workspaces that match the include pattern and do not match the exclude pattern.For example, to mount Secret data as environment variables:
kind: Secret apiVersion: v1 metadata: name: my-env-secret labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/mount-as: env type: Opaque stringData: DATABASE_URL: postgresql://localhost:5432/mydb API_SECRET: my-secret-keyFor example, to mount a Maven
settings.xmlfile to the/home/user/.m2/path usingsubpath:kind: Secret apiVersion: v1 metadata: name: maven-settings labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/mount-path: /home/user/.m2/ controller.devfile.io/mount-as: subpath type: Opaque stringData: settings.xml: | <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> </settings>After the workspace starts, the
/home/user/.m2/settings.xmlfile is available in theDevWorkspacecontainers. To use a custom path with Maven, runmvn --settings /home/user/.m2/settings.xml clean install. -
Start or restart your workspace to apply the mounted Secret.
-
For
fileorsubpathmounts, verify the Secret data is available at the mount path:$ ls /etc/my-credentials -
For
envmounts, verify the environment variables are set:$ echo $DATABASE_URL