def test_get_main_container_simple_params(self):
        initial_mounts = [
            k8s_schemas.V1VolumeMount(name="test",
                                      mount_path="/mount_test",
                                      read_only=True)
        ]
        resources = k8s_schemas.V1ResourceRequirements(
            requests={
                "cpu": "1",
                "memory": "256Mi"
            },
            limits={
                "cpu": "1",
                "memory": "256Mi"
            },
        )
        container = get_main_container(
            container_id="new-name",
            main_container=k8s_schemas.V1Container(
                name="main",
                image="job_docker_image",
                image_pull_policy="IfNotPresent",
                command=["cmd", "-p", "-c"],
                args=["arg1", "arg2"],
                resources=resources,
            ),
            contexts=None,
            volume_mounts=initial_mounts,
            log_level="info",
            artifacts_store=None,
            init=None,
            connection_by_names=None,
            connections=None,
            secrets=None,
            config_maps=None,
            kv_env_vars=None,
            env=None,
            ports=23,
            run_path=None,
        )

        assert container.name == "new-name"
        assert container.image == "job_docker_image"
        assert container.image_pull_policy == "IfNotPresent"
        assert container.command == ["cmd", "-p", "-c"]
        assert container.args == ["arg1", "arg2"]
        assert container.ports == [
            k8s_schemas.V1ContainerPort(container_port=23)
        ]
        assert container.env == [
            get_env_var(name=POLYAXON_KEYS_LOG_LEVEL, value="info")
        ]
        assert container.env_from == []
        assert container.resources == resources
        assert container.volume_mounts == initial_mounts
Esempio n. 2
0
def get_main_container(
    container_id: str,
    main_container: k8s_schemas.V1Container,
    volume_mounts: List[k8s_schemas.V1VolumeMount],
    contexts: PluginsContextsSpec,
    artifacts_store: Optional[V1ConnectionType],
    init: Optional[List[V1Init]],
    connections: Optional[List[str]],
    connection_by_names: Dict[str, V1ConnectionType],
    secrets: Optional[Iterable[V1K8sResourceType]],
    config_maps: Optional[Iterable[V1K8sResourceType]],
    log_level: str,
    run_path: Optional[str],
    kv_env_vars=None,
    env: List[k8s_schemas.V1EnvVar] = None,
    ports: List[int] = None,
) -> k8s_schemas.V1Container:
    """Pod job container for task."""
    connections = connections or []
    connection_by_names = connection_by_names or {}
    secrets = secrets or []
    config_maps = config_maps or []

    if artifacts_store and not run_path:
        raise PolypodException("Run path is required for main container.")

    if artifacts_store and not contexts.collect_artifacts:
        if artifacts_store.name not in connection_by_names:
            connection_by_names[artifacts_store.name] = artifacts_store
            connections.append(artifacts_store.name)

    requested_connections = [connection_by_names[c] for c in connections]
    requested_config_maps = get_requested_config_maps(
        config_maps=config_maps, connections=requested_connections)
    requested_secrets = get_requested_secrets(
        secrets=secrets, connections=requested_connections)

    # Mounts
    volume_mounts = to_list(volume_mounts, check_none=True)
    volume_mounts = volume_mounts + get_volume_mounts(
        contexts=contexts,
        init=init,
        connections=requested_connections,
        secrets=requested_secrets,
        config_maps=requested_config_maps,
    )

    # Env vars
    env = to_list(env, check_none=True)
    env = env + get_env_vars(
        contexts=contexts,
        log_level=log_level,
        kv_env_vars=kv_env_vars,
        connections=requested_connections,
        secrets=requested_secrets,
        config_maps=requested_config_maps,
    )

    # Env from
    env_from = get_env_from_k8s_resources(secrets=requested_secrets,
                                          config_maps=requested_config_maps)

    ports = [
        k8s_schemas.V1ContainerPort(container_port=port)
        for port in to_list(ports, check_none=True)
    ]

    return patch_container(
        container=main_container,
        name=container_id,
        env=env,
        env_from=env_from,
        volume_mounts=volume_mounts,
        ports=ports or None,
    )