Skip to content

Commit

Permalink
Fixes add-app in wego-server (#1015)
Browse files Browse the repository at this point in the history
* Read cluster name from env var if set

- Fixes running add-app in-cluster

* wsl
  • Loading branch information
foot authored Nov 3, 2021
1 parent 9f14489 commit 481425c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
55 changes: 36 additions & 19 deletions pkg/kube/kubehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"

v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, "@") {
Expand Down
16 changes: 15 additions & 1 deletion pkg/kube/kubehttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})

Expand Down

0 comments on commit 481425c

Please sign in to comment.