-
Notifications
You must be signed in to change notification settings - Fork 0
feat: try loading k8s from outside cluster #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import logging | ||
| import os | ||
| from base64 import b64decode | ||
| from typing import Any | ||
|
|
||
| from kubernetes import client, config as cluster_config | ||
| from kubernetes.client.rest import ApiException | ||
|
|
||
| from qscaler.configuration.config import config | ||
omri2001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from qscaler.utils.singleton import SingletonMeta | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
@@ -14,18 +16,22 @@ | |
| class K8sClient(metaclass=SingletonMeta): | ||
|
|
||
| def __init__(self): | ||
| cluster_config.load_incluster_config() | ||
| cluster_config.load_config() | ||
| self.api_group = "quickube.com" | ||
| self.api_version = "v1alpha1" | ||
| self.namespace = self._load_namespace_from_file() | ||
| self.namespace = self._load_namespace() | ||
|
|
||
| @staticmethod | ||
| def _load_namespace_from_file(): | ||
| try: | ||
| with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r") as f: | ||
| def _load_namespace() -> str | None: | ||
| ns_path = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | ||
| if os.path.exists(ns_path): | ||
| with open(ns_path) as f: | ||
| return f.read().strip() | ||
| except FileNotFoundError: | ||
| raise RuntimeError("Namespace file not found. Are you running in a Kubernetes cluster?") | ||
| try: | ||
| contexts, active_context = cluster_config.list_kube_config_contexts() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you think about loading from env var, with fall back to default?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if im getting you right. that might cause problems, if a mistake was entered in the env var and is different then the "/var/run/secrets/kubernetes.io/serviceaccount/namespace". i think default should be the 'incluster' namespace file, then fallback to other methods
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using kubeconfig context namespace used only for development purposes.. the only reason to provide namespace will be for development reasons.. |
||
| return active_context["context"]["namespace"] | ||
| except (KeyError, StopIteration): | ||
| return "default" | ||
|
|
||
| def get_qworker(self, name: str) -> dict: | ||
| api_instance = client.CustomObjectsApi() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.