Ejemplo n.º 1
0
def get_volume(
    volume: str, claim_name: str = None, host_path: str = None, read_only: bool = None
) -> k8s_schemas.V1Volume:
    if claim_name:
        pv_claim = k8s_schemas.V1PersistentVolumeClaimVolumeSource(
            claim_name=claim_name, read_only=read_only
        )
        return k8s_schemas.V1Volume(name=volume, persistent_volume_claim=pv_claim)

    if host_path:
        return k8s_schemas.V1Volume(
            name=volume, host_path=k8s_schemas.V1HostPathVolumeSource(path=host_path)
        )

    empty_dir = k8s_schemas.V1EmptyDirVolumeSource()
    return k8s_schemas.V1Volume(name=volume, empty_dir=empty_dir)
Ejemplo n.º 2
0
def get_volume_from_secret(secret: V1K8sResourceType) -> Optional[k8s_schemas.V1Volume]:
    if not secret:
        return None
    if secret.schema.mount_path:
        secret_volume = k8s_schemas.V1SecretVolumeSource(
            secret_name=secret.name, items=secret.schema.items
        )
        return k8s_schemas.V1Volume(name=secret.name, secret=secret_volume)
Ejemplo n.º 3
0
def get_volume_from_connection(
    connection: V1ConnectionType, ) -> Optional[k8s_schemas.V1Volume]:
    if not connection:
        return None
    if connection.is_volume_claim:
        pv_claim = k8s_schemas.V1PersistentVolumeClaimVolumeSource(
            claim_name=connection.schema.volume_claim,
            read_only=connection.schema.read_only,
        )
        return k8s_schemas.V1Volume(name=connection.name,
                                    persistent_volume_claim=pv_claim)

    if connection.is_host_path:
        return k8s_schemas.V1Volume(
            name=connection.name,
            host_path=k8s_schemas.V1HostPathVolumeSource(
                path=connection.schema.host_path),
        )
Ejemplo n.º 4
0
def get_volume_from_config_map(
    config_map: V1K8sResourceType,
) -> Optional[k8s_schemas.V1Volume]:
    if not config_map:
        return None
    if config_map.schema.mount_path:
        config_map_volume = k8s_schemas.V1ConfigMapVolumeSource(
            name=config_map.name, items=config_map.schema.items
        )
        return k8s_schemas.V1Volume(name=config_map.name, config_map=config_map_volume)
Ejemplo n.º 5
0
def get_shm_context_volume() -> k8s_schemas.V1Volume:
    """
    Mount an tmpfs volume to /dev/shm.
    This will set /dev/shm size to half of the RAM of node.
    By default, /dev/shm is very small, only 64MB.
    Some experiments will fail due to lack of share memory,
    such as some experiments running on Pytorch.
    """
    return k8s_schemas.V1Volume(
        name=constants.CONTEXT_VOLUME_SHM,
        empty_dir=k8s_schemas.V1EmptyDirVolumeSource(medium="Memory"),
    )
Ejemplo n.º 6
0
    def test_get_pod_spec(self):
        init_container = k8s_schemas.V1Container(name="init")
        main_container = k8s_schemas.V1Container(name="main")
        sidecar_container = k8s_schemas.V1Container(name="sidecar")
        volumes = [k8s_schemas.V1Volume(name="vol")]
        labels = {"key": "labels"}
        annotations = {"key": "annotations"}
        node_selector = {"key": "selector"}
        affinity = [{"key": "affinity"}]
        tolerations = {"key": "tolerations"}
        security_context = {"uid": 222, "gid": 222}
        restart_policy = "never"

        with self.assertRaises(PolypodException):
            get_pod_spec(
                namespace="default",
                main_container=None,
                sidecar_containers=None,
                init_containers=None,
                resource_name="foo",
                volumes=None,
                environment=V1Environment(),
                labels={},
                annotations={},
            )

        environment = V1Environment(
            service_account_name="sa",
            labels=labels,
            annotations=annotations,
            node_selector=node_selector,
            affinity=affinity,
            tolerations=tolerations,
            security_context=security_context,
            image_pull_secrets=[],
            restart_policy=restart_policy,
        )
        metadata, pod_spec = get_pod_spec(
            namespace="default",
            resource_name="foo",
            main_container=main_container,
            sidecar_containers=None,
            init_containers=None,
            volumes=None,
            environment=environment,
            labels=environment.labels,
            annotations=environment.annotations,
        )

        assert metadata.name == "foo"
        assert metadata.labels == labels
        assert metadata.namespace == "default"
        assert metadata.annotations == annotations

        assert isinstance(pod_spec, k8s_schemas.V1PodSpec)
        assert pod_spec.security_context == security_context
        assert pod_spec.restart_policy == "never"
        assert pod_spec.service_account_name == "sa"
        assert pod_spec.init_containers == []
        assert pod_spec.containers == [main_container]
        assert pod_spec.volumes is None
        assert pod_spec.node_selector == node_selector
        assert pod_spec.tolerations == tolerations
        assert pod_spec.affinity == affinity

        environment = V1Environment(
            service_account_name="sa",
            labels=labels,
            annotations=annotations,
            node_selector=node_selector,
            affinity=affinity,
            tolerations=tolerations,
            security_context=security_context,
            image_pull_secrets=[],
            restart_policy=restart_policy,
        )
        metadata, pod_spec = get_pod_spec(
            namespace="default",
            main_container=main_container,
            sidecar_containers=[sidecar_container],
            init_containers=[init_container],
            resource_name="foo",
            volumes=volumes,
            environment=environment,
            labels={},
            annotations={},
        )

        assert pod_spec.init_containers == [init_container]
        assert pod_spec.containers == [main_container, sidecar_container]
        assert pod_spec.volumes == volumes
        assert metadata.annotations == {}