Skip to content

Commit

Permalink
Saas detection error handling (#493)
Browse files Browse the repository at this point in the history
Paired with @oleschoenburg on this:

* We agreed on to "simply" pass the error to the front, to the
respective commands so they can either fail or do what ever
* For errors indicating resource not-found we decided to return false,
and use SM setting (as we expect this CRD not in the SM env)
 * We wrote some more tests for the error handling
* I had to refactor a bit the client creation, so we test the right
things here


closes #488 
closes #490
  • Loading branch information
ChrisKujawa authored Feb 9, 2024
2 parents 879781b + 1de84b6 commit cc9c847
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
4 changes: 3 additions & 1 deletion go-chaos/internal/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ func (c *K8Client) createSaaSCRD(t *testing.T) {
_, err := c.DynamicClient.Resource(zeebeCrd).Create(context.TODO(), obj, metav1.CreateOptions{})
require.NoError(t, err)

c.SaaSEnv = c.isSaaSEnvironment()
saas, err := c.isSaaSEnvironment()
require.NoError(t, err)
c.SaaSEnv = saas
}

func (c K8Client) CreateStatefulSetWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, name string) {
Expand Down
28 changes: 20 additions & 8 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,14 +93,6 @@ func createK8Client(settings KubernetesSettings) (K8Client, error) {
}

client := K8Client{Clientset: clientset, ClientConfig: clientConfig, DynamicClient: dynamicClient}
client.SaaSEnv = client.isSaaSEnvironment()

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
11 changes: 6 additions & 5 deletions go-chaos/internal/saas.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package internal
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
"strings"

meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -55,15 +56,15 @@ func (c K8Client) setPauseFlag(pauseEnabled bool) error {
return err
}

func (c K8Client) isSaaSEnvironment() bool {
func (c K8Client) isSaaSEnvironment() (bool, error) {
namespace := c.GetCurrentNamespace()
clusterId := strings.TrimSuffix(namespace, "-zeebe")
zeebeCrd := schema.GroupVersionResource{Group: "cloud.camunda.io", Version: "v1alpha1", Resource: "zeebeclusters"}
resource, err := c.DynamicClient.Resource(zeebeCrd).Get(context.TODO(), clusterId, meta.GetOptions{})

if err != nil {
LogInfo("Failed to retrieve SaaS CRD, fallback to self-managed mode. %v", err)
return false
if errors.IsNotFound(err) {
return false, nil
}
return resource != nil

return resource != nil, err
}
45 changes: 43 additions & 2 deletions go-chaos/internal/saas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
package internal

import (
"errors"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
dynamicFake "k8s.io/client-go/dynamic/fake"
k8testing "k8s.io/client-go/testing"
"testing"

"github.com/stretchr/testify/assert"
k8errors "k8s.io/apimachinery/pkg/api/errors"
)

func Test_ShouldReturnTrueWhenCRDDeployed(t *testing.T) {
Expand All @@ -26,8 +33,9 @@ func Test_ShouldReturnTrueWhenCRDDeployed(t *testing.T) {
k8Client.createSaaSCRD(t)

// when
isSaaSEnvironment := k8Client.isSaaSEnvironment()
isSaaSEnvironment, err := k8Client.isSaaSEnvironment()

require.NoError(t, err)
assert.True(t, isSaaSEnvironment)
}

Expand All @@ -36,7 +44,40 @@ func Test_ShouldReturnFalseWhenNoCRDDeployed(t *testing.T) {
k8Client := CreateFakeClient()

// when
isSaaSEnvironment := k8Client.isSaaSEnvironment()
isSaaSEnvironment, err := k8Client.isSaaSEnvironment()

require.NoError(t, err)
assert.False(t, isSaaSEnvironment)
}

func Test_ShouldThrowErrorWhenNoCRDDeployed(t *testing.T) {
// given
k8Client := CreateFakeClient()
k8Client.DynamicClient.(*dynamicFake.FakeDynamicClient).Fake.PrependReactor("get", "zeebeclusters", func(action k8testing.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, errors.New("timeout")
})

// when
isSaaSEnvironment, err := k8Client.isSaaSEnvironment()

require.Error(t, err)
assert.False(t, isSaaSEnvironment)
}

func Test_ShouldReturnFalseOnNotFound(t *testing.T) {
// given
k8Client := CreateFakeClient()
k8Client.DynamicClient.(*dynamicFake.FakeDynamicClient).Fake.PrependReactor("get", "zeebeclusters", func(action k8testing.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, k8errors.NewNotFound(
schema.GroupResource{
Group: "cloud.camunda.io",
Resource: "zeebeclusters"},
"test")
})

// when
isSaaSEnvironment, err := k8Client.isSaaSEnvironment()

require.NoError(t, err)
assert.False(t, isSaaSEnvironment)
}

0 comments on commit cc9c847

Please sign in to comment.