From f324ac4d5cb6324266a77354bf3e765affd9c5bd Mon Sep 17 00:00:00 2001 From: Michael Burman Date: Tue, 23 Apr 2024 15:43:58 +0300 Subject: [PATCH 1/2] Add a method to extract restConfig for certain contextName in kubeconfig --- pkg/kubernetes/config.go | 34 ++++++++++++++++++++++++++++++++++ pkg/kubernetes/config_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 pkg/kubernetes/config.go create mode 100644 pkg/kubernetes/config_test.go diff --git a/pkg/kubernetes/config.go b/pkg/kubernetes/config.go new file mode 100644 index 0000000..76be017 --- /dev/null +++ b/pkg/kubernetes/config.go @@ -0,0 +1,34 @@ +package kubernetes + +import ( + "fmt" + + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" +) + +func LoadClientConfig(cfgFile, contextName string) (*rest.Config, error) { + config, err := clientcmd.LoadFromFile(cfgFile) + if err != nil { + return nil, err + } + + if contextName != "" { + if _, found := config.Contexts[contextName]; !found { + return nil, fmt.Errorf("context %s not found in kubeconfig file", contextName) + } + } + config.CurrentContext = contextName + + b, err := clientcmd.Write(*config) + if err != nil { + return nil, err + } + + clientConfig, err := clientcmd.NewClientConfigFromBytes(b) + if err != nil { + return nil, err + } + + return clientConfig.ClientConfig() +} diff --git a/pkg/kubernetes/config_test.go b/pkg/kubernetes/config_test.go new file mode 100644 index 0000000..ba1f44d --- /dev/null +++ b/pkg/kubernetes/config_test.go @@ -0,0 +1,26 @@ +package kubernetes + +import ( + "os/user" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestLoadConfigurationContext(t *testing.T) { + t.Skip("requires kind cluster with defaults to be running") + require := require.New(t) + + u, err := user.Current() + require.NoError(err) + cfgFile := filepath.Join(u.HomeDir, ".kube", "config") + + restConfig, err := LoadClientConfig(cfgFile, "kind-kind") + require.NoError(err) + + require.NotNil(restConfig.KeyData) + require.NotNil(restConfig.CertData) + require.True(strings.HasPrefix(restConfig.Host, "https://127.0.0.1")) +} From f61cbf12891279fdb68dacbc1d140fdabb60fc38 Mon Sep 17 00:00:00 2001 From: Michael Burman Date: Tue, 23 Apr 2024 16:07:53 +0300 Subject: [PATCH 2/2] Add more complex tests --- pkg/kubernetes/config_test.go | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/pkg/kubernetes/config_test.go b/pkg/kubernetes/config_test.go index ba1f44d..b9888bd 100644 --- a/pkg/kubernetes/config_test.go +++ b/pkg/kubernetes/config_test.go @@ -1,26 +1,61 @@ package kubernetes import ( + "math/rand" "os/user" "path/filepath" + "reflect" "strings" "testing" "github.com/stretchr/testify/require" + "k8s.io/client-go/tools/clientcmd" ) func TestLoadConfigurationContext(t *testing.T) { - t.Skip("requires kind cluster with defaults to be running") require := require.New(t) - u, err := user.Current() + conf, err := clientcmd.NewDefaultClientConfigLoadingRules().Load() require.NoError(err) - cfgFile := filepath.Join(u.HomeDir, ".kube", "config") - restConfig, err := LoadClientConfig(cfgFile, "kind-kind") + if _, found := conf.Contexts["kind-kind"]; !found { + t.Skip("kind-kind context not found in kubeconfig") + } + + restConfig, err := LoadClientConfig(defaultConfigPath(t), "kind-kind") require.NoError(err) + // mTLS auth has KeyData & CertData require.NotNil(restConfig.KeyData) require.NotNil(restConfig.CertData) + require.NotNil(restConfig.CAData) require.True(strings.HasPrefix(restConfig.Host, "https://127.0.0.1")) } + +func TestVerifyContextWasChanged(t *testing.T) { + require := require.New(t) + + conf, err := clientcmd.NewDefaultClientConfigLoadingRules().Load() + require.NoError(err) + + if len(conf.Contexts) < 1 { + t.Skip("no context found in kubeconfig") + } + + keys := reflect.ValueOf(conf.Contexts).MapKeys() + randomContextName := keys[rand.Intn(len(keys))].String() + + randomContext := conf.Contexts[randomContextName] + randomServer := conf.Clusters[randomContext.Cluster].Server + + restConfig, err := LoadClientConfig(defaultConfigPath(t), randomContextName) + require.NoError(err) + require.NotNil(restConfig.CAData) + require.Equal(randomServer, restConfig.Host) +} + +func defaultConfigPath(t *testing.T) string { + u, err := user.Current() + require.NoError(t, err) + return filepath.Join(u.HomeDir, ".kube", "config") +}