Skip to content

Commit

Permalink
Programmatically check if in-cluster config or kube config should be …
Browse files Browse the repository at this point in the history
…loaded

Rename endpoint of server tests to /config instead of /key
  • Loading branch information
meffmadd committed Jul 29, 2024
1 parent 9d5689f commit 310ea94
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ jobs:

helm:
runs-on: ubuntu-latest
needs:
- server-tests
- operator-tests
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
16 changes: 13 additions & 3 deletions opr/operator.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/server_tests/test_key_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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')

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"}))

Expand All @@ -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()

0 comments on commit 310ea94

Please sign in to comment.