Skip to content

Commit

Permalink
prefect-kubernetes: add missing config loaders (#15218)
Browse files Browse the repository at this point in the history
Co-authored-by: Simone Ripamonti <[email protected]>
  • Loading branch information
desertaxle and srsapient authored Sep 4, 2024
1 parent 7b9612b commit cc9893c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ async def get_client(
context=context,
client_configuration=client_configuration,
)
else:
try:
config.load_incluster_config(client_configuration=client_configuration)
except config.ConfigException:
# If in-cluster config fails, load the local kubeconfig
config.load_kube_config(
client_configuration=client_configuration,
)
async with ApiClient(configuration=client_configuration) as api_client:
try:
yield await self.get_resource_specific_client(
Expand Down
35 changes: 34 additions & 1 deletion src/integrations/prefect-kubernetes/tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
from pathlib import Path
from typing import Dict
from unittest.mock import AsyncMock, MagicMock

import pytest
import yaml
Expand All @@ -13,9 +14,13 @@
CoreV1Api,
CustomObjectsApi,
)
from kubernetes_asyncio.config import ConfigException
from kubernetes_asyncio.config.kube_config import list_kube_config_contexts
from OpenSSL import crypto
from prefect_kubernetes.credentials import KubernetesClusterConfig
from prefect_kubernetes.credentials import (
KubernetesClusterConfig,
KubernetesCredentials,
)
from pydantic.version import VERSION as PYDANTIC_VERSION

if PYDANTIC_VERSION.startswith("2."):
Expand Down Expand Up @@ -113,6 +118,19 @@ def config_file(tmp_path, config_context) -> Path:
return config_file


@pytest.fixture
def mock_cluster_config(monkeypatch):
mock = MagicMock()
# We cannot mock this or the `except` clause will complain
mock.ConfigException.return_value = ConfigException
mock.load_kube_config = AsyncMock()
monkeypatch.setattr("prefect_kubernetes.credentials.config", mock)
monkeypatch.setattr(
"prefect_kubernetes.credentials.config.ConfigException", ConfigException
)
return mock


class TestCredentials:
@pytest.mark.parametrize(
"resource_type,client_type",
Expand All @@ -136,6 +154,21 @@ async def test_client_bad_resource_type(self, kubernetes_credentials):
async with kubernetes_credentials.get_client("shoo-ba-daba-doo"):
pass

async def test_incluster_config(self, mock_cluster_config):
kubernetes_credentials = KubernetesCredentials()
mock_cluster_config.load_incluster_config.return_value = None
async with kubernetes_credentials.get_client("batch"):
assert mock_cluster_config.load_incluster_config.called
assert not mock_cluster_config.load_kube_config.called

async def test_load_kube_config(self, mock_cluster_config):
kubernetes_credentials = KubernetesCredentials()
mock_cluster_config.load_incluster_config.side_effect = ConfigException()
mock_cluster_config.load_kube_config.return_value = None
async with kubernetes_credentials.get_client("batch"):
assert mock_cluster_config.load_incluster_config.called
assert mock_cluster_config.load_kube_config.called


class TestKubernetesClusterConfig:
async def test_instantiation_from_file(self, config_file, config_context):
Expand Down

0 comments on commit cc9893c

Please sign in to comment.