This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Questo contenuto non è disponibile nella lingua selezionata.
Chapter 27. Downward API
27.1. Overview Copia collegamentoCollegamento copiato negli appunti!
It is common for containers to consume information about API objects. The downward API is a mechanism that allows containers to do this without coupling to OpenShift Enterprise. Containers can consume information from the downward API using environment variables or a volume plug-in.
27.2. Selecting Fields Copia collegamentoCollegamento copiato negli appunti!
Fields within the pod are selected using the FieldRef
API type. FieldRef
has two fields:
Field | Description |
---|---|
| The path of the field to select, relative to the pod. |
|
The API version to interpret the |
Currently, there are four valid selectors in the v1 API:
Selector | Description |
---|---|
| The pod’s name. |
| The pod’s namespace. |
| The pod’s labels. |
| The pod’s annotations. |
The apiVersion
field, if not specified, defaults to the API version of the enclosing pod template.
In the future, more information, such as resource limits for pods and information about services, will be available using the downward API.
27.3. Using Environment Variables Copia collegamentoCollegamento copiato negli appunti!
One mechanism for consuming the downward API is using a container’s environment variables. The EnvVar
type’s valueFrom
field (of type EnvVarSource
) is used to specify that the variable’s value should come from a FieldRef
source instead of the literal value specified by the value
field. In the future, additional sources may be supported; currently the source’s fieldRef
field is used to select a field from the downward API.
Only constant attributes of the pod can be consumed this way, as environment variables cannot be updated once a process is started in a way that allows the process to be notified that the value of a variable has changed. The fields supported using environment variables are:
- Pod name
- Pod namespace
For example:
Create a
pod.json
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
pod.json
file:oc create -f pod.json
$ oc create -f pod.json
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs:
oc logs -p dapi-env-test-pod
$ oc logs -p dapi-env-test-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You should see
MY_POD_NAME
andMY_POD_NAMESPACE
in the logs.
27.4. Using the Volume Plug-in Copia collegamentoCollegamento copiato negli appunti!
Another mechanism for consuming the downward API is using a volume plug-in. The downward API volume plug-in creates a volume with configured fields projected into files. The metadata
field of the VolumeSource
API object is used to configure this volume. The plug-in supports the following fields:
- Pod name
- Pod namespace
- Pod annotations
- Pod labels
Example 27.1. Downward API Volume Plug-in Configuration
For example:
Create a
volume-pod.json
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
volume-pod.json
file:oc create -f volume-pod.json
$ oc create -f volume-pod.json
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs and verify the presence of the configured fields:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow