OpenShift/k8s Provider: Allow passing env vars to Pods

For the OpenShift and Kubernetes drivers, allow passing env vars to the
Pod nodes via their label config.
It is not possible to set persistent env vars in containers on run time
because there is no login shell available. Thus, we need to pass in any
env vars during node launch. This allows to set, e.g., ``http_proxy``
variables.

The env vars are passed as a list of dicts with ``name`` and ``value``
fields as per the k8s Pod YAML schema. [1]

```
- name: pod-fedora
  type: pod
  image: docker.io/fedora:28
  env:
  - name: foo
    value: bar
```

[1] https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

Change-Id: Ibbd9222fcd8f7dc5be227e7f5c8d8772a4c594e2
This commit is contained in:
Benjamin Schanzel 2020-07-13 15:03:31 +02:00
parent 369799dea6
commit b76a0f458e
8 changed files with 108 additions and 2 deletions

View File

@ -1400,6 +1400,25 @@ Selecting the kubernetes driver adds the following options to the
:value:`providers.[kubernetes].pools.labels.type.pod` label type;
specifies the amount of memory in MB to request for the pod.
.. attr:: env
:type: list
:default: []
Only used by the
:value:`providers.[kubernetes].pools.labels.type.pod` label type;
A list of environment variables to pass to the Pod.
.. attr:: name
:type: str
:required:
The name of the environment variable passed to the Pod.
.. attr:: value
:type: str
:required:
The value of the environment variable passed to the Pod.
Openshift Driver
@ -1552,6 +1571,26 @@ Selecting the openshift driver adds the following options to the
:value:`providers.[openshift].labels.type.pod` label type;
specifies the amount of memory in MB to request for the pod.
.. attr:: env
:type: list
:default: []
Only used by the
:value:`providers.[openshift].labels.type.pod` label type;
A list of environment variables to pass to the Pod.
.. attr:: name
:type: str
:required:
The name of the environment variable passed to the Pod.
.. attr:: value
:type: str
:required:
The value of the environment variable passed to the Pod.
Openshift Pods Driver
---------------------
@ -1664,6 +1703,24 @@ Selecting the openshift pods driver adds the following options to the
interpreter on Ansible >=2.8, and default to
``/usr/bin/python2`` for earlier versions.
.. attr:: env
:type: list
:default: []
A list of environment variables to pass to the Pod.
.. attr:: name
:type: str
:required:
The name of the environment variable passed to the Pod.
.. attr:: value
:type: str
:required:
The value of the environment variable passed to the Pod.
AWS EC2 Driver
--------------

View File

@ -30,7 +30,8 @@ class KubernetesLabel(ConfigValue):
other.python_path == self.python_path and
other.image == self.image and
other.cpu == self.cpu and
other.memory == self.memory)
other.memory == self.memory and
other.env == self.env)
return False
def __repr__(self):
@ -61,6 +62,7 @@ class KubernetesPool(ConfigPool):
pl.python_path = label.get('python-path', 'auto')
pl.cpu = label.get('cpu')
pl.memory = label.get('memory')
pl.env = label.get('env', [])
pl.pool = self
self.labels[pl.name] = pl
full_config.labels[label['name']].pools.append(self)
@ -97,6 +99,11 @@ class KubernetesProviderConfig(ProviderConfig):
self.pools[pp.name] = pp
def getSchema(self):
env_var = {
v.Required('name'): str,
v.Required('value'): str,
}
k8s_label = {
v.Required('name'): str,
v.Required('type'): str,
@ -105,6 +112,7 @@ class KubernetesProviderConfig(ProviderConfig):
'python-path': str,
'cpu': int,
'memory': int,
'env': [env_var],
}
pool = ConfigPool.getCommonSchemaDict()

View File

@ -276,6 +276,7 @@ class KubernetesProvider(Provider):
'command': ["/bin/sh", "-c"],
'args': ["while true; do sleep 30; done;"],
'workingDir': '/tmp',
'env': label.env,
}
if label.cpu or label.memory:

View File

@ -31,7 +31,8 @@ class OpenshiftLabel(ConfigValue):
other.image == self.image and
other.cpu == self.cpu and
other.memory == self.memory and
other.python_path == self.python_path)
other.python_path == self.python_path and
other.env == self.env)
return False
def __repr__(self):
@ -62,6 +63,7 @@ class OpenshiftPool(ConfigPool):
pl.cpu = label.get('cpu')
pl.memory = label.get('memory')
pl.python_path = label.get('python-path', 'auto')
pl.env = label.get('env', [])
pl.pool = self
self.labels[pl.name] = pl
full_config.labels[label['name']].pools.append(self)
@ -99,6 +101,11 @@ class OpenshiftProviderConfig(ProviderConfig):
self.pools[pp.name] = pp
def getSchema(self):
env_var = {
v.Required('name'): str,
v.Required('value'): str,
}
openshift_label = {
v.Required('name'): str,
v.Required('type'): str,
@ -107,6 +114,7 @@ class OpenshiftProviderConfig(ProviderConfig):
'cpu': int,
'memory': int,
'python-path': str,
'env': [env_var],
}
pool = ConfigPool.getCommonSchemaDict()

View File

@ -211,6 +211,7 @@ class OpenshiftProvider(Provider):
'command': ["/bin/sh", "-c"],
'args': ["while true; do sleep 30; done;"],
'workingDir': '/tmp',
'env': label.env,
}
if label.cpu or label.memory:
spec_body['resources'] = {}

View File

@ -44,6 +44,11 @@ class OpenshiftPodsProviderConfig(OpenshiftProviderConfig):
self.pools[pp.name] = pp
def getSchema(self):
env_var = {
v.Required('name'): str,
v.Required('value'): str,
}
openshift_label = {
v.Required('name'): str,
v.Required('image'): str,
@ -51,6 +56,7 @@ class OpenshiftPodsProviderConfig(OpenshiftProviderConfig):
'cpu': int,
'memory': int,
'python-path': str,
'env': [env_var],
}
pool = ConfigPool.getCommonSchemaDict()

View File

@ -138,6 +138,11 @@ providers:
image: docker.io/fedora:28
cpu: 2
memory: 512
env:
- name: FOO
value: hello
- name: BAR
value: world
- name: openshift
driver: openshift
@ -153,6 +158,11 @@ providers:
python-path: /usr/bin/python3
memory: 512
cpu: 2
env:
- name: FOO
value: hello
- name: BAR
value: world
- name: ec2-us-east-2
driver: aws
@ -183,6 +193,9 @@ providers:
labels:
- name: openshift-pod
image: docker.io/fedora:28
env:
- name: FOO
value: bar
diskimages:
- name: trusty

View File

@ -0,0 +1,12 @@
---
features:
- |
Support for passing environment variables to k8s and OpenShift Pod build
nodes has been added.
It is not possible to set persistent env vars in containers on run time
because there is no login shell available. Thus, we need to pass in any
env vars during node launch. This allows to set, e.g., `http_proxy`
variables. Environment variables can be defined on node labels as a list
of dictionaries with `name` and `value` fields as per the k8s container
YAML schema.