Skip to content

Commit

Permalink
Add volumes from the spec instead of inferring them
Browse files Browse the repository at this point in the history
  • Loading branch information
gphillips8frw committed Nov 4, 2021
1 parent a526f5b commit 1e21296
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
13 changes: 6 additions & 7 deletions kubeluigi/k8s.py
Original file line number Diff line number Diff line change
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
22 changes: 3 additions & 19 deletions test/kubernetes_helpers_test.py
Original file line number Diff line number Diff line change
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 @@ -75,20 +75,6 @@ def test_pod_spec_from_dict():
],
}

# dummy_pod_spec_with_volume = {
# "containers": [
# {
# "name": "container_name",
# "image": "my_image",
# "args": ["my_arg"],
# "imagePullPolicy": "Always",
# "env": [{"name": "my_env", "value": "env"}],
# "volume_mounts":[
# {"name": "Vname", "mountPath": "VmountPath", "host_path": "VhostPath"}
# ]
# }
# ]
# }

def test_pod_spec_with_volume_from_dict():

Expand All @@ -108,7 +94,6 @@ def test_pod_spec_with_volume_from_dict():
assert container.volume_mounts == [V1VolumeMount(mount_path="VmountPath", name="Vname")]



dummy_container = {
"name": "container_name",
"image": "my_image",
Expand All @@ -120,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 1e21296

Please sign in to comment.