Skip to content

Commit 4765331

Browse files
Merge pull request #3 from optimizely/gp-volumes-fix
Gp volumes fix
2 parents ade8736 + 1e21296 commit 4765331

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

kubeluigi/k8s.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,31 @@ def pod_spec_from_dict(
5959
if "imagePullPolicy" in container:
6060
container["image_pull_policy"] = container.pop("imagePullPolicy")
6161
if "volume_mounts" in container and container['volume_mounts']:
62-
container, container_volumes = get_container_and_volumes(container)
63-
volumes.append(container_volumes)
62+
container = get_container_with_volume_mounts(container)
6463
containers.append(V1Container(**container))
64+
if 'volumes' in spec_schema:
65+
for volume in spec_schema['volumes']:
66+
volumes.append(V1Volume(**volume))
6567
pod_template = V1PodTemplateSpec(
6668
metadata=V1ObjectMeta(name=name, labels=labels),
6769
spec=V1PodSpec(restart_policy=restartPolicy, containers=containers, volumes=volumes),
6870
)
6971
return pod_template
7072

7173

72-
def get_container_and_volumes(container):
74+
def get_container_with_volume_mounts(container):
7375
"""
7476
Returns a container with V1VolumeMount objects from the spec schema of a container
7577
and a list of V1volume objects
7678
"""
7779
volumes_spec = container['volume_mounts']
7880
mount_volumes = []
79-
volumes = []
8081
for volume in volumes_spec:
8182
mount_path = volume['mountPath']
8283
name = volume['name']
83-
host_path = volume['host_path']
8484
mount_volumes.append(V1VolumeMount(mount_path=mount_path, name=name))
85-
volumes.append(V1Volume(name=name, host_path=V1HostPathVolumeSource(path=host_path)))
8685
container['volume_mounts'] = mount_volumes
87-
return container, volumes
86+
return container
8887

8988

9089
def get_job_pods(job: V1Job) -> List[V1Pod]:

test/kubernetes_helpers_test.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
FailedJob,
1212
run_and_track_job,
1313
BackgroundJobLogger,
14-
get_container_and_volumes
14+
get_container_with_volume_mounts
1515
)
1616

1717
from kubernetes.client import V1Pod, V1PodCondition
@@ -55,13 +55,27 @@ def test_pod_spec_from_dict():
5555
"args": ["my_arg"],
5656
"imagePullPolicy": "Always",
5757
"env": [{"name": "my_env", "value": "env"}],
58-
"volume_mounts":[
58+
"volume_mounts": [
5959
{"name": "Vname", "mountPath": "VmountPath", "host_path": "VhostPath"}
60-
]
60+
],
6161
}
62-
]
62+
],
63+
"volumes": [
64+
{
65+
"name": "volname",
66+
"csi": {
67+
"driver": "blob.csi.azure.com",
68+
"volumeAttributes": {
69+
"containerName": "volcontainername",
70+
"secretName": "volsecret",
71+
"mountOptions": "-o allow_other --file-cache-timeout-in-seconds=120",
72+
},
73+
},
74+
}
75+
],
6376
}
6477

78+
6579
def test_pod_spec_with_volume_from_dict():
6680

6781
labels = {"l1": "label1"}
@@ -70,14 +84,16 @@ def test_pod_spec_with_volume_from_dict():
7084
assert pod_spec.metadata.name == "name_of_pod"
7185
assert pod_spec.metadata.labels == labels
7286
assert pod_spec.spec.restart_policy == "Never"
87+
assert pod_spec.spec.volumes == [
88+
V1Volume(**dummy_pod_spec_with_volume['volumes'][0])
89+
]
7390
container = pod_spec.spec.containers[0]
74-
assert container.name == dummy_pod_spec["containers"][0]["name"]
75-
assert container.image == dummy_pod_spec["containers"][0]["image"]
76-
assert container.env == dummy_pod_spec["containers"][0]["env"]
91+
assert container.name == dummy_pod_spec_with_volume["containers"][0]["name"]
92+
assert container.image == dummy_pod_spec_with_volume["containers"][0]["image"]
93+
assert container.env == dummy_pod_spec_with_volume["containers"][0]["env"]
7794
assert container.volume_mounts == [V1VolumeMount(mount_path="VmountPath", name="Vname")]
7895

7996

80-
8197
dummy_container = {
8298
"name": "container_name",
8399
"image": "my_image",
@@ -89,10 +105,9 @@ def test_pod_spec_with_volume_from_dict():
89105
]
90106
}
91107

92-
def test_get_containers_and_volumes():
93-
container, volumes = get_container_and_volumes(dummy_container)
108+
def test_get_container_with_volume_mounts():
109+
container = get_container_with_volume_mounts(dummy_container)
94110
assert container['volume_mounts'] == [V1VolumeMount(mount_path="VmountPath", name="Vname")]
95-
assert volumes == [V1Volume(name='Vname', host_path=V1HostPathVolumeSource(path='VhostPath'))]
96111

97112

98113
def test_job_definition():

0 commit comments

Comments
 (0)