Skip to content

Commit

Permalink
refactor: extract client creation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ChrisKujawa committed Feb 9, 2024
1 parent 76e0aee commit 1de84b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
31 changes: 20 additions & 11 deletions go-chaos/internal/k8helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions go-chaos/internal/k8helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1de84b6

Please sign in to comment.