Ejemplo n.º 1
0
def test_setup_kubeconfig_raw_empty(mockenv: MockEnv) -> None:
    extra_env = {"KUBECONFIG_RAW": "", "KUBECONFIG": "Value from KUBECONFIG"}

    with mockenv(extra_env):
        value, key = settings.setup_kubeconfig("fake_track")

    assert key == "KUBECONFIG"
    assert value == "Value from KUBECONFIG"
Ejemplo n.º 2
0
def test_setup_kubeconfig_raw_with_track(mockenv: MockEnv) -> None:
    extra_env = {
        "KUBECONFIG_RAW": "This value is from KUBECONFIG_RAW",
        "KUBECONFIG_RAW_STABLE": "This value is from KUBECONFIG_RAW_STABLE",
    }

    with mockenv(extra_env):
        value, key = settings.setup_kubeconfig("STABLE")

    assert key == "KUBECONFIG_RAW_STABLE"
Ejemplo n.º 3
0
def test_setup_kubeconfig_raw(mockenv: MockEnv) -> None:
    extra_env = {"KUBECONFIG_RAW": "This value is from KUBECONFIG_RAW"}

    with mockenv(extra_env):
        filename, key = settings.setup_kubeconfig("fake_track")

    with open(filename) as fobj:
        result = fobj.read()

    assert key == "KUBECONFIG_RAW"
    assert "This value is from KUBECONFIG_RAW" == result
Ejemplo n.º 4
0
def test_setup_kubeconfig_with_track(track: str, is_track_present: bool,
                                     expected_variable: str) -> None:
    os.environ.update({
        kubeconfig_key():
        "Value from fall-back KUBECONFIG",
        kubeconfig_key(fake_track(track)):
        "A totally wrong KUBECONFIG",
    })

    if is_track_present:
        os.environ[kubeconfig_key(
            track)] = "Value from track-specific KUBECONFIG"

    expected_value = os.environ[expected_variable]

    assert settings.setup_kubeconfig(track) == (expected_value,
                                                expected_variable)
    assert settings.KUBECONFIG == os.environ["KUBECONFIG"] == expected_value
Ejemplo n.º 5
0
def test_setup_kubeconfig_with_track(mockenv: MockEnv, track: str,
                                     is_track_present: bool,
                                     expected_variable: str) -> None:
    extra_env = {
        kubeconfig_key(): "Value from fall-back KUBECONFIG",
        kubeconfig_key(fake_track(track)): "A totally wrong KUBECONFIG",
    }

    if is_track_present:
        extra_env[kubeconfig_key(
            track)] = "Value from track-specific KUBECONFIG"

    with mockenv(extra_env) as env:
        expected_value = env[expected_variable]
        value, variable = settings.setup_kubeconfig(track)
        kubeconfig = env["KUBECONFIG"]

    assert (value, variable) == (expected_value, expected_variable)
    assert settings.KUBECONFIG == kubeconfig == expected_value
Ejemplo n.º 6
0
    def create_client(self, track: str) -> k8s_client.ApiClient:
        try:
            kubeconfig, method = settings.setup_kubeconfig(track)
        except NoClusterConfigError as exc:
            logger.error(
                icon=f"{self.ICON}  🔑",
                message="Can't log in to Kubernetes cluster, all auth methods exhausted",
                error=exc,
                raise_exception=True,
            )

        logger.success(
            icon=f"{self.ICON}  🔑", message=f"Using {method} for Kubernetes auth"
        )

        config = k8s_client.Configuration()
        k8s_config.load_kube_config(client_configuration=config, config_file=kubeconfig)

        return k8s_client.ApiClient(configuration=config)