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

Allow overriding k8s namespace #193

Merged
merged 9 commits into from
Oct 14, 2022
25 changes: 17 additions & 8 deletions go-chaos/internal/k8helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"
// in order to authenticate with gcp
// https://github.com/kubernetes/client-go/issues/242#issuecomment-314642965
Expand All @@ -40,15 +41,14 @@ func (c *K8Client) GetCurrentNamespace() string {

// Creates a kubernetes client, based on the local kubeconfig
func CreateK8Client() (K8Client, error) {
kubeconfig := findKubeconfigPath()

return createK8Client(kubeconfig)
settings := findKubernetesSettings()
return createK8Client(settings)
}

func createK8Client(kubeconfig *string) (K8Client, error) {
func createK8Client(settings KubernetesSettings) (K8Client, error) {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: *kubeconfig},
&clientcmd.ConfigOverrides{})
&clientcmd.ClientConfigLoadingRules{ExplicitPath: settings.kubeConfigPath},
&clientcmd.ConfigOverrides{Context: api.Context{Namespace: settings.namespace}})

k8ClientConfig, err := clientConfig.ClientConfig()
if err != nil {
Expand All @@ -70,14 +70,23 @@ func createK8Client(kubeconfig *string) (K8Client, error) {
return K8Client{Clientset: clientset, ClientConfig: clientConfig}, nil
}

func findKubeconfigPath() *string {
type KubernetesSettings struct {
kubeConfigPath string
namespace string
}

func findKubernetesSettings() KubernetesSettings {
//// based on https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
namespace := flag.String("namespace", "", "Kubernetes namespace to use")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ This is a bit hidden. I would like to use a presistent flag https://stackoverflow.com/a/63498490/2165134 as we do with verbose here https://github.com/zeebe-io/zeebe-chaos/blob/main/go-chaos/cmd/root.go#L53

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what I tried first but that introduced a circular dependency from cmd -> internal -> cmd again. I guess that's why the kubeconfig parameter is also defined here and not in cmd?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well what you need to do is then to set a internal property like we do for verbosity see here https://github.com/zeebe-io/zeebe-chaos/blob/main/go-chaos/cmd/root.go#L48

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, neat 👍 Do you remember if there was a reason to not do the same for the kubeconfig param?

flag.Parse()
return kubeconfig
return KubernetesSettings{
kubeConfigPath: *kubeconfig,
namespace: *namespace,
}
}
28 changes: 22 additions & 6 deletions go-chaos/internal/k8helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

func Test_CreateK8ClientWithPath(t *testing.T) {
// given
path := "kubeconfigtest.yml"
settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml"}

// when
client, err := createK8Client(&path)
client, err := createK8Client(settings)

// then
assert.NoError(t, err)
Expand All @@ -39,8 +39,8 @@ func Test_CreateK8ClientWithPath(t *testing.T) {

func Test_ShouldReturnNamespace(t *testing.T) {
// given
path := "kubeconfigtest.yml"
client, err := createK8Client(&path)
settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml"}
client, err := createK8Client(settings)
require.NoError(t, err)

// when
Expand All @@ -54,13 +54,29 @@ func Test_ShouldReturnNamespace(t *testing.T) {
assert.Equal(t, namespace, currentNamespace)
}

func Test_ShouldReturnNamespaceOverride(t *testing.T) {
// given
settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml", namespace: "namespace-override"}
client, err := createK8Client(settings)
require.NoError(t, err)

// when
currentNamespace := client.GetCurrentNamespace()
clientNamespace, _, err := client.ClientConfig.Namespace()
assert.NoError(t, err)

// then
assert.Equal(t, "namespace-override", currentNamespace)
assert.Equal(t, currentNamespace, clientNamespace)
}

func Test_ResolveDefaultKubePath(t *testing.T) {
// given
home := homedir.HomeDir()

// when
path := findKubeconfigPath()
settings := findKubernetesSettings()

// then
assert.Equal(t, strings.Join([]string{home, ".kube/config"}, "/"), *path)
assert.Equal(t, strings.Join([]string{home, ".kube/config"}, "/"), settings.kubeConfigPath)
}