Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to extract restConfig for certain contextName in kubeconfig #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkg/kubernetes/config.go
Original file line number Diff line number Diff line change
@@ -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()
}
61 changes: 61 additions & 0 deletions pkg/kubernetes/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +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) {
require := require.New(t)

conf, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
require.NoError(err)

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")
}
Loading