From d241e08d80bfbae418ab0a0a665c8a2b22052f76 Mon Sep 17 00:00:00 2001 From: Bruno Salzano Date: Fri, 21 Aug 2026 11:25:24 +0000 Subject: [PATCH] fix: kube type detection --- .gitignore | 1 + Taskfile.yml | 4 ++- TaskfileBuild.yml | 5 ++++ nuvolaris/kube.py | 50 +++++++++++++++++++++++++++++++----- nuvolaris/registry_deploy.py | 5 ++-- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index ab4a642a..566f6b04 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ actions/github/webhook/common .direnv/ .idea/** .vscode/** +misc \ No newline at end of file diff --git a/Taskfile.yml b/Taskfile.yml index 51341e5c..69a4e6bb 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -22,7 +22,9 @@ vars: N: "" T: "" P: "" - BASETAG: 0.1.0-incubating + BASETAG: + # fixed to 0.1.0-incubating - overridable with MY_BASETAG env var + sh: echo "${MY_BASETAG:-0.1.0-incubating}" KUBE: sh: ./detect.sh NS: "nuvolaris" diff --git a/TaskfileBuild.yml b/TaskfileBuild.yml index b6f24b4c..81e018b9 100644 --- a/TaskfileBuild.yml +++ b/TaskfileBuild.yml @@ -53,6 +53,11 @@ tasks: ${MY_OPERATOR_IMAGE:-{{.OPERATOR_IMAGE}}}:{{.OPERATOR_TAG}} --name=nuvolaris + k3s:build-and-load: + - task: build + - > + docker save ${MY_OPERATOR_IMAGE:-{{.OPERATOR_IMAGE}}}:{{.OPERATOR_TAG}} | sudo k3s ctr images import - + buildx-and-push: - > docker buildx build diff --git a/nuvolaris/kube.py b/nuvolaris/kube.py index a9211734..87727445 100644 --- a/nuvolaris/kube.py +++ b/nuvolaris/kube.py @@ -21,6 +21,7 @@ import subprocess import json import logging +import re import yaml @@ -172,11 +173,46 @@ def rollout(name, namespace="nuvolaris"): except: return None -def detect_kind(): +def detect() -> str | None: + """ + >>> import nuvolaris.kube as kube + >>> kube.mocker.config("get nodes", [{"nuvolaris.io/kube": "kind"}]) + >>> kube.detect() + 'kind' + >>> kube.mocker.config("get nodes", [{"eksctl.io/cluster-name": "prod"}]) + >>> kube.detect() + 'eks' + >>> kube.mocker.config("get nodes", [{"kubernetes.io/os": "linux"}]) + >>> kube.detect() + 'generic' + >>> kube.mocker.reset() + """ try: - is_kind = kubectl("get","node/nuvolaris-control-plane", - namespace=None, - jsonpath='{.metadata.labels.nuvolaris\\.io/kube}') - return is_kind and "kind" in is_kind - except: - return False \ No newline at end of file + nodes_labels = kubectl("get", "nodes", namespace=None, + jsonpath='{.items[].metadata.labels}') + except Exception: + return None + + if not nodes_labels or not isinstance(nodes_labels, (list, dict)): + return None + + if isinstance(nodes_labels, dict): + nodes_labels = [nodes_labels] + + text = json.dumps(nodes_labels) + + if "eksctl.io" in text: + return "eks" + elif "microk8s.io" in text: + return "microk8s" + elif "lke.linode.com" in text: + return "lks" + elif "openshift.io" in text: + return "openshift" + elif re.search(r"instance-type.*k3s", text): + return "k3s" + elif any(isinstance(labels, dict) and "kind" in labels.get("nuvolaris.io/kube", "") + for labels in nodes_labels): + return "kind" + else: + return "generic" \ No newline at end of file diff --git a/nuvolaris/registry_deploy.py b/nuvolaris/registry_deploy.py index e9c13fe4..5a2f445f 100644 --- a/nuvolaris/registry_deploy.py +++ b/nuvolaris/registry_deploy.py @@ -70,8 +70,9 @@ def create_internal_registry(data, owner=None): #path the registry pull secret repoInternalHost = data['repoHostname'] - if kube.detect_kind(): - # when repo is kind, the registry pull secret will point + kube_kind = kube.detect() + if kube_kind == 'kind' or kube_kind == 'k3s': + # when kubernetes is installed on kind or k3s, the registry pull secret will point # to the node port exposed on the node repoInternalHost = "127.0.0.1:32000"