Skip to content

Commit

Permalink
Merge pull request #426 from consideRatio/full-env-var-support
Browse files Browse the repository at this point in the history
Support EnvVar's with 'valueFrom' as well as with 'value'
  • Loading branch information
yuvipanda authored Sep 3, 2020
2 parents b209ce1 + 9339d82 commit 2469553
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
14 changes: 13 additions & 1 deletion kubespawner/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,24 @@ def make_pod(
if all([e is None for e in container_security_context.to_dict().values()]):
container_security_context = None

# Transform a dict into valid Kubernetes EnvVar Python representations. This
# representation shall always have a "name" field as well as either a
# "value" field or "value_from" field. For examples see the
# test_make_pod_with_env function.
prepared_env = []
for k, v in (env or {}).items():
if type(v) == dict:
if not "name" in v:
v["name"] = k
prepared_env.append(get_k8s_model(V1EnvVar, v))
else:
prepared_env.append(V1EnvVar(name=k, value=v))
notebook_container = V1Container(
name='notebook',
image=image,
working_dir=working_dir,
ports=[V1ContainerPort(name='notebook-port', container_port=port)],
env=[V1EnvVar(k, v) for k, v in (env or {}).items()],
env=prepared_env,
args=cmd,
image_pull_policy=image_pull_policy,
lifecycle=lifecycle_hooks,
Expand Down
46 changes: 43 additions & 3 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,30 @@ def test_make_pod_resources_all():

def test_make_pod_with_env():
"""
Test specification of a pod with custom environment variables
Test specification of a pod with custom environment variables.
"""
assert api_client.sanitize_for_serialization(make_pod(
name='test',
image='jupyter/singleuser:latest',
env={
'TEST_KEY': 'TEST_VALUE'
'TEST_KEY_1': 'TEST_VALUE',
'TEST_KEY_2': {
'valueFrom': {
'secretKeyRef': {
'name': 'my-k8s-secret',
'key': 'password',
},
},
},
'TEST_KEY_NAME_IGNORED': {
'name': 'TEST_KEY_3',
'valueFrom': {
'secretKeyRef': {
'name': 'my-k8s-secret',
'key': 'password',
},
},
},
},
cmd=['jupyterhub-singleuser'],
port=8888,
Expand All @@ -464,7 +481,30 @@ def test_make_pod_with_env():
'automountServiceAccountToken': False,
"containers": [
{
"env": [{'name': 'TEST_KEY', 'value': 'TEST_VALUE'}],
"env": [
{
'name': 'TEST_KEY_1',
'value': 'TEST_VALUE',
},
{
'name': 'TEST_KEY_2',
'valueFrom': {
'secretKeyRef': {
'name': 'my-k8s-secret',
'key': 'password',
},
},
},
{
'name': 'TEST_KEY_3',
'valueFrom': {
'secretKeyRef': {
'name': 'my-k8s-secret',
'key': 'password',
},
},
},
],
"name": "notebook",
"image": "jupyter/singleuser:latest",
"imagePullPolicy": "IfNotPresent",
Expand Down

0 comments on commit 2469553

Please sign in to comment.