From e45d53d476940ca93766ecbc8f96ece1532340c0 Mon Sep 17 00:00:00 2001 From: Michal Grzybek Date: Tue, 24 May 2022 11:02:14 +0200 Subject: [PATCH 1/6] Implements `automatic-kubernetes-api-monitoring-cluster-label` ff --- src/api/v1beta1/feature_flags.go | 21 ++++++++----- .../automaticapimonitoring/reconciler.go | 12 +++---- .../automaticapimonitoring/reconciler_test.go | 2 +- .../dynakube/dtclient_reconciler_test.go | 2 +- .../dynakube/dynakube_controller.go | 8 ++++- .../dynakube/dynakube_controller_test.go | 31 ++++++++++++++++++- src/dtclient/kubernetes_settings.go | 4 +-- 7 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/api/v1beta1/feature_flags.go b/src/api/v1beta1/feature_flags.go index 6451c6f1c3..9c5ab6f604 100644 --- a/src/api/v1beta1/feature_flags.go +++ b/src/api/v1beta1/feature_flags.go @@ -31,12 +31,14 @@ const ( AnnotationFeaturePrefix = "feature.dynatrace.com/" // activeGate - AnnotationFeatureDisableActiveGateUpdates = AnnotationFeaturePrefix + "disable-activegate-updates" - AnnotationFeatureDisableActiveGateRawImage = AnnotationFeaturePrefix + "disable-activegate-raw-image" - AnnotationFeatureActiveGateAppArmor = AnnotationFeaturePrefix + "activegate-apparmor" - AnnotationFeatureActiveGateReadOnlyFilesystem = AnnotationFeaturePrefix + "activegate-readonly-fs" - AnnotationFeatureAutomaticKubernetesApiMonitoring = AnnotationFeaturePrefix + "automatic-kubernetes-api-monitoring" - AnnotationFeatureActiveGateIgnoreProxy = AnnotationFeaturePrefix + "activegate-ignore-proxy" + AnnotationFeatureDisableActiveGateUpdates = AnnotationFeaturePrefix + "disable-activegate-updates" + AnnotationFeatureDisableActiveGateRawImage = AnnotationFeaturePrefix + "disable-activegate-raw-image" + AnnotationFeatureActiveGateAppArmor = AnnotationFeaturePrefix + "activegate-apparmor" + AnnotationFeatureActiveGateReadOnlyFilesystem = AnnotationFeaturePrefix + "activegate-readonly-fs" + AnnotationFeatureAutomaticK8sApiMonitoring = AnnotationFeaturePrefix + "automatic-kubernetes-api-monitoring" + AnnotationFeatureAutomaticK8sApiMonitoringClusterLabel = AnnotationFeaturePrefix + "automatic-kubernetes-api-monitoring-cluster-label" + AnnotationFeatureActiveGateIgnoreProxy = AnnotationFeaturePrefix + "activegate-ignore-proxy" + AnnotationFeatureActiveGateAuthToken = AnnotationFeaturePrefix + "enable-activegate-authtoken" AnnotationFeatureActiveGateAuthToken = AnnotationFeaturePrefix + "enable-activegate-authtoken" // statsD @@ -130,7 +132,12 @@ func (dk *DynaKube) getDefaultIgnoredNamespaces() []string { // FeatureAutomaticKubernetesApiMonitoring is a feature flag to enable automatic kubernetes api monitoring, // which ensures that settings for this kubernetes cluster exist in Dynatrace func (dk *DynaKube) FeatureAutomaticKubernetesApiMonitoring() bool { - return dk.getFeatureFlagRaw(AnnotationFeatureAutomaticKubernetesApiMonitoring) == "true" + return dk.getFeatureFlagRaw(AnnotationFeatureAutomaticK8sApiMonitoring) == "true" +} + +// FeatureAutomaticKubernetesApiMonitoringClusterLabel is a feature flag to set custom cluster name for automatic-kubernetes-api-monitoring +func (dk *DynaKube) FeatureAutomaticKubernetesApiMonitoringClusterLabel() string { + return dk.getFeatureFlagRaw(AnnotationFeatureAutomaticK8sApiMonitoringClusterLabel) } // FeatureDisableMetadataEnrichment is a feature flag to disable metadata enrichment, diff --git a/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler.go b/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler.go index 6cc1011b2e..1ee0cbb14a 100644 --- a/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler.go +++ b/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler.go @@ -9,14 +9,14 @@ import ( type AutomaticApiMonitoringReconciler struct { dtc dtclient.Client - name string + clusterLabel string kubeSystemUUID string } -func NewReconciler(dtc dtclient.Client, name, kubeSystemUUID string) *AutomaticApiMonitoringReconciler { +func NewReconciler(dtc dtclient.Client, clusterLabel, kubeSystemUUID string) *AutomaticApiMonitoringReconciler { return &AutomaticApiMonitoringReconciler{ dtc, - name, + clusterLabel, kubeSystemUUID, } } @@ -29,9 +29,9 @@ func (r *AutomaticApiMonitoringReconciler) Reconcile() error { } if objectID != "" { - log.Info("created kubernetes cluster setting", "name", r.name, "cluster", r.kubeSystemUUID, "object id", objectID) + log.Info("created kubernetes cluster setting", "clusterLabel", r.clusterLabel, "cluster", r.kubeSystemUUID, "object id", objectID) } else { - log.Info("kubernetes cluster setting already exists", "name", r.name, "cluster", r.kubeSystemUUID) + log.Info("kubernetes cluster setting already exists", "clusterLabel", r.clusterLabel, "cluster", r.kubeSystemUUID) } return nil @@ -60,7 +60,7 @@ func (r *AutomaticApiMonitoringReconciler) ensureSettingExists() (string, error) // determine newest ME (can be empty string), and create or update a settings object accordingly meID := determineNewestMonitoredEntity(monitoredEntities) - objectID, err := r.dtc.CreateOrUpdateKubernetesSetting(r.name, r.kubeSystemUUID, meID) + objectID, err := r.dtc.CreateOrUpdateKubernetesSetting(r.clusterLabel, r.kubeSystemUUID, meID) if err != nil { return "", err diff --git a/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler_test.go b/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler_test.go index 6bbdf5d528..8c4aaf2919 100644 --- a/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler_test.go +++ b/src/controllers/activegate/reconciler/automaticapimonitoring/reconciler_test.go @@ -12,7 +12,7 @@ import ( const ( testUID = "test-uid" - testName = "test-name" + testName = "test-clusterLabel" testObjectID = "test-objectid" ) diff --git a/src/controllers/dynakube/dtclient_reconciler_test.go b/src/controllers/dynakube/dtclient_reconciler_test.go index d5f8517382..6dd38a82af 100644 --- a/src/controllers/dynakube/dtclient_reconciler_test.go +++ b/src/controllers/dynakube/dtclient_reconciler_test.go @@ -238,7 +238,7 @@ func TestReconcileDynatraceClient_TokenValidation(t *testing.T) { t.Run("API token has missing scope for automatic kubernetes api monitoring", func(t *testing.T) { dk := base.DeepCopy() dk.Annotations = map[string]string{ - dynatracev1beta1.AnnotationFeatureAutomaticKubernetesApiMonitoring: "true", + dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoring: "true", } dk.Spec.ActiveGate = dynatracev1beta1.ActiveGateSpec{ Capabilities: []dynatracev1beta1.CapabilityDisplayName{dynatracev1beta1.KubeMonCapability.DisplayName}, diff --git a/src/controllers/dynakube/dynakube_controller.go b/src/controllers/dynakube/dynakube_controller.go index fb88851cf4..69b6800767 100644 --- a/src/controllers/dynakube/dynakube_controller.go +++ b/src/controllers/dynakube/dynakube_controller.go @@ -387,7 +387,13 @@ func (controller *DynakubeController) reconcileActiveGateCapabilities(dynakubeSt if dynakubeState.Instance.Status.KubeSystemUUID != "" && dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoring() && dynakubeState.Instance.KubernetesMonitoringMode() { - err := automaticapimonitoring.NewReconciler(dtc, dynakubeState.Instance.Name, dynakubeState.Instance.Status.KubeSystemUUID). + + clusterLabel := dynakubeState.Instance.Name + if dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoringClusterLabel() != "" { + clusterLabel = dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoringClusterLabel() + } + + err := automaticapimonitoring.NewReconciler(dtc, clusterLabel, dynakubeState.Instance.Status.KubeSystemUUID). Reconcile() if err != nil { log.Error(err, "could not create setting") diff --git a/src/controllers/dynakube/dynakube_controller_test.go b/src/controllers/dynakube/dynakube_controller_test.go index 0f2fda8f17..6218962dd9 100644 --- a/src/controllers/dynakube/dynakube_controller_test.go +++ b/src/controllers/dynakube/dynakube_controller_test.go @@ -154,7 +154,36 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { Name: testName, Namespace: testNamespace, Annotations: map[string]string{ - dynatracev1beta1.AnnotationFeatureAutomaticKubernetesApiMonitoring: "true", + dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoring: "true", + }, + }, + Spec: dynatracev1beta1.DynaKubeSpec{ + ActiveGate: dynatracev1beta1.ActiveGateSpec{ + Capabilities: []dynatracev1beta1.CapabilityDisplayName{ + dynatracev1beta1.KubeMonCapability.DisplayName, + }, + }, + }} + controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + + result, err := controller.Reconcile(context.TODO(), reconcile.Request{ + NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, + }) + + assert.NoError(t, err) + assert.NotNil(t, result) + }) + t.Run(`Reconcile reconciles automatic kubernetes api monitoring with custom cluster label`, func(t *testing.T) { + const clusterLabel = "..blabla..;.🙃" + mockClient := createDTMockClient(dtclient.TokenScopes{dtclient.TokenScopeInstallerDownload}, + dtclient.TokenScopes{dtclient.TokenScopeDataExport}) + instance := &dynatracev1beta1.DynaKube{ + ObjectMeta: metav1.ObjectMeta{ + Name: testName, + Namespace: testNamespace, + Annotations: map[string]string{ + dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoring: "true", + dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoringClusterLabel: clusterLabel, }, }, Spec: dynatracev1beta1.DynaKubeSpec{ diff --git a/src/dtclient/kubernetes_settings.go b/src/dtclient/kubernetes_settings.go index ee22364913..65d88bd1cb 100644 --- a/src/dtclient/kubernetes_settings.go +++ b/src/dtclient/kubernetes_settings.go @@ -67,7 +67,7 @@ type constraintViolations []struct { Path string } -func (dtc *dynatraceClient) CreateOrUpdateKubernetesSetting(name, kubeSystemUUID, scope string) (string, error) { +func (dtc *dynatraceClient) CreateOrUpdateKubernetesSetting(clusterLabel, kubeSystemUUID, scope string) (string, error) { if kubeSystemUUID == "" { return "", errors.New("no kube-system namespace UUID given") } @@ -78,7 +78,7 @@ func (dtc *dynatraceClient) CreateOrUpdateKubernetesSetting(name, kubeSystemUUID SchemaVersion: "1.0.27", Value: postKubernetesSettings{ Enabled: true, - Label: name, + Label: clusterLabel, ClusterIdEnabled: true, ClusterId: kubeSystemUUID, CloudApplicationPipelineEnabled: true, From 268acb923743337ecefeab81ef4c2c97cb506877 Mon Sep 17 00:00:00 2001 From: Michal Grzybek Date: Tue, 24 May 2022 13:44:18 +0200 Subject: [PATCH 2/6] `createFakeClientAndReconcile` -> `createFakeClientAndReconciler` --- .../dynakube/dynakube_controller_test.go | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/controllers/dynakube/dynakube_controller_test.go b/src/controllers/dynakube/dynakube_controller_test.go index 6218962dd9..f0f900a587 100644 --- a/src/controllers/dynakube/dynakube_controller_test.go +++ b/src/controllers/dynakube/dynakube_controller_test.go @@ -71,7 +71,7 @@ func TestMonitoringModesDynakube_Reconcile(t *testing.T) { OneAgent: deploymentModes[mode], }, } - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -107,7 +107,7 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { Name: testName, Namespace: testNamespace, }} - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -128,7 +128,7 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { KubernetesMonitoring: dynatracev1beta1.KubernetesMonitoringSpec{ Enabled: true, }}} - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -164,7 +164,7 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { }, }, }} - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -193,7 +193,7 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { }, }, }} - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -215,7 +215,7 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { Value: "https://proxy:1234", ValueFrom: "", }}} - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -245,7 +245,7 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { ValueFrom: "", }}} instance.Annotations = map[string]string{dynatracev1beta1.AnnotationFeatureActiveGateIgnoreProxy: "true"} - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -274,7 +274,7 @@ func TestReconcileOnlyOneTokenProvided_Reconcile(t *testing.T) { Namespace: testNamespace, }, Spec: dynatracev1beta1.DynaKubeSpec{}} - controller := createFakeClientAndReconcile(mockClient, instance, "", testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, "", testAPIToken) result, err := controller.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, @@ -364,7 +364,7 @@ func TestReconcile_RemoveRoutingIfDisabled(t *testing.T) { Routing: dynatracev1beta1.RoutingSpec{ Enabled: true, }}} - controller := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + controller := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) request := reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, } @@ -446,7 +446,7 @@ func TestReconcile_ActiveGateMultiCapability(t *testing.T) { }, }, }} - r := createFakeClientAndReconcile(mockClient, instance, testPaasToken, testAPIToken) + r := createFakeClientAndReconciler(mockClient, instance, testPaasToken, testAPIToken) request := reconcile.Request{ NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, } @@ -542,7 +542,7 @@ func createDTMockClient(paasTokenScopes, apiTokenScopes dtclient.TokenScopes) *d return mockClient } -func createFakeClientAndReconcile(mockClient dtclient.Client, instance *dynatracev1beta1.DynaKube, paasToken, apiToken string) *DynakubeController { +func createFakeClientAndReconciler(mockClient dtclient.Client, instance *dynatracev1beta1.DynaKube, paasToken, apiToken string) *DynakubeController { data := map[string][]byte{ dtclient.DynatraceApiToken: []byte(apiToken), } From a72df00cc2f8ed76bad603f120565f46dae60703 Mon Sep 17 00:00:00 2001 From: Michal Grzybek Date: Tue, 24 May 2022 14:09:03 +0200 Subject: [PATCH 3/6] Fix code smell --- src/controllers/dynakube/dynakube_controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/dynakube/dynakube_controller.go b/src/controllers/dynakube/dynakube_controller.go index 69b6800767..8c8f0b0894 100644 --- a/src/controllers/dynakube/dynakube_controller.go +++ b/src/controllers/dynakube/dynakube_controller.go @@ -389,8 +389,8 @@ func (controller *DynakubeController) reconcileActiveGateCapabilities(dynakubeSt dynakubeState.Instance.KubernetesMonitoringMode() { clusterLabel := dynakubeState.Instance.Name - if dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoringClusterLabel() != "" { - clusterLabel = dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoringClusterLabel() + if ffClusterLabel := dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoringClusterLabel(); ffClusterLabel != "" { + clusterLabel = ffClusterLabel } err := automaticapimonitoring.NewReconciler(dtc, clusterLabel, dynakubeState.Instance.Status.KubeSystemUUID). From bddd7aceebfcf809a8e28c2cc2ed0c495daf3ad4 Mon Sep 17 00:00:00 2001 From: Michal Grzybek Date: Wed, 25 May 2022 09:15:07 +0200 Subject: [PATCH 4/6] Rename annotation - `...-cluster-label` -> `...-cluster-name` --- src/api/v1beta1/feature_flags.go | 19 +++++++++---------- .../dynakube/dynakube_controller_test.go | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/api/v1beta1/feature_flags.go b/src/api/v1beta1/feature_flags.go index 9c5ab6f604..47ad11a9ac 100644 --- a/src/api/v1beta1/feature_flags.go +++ b/src/api/v1beta1/feature_flags.go @@ -31,15 +31,14 @@ const ( AnnotationFeaturePrefix = "feature.dynatrace.com/" // activeGate - AnnotationFeatureDisableActiveGateUpdates = AnnotationFeaturePrefix + "disable-activegate-updates" - AnnotationFeatureDisableActiveGateRawImage = AnnotationFeaturePrefix + "disable-activegate-raw-image" - AnnotationFeatureActiveGateAppArmor = AnnotationFeaturePrefix + "activegate-apparmor" - AnnotationFeatureActiveGateReadOnlyFilesystem = AnnotationFeaturePrefix + "activegate-readonly-fs" - AnnotationFeatureAutomaticK8sApiMonitoring = AnnotationFeaturePrefix + "automatic-kubernetes-api-monitoring" - AnnotationFeatureAutomaticK8sApiMonitoringClusterLabel = AnnotationFeaturePrefix + "automatic-kubernetes-api-monitoring-cluster-label" - AnnotationFeatureActiveGateIgnoreProxy = AnnotationFeaturePrefix + "activegate-ignore-proxy" - AnnotationFeatureActiveGateAuthToken = AnnotationFeaturePrefix + "enable-activegate-authtoken" - AnnotationFeatureActiveGateAuthToken = AnnotationFeaturePrefix + "enable-activegate-authtoken" + AnnotationFeatureDisableActiveGateUpdates = AnnotationFeaturePrefix + "disable-activegate-updates" + AnnotationFeatureDisableActiveGateRawImage = AnnotationFeaturePrefix + "disable-activegate-raw-image" + AnnotationFeatureActiveGateAppArmor = AnnotationFeaturePrefix + "activegate-apparmor" + AnnotationFeatureActiveGateReadOnlyFilesystem = AnnotationFeaturePrefix + "activegate-readonly-fs" + AnnotationFeatureAutomaticK8sApiMonitoring = AnnotationFeaturePrefix + "automatic-kubernetes-api-monitoring" + AnnotationFeatureAutomaticK8sApiMonitoringClusterName = AnnotationFeaturePrefix + "automatic-kubernetes-api-monitoring-cluster-name" + AnnotationFeatureActiveGateIgnoreProxy = AnnotationFeaturePrefix + "activegate-ignore-proxy" + AnnotationFeatureActiveGateAuthToken = AnnotationFeaturePrefix + "enable-activegate-authtoken" // statsD AnnotationFeatureUseActiveGateImageForStatsd = AnnotationFeaturePrefix + "use-activegate-image-for-statsd" @@ -137,7 +136,7 @@ func (dk *DynaKube) FeatureAutomaticKubernetesApiMonitoring() bool { // FeatureAutomaticKubernetesApiMonitoringClusterLabel is a feature flag to set custom cluster name for automatic-kubernetes-api-monitoring func (dk *DynaKube) FeatureAutomaticKubernetesApiMonitoringClusterLabel() string { - return dk.getFeatureFlagRaw(AnnotationFeatureAutomaticK8sApiMonitoringClusterLabel) + return dk.getFeatureFlagRaw(AnnotationFeatureAutomaticK8sApiMonitoringClusterName) } // FeatureDisableMetadataEnrichment is a feature flag to disable metadata enrichment, diff --git a/src/controllers/dynakube/dynakube_controller_test.go b/src/controllers/dynakube/dynakube_controller_test.go index f0f900a587..f2de24c74e 100644 --- a/src/controllers/dynakube/dynakube_controller_test.go +++ b/src/controllers/dynakube/dynakube_controller_test.go @@ -182,8 +182,8 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { Name: testName, Namespace: testNamespace, Annotations: map[string]string{ - dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoring: "true", - dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoringClusterLabel: clusterLabel, + dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoring: "true", + dynatracev1beta1.AnnotationFeatureAutomaticK8sApiMonitoringClusterName: clusterLabel, }, }, Spec: dynatracev1beta1.DynaKubeSpec{ From b56d666e7becd72cb4c950f6b7395d01a7690c47 Mon Sep 17 00:00:00 2001 From: Michal Grzybek Date: Wed, 25 May 2022 11:38:06 +0200 Subject: [PATCH 5/6] Simplified if condition --- src/api/v1beta1/feature_flags.go | 4 ++-- src/controllers/dynakube/dynakube_controller.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/v1beta1/feature_flags.go b/src/api/v1beta1/feature_flags.go index 47ad11a9ac..24699bce97 100644 --- a/src/api/v1beta1/feature_flags.go +++ b/src/api/v1beta1/feature_flags.go @@ -134,8 +134,8 @@ func (dk *DynaKube) FeatureAutomaticKubernetesApiMonitoring() bool { return dk.getFeatureFlagRaw(AnnotationFeatureAutomaticK8sApiMonitoring) == "true" } -// FeatureAutomaticKubernetesApiMonitoringClusterLabel is a feature flag to set custom cluster name for automatic-kubernetes-api-monitoring -func (dk *DynaKube) FeatureAutomaticKubernetesApiMonitoringClusterLabel() string { +// FeatureAutomaticKubernetesApiMonitoringClusterName is a feature flag to set custom cluster name for automatic-kubernetes-api-monitoring +func (dk *DynaKube) FeatureAutomaticKubernetesApiMonitoringClusterName() string { return dk.getFeatureFlagRaw(AnnotationFeatureAutomaticK8sApiMonitoringClusterName) } diff --git a/src/controllers/dynakube/dynakube_controller.go b/src/controllers/dynakube/dynakube_controller.go index 8c8f0b0894..f8f609fe01 100644 --- a/src/controllers/dynakube/dynakube_controller.go +++ b/src/controllers/dynakube/dynakube_controller.go @@ -388,9 +388,9 @@ func (controller *DynakubeController) reconcileActiveGateCapabilities(dynakubeSt dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoring() && dynakubeState.Instance.KubernetesMonitoringMode() { - clusterLabel := dynakubeState.Instance.Name - if ffClusterLabel := dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoringClusterLabel(); ffClusterLabel != "" { - clusterLabel = ffClusterLabel + clusterLabel := dynakubeState.Instance.FeatureAutomaticKubernetesApiMonitoringClusterName() + if clusterLabel == "" { + clusterLabel = dynakubeState.Instance.Name } err := automaticapimonitoring.NewReconciler(dtc, clusterLabel, dynakubeState.Instance.Status.KubeSystemUUID). From 81ace4754c408b0a6c01a2334a746a6eb5f6adb1 Mon Sep 17 00:00:00 2001 From: Michal Grzybek Date: Wed, 25 May 2022 15:34:19 +0200 Subject: [PATCH 6/6] Check if mock client was called; fix token scopes --- .../dynakube/dynakube_controller_test.go | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/controllers/dynakube/dynakube_controller_test.go b/src/controllers/dynakube/dynakube_controller_test.go index f2de24c74e..2a6103c489 100644 --- a/src/controllers/dynakube/dynakube_controller_test.go +++ b/src/controllers/dynakube/dynakube_controller_test.go @@ -148,7 +148,7 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { }) t.Run(`Reconcile reconciles automatic kubernetes api monitoring`, func(t *testing.T) { mockClient := createDTMockClient(dtclient.TokenScopes{dtclient.TokenScopeInstallerDownload}, - dtclient.TokenScopes{dtclient.TokenScopeDataExport}) + dtclient.TokenScopes{dtclient.TokenScopeDataExport, dtclient.TokenScopeEntitiesRead, dtclient.TokenScopeSettingsRead, dtclient.TokenScopeSettingsWrite}) instance := &dynatracev1beta1.DynaKube{ ObjectMeta: metav1.ObjectMeta{ Name: testName, @@ -170,13 +170,23 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, }) + mockClient.AssertCalled(t, "CreateOrUpdateKubernetesSetting", + testName, + testUID, + mock.AnythingOfType("string")) assert.NoError(t, err) - assert.NotNil(t, result) + assert.Equal(t, false, result.Requeue) }) - t.Run(`Reconcile reconciles automatic kubernetes api monitoring with custom cluster label`, func(t *testing.T) { + t.Run(`Reconcile reconciles automatic kubernetes api monitoring with custom cluster name`, func(t *testing.T) { const clusterLabel = "..blabla..;.🙃" + mockClient := createDTMockClient(dtclient.TokenScopes{dtclient.TokenScopeInstallerDownload}, - dtclient.TokenScopes{dtclient.TokenScopeDataExport}) + dtclient.TokenScopes{dtclient.TokenScopeDataExport, dtclient.TokenScopeEntitiesRead, dtclient.TokenScopeSettingsRead, dtclient.TokenScopeSettingsWrite}) + mockClient.On("CreateOrUpdateKubernetesSetting", + mock.AnythingOfType("string"), + mock.AnythingOfType("string"), + mock.AnythingOfType("string")).Return(testUID, nil) + instance := &dynatracev1beta1.DynaKube{ ObjectMeta: metav1.ObjectMeta{ Name: testName, @@ -199,8 +209,13 @@ func TestReconcileActiveGate_Reconcile(t *testing.T) { NamespacedName: types.NamespacedName{Namespace: testNamespace, Name: testName}, }) + mockClient.AssertCalled(t, "CreateOrUpdateKubernetesSetting", + clusterLabel, + testUID, + mock.AnythingOfType("string")) + assert.NoError(t, err) - assert.NotNil(t, result) + assert.Equal(t, false, result.Requeue) }) t.Run(`Reconcile reconciles proxy secret`, func(t *testing.T) { mockClient := createDTMockClient(dtclient.TokenScopes{dtclient.TokenScopeInstallerDownload},