diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0123f62..7961e68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -128,6 +128,9 @@ jobs: helm: runs-on: ubuntu-latest + needs: + - server-tests + - operator-tests steps: - name: Checkout repository uses: actions/checkout@v4 diff --git a/opr/operator.py b/opr/operator.py index c84f316..4a976c4 100644 --- a/opr/operator.py +++ b/opr/operator.py @@ -1,14 +1,24 @@ import json import logging +import os import kopf import kubernetes from kubernetes.client import ApiClient, CoreV1Api, AppsV1Api, CustomObjectsApi, V1ConfigMap, V1Service +def load_kubernetes_config(): + if 'KUBERNETES_SERVICE_HOST' in os.environ: + # We're running inside a Kubernetes cluster + return kubernetes.config.load_incluster_config() + else: + # We're running outside the cluster + return kubernetes.config.load_kube_config() + + @kopf.on.create('configserver') def create_fn(meta, spec, **kwargs): - client = ApiClient(configuration=kubernetes.config.load_incluster_config()) + client = ApiClient(configuration=load_kubernetes_config()) api = CoreV1Api(api_client=client) apps_api = AppsV1Api(api_client=client) crd_api = CustomObjectsApi(api_client=client) @@ -100,7 +110,7 @@ def create_fn(meta, spec, **kwargs): @kopf.on.delete('configserver') def delete_fn(meta, spec, **kwargs): - client = ApiClient(configuration=kubernetes.config.load_incluster_config()) + client = ApiClient(configuration=load_kubernetes_config()) api = CoreV1Api(api_client=client) apps_api = AppsV1Api(api_client=client) @@ -122,7 +132,7 @@ def delete_fn(meta, spec, **kwargs): def _get_config_map(config_name: str, namespace: str, logger: logging.Logger) -> tuple[V1ConfigMap | None, CoreV1Api]: - client = kubernetes.client.api_client.ApiClient(configuration=kubernetes.config.load_incluster_config()) + client = kubernetes.client.api_client.ApiClient(configuration=load_kubernetes_config()) api = kubernetes.client.CoreV1Api(api_client=client) try: config_map = api.read_namespaced_config_map(name=f"{config_name}-values", namespace=namespace) diff --git a/tests/server_tests/test_key_handler.py b/tests/server_tests/test_key_handler.py index 29106fd..0f8bc90 100644 --- a/tests/server_tests/test_key_handler.py +++ b/tests/server_tests/test_key_handler.py @@ -24,7 +24,7 @@ def test_root(self): self.assertEqual(response.code, 404) def test_not_found(self): - response = self.fetch('/key/not-found') + response = self.fetch('/config/not-found') self.assertEqual(response.code, 404) # self.assertEqual(response.body, 'Hello, world') @@ -32,7 +32,7 @@ def test_key(self): test_file = self.config_values / "test" test_file.write_text(json.dumps({"foo": "bar"})) - response = self.fetch('/key/test') + response = self.fetch('/config/test') self.assertEqual(response.code, 200) self.assertEqual(response.body.decode(), json.dumps({"foo": "bar"})) @@ -42,7 +42,7 @@ def test_key_not_json(self): test_file = self.config_values / "test" test_file.write_text("something: not json") - response = self.fetch('/key/test') + response = self.fetch('/config/test') self.assertEqual(response.code, 400) test_file.unlink()