diff --git a/pkg/helmvm/util.go b/pkg/helmvm/util.go index 7d2817f93e..48dfb2c15f 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 embedded cluster 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) {