From 1de84b63e2833859a56051e15e0609e628f64927 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Fri, 9 Feb 2024 12:23:14 +0100 Subject: [PATCH] refactor: extract client creation Extract the client creation from the checking of the environment. Use the internal client creation in the tests, which are checking actual configurations changes here we don't need any SaaS/SM differentiation --- go-chaos/internal/k8helper.go | 31 +++++++++++++++++++----------- go-chaos/internal/k8helper_test.go | 6 +++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/go-chaos/internal/k8helper.go b/go-chaos/internal/k8helper.go index 36ad8cfbb..d161c0b15 100644 --- a/go-chaos/internal/k8helper.go +++ b/go-chaos/internal/k8helper.go @@ -49,6 +49,26 @@ func CreateK8Client(kubeConfigPath string, namespace string) (K8Client, error) { } func createK8Client(settings KubernetesSettings) (K8Client, error) { + client, err := internalCreateClient(settings) + if err != nil { + return client, err + } + + client.SaaSEnv, err = client.isSaaSEnvironment() + if err != nil { + return client, err + } + + if client.SaaSEnv { + LogVerbose("Running experiment in SaaS environment.") + } else { + LogVerbose("Running experiment in self-managed environment.") + } + + return client, nil +} + +func internalCreateClient(settings KubernetesSettings) (K8Client, error) { clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( &clientcmd.ClientConfigLoadingRules{ExplicitPath: settings.kubeConfigPath}, &clientcmd.ConfigOverrides{Context: api.Context{Namespace: settings.namespace}}) @@ -73,17 +93,6 @@ func createK8Client(settings KubernetesSettings) (K8Client, error) { } client := K8Client{Clientset: clientset, ClientConfig: clientConfig, DynamicClient: dynamicClient} - client.SaaSEnv, err = client.isSaaSEnvironment() - if err != nil { - return client, err - } - - if client.SaaSEnv { - LogVerbose("Running experiment in SaaS environment.") - } else { - LogVerbose("Running experiment in self-managed environment.") - } - return client, nil } diff --git a/go-chaos/internal/k8helper_test.go b/go-chaos/internal/k8helper_test.go index a09c8f83f..74160070d 100644 --- a/go-chaos/internal/k8helper_test.go +++ b/go-chaos/internal/k8helper_test.go @@ -28,7 +28,7 @@ func Test_CreateK8ClientWithPath(t *testing.T) { settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml"} // when - client, err := createK8Client(settings) + client, err := internalCreateClient(settings) // then assert.NoError(t, err) @@ -40,7 +40,7 @@ func Test_CreateK8ClientWithPath(t *testing.T) { func Test_ShouldReturnNamespace(t *testing.T) { // given settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml"} - client, err := createK8Client(settings) + client, err := internalCreateClient(settings) require.NoError(t, err) // when @@ -57,7 +57,7 @@ func Test_ShouldReturnNamespace(t *testing.T) { func Test_ShouldReturnNamespaceOverride(t *testing.T) { // given settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml", namespace: "namespace-override"} - client, err := createK8Client(settings) + client, err := internalCreateClient(settings) require.NoError(t, err) // when