From d583c73fc53de1b7e3f248326ee7631dfcea4bde Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Mon, 16 Oct 2023 10:29:42 -0600 Subject: [PATCH] implement 'IsHelmVM' function based on presence of configmap if embedded-cluster-config exists in kube-system it is helmvm --- pkg/helmvm/util.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/helmvm/util.go b/pkg/helmvm/util.go index 7d2817f93e..3c2e4fbe66 100644 --- a/pkg/helmvm/util.go +++ b/pkg/helmvm/util.go @@ -1,11 +1,43 @@ package helmvm import ( + "context" + "fmt" + corev1 "k8s.io/api/core/v1" + + kuberneteserrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" ) +const configMapName = "embedded-cluster-config" +const configMapNamespace = "kube-system" + +// ReadConfigMap will read the Kurl config from a configmap +func ReadConfigMap(client kubernetes.Interface) (*corev1.ConfigMap, error) { + return client.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{}) +} + func IsHelmVM(clientset kubernetes.Interface) (bool, error) { - return false, nil + if clientset == nil { + return false, fmt.Errorf("clientset is nil") + } + + configMapExists := false + _, err := ReadConfigMap(clientset) + if err == nil { + configMapExists = true + } else if kuberneteserrors.IsNotFound(err) { + configMapExists = false + } else if kuberneteserrors.IsUnauthorized(err) { + configMapExists = false + } else if kuberneteserrors.IsForbidden(err) { + configMapExists = false + } else if err != nil { + return false, fmt.Errorf("failed to get embedded cluster configmap: %w", err) + } + + return configMapExists, nil } func IsHA(clientset kubernetes.Interface) (bool, error) {