From 481425ca79d0f7bacb22c50c607bc87c8a703099 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 3 Nov 2021 16:49:10 +0100 Subject: [PATCH] Fixes add-app in wego-server (#1015) * Read cluster name from env var if set - Fixes running add-app in-cluster * wsl --- pkg/kube/kubehttp.go | 55 +++++++++++++++++++++++++-------------- pkg/kube/kubehttp_test.go | 16 +++++++++++- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/pkg/kube/kubehttp.go b/pkg/kube/kubehttp.go index 8a7e4652c0..f68d9d380a 100644 --- a/pkg/kube/kubehttp.go +++ b/pkg/kube/kubehttp.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "strings" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -98,30 +99,45 @@ func NewKubeHTTPClient() (Kube, client.Client, error) { } func RestConfig() (*rest.Config, string, error) { - var kubeContext, clusterName string - config, err := InClusterConfig() + if err != nil { + if err == rest.ErrNotInCluster { + return outOfClusterConfig() + } + // Handle other errors + return nil, "", fmt.Errorf("could not create in-cluster config: %w", err) + } - if err == rest.ErrNotInCluster { - cfgLoadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + return config, inClusterConfigClusterName(), nil +} - kubeContext, clusterName, err = initialContext(cfgLoadingRules) - if err != nil { - return nil, "", fmt.Errorf("could not get initial context: %w", err) - } +func inClusterConfigClusterName() string { + // kube clusters don't really know their own names + // try and read a unique name from the env, fall back to "default" + clusterName := os.Getenv("CLUSTER_NAME") + if clusterName == "" { + clusterName = "default" + } - configOverrides := clientcmd.ConfigOverrides{CurrentContext: kubeContext} + return clusterName +} - config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - cfgLoadingRules, - &configOverrides, - ).ClientConfig() - if err != nil { - return nil, "", fmt.Errorf("could not create rest config: %w", err) - } - } else { - // TODO when running in a cluster and not used for bootstrapping, what is the cluster name used for? - clusterName = config.Host +func outOfClusterConfig() (*rest.Config, string, error) { + cfgLoadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + + kubeContext, clusterName, err := initialContext(cfgLoadingRules) + if err != nil { + return nil, "", fmt.Errorf("could not get initial context: %w", err) + } + + configOverrides := clientcmd.ConfigOverrides{CurrentContext: kubeContext} + + config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + cfgLoadingRules, + &configOverrides, + ).ClientConfig() + if err != nil { + return nil, "", fmt.Errorf("could not create rest config: %w", err) } return config, clusterName, nil @@ -374,6 +390,7 @@ func initialContext(cfgLoadingRules *clientcmd.ClientConfigLoadingRules) (curren return rules.CurrentContext, sanitizeClusterName(c.Cluster), nil } + func sanitizeClusterName(s string) string { // remove leading email address or username prefix from context if strings.Contains(s, "@") { diff --git a/pkg/kube/kubehttp_test.go b/pkg/kube/kubehttp_test.go index d679f26ca4..4c671ab6fc 100644 --- a/pkg/kube/kubehttp_test.go +++ b/pkg/kube/kubehttp_test.go @@ -343,7 +343,21 @@ metadata: createKubeconfig("foo", "bar", dir, false) _, _, err = kube.RestConfig() Expect(err).To(HaveOccurred(), "Should receive an error about no current context ") - + }) + It("returns a sensisble clusterName inCluster", func() { + kube.InClusterConfig = func() (*rest.Config, error) { return nil, nil } + _, clusterName, err := kube.RestConfig() + Expect(err).ToNot(HaveOccurred()) + Expect(clusterName).To(Equal("default")) + }) + It("derives the clusterName from the env inCluster", func() { + kube.InClusterConfig = func() (*rest.Config, error) { return nil, nil } + origcn := os.Getenv("CLUSTER_NAME") + defer os.Setenv("CLUSTER_NAME", origcn) + os.Setenv("CLUSTER_NAME", "foo") + _, clusterName, err := kube.RestConfig() + Expect(err).ToNot(HaveOccurred()) + Expect(clusterName).To(Equal("foo")) }) })