Skip to content

Commit

Permalink
Merge pull request #3 from optimizely/gp-volumes-fix
Browse files Browse the repository at this point in the history
Gp volumes fix
  • Loading branch information
gphillips8frw committed Nov 5, 2021
2 parents ade8736 + 1e21296 commit 4765331
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
13 changes: 6 additions & 7 deletions kubeluigi/k8s.py
Expand Up @@ -59,32 +59,31 @@ def pod_spec_from_dict(
if "imagePullPolicy" in container:
container["image_pull_policy"] = container.pop("imagePullPolicy")
if "volume_mounts" in container and container['volume_mounts']:
container, container_volumes = get_container_and_volumes(container)
volumes.append(container_volumes)
container = get_container_with_volume_mounts(container)
containers.append(V1Container(**container))
if 'volumes' in spec_schema:
for volume in spec_schema['volumes']:
volumes.append(V1Volume(**volume))
pod_template = V1PodTemplateSpec(
metadata=V1ObjectMeta(name=name, labels=labels),
spec=V1PodSpec(restart_policy=restartPolicy, containers=containers, volumes=volumes),
)
return pod_template


def get_container_and_volumes(container):
def get_container_with_volume_mounts(container):
"""
Returns a container with V1VolumeMount objects from the spec schema of a container
and a list of V1volume objects
"""
volumes_spec = container['volume_mounts']
mount_volumes = []
volumes = []
for volume in volumes_spec:
mount_path = volume['mountPath']
name = volume['name']
host_path = volume['host_path']
mount_volumes.append(V1VolumeMount(mount_path=mount_path, name=name))
volumes.append(V1Volume(name=name, host_path=V1HostPathVolumeSource(path=host_path)))
container['volume_mounts'] = mount_volumes
return container, volumes
return container


def get_job_pods(job: V1Job) -> List[V1Pod]:
Expand Down
37 changes: 26 additions & 11 deletions test/kubernetes_helpers_test.py
Expand Up @@ -11,7 +11,7 @@
FailedJob,
run_and_track_job,
BackgroundJobLogger,
get_container_and_volumes
get_container_with_volume_mounts
)

from kubernetes.client import V1Pod, V1PodCondition
Expand Down Expand Up @@ -55,13 +55,27 @@ def test_pod_spec_from_dict():
"args": ["my_arg"],
"imagePullPolicy": "Always",
"env": [{"name": "my_env", "value": "env"}],
"volume_mounts":[
"volume_mounts": [
{"name": "Vname", "mountPath": "VmountPath", "host_path": "VhostPath"}
]
],
}
]
],
"volumes": [
{
"name": "volname",
"csi": {
"driver": "blob.csi.azure.com",
"volumeAttributes": {
"containerName": "volcontainername",
"secretName": "volsecret",
"mountOptions": "-o allow_other --file-cache-timeout-in-seconds=120",
},
},
}
],
}


def test_pod_spec_with_volume_from_dict():

labels = {"l1": "label1"}
Expand All @@ -70,14 +84,16 @@ def test_pod_spec_with_volume_from_dict():
assert pod_spec.metadata.name == "name_of_pod"
assert pod_spec.metadata.labels == labels
assert pod_spec.spec.restart_policy == "Never"
assert pod_spec.spec.volumes == [
V1Volume(**dummy_pod_spec_with_volume['volumes'][0])
]
container = pod_spec.spec.containers[0]
assert container.name == dummy_pod_spec["containers"][0]["name"]
assert container.image == dummy_pod_spec["containers"][0]["image"]
assert container.env == dummy_pod_spec["containers"][0]["env"]
assert container.name == dummy_pod_spec_with_volume["containers"][0]["name"]
assert container.image == dummy_pod_spec_with_volume["containers"][0]["image"]
assert container.env == dummy_pod_spec_with_volume["containers"][0]["env"]
assert container.volume_mounts == [V1VolumeMount(mount_path="VmountPath", name="Vname")]



dummy_container = {
"name": "container_name",
"image": "my_image",
Expand All @@ -89,10 +105,9 @@ def test_pod_spec_with_volume_from_dict():
]
}

def test_get_containers_and_volumes():
container, volumes = get_container_and_volumes(dummy_container)
def test_get_container_with_volume_mounts():
container = get_container_with_volume_mounts(dummy_container)
assert container['volume_mounts'] == [V1VolumeMount(mount_path="VmountPath", name="Vname")]
assert volumes == [V1Volume(name='Vname', host_path=V1HostPathVolumeSource(path='VhostPath'))]


def test_job_definition():
Expand Down

0 comments on commit 4765331

Please sign in to comment.