From 14684ab14308cf2a1bbef5fa0d424f672bcdea19 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Tue, 15 Nov 2022 16:14:17 +0100 Subject: [PATCH 01/13] feat: detect environment on client creation This detection and setting an internal flag should allow to simplify the code on several places --- go-chaos/internal/flags.go | 6 +++++- go-chaos/internal/k8helper.go | 17 +++++++++++++++-- go-chaos/internal/saas.go | 10 ++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/go-chaos/internal/flags.go b/go-chaos/internal/flags.go index 2b0a3b18d..2a96bceb0 100644 --- a/go-chaos/internal/flags.go +++ b/go-chaos/internal/flags.go @@ -17,7 +17,11 @@ package internal import "github.com/camunda/zeebe/clients/go/v8/pkg/zbc" // defines whether the functions should print verbose output -var Verbosity bool = false +var Verbosity = false + +// defines whether we running in saas or self-managed +// will be resolved automatically when creating the k8 client +var saasEnv = false // defines if a custom kube config should be used instead of the default one found by k8s var KubeConfigPath string diff --git a/go-chaos/internal/k8helper.go b/go-chaos/internal/k8helper.go index f13f168ea..dd025eec4 100644 --- a/go-chaos/internal/k8helper.go +++ b/go-chaos/internal/k8helper.go @@ -16,9 +16,10 @@ package internal import ( "fmt" - "k8s.io/client-go/dynamic" "path/filepath" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" @@ -72,7 +73,19 @@ func createK8Client(settings KubernetesSettings) (K8Client, error) { if err != nil { return K8Client{}, err } - return K8Client{Clientset: clientset, ClientConfig: clientConfig, DynamicClient: dynamicClient}, nil + + client := K8Client{Clientset: clientset, ClientConfig: clientConfig, DynamicClient: dynamicClient} + saasEnv = client.isSaaSEnvironment() + + if Verbosity { + if saasEnv { + fmt.Println("Running experiment in SaaS environment.") + } else { + fmt.Println("Running experiment in self-managed environment.") + } + } + + return client, nil } type KubernetesSettings struct { diff --git a/go-chaos/internal/saas.go b/go-chaos/internal/saas.go index 932842ee1..25fbcfbaa 100644 --- a/go-chaos/internal/saas.go +++ b/go-chaos/internal/saas.go @@ -55,3 +55,13 @@ func (c K8Client) setPauseFlag(pauseEnabled bool) error { } return err } + +func (c K8Client) isSaaSEnvironment() bool { + zeebeCrd := schema.GroupVersionResource{Group: "cloud.camunda.io", Version: "v1alpha1", Resource: "zeebeclusters"} + list, err := c.DynamicClient.Resource(zeebeCrd).List(context.TODO(), meta.ListOptions{}) + + if err != nil || len(list.Items) <= 0 { + return false + } + return true +} From fce92f47d1a238f5d1a5565e549d3ae9d4ac1c49 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Tue, 15 Nov 2022 21:44:13 +0100 Subject: [PATCH 02/13] test: support dynamic client and crd In tests we are able to support CRD and the saas flag --- go-chaos/internal/helper_test.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/go-chaos/internal/helper_test.go b/go-chaos/internal/helper_test.go index 0d0f5bf16..7de25d50f 100644 --- a/go-chaos/internal/helper_test.go +++ b/go-chaos/internal/helper_test.go @@ -16,12 +16,17 @@ package internal import ( "context" + "strings" "testing" "github.com/stretchr/testify/require" v12 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + dynamicFake "k8s.io/client-go/dynamic/fake" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -29,7 +34,13 @@ import ( ) func CreateFakeClient() K8Client { - k8Client := K8Client{Clientset: fake.NewSimpleClientset(), ClientConfig: &testClientConfig{namespace: "testNamespace"}} + scheme := runtime.NewScheme() + groupVersionKind := schema.GroupVersionKind{Group: "cloud.camunda.io", Version: "v1alpha1", Kind: "zeebeclusters"} + scheme.AddKnownTypeWithName(groupVersionKind, &unstructured.Unstructured{}) + + k8Client := K8Client{Clientset: fake.NewSimpleClientset(), + DynamicClient: dynamicFake.NewSimpleDynamicClient(scheme), + ClientConfig: &testClientConfig{namespace: "testNamespace"}} return k8Client } @@ -78,6 +89,20 @@ func (c K8Client) CreateDeploymentWithLabelsAndName(t *testing.T, selector *meta require.NoError(t, err) } + +func (c K8Client) createSaaSCRD(t *testing.T) { + zeebeCrd := schema.GroupVersionResource{Group: "cloud.camunda.io", Version: "v1alpha1", Resource: "zeebeclusters"} + obj := &unstructured.Unstructured{} + namespace := c.GetCurrentNamespace() + clusterId := strings.TrimSuffix(namespace, "-zeebe") + obj.SetName(clusterId) + + _, err := c.DynamicClient.Resource(zeebeCrd).Create(context.TODO(), obj, metav1.CreateOptions{}) + require.NoError(t, err) + + saasEnv = c.isSaaSEnvironment() +} + func (c K8Client) CreateStatefulSetWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, name string) { _, err := c.Clientset.AppsV1().StatefulSets(c.GetCurrentNamespace()).Create(context.TODO(), &v12.StatefulSet{ ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: name}, From 41a95cfecd6c80ad4dbef9d3712ad1844de83264 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Tue, 15 Nov 2022 21:45:01 +0100 Subject: [PATCH 03/13] refactor: use get instead of list This simplifies the test setup since we don't need to mock the list scheme for the custom resource definition, which caused some issues --- go-chaos/internal/saas.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/go-chaos/internal/saas.go b/go-chaos/internal/saas.go index 25fbcfbaa..068e35656 100644 --- a/go-chaos/internal/saas.go +++ b/go-chaos/internal/saas.go @@ -57,10 +57,12 @@ func (c K8Client) setPauseFlag(pauseEnabled bool) error { } func (c K8Client) isSaaSEnvironment() bool { + namespace := c.GetCurrentNamespace() + clusterId := strings.TrimSuffix(namespace, "-zeebe") zeebeCrd := schema.GroupVersionResource{Group: "cloud.camunda.io", Version: "v1alpha1", Resource: "zeebeclusters"} - list, err := c.DynamicClient.Resource(zeebeCrd).List(context.TODO(), meta.ListOptions{}) + resource, err := c.DynamicClient.Resource(zeebeCrd).Get(context.TODO(), clusterId, meta.GetOptions{}) - if err != nil || len(list.Items) <= 0 { + if err != nil || resource == nil { return false } return true From 67d74ae9b698edd500672f5929a45a090ce7eb14 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Tue, 15 Nov 2022 21:46:13 +0100 Subject: [PATCH 04/13] refactor: simplify code with saas flag --- go-chaos/internal/deployment.go | 19 +++++-------------- go-chaos/internal/deployment_test.go | 2 ++ go-chaos/internal/labels.go | 8 ++++++++ go-chaos/internal/saas.go | 13 +++++++------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/go-chaos/internal/deployment.go b/go-chaos/internal/deployment.go index a64da78c5..e32922d57 100644 --- a/go-chaos/internal/deployment.go +++ b/go-chaos/internal/deployment.go @@ -24,28 +24,19 @@ import ( ) func (c K8Client) getGatewayDeployment() (*v12.Deployment, error) { + listOptions := metav1.ListOptions{ - LabelSelector: getSelfManagedGatewayLabels(), + LabelSelector: getGatewayLabels(), } deploymentList, err := c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions) if err != nil { return nil, err } + // here it is currently hard to distingush between not existing and embedded gateway; + // since we don't use embedded gateway in our current chaos setup I would not support it right now here if deploymentList == nil || len(deploymentList.Items) <= 0 { - // lets check for SaaS setup - listOptions.LabelSelector = getSaasGatewayLabels() - deploymentList, err = c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions) - if err != nil { - return nil, err - } - - // here it is currently hard to distingush between not existing and embedded gateway; - // since we don't use embedded gateway in our current chaos setup I would not support it right now here - if deploymentList == nil || len(deploymentList.Items) <= 0 { - return nil, errors.New(fmt.Sprintf("Expected to find standalone gateway deployment in namespace %s, but none found!", c.GetCurrentNamespace())) - } + return nil, errors.New(fmt.Sprintf("Expected to find standalone gateway deployment in namespace %s, but none found!", c.GetCurrentNamespace())) } - return &deploymentList.Items[0], err } diff --git a/go-chaos/internal/deployment_test.go b/go-chaos/internal/deployment_test.go index d5c557323..19f5e6aaa 100644 --- a/go-chaos/internal/deployment_test.go +++ b/go-chaos/internal/deployment_test.go @@ -42,6 +42,7 @@ func Test_ShouldReturnTrueForRunningSaaSGatewayDeployment(t *testing.T) { k8Client := CreateFakeClient() selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels()) require.NoError(t, err) + k8Client.createSaaSCRD(t) k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway") // when @@ -85,6 +86,7 @@ func Test_ShouldReturnSaaSGatewayDeployment(t *testing.T) { k8Client := CreateFakeClient() selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels()) require.NoError(t, err) + k8Client.createSaaSCRD(t) k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway") // when diff --git a/go-chaos/internal/labels.go b/go-chaos/internal/labels.go index f8512c72c..dead8fc6c 100644 --- a/go-chaos/internal/labels.go +++ b/go-chaos/internal/labels.go @@ -58,3 +58,11 @@ func getSaasGatewayLabels() string { } return labels.Set(labelSelector.MatchLabels).String() } + +func getGatewayLabels() string { + if saasEnv { + return getSaasGatewayLabels() + } else { + return getSelfManagedGatewayLabels() + } +} diff --git a/go-chaos/internal/saas.go b/go-chaos/internal/saas.go index 068e35656..9fb4f2d1b 100644 --- a/go-chaos/internal/saas.go +++ b/go-chaos/internal/saas.go @@ -19,7 +19,6 @@ import ( "fmt" "strings" - k8sErrors "k8s.io/apimachinery/pkg/api/errors" meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" @@ -39,6 +38,13 @@ func (c K8Client) ResumeReconciliation() error { // otherwise it gets overwritten on the next reconcilation loop by the controller // Based on https://github.com/camunda-cloud/zeebe-controller-k8s#turning-the-controller-off func (c K8Client) setPauseFlag(pauseEnabled bool) error { + if !saasEnv { + if Verbosity { + fmt.Printf("Did not find zeebe cluster to pause reconciliation, ignoring. \n") + } + return nil + } + ctx := context.TODO() namespace := c.GetCurrentNamespace() clusterId := strings.TrimSuffix(namespace, "-zeebe") @@ -48,11 +54,6 @@ func (c K8Client) setPauseFlag(pauseEnabled bool) error { _, err := c.DynamicClient.Resource(zeebeCrd).Patch(ctx, clusterId, types.MergePatchType, []byte(payload), meta.PatchOptions{}) return err }) - if k8sErrors.IsNotFound(err) { - // No zb resource found so probably not Saas. Ignore for now. - fmt.Printf("Did not find zeebe cluster to pause reconciliation, ignoring. %s\n", err) - return nil - } return err } From 6f1fdd8990d46e5ae32aa9c77eef0b3ea99143c0 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Tue, 15 Nov 2022 22:20:53 +0100 Subject: [PATCH 05/13] refactor: move saas flag to k8client In order to better test the functionality (globals are always bad) and to make more sense why it is set on client creation, move the saas indication flag to the k8client --- go-chaos/internal/deployment.go | 2 +- go-chaos/internal/flags.go | 4 ---- go-chaos/internal/helper_test.go | 2 +- go-chaos/internal/k8helper.go | 5 +++-- go-chaos/internal/labels.go | 10 +++++++++- go-chaos/internal/pods.go | 24 ++++-------------------- go-chaos/internal/pods_test.go | 4 +++- go-chaos/internal/saas.go | 4 ++-- 8 files changed, 23 insertions(+), 32 deletions(-) diff --git a/go-chaos/internal/deployment.go b/go-chaos/internal/deployment.go index e32922d57..e8071e4cf 100644 --- a/go-chaos/internal/deployment.go +++ b/go-chaos/internal/deployment.go @@ -26,7 +26,7 @@ import ( func (c K8Client) getGatewayDeployment() (*v12.Deployment, error) { listOptions := metav1.ListOptions{ - LabelSelector: getGatewayLabels(), + LabelSelector: getGatewayLabels(c.SaaSEnv), } deploymentList, err := c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions) if err != nil { diff --git a/go-chaos/internal/flags.go b/go-chaos/internal/flags.go index 2a96bceb0..24eaefce0 100644 --- a/go-chaos/internal/flags.go +++ b/go-chaos/internal/flags.go @@ -19,10 +19,6 @@ import "github.com/camunda/zeebe/clients/go/v8/pkg/zbc" // defines whether the functions should print verbose output var Verbosity = false -// defines whether we running in saas or self-managed -// will be resolved automatically when creating the k8 client -var saasEnv = false - // defines if a custom kube config should be used instead of the default one found by k8s var KubeConfigPath string diff --git a/go-chaos/internal/helper_test.go b/go-chaos/internal/helper_test.go index 7de25d50f..91f2897c4 100644 --- a/go-chaos/internal/helper_test.go +++ b/go-chaos/internal/helper_test.go @@ -100,7 +100,7 @@ func (c K8Client) createSaaSCRD(t *testing.T) { _, err := c.DynamicClient.Resource(zeebeCrd).Create(context.TODO(), obj, metav1.CreateOptions{}) require.NoError(t, err) - saasEnv = c.isSaaSEnvironment() + c.SaaSEnv = c.isSaaSEnvironment() } func (c K8Client) CreateStatefulSetWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, name string) { diff --git a/go-chaos/internal/k8helper.go b/go-chaos/internal/k8helper.go index dd025eec4..48d5a7140 100644 --- a/go-chaos/internal/k8helper.go +++ b/go-chaos/internal/k8helper.go @@ -34,6 +34,7 @@ type K8Client struct { ClientConfig clientcmd.ClientConfig DynamicClient dynamic.Interface Clientset kubernetes.Interface + SaaSEnv bool } // Returns the current namespace, defined in the kubeconfig @@ -75,10 +76,10 @@ func createK8Client(settings KubernetesSettings) (K8Client, error) { } client := K8Client{Clientset: clientset, ClientConfig: clientConfig, DynamicClient: dynamicClient} - saasEnv = client.isSaaSEnvironment() + client.SaaSEnv = client.isSaaSEnvironment() if Verbosity { - if saasEnv { + if client.SaaSEnv { fmt.Println("Running experiment in SaaS environment.") } else { fmt.Println("Running experiment in self-managed environment.") diff --git a/go-chaos/internal/labels.go b/go-chaos/internal/labels.go index dead8fc6c..fc23c7f7c 100644 --- a/go-chaos/internal/labels.go +++ b/go-chaos/internal/labels.go @@ -26,6 +26,14 @@ func getSelfManagedZeebeStatefulSetLabels() string { return labels.Set(labelSelector.MatchLabels).String() } +func getBrokerLabels(saasEnv bool) string { + if saasEnv { + return getSaasBrokerLabels() + } else { + return getSelfManagedBrokerLabels() + } +} + func getSelfManagedBrokerLabels() string { labelSelector := metav1.LabelSelector{ MatchLabels: map[string]string{"app.kubernetes.io/component": "zeebe-broker"}, @@ -59,7 +67,7 @@ func getSaasGatewayLabels() string { return labels.Set(labelSelector.MatchLabels).String() } -func getGatewayLabels() string { +func getGatewayLabels(saasEnv bool) string { if saasEnv { return getSaasGatewayLabels() } else { diff --git a/go-chaos/internal/pods.go b/go-chaos/internal/pods.go index 2425e9ede..53e7b417f 100644 --- a/go-chaos/internal/pods.go +++ b/go-chaos/internal/pods.go @@ -35,7 +35,7 @@ import ( func (c K8Client) GetBrokerPods() (*v1.PodList, error) { listOptions := metav1.ListOptions{ - LabelSelector: getSelfManagedBrokerLabels(), + LabelSelector: getBrokerLabels(c.SaaSEnv), } list, err := c.Clientset.CoreV1().Pods(c.GetCurrentNamespace()).List(context.TODO(), listOptions) @@ -43,13 +43,7 @@ func (c K8Client) GetBrokerPods() (*v1.PodList, error) { return nil, err } - if list != nil && len(list.Items) > 0 { - return list, err - } - - // lets check for SaaS setup - listOptions.LabelSelector = getSaasBrokerLabels() - return c.Clientset.CoreV1().Pods(c.GetCurrentNamespace()).List(context.TODO(), listOptions) + return list, err } func (c K8Client) GetBrokerPodNames() ([]string, error) { @@ -74,7 +68,7 @@ func (c K8Client) extractPodNames(list *v1.PodList) ([]string, error) { func (c K8Client) GetGatewayPodNames() ([]string, error) { listOptions := metav1.ListOptions{ - LabelSelector: getSelfManagedGatewayLabels(), + LabelSelector: getGatewayLabels(c.SaaSEnv), // we check for running gateways, since terminated gateways can be lying around FieldSelector: "status.phase=Running", } @@ -85,17 +79,7 @@ func (c K8Client) GetGatewayPodNames() ([]string, error) { } if list == nil || len(list.Items) == 0 { - // lets check for SaaS setup - listOptions.LabelSelector = getSaasGatewayLabels() - list, err = c.Clientset.CoreV1().Pods(c.GetCurrentNamespace()).List(context.TODO(), listOptions) - if err != nil { - return nil, err - } - - if list == nil || len(list.Items) == 0 { - // maybe we have an embedded gateway setup - return c.GetBrokerPodNames() - } + return c.GetBrokerPodNames() } return c.extractPodNames(list) diff --git a/go-chaos/internal/pods_test.go b/go-chaos/internal/pods_test.go index b3860b08c..9695f566a 100644 --- a/go-chaos/internal/pods_test.go +++ b/go-chaos/internal/pods_test.go @@ -64,6 +64,7 @@ func Test_GetSaasBrokerPods(t *testing.T) { require.NoError(t, err) k8Client := CreateFakeClient() + k8Client.createSaaSCRD(t) k8Client.CreatePodWithLabels(t, selector) // when @@ -78,7 +79,7 @@ func Test_GetSaasBrokerPods(t *testing.T) { func Test_GetBrokersInOrder(t *testing.T) { // given - selector, err := metav1.ParseToLabelSelector(getSaasBrokerLabels()) + selector, err := metav1.ParseToLabelSelector(getSelfManagedBrokerLabels()) require.NoError(t, err) k8Client := CreateFakeClient() @@ -149,6 +150,7 @@ func Test_GetSelfManagedGatewayPodNames(t *testing.T) { func Test_GetSaasGatewayPodNames(t *testing.T) { // given k8Client := CreateFakeClient() + k8Client.createSaaSCRD(t) // gateway selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels()) diff --git a/go-chaos/internal/saas.go b/go-chaos/internal/saas.go index 9fb4f2d1b..87f10fc59 100644 --- a/go-chaos/internal/saas.go +++ b/go-chaos/internal/saas.go @@ -38,7 +38,7 @@ func (c K8Client) ResumeReconciliation() error { // otherwise it gets overwritten on the next reconcilation loop by the controller // Based on https://github.com/camunda-cloud/zeebe-controller-k8s#turning-the-controller-off func (c K8Client) setPauseFlag(pauseEnabled bool) error { - if !saasEnv { + if !c.SaaSEnv { if Verbosity { fmt.Printf("Did not find zeebe cluster to pause reconciliation, ignoring. \n") } @@ -63,7 +63,7 @@ func (c K8Client) isSaaSEnvironment() bool { 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 || resource == nil { + if err != nil || resource == nil { return false } return true From 96b24914f0d8a14bc355fee7cf5f17e67a58f0c8 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Tue, 15 Nov 2022 22:21:22 +0100 Subject: [PATCH 06/13] refactor: use pointer in order to make changes on a struct we need the pointer --- go-chaos/internal/helper_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/go-chaos/internal/helper_test.go b/go-chaos/internal/helper_test.go index 91f2897c4..efe6c60a2 100644 --- a/go-chaos/internal/helper_test.go +++ b/go-chaos/internal/helper_test.go @@ -89,8 +89,7 @@ func (c K8Client) CreateDeploymentWithLabelsAndName(t *testing.T, selector *meta require.NoError(t, err) } - -func (c K8Client) createSaaSCRD(t *testing.T) { +func (c *K8Client) createSaaSCRD(t *testing.T) { zeebeCrd := schema.GroupVersionResource{Group: "cloud.camunda.io", Version: "v1alpha1", Resource: "zeebeclusters"} obj := &unstructured.Unstructured{} namespace := c.GetCurrentNamespace() From 7048c9810b0f5c3d15bfb77525471db4741fec26 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Tue, 15 Nov 2022 22:21:35 +0100 Subject: [PATCH 07/13] test: fix failing test --- go-chaos/internal/network_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-chaos/internal/network_test.go b/go-chaos/internal/network_test.go index 8a9d0db27..1086a0891 100644 --- a/go-chaos/internal/network_test.go +++ b/go-chaos/internal/network_test.go @@ -44,7 +44,7 @@ func Test_ShouldApplyNetworkPatchOnStatefulSet(t *testing.T) { func Test_ShouldApplyNetworkPatchOnDeployment(t *testing.T) { // given k8Client := CreateFakeClient() - selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels()) + selector, err := metav1.ParseToLabelSelector(getSelfManagedGatewayLabels()) require.NoError(t, err) k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway") From ad5484488e2e812c1b6cdb1699545cb1b581ec56 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Wed, 16 Nov 2022 07:37:00 +0100 Subject: [PATCH 08/13] refactor: move getLabels to k8Client this removes the need of using the boolean flag as parameter --- go-chaos/internal/deployment.go | 2 +- go-chaos/internal/labels.go | 8 ++++---- go-chaos/internal/pods.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/go-chaos/internal/deployment.go b/go-chaos/internal/deployment.go index e8071e4cf..e802dadf2 100644 --- a/go-chaos/internal/deployment.go +++ b/go-chaos/internal/deployment.go @@ -26,7 +26,7 @@ import ( func (c K8Client) getGatewayDeployment() (*v12.Deployment, error) { listOptions := metav1.ListOptions{ - LabelSelector: getGatewayLabels(c.SaaSEnv), + LabelSelector: c.getGatewayLabels(), } deploymentList, err := c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions) if err != nil { diff --git a/go-chaos/internal/labels.go b/go-chaos/internal/labels.go index fc23c7f7c..8f830f018 100644 --- a/go-chaos/internal/labels.go +++ b/go-chaos/internal/labels.go @@ -26,8 +26,8 @@ func getSelfManagedZeebeStatefulSetLabels() string { return labels.Set(labelSelector.MatchLabels).String() } -func getBrokerLabels(saasEnv bool) string { - if saasEnv { +func (c K8Client) getBrokerLabels() string { + if c.SaaSEnv { return getSaasBrokerLabels() } else { return getSelfManagedBrokerLabels() @@ -67,8 +67,8 @@ func getSaasGatewayLabels() string { return labels.Set(labelSelector.MatchLabels).String() } -func getGatewayLabels(saasEnv bool) string { - if saasEnv { +func (c K8Client) getGatewayLabels() string { + if c.SaaSEnv { return getSaasGatewayLabels() } else { return getSelfManagedGatewayLabels() diff --git a/go-chaos/internal/pods.go b/go-chaos/internal/pods.go index 53e7b417f..47644332b 100644 --- a/go-chaos/internal/pods.go +++ b/go-chaos/internal/pods.go @@ -35,7 +35,7 @@ import ( func (c K8Client) GetBrokerPods() (*v1.PodList, error) { listOptions := metav1.ListOptions{ - LabelSelector: getBrokerLabels(c.SaaSEnv), + LabelSelector: c.getBrokerLabels(), } list, err := c.Clientset.CoreV1().Pods(c.GetCurrentNamespace()).List(context.TODO(), listOptions) @@ -68,7 +68,7 @@ func (c K8Client) extractPodNames(list *v1.PodList) ([]string, error) { func (c K8Client) GetGatewayPodNames() ([]string, error) { listOptions := metav1.ListOptions{ - LabelSelector: getGatewayLabels(c.SaaSEnv), + LabelSelector: c.getGatewayLabels(), // we check for running gateways, since terminated gateways can be lying around FieldSelector: "status.phase=Running", } From a9fa862f9c0b53ebe2de38fd04899b0cbeee4c9c Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Wed, 16 Nov 2022 07:37:49 +0100 Subject: [PATCH 09/13] refactor: use saas flag in getStatefulSet --- go-chaos/internal/statefulset.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/go-chaos/internal/statefulset.go b/go-chaos/internal/statefulset.go index 674413a56..55fd623bf 100644 --- a/go-chaos/internal/statefulset.go +++ b/go-chaos/internal/statefulset.go @@ -31,18 +31,19 @@ func (c K8Client) GetZeebeStatefulSet() (*v1.StatefulSet, error) { ctx := context.TODO() statefulSets := c.Clientset.AppsV1().StatefulSets(namespace) - sfs, err := statefulSets.List(ctx, meta.ListOptions{LabelSelector: getSelfManagedZeebeStatefulSetLabels()}) - if err != nil { - return nil, err - } - if len(sfs.Items) == 1 { - return &sfs.Items[0], nil - } - if len(sfs.Items) == 0 { + if c.SaaSEnv { // On SaaS the StatefulSet is just named "zeebe" without any identifying labels return statefulSets.Get(ctx, "zeebe", meta.GetOptions{}) + } else { + sfs, err := statefulSets.List(ctx, meta.ListOptions{LabelSelector: getSelfManagedZeebeStatefulSetLabels()}) + if err != nil { + return nil, err + } + if len(sfs.Items) == 1 { + return &sfs.Items[0], nil + } + return nil, errors.New("could not uniquely identify the stateful set for Zeebe") } - return nil, errors.New("could not uniquely identify the stateful set for Zeebe") } // ScaleZeebeCluster Scales the StatefulSet for Zeebe. Waits until scaling is complete before returning the initial scale. From be65317339e8013080ec5dab07baa067d4bc6e30 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Wed, 16 Nov 2022 07:47:06 +0100 Subject: [PATCH 10/13] test: new saas test --- go-chaos/internal/saas_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 go-chaos/internal/saas_test.go diff --git a/go-chaos/internal/saas_test.go b/go-chaos/internal/saas_test.go new file mode 100644 index 000000000..b0ae375f9 --- /dev/null +++ b/go-chaos/internal/saas_test.go @@ -0,0 +1,28 @@ +package internal + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_ShouldReturnTrueWhenCRDDeployed(t *testing.T) { + // given + k8Client := CreateFakeClient() + k8Client.createSaaSCRD(t) + + // when + isSaaSEnvironment := k8Client.isSaaSEnvironment() + + assert.True(t, isSaaSEnvironment) +} + +func Test_ShouldReturnFalseWhenNoCRDDeployed(t *testing.T) { + // given + k8Client := CreateFakeClient() + + // when + isSaaSEnvironment := k8Client.isSaaSEnvironment() + + assert.False(t, isSaaSEnvironment) +} From 031cf900420090b1b905bb35fe0a075097b0081a Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Wed, 16 Nov 2022 07:47:22 +0100 Subject: [PATCH 11/13] test: fix failing statefulset tests --- go-chaos/internal/network_test.go | 1 + go-chaos/internal/statefulset_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/go-chaos/internal/network_test.go b/go-chaos/internal/network_test.go index 1086a0891..0571b2009 100644 --- a/go-chaos/internal/network_test.go +++ b/go-chaos/internal/network_test.go @@ -26,6 +26,7 @@ import ( func Test_ShouldApplyNetworkPatchOnStatefulSet(t *testing.T) { // given k8Client := CreateFakeClient() + k8Client.createSaaSCRD(t) k8Client.CreateStatefulSetWithLabelsAndName(t, &metav1.LabelSelector{}, "zeebe") // when diff --git a/go-chaos/internal/statefulset_test.go b/go-chaos/internal/statefulset_test.go index 02b9de048..f79010ff1 100644 --- a/go-chaos/internal/statefulset_test.go +++ b/go-chaos/internal/statefulset_test.go @@ -41,6 +41,7 @@ func Test_ShouldReturnSelfManagedStatefulSet(t *testing.T) { func Test_ShouldReturnSaaSStatefulSet(t *testing.T) { // given k8Client := CreateFakeClient() + k8Client.createSaaSCRD(t) k8Client.CreateStatefulSetWithLabelsAndName(t, &metav1.LabelSelector{}, "zeebe") // when @@ -59,6 +60,20 @@ func Test_ShouldReturnErrorForNonExistingStatefulSet(t *testing.T) { // when statefulset, err := k8Client.GetZeebeStatefulSet() + // then + require.Error(t, err) + require.Contains(t, err.Error(), "could not uniquely identify the stateful set for Zeebe") + assert.Nil(t, statefulset) +} + +func Test_ShouldReturnErrorForNonExistingStatefulSetInSaaS(t *testing.T) { + // given + k8Client := CreateFakeClient() + k8Client.createSaaSCRD(t) + + // when + statefulset, err := k8Client.GetZeebeStatefulSet() + // then require.Error(t, err) require.Contains(t, err.Error(), "statefulsets.apps \"zeebe\" not found") From 8cd13de6e158a6f227f6f91c929646ab53ba1375 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Wed, 16 Nov 2022 07:49:14 +0100 Subject: [PATCH 12/13] build: add license --- go-chaos/internal/saas_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/go-chaos/internal/saas_test.go b/go-chaos/internal/saas_test.go index b0ae375f9..90ebe09db 100644 --- a/go-chaos/internal/saas_test.go +++ b/go-chaos/internal/saas_test.go @@ -1,3 +1,17 @@ +// Copyright 2022 Camunda Services GmbH +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package internal import ( From e9f568e8fdfd609343b95ad0a894ea5b683c5120 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Fri, 18 Nov 2022 13:02:53 +0100 Subject: [PATCH 13/13] refactor: enhance error msg --- go-chaos/internal/deployment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-chaos/internal/deployment.go b/go-chaos/internal/deployment.go index e802dadf2..290909abd 100644 --- a/go-chaos/internal/deployment.go +++ b/go-chaos/internal/deployment.go @@ -36,7 +36,7 @@ func (c K8Client) getGatewayDeployment() (*v12.Deployment, error) { // here it is currently hard to distingush between not existing and embedded gateway; // since we don't use embedded gateway in our current chaos setup I would not support it right now here if deploymentList == nil || len(deploymentList.Items) <= 0 { - return nil, errors.New(fmt.Sprintf("Expected to find standalone gateway deployment in namespace %s, but none found!", c.GetCurrentNamespace())) + return nil, errors.New(fmt.Sprintf("Expected to find standalone gateway deployment in namespace %s, but none found! The embedded gateway is not supported.", c.GetCurrentNamespace())) } return &deploymentList.Items[0], err }