From 2cd871ee9e551db71e2379aeb224b0649ba95dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Wed, 3 Jan 2024 08:42:26 +0100 Subject: [PATCH 01/15] WiP GardenCluster States gauge metric that react to CR deletion --- .../controller/gardener_cluster_controller.go | 12 ++++++++++++ .../gardener_cluster_controller_test.go | 5 ++++- internal/controller/metrics/metrics.go | 18 +++++++++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index b429a7ab..9ffba95e 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -89,8 +89,11 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req metrics.IncrementReconciliationLoopsStarted() err := controller.Get(ctx, req.NamespacedName, &cluster) + if err != nil { if k8serrors.IsNotFound(err) { + + controller.unsetStateMetric(ctx, req) err = controller.deleteKubeconfigSecret(ctx, req.Name) } @@ -148,6 +151,15 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req return controller.resultWithRequeue(&cluster, requeueAfter), nil } +func (controller *GardenerClusterController) unsetStateMetric(ctx context.Context, req ctrl.Request) { + var secretKey = "kubeconfig-" + req.NamespacedName.Name + var secretNamespacedName = types.NamespacedName{Name: secretKey, Namespace: "kcp-system"} + var kubeconfigSecret corev1.Secret + _ = controller.Get(ctx, secretNamespacedName, &kubeconfigSecret) + + metrics.UnSetGardenerClusterStates(kubeconfigSecret) +} + func loggingContextFromCluster(cluster *imv1.GardenerCluster) []any { return []any{"GardenerCluster", cluster.Name, "Namespace", cluster.Namespace} } diff --git a/internal/controller/gardener_cluster_controller_test.go b/internal/controller/gardener_cluster_controller_test.go index f60ae6fd..30b46088 100644 --- a/internal/controller/gardener_cluster_controller_test.go +++ b/internal/controller/gardener_cluster_controller_test.go @@ -16,6 +16,7 @@ import ( var _ = Describe("Gardener Cluster controller", func() { Context("Secret with kubeconfig doesn't exist", func() { It("Should create secret, and set Ready status on CR", func() { + Skip("Skipping for now, do not merge with this!") kymaName := "kymaname1" secretName := "secret-name1" shootName := "shootName1" @@ -56,6 +57,7 @@ var _ = Describe("Gardener Cluster controller", func() { }) It("Should delete secret", func() { + Skip("Skipping for now, do not merge with this!") kymaName := "kymaname2" secretName := "secret-name2" shootName := "shootName2" @@ -85,6 +87,7 @@ var _ = Describe("Gardener Cluster controller", func() { }) It("Should set Error status on CR if failed to fetch kubeconfig", func() { + Skip("Skipping for now, do not merge with this!") kymaName := "kymaname3" secretName := "secret-name3" shootName := "shootName3" @@ -108,8 +111,8 @@ var _ = Describe("Gardener Cluster controller", func() { Context("Secret with kubeconfig exists", func() { namespace := "default" - DescribeTable("Should update secret", func(gardenerClusterCR imv1.GardenerCluster, secret corev1.Secret, expectedKubeconfig string) { + Skip("Skipping for now, do not merge with this!") By("Create kubeconfig secret") Expect(k8sClient.Create(context.Background(), &secret)).To(Succeed()) diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index c6f7a99a..ad997a86 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -1,13 +1,15 @@ package metrics import ( + "fmt" v1 "github.com/kyma-project/infrastructure-manager/api/v1" "github.com/prometheus/client_golang/prometheus" + corev1 "k8s.io/api/core/v1" ctrlMetrics "sigs.k8s.io/controller-runtime/pkg/metrics" ) const ( - shootName = "shootName" + runtimeId = "runtimeId" state = "state" ) @@ -26,7 +28,7 @@ var ( Subsystem: "infrastructure_manager", Name: "im_gardener_clusters_state", Help: "Indicates the Status.state for GardenerCluster CRs", - }, []string{shootName, state}) + }, []string{runtimeId, state}) ) func init() { @@ -38,5 +40,15 @@ func IncrementReconciliationLoopsStarted() { } func SetGardenerClusterStates(cluster v1.GardenerCluster) { - metricGardenerClustersState.WithLabelValues(cluster.Spec.Shoot.Name, string(cluster.Status.State)).Set(1) + metricGardenerClustersState.WithLabelValues(cluster.Name, string(cluster.Status.State)).Set(1) +} + +func UnSetGardenerClusterStates(secret corev1.Secret) { + var runtimeId = secret.GetLabels()["kyma-project.io/runtime-id"] + var deletedReady = metricGardenerClustersState.DeleteLabelValues(runtimeId, "Ready") + var deletedError = metricGardenerClustersState.DeleteLabelValues(runtimeId, "Error") + + if deletedReady || deletedError { + fmt.Printf("GardenerClusterStates deleted value for %v", runtimeId) + } } From 814ea1b67864436bfb1430cf769aa62d1e05a442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Fri, 1 Mar 2024 09:06:27 +0100 Subject: [PATCH 02/15] fixes apply-sample-cr makefile target --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1e6039b4..119c2617 100644 --- a/Makefile +++ b/Makefile @@ -135,7 +135,7 @@ k3d-import-img: .PHONY: apply-sample-cr apply-sample-cr: - kubectl apply -f config/samples/clusterinventory_v1_gardenercluster.yaml + kubectl apply -f config/samples/infrastructuremanager_v1_gardenercluster.yaml .PHONE: local-build-and-deploy local-build-and-deploy: docker-build k3d-import-img deploy gardener-secret-deploy apply-sample-cr From 33e1cab0d5c35090d881d2360ea984c924ad6a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Mon, 4 Mar 2024 11:36:40 +0100 Subject: [PATCH 03/15] GardenerCluster states metric --- cmd/main.go | 4 +- .../controller/gardener_cluster_controller.go | 16 ++-- internal/controller/metrics/metrics.go | 80 +++++++++++-------- internal/controller/suite_test.go | 4 +- 4 files changed, 60 insertions(+), 44 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 86cc8ff9..1edddcec 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,6 +19,7 @@ package main import ( "flag" "fmt" + "github.com/kyma-project/infrastructure-manager/internal/controller/metrics" "os" "time" @@ -120,7 +121,8 @@ func main() { } rotationPeriod := time.Duration(minimalRotationTimeRatio*expirationTime.Minutes()) * time.Minute - if err = (controller.NewGardenerClusterController(mgr, kubeconfigProvider, logger, rotationPeriod)).SetupWithManager(mgr); err != nil { + metrics := metrics.NewMetrics() + if err = (controller.NewGardenerClusterController(mgr, kubeconfigProvider, logger, rotationPeriod, metrics)).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "GardenerCluster") os.Exit(1) } diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index 9ffba95e..1135f49d 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -52,15 +52,17 @@ type GardenerClusterController struct { KubeconfigProvider KubeconfigProvider log logr.Logger rotationPeriod time.Duration + metrics metrics.Metrics } -func NewGardenerClusterController(mgr ctrl.Manager, kubeconfigProvider KubeconfigProvider, logger logr.Logger, rotationPeriod time.Duration) *GardenerClusterController { +func NewGardenerClusterController(mgr ctrl.Manager, kubeconfigProvider KubeconfigProvider, logger logr.Logger, rotationPeriod time.Duration, metrics metrics.Metrics) *GardenerClusterController { return &GardenerClusterController{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), KubeconfigProvider: kubeconfigProvider, log: logger, rotationPeriod: rotationPeriod, + metrics: metrics, } } @@ -86,7 +88,6 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req controller.log.Info("Starting reconciliation.", loggingContext(req)...) var cluster imv1.GardenerCluster - metrics.IncrementReconciliationLoopsStarted() err := controller.Get(ctx, req.NamespacedName, &cluster) @@ -152,12 +153,7 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req } func (controller *GardenerClusterController) unsetStateMetric(ctx context.Context, req ctrl.Request) { - var secretKey = "kubeconfig-" + req.NamespacedName.Name - var secretNamespacedName = types.NamespacedName{Name: secretKey, Namespace: "kcp-system"} - var kubeconfigSecret corev1.Secret - _ = controller.Get(ctx, secretNamespacedName, &kubeconfigSecret) - - metrics.UnSetGardenerClusterStates(kubeconfigSecret) + controller.metrics.UnSetGardenerClusterStates(req.NamespacedName.Name) } func loggingContextFromCluster(cluster *imv1.GardenerCluster) []any { @@ -171,7 +167,7 @@ func loggingContext(req ctrl.Request) []any { func (controller *GardenerClusterController) resultWithRequeue(cluster *imv1.GardenerCluster, requeueAfter time.Duration) ctrl.Result { controller.log.Info("result with requeue", "RequeueAfter", requeueAfter.String()) - metrics.SetGardenerClusterStates(*cluster) + controller.metrics.SetGardenerClusterStates(*cluster) return ctrl.Result{ Requeue: true, @@ -181,7 +177,7 @@ func (controller *GardenerClusterController) resultWithRequeue(cluster *imv1.Gar func (controller *GardenerClusterController) resultWithoutRequeue(cluster *imv1.GardenerCluster) ctrl.Result { //nolint:unparam controller.log.Info("result without requeue") - metrics.SetGardenerClusterStates(*cluster) + controller.metrics.SetGardenerClusterStates(*cluster) return ctrl.Result{} } diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index ad997a86..22b4baeb 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -4,51 +4,67 @@ import ( "fmt" v1 "github.com/kyma-project/infrastructure-manager/api/v1" "github.com/prometheus/client_golang/prometheus" - corev1 "k8s.io/api/core/v1" ctrlMetrics "sigs.k8s.io/controller-runtime/pkg/metrics" ) const ( - runtimeId = "runtimeId" - state = "state" + runtimeIdKeyName = "runtimeId" + state = "state" + reason = "reason" + runtimeIdLabel = "kyma-project.io/runtime-id" + componentName = "infrastructure_manager" ) -var ( - - //nolint:godox //TODO: test custom metric, remove when done with https://github.com/kyma-project/infrastructure-manager/issues/11 - playgroundTotalReconciliationLoopsStarted = prometheus.NewCounter( //nolint:gochecknoglobals - prometheus.CounterOpts{ - Name: "im_playground_reconciliation_loops_started_total", - Help: "Number of times reconciliation loop was started", - }, - ) - - metricGardenerClustersState = prometheus.NewGaugeVec( //nolint:gochecknoglobals - prometheus.GaugeOpts{ //nolint:gochecknoglobals - Subsystem: "infrastructure_manager", - Name: "im_gardener_clusters_state", - Help: "Indicates the Status.state for GardenerCluster CRs", - }, []string{runtimeId, state}) -) +type Metrics struct { + gardenerClustersStateGaugeVec *prometheus.GaugeVec +} -func init() { - ctrlMetrics.Registry.MustRegister(playgroundTotalReconciliationLoopsStarted, metricGardenerClustersState) +func NewMetrics() Metrics { + m := Metrics{ + gardenerClustersStateGaugeVec: prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Subsystem: componentName, + Name: "im_gardener_clusters_state", + Help: "Indicates the Status.state for GardenerCluster CRs", + }, []string{runtimeIdKeyName, state, reason}), + } + ctrlMetrics.Registry.MustRegister(m.gardenerClustersStateGaugeVec) + return m } -func IncrementReconciliationLoopsStarted() { - playgroundTotalReconciliationLoopsStarted.Inc() +func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { + var runtimeId = cluster.GetLabels()[runtimeIdLabel] + + if runtimeId != "" { + var reason = cluster.Status.Conditions[0].Reason + + //first clean the old metric + m.cleanUpGardenerClusterGauge(runtimeId) + m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeId, string(cluster.Status.State), reason).Set(1) + } } -func SetGardenerClusterStates(cluster v1.GardenerCluster) { - metricGardenerClustersState.WithLabelValues(cluster.Name, string(cluster.Status.State)).Set(1) +func (m Metrics) UnSetGardenerClusterStates(runtimeId string) { + m.cleanUpGardenerClusterGauge(runtimeId) } -func UnSetGardenerClusterStates(secret corev1.Secret) { - var runtimeId = secret.GetLabels()["kyma-project.io/runtime-id"] - var deletedReady = metricGardenerClustersState.DeleteLabelValues(runtimeId, "Ready") - var deletedError = metricGardenerClustersState.DeleteLabelValues(runtimeId, "Error") +func (m Metrics) cleanUpGardenerClusterGauge(runtimeId string) { + + var readyMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeId, "Ready") + if readyMetric != nil { + readyMetric.Set(0) + } + var errorMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeId, "Error") + if errorMetric != nil { + errorMetric.Set(0) + } + fmt.Printf("GardenerClusterStates set value to 0 for %v", runtimeId) + + metricsDeleted := m.gardenerClustersStateGaugeVec.DeletePartialMatch(prometheus.Labels{ + runtimeIdKeyName: runtimeId, + }) - if deletedReady || deletedError { - fmt.Printf("GardenerClusterStates deleted value for %v", runtimeId) + if metricsDeleted > 0 { + fmt.Printf("gardenerClusterStateGauge deleted %d metrics for runtimeId %v", metricsDeleted, runtimeId) } } diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index d4c08cf4..1e6044b6 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -18,6 +18,7 @@ package controller import ( "context" + metrics "github.com/kyma-project/infrastructure-manager/internal/controller/metrics" "path/filepath" "testing" "time" @@ -81,8 +82,9 @@ var _ = BeforeSuite(func() { kubeconfigProviderMock := &mocks.KubeconfigProvider{} setupKubeconfigProviderMock(kubeconfigProviderMock) + metrics := metrics.NewMetrics() - controller := NewGardenerClusterController(mgr, kubeconfigProviderMock, logger, TestKubeconfigValidityTime) + controller := NewGardenerClusterController(mgr, kubeconfigProviderMock, logger, TestKubeconfigValidityTime, metrics) Expect(controller).NotTo(BeNil()) err = controller.SetupWithManager(mgr) From 94d8738cd6e3cdf05ec512a932c87d1d41d8d645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Mon, 4 Mar 2024 16:25:07 +0100 Subject: [PATCH 04/15] fix gci linter errors --- cmd/main.go | 2 +- internal/controller/gardener_cluster_controller.go | 1 - internal/controller/suite_test.go | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 1edddcec..322fa5be 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,7 +19,6 @@ package main import ( "flag" "fmt" - "github.com/kyma-project/infrastructure-manager/internal/controller/metrics" "os" "time" @@ -27,6 +26,7 @@ import ( gardener_apis "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1" infrastructuremanagerv1 "github.com/kyma-project/infrastructure-manager/api/v1" "github.com/kyma-project/infrastructure-manager/internal/controller" + "github.com/kyma-project/infrastructure-manager/internal/controller/metrics" "github.com/kyma-project/infrastructure-manager/internal/gardener" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/runtime" diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index 1135f49d..12bf2579 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -93,7 +93,6 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req if err != nil { if k8serrors.IsNotFound(err) { - controller.unsetStateMetric(ctx, req) err = controller.deleteKubeconfigSecret(ctx, req.Name) } diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 1ca78426..041be3b2 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -18,12 +18,12 @@ package controller import ( "context" - metrics "github.com/kyma-project/infrastructure-manager/internal/controller/metrics" "path/filepath" "testing" "time" infrastructuremanagerv1 "github.com/kyma-project/infrastructure-manager/api/v1" + metrics "github.com/kyma-project/infrastructure-manager/internal/controller/metrics" "github.com/kyma-project/infrastructure-manager/internal/controller/mocks" . "github.com/onsi/ginkgo/v2" //nolint:revive . "github.com/onsi/gomega" //nolint:revive From 0e2f13ba39cb53a5dce8d9f4591480784eff1eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Mon, 4 Mar 2024 16:29:06 +0100 Subject: [PATCH 05/15] further linter corrections --- internal/controller/metrics/metrics.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index 22b4baeb..cea8ae51 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -8,10 +8,10 @@ import ( ) const ( - runtimeIdKeyName = "runtimeId" + runtimeIDKeyName = "runtimeId" state = "state" reason = "reason" - runtimeIdLabel = "kyma-project.io/runtime-id" + runtimeIDLabel = "kyma-project.io/runtime-id" componentName = "infrastructure_manager" ) @@ -26,19 +26,19 @@ func NewMetrics() Metrics { Subsystem: componentName, Name: "im_gardener_clusters_state", Help: "Indicates the Status.state for GardenerCluster CRs", - }, []string{runtimeIdKeyName, state, reason}), + }, []string{runtimeIDKeyName, state, reason}), } ctrlMetrics.Registry.MustRegister(m.gardenerClustersStateGaugeVec) return m } func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { - var runtimeId = cluster.GetLabels()[runtimeIdLabel] + var runtimeId = cluster.GetLabels()[runtimeIDLabel] if runtimeId != "" { var reason = cluster.Status.Conditions[0].Reason - //first clean the old metric + // first clean the old metric m.cleanUpGardenerClusterGauge(runtimeId) m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeId, string(cluster.Status.State), reason).Set(1) } @@ -61,7 +61,7 @@ func (m Metrics) cleanUpGardenerClusterGauge(runtimeId string) { fmt.Printf("GardenerClusterStates set value to 0 for %v", runtimeId) metricsDeleted := m.gardenerClustersStateGaugeVec.DeletePartialMatch(prometheus.Labels{ - runtimeIdKeyName: runtimeId, + runtimeIDKeyName: runtimeId, }) if metricsDeleted > 0 { From 7d902adff089224e806c07f66c4477b46001e260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 5 Mar 2024 10:12:27 +0100 Subject: [PATCH 06/15] fixes tests --- .../controller/gardener_cluster_controller_test.go | 7 ++----- internal/controller/metrics/metrics.go | 10 ++++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/internal/controller/gardener_cluster_controller_test.go b/internal/controller/gardener_cluster_controller_test.go index 3a5f4531..702f42a9 100644 --- a/internal/controller/gardener_cluster_controller_test.go +++ b/internal/controller/gardener_cluster_controller_test.go @@ -16,7 +16,6 @@ import ( var _ = Describe("Gardener Cluster controller", func() { Context("Secret with kubeconfig doesn't exist", func() { It("Should create secret, and set Ready status on CR", func() { - Skip("Skipping for now, do not merge with this!") kymaName := "kymaname1" secretName := "secret-name1" shootName := "shootName1" @@ -57,7 +56,6 @@ var _ = Describe("Gardener Cluster controller", func() { }) It("Should delete secret", func() { - Skip("Skipping for now, do not merge with this!") kymaName := "kymaname2" secretName := "secret-name2" shootName := "shootName2" @@ -87,7 +85,6 @@ var _ = Describe("Gardener Cluster controller", func() { }) It("Should set Error status on CR if failed to fetch kubeconfig", func() { - Skip("Skipping for now, do not merge with this!") kymaName := "kymaname3" secretName := "secret-name3" shootName := "shootName3" @@ -112,7 +109,6 @@ var _ = Describe("Gardener Cluster controller", func() { Context("Secret with kubeconfig exists", func() { namespace := "default" DescribeTable("Should update secret", func(gardenerClusterCR imv1.GardenerCluster, secret corev1.Secret, expectedKubeconfig string) { - Skip("Skipping for now, do not merge with this!") By("Create kubeconfig secret") Expect(k8sClient.Create(context.Background(), &secret)).To(Succeed()) @@ -249,7 +245,8 @@ func fixSecretLabels(kymaName, shootName string) map[string]string { func fixGardenerClusterCR(kymaName, namespace, shootName, secretName string) imv1.GardenerCluster { return newTestGardenerClusterCR(kymaName, namespace, shootName, secretName). - WithLabels(fixGardenerClusterLabels(kymaName, shootName)).ToCluster() + WithLabels(fixGardenerClusterLabels(kymaName, shootName)). + ToCluster() } func fixGardenerClusterCRWithForceRotationAnnotation(kymaName, namespace, shootName, secretName string) imv1.GardenerCluster { diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index cea8ae51..405acda6 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -36,11 +36,13 @@ func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { var runtimeId = cluster.GetLabels()[runtimeIDLabel] if runtimeId != "" { - var reason = cluster.Status.Conditions[0].Reason + if len(cluster.Status.Conditions) != 0 { + var reason = cluster.Status.Conditions[0].Reason - // first clean the old metric - m.cleanUpGardenerClusterGauge(runtimeId) - m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeId, string(cluster.Status.State), reason).Set(1) + // first clean the old metric + m.cleanUpGardenerClusterGauge(runtimeId) + m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeId, string(cluster.Status.State), reason).Set(1) + } } } From f519fa29325234ecfdf7efd0b7adca21f57c444a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 5 Mar 2024 10:25:05 +0100 Subject: [PATCH 07/15] fix golanglint --- internal/controller/metrics/metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index 405acda6..8264eb00 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -2,6 +2,7 @@ package metrics import ( "fmt" + v1 "github.com/kyma-project/infrastructure-manager/api/v1" "github.com/prometheus/client_golang/prometheus" ctrlMetrics "sigs.k8s.io/controller-runtime/pkg/metrics" @@ -51,7 +52,6 @@ func (m Metrics) UnSetGardenerClusterStates(runtimeId string) { } func (m Metrics) cleanUpGardenerClusterGauge(runtimeId string) { - var readyMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeId, "Ready") if readyMetric != nil { readyMetric.Set(0) From e85d68a47fca04893c786cbfe5ba00b5b842c27d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 5 Mar 2024 10:50:21 +0100 Subject: [PATCH 08/15] fix golanglint --- .../controller/gardener_cluster_controller.go | 2 +- internal/controller/metrics/metrics.go | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index 12bf2579..548f756a 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -151,7 +151,7 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req return controller.resultWithRequeue(&cluster, requeueAfter), nil } -func (controller *GardenerClusterController) unsetStateMetric(ctx context.Context, req ctrl.Request) { +func (controller *GardenerClusterController) unsetStateMetric(req ctrl.Request) { controller.metrics.UnSetGardenerClusterStates(req.NamespacedName.Name) } diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index 8264eb00..aa38d5e6 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -34,39 +34,39 @@ func NewMetrics() Metrics { } func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { - var runtimeId = cluster.GetLabels()[runtimeIDLabel] + var runtimeID = cluster.GetLabels()[runtimeIDLabel] - if runtimeId != "" { + if runtimeID != "" { if len(cluster.Status.Conditions) != 0 { var reason = cluster.Status.Conditions[0].Reason // first clean the old metric - m.cleanUpGardenerClusterGauge(runtimeId) - m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeId, string(cluster.Status.State), reason).Set(1) + m.cleanUpGardenerClusterGauge(runtimeID) + m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeID, string(cluster.Status.State), reason).Set(1) } } } -func (m Metrics) UnSetGardenerClusterStates(runtimeId string) { - m.cleanUpGardenerClusterGauge(runtimeId) +func (m Metrics) UnSetGardenerClusterStates(runtimeID string) { + m.cleanUpGardenerClusterGauge(runtimeID) } -func (m Metrics) cleanUpGardenerClusterGauge(runtimeId string) { - var readyMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeId, "Ready") +func (m Metrics) cleanUpGardenerClusterGauge(runtimeID string) { + var readyMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeID, "Ready") if readyMetric != nil { readyMetric.Set(0) } - var errorMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeId, "Error") + var errorMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeID, "Error") if errorMetric != nil { errorMetric.Set(0) } - fmt.Printf("GardenerClusterStates set value to 0 for %v", runtimeId) + fmt.Printf("GardenerClusterStates set value to 0 for %v", runtimeID) metricsDeleted := m.gardenerClustersStateGaugeVec.DeletePartialMatch(prometheus.Labels{ - runtimeIDKeyName: runtimeId, + runtimeIDKeyName: runtimeID, }) if metricsDeleted > 0 { - fmt.Printf("gardenerClusterStateGauge deleted %d metrics for runtimeId %v", metricsDeleted, runtimeId) + fmt.Printf("gardenerClusterStateGauge deleted %d metrics for runtimeID %v", metricsDeleted, runtimeID) } } From af57cd751ebf90dda5d1b2f44752f571209bbb9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 5 Mar 2024 10:53:44 +0100 Subject: [PATCH 09/15] removes redundant ctx parameter --- internal/controller/gardener_cluster_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index 548f756a..d234b52a 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -93,7 +93,7 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req if err != nil { if k8serrors.IsNotFound(err) { - controller.unsetStateMetric(ctx, req) + controller.unsetStateMetric(req) err = controller.deleteKubeconfigSecret(ctx, req.Name) } From b7578dcbee81a5443e00817a97a43abc9881aca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Wed, 6 Mar 2024 11:05:56 +0100 Subject: [PATCH 10/15] adds test cases for metrics in debug mode --- .../gardener_cluster_controller_test.go | 73 ++++++++++++++++++- internal/controller/metrics/metrics.go | 21 +++--- 2 files changed, 82 insertions(+), 12 deletions(-) diff --git a/internal/controller/gardener_cluster_controller_test.go b/internal/controller/gardener_cluster_controller_test.go index 702f42a9..ff186ae9 100644 --- a/internal/controller/gardener_cluster_controller_test.go +++ b/internal/controller/gardener_cluster_controller_test.go @@ -2,6 +2,10 @@ package controller import ( "context" + "fmt" + "io" + "net/http" + "regexp" "time" imv1 "github.com/kyma-project/infrastructure-manager/api/v1" @@ -53,6 +57,10 @@ var _ = Describe("Gardener Cluster controller", func() { lastSyncTime := kubeconfigSecret.Annotations[lastKubeconfigSyncAnnotation] Expect(lastSyncTime).ToNot(BeEmpty()) + By("Metrics should be updated") + metricReason, metricState := getMetricsAttributes(kymaName) + Expect(metricReason).To(Equal(string(imv1.ConditionReasonKubeconfigSecretCreated))) + Expect(metricState).To(Equal(string(imv1.ReadyState))) }) It("Should delete secret", func() { @@ -80,8 +88,18 @@ var _ = Describe("Gardener Cluster controller", func() { By("Wait for secret deletion") Eventually(func() bool { err := k8sClient.Get(context.Background(), secretKey, &kubeconfigSecret) - return err != nil && k8serrors.IsNotFound(err) - }, time.Second*30, time.Second*3).Should(BeTrue()) + secretNotFound := err != nil && k8serrors.IsNotFound(err) + fmt.Printf("\nEventually->Wait for secret deletion->secretDeleted:%v", secretNotFound) + return secretNotFound + }, time.Minute*30, time.Second*3).Should(BeTrue()) + + By("Metrics should be cleared") + Eventually(func() string { + fmt.Printf("\n=== Eventually->Metrics should be cleared!\n") + metricReason, _ := getMetricsAttributes(kymaName) + return metricReason + }, time.Second*30, time.Second*3).Should(BeEmpty()) + }) It("Should set Error status on CR if failed to fetch kubeconfig", func() { @@ -103,6 +121,12 @@ var _ = Describe("Gardener Cluster controller", func() { return newGardenerCluster.Status.State == imv1.ErrorState }, time.Second*30, time.Second*3).Should(BeTrue()) + + By("Metrics should contain error label") + metricReason, metricState := getMetricsAttributes(kymaName) + Expect(metricReason).To(Equal(string(imv1.ConditionReasonFailedToGetKubeconfig))) + Expect(metricState).To(Equal(string(imv1.ErrorState))) + }) }) @@ -152,6 +176,10 @@ var _ = Describe("Gardener Cluster controller", func() { lastSyncTime := kubeconfigSecret.Annotations[lastKubeconfigSyncAnnotation] Expect(lastSyncTime).ToNot(BeEmpty()) + By("Metrics should show SecretRotated reason") + metricReason, metricState := getMetricsAttributes(gardenerClusterKey.Name) + Expect(metricReason).To(Equal(string(imv1.ConditionReasonKubeconfigSecretRotated))) + Expect(metricState).To(Equal(string(imv1.ReadyState))) }, Entry("Rotate kubeconfig when rotation time passed", fixGardenerClusterCR("kymaname4", namespace, "shootName4", "secret-name4"), @@ -191,6 +219,45 @@ var _ = Describe("Gardener Cluster controller", func() { }) }) +func getMetricsAttributes(runtimeID string) (outputReason, outputState string) { + stringBody, _ := getMetricsBody() + clusterStateMetricRegex := getGardenerClusterStateMetricRegex(runtimeID) + matches := clusterStateMetricRegex.FindStringSubmatch(stringBody) + fmt.Printf("\n===matches:%v", matches) + //fmt.Printf("\n!!!!stringBody:%v", stringBody) + if len(matches) > 0 { + outputReason = matches[1] + outputState = matches[2] + } + + return outputReason, outputState +} + +func getGardenerClusterStateMetricRegex(runtimeID string) *regexp.Regexp { + //infrastructure_manager_im_gardener_clusters_state{reason="KubeconfigSecretCreated",runtimeId="runtimeID",state="Ready"} 1 + return regexp.MustCompile(fmt.Sprintf("infrastructure_manager_im_gardener_clusters_state.*reason=\"(.*?)\",runtimeId=\"%v\",state=\"(.*?)\"", runtimeID)) +} + +func getMetricsBody() (string, error) { + clnt := &http.Client{} + request, err := http.NewRequestWithContext(suiteCtx, http.MethodGet, "http://localhost:8080/metrics", nil) + if err != nil { + return "", fmt.Errorf("request to metrics endpoint :%w", err) + } + response, err := clnt.Do(request) + if err != nil { + return "", fmt.Errorf("response from metrics endpoint :%w", err) + } + defer response.Body.Close() + bodyBytes, err := io.ReadAll(response.Body) + if err != nil { + return "", fmt.Errorf("response body:%w", err) + } + bodyString := string(bodyBytes) + + return bodyString, nil +} + func fixNewSecret(name, namespace, kymaName, shootName, data string, lastSyncTime string) corev1.Secret { labels := fixSecretLabels(kymaName, shootName) annotations := map[string]string{lastKubeconfigSyncAnnotation: lastSyncTime} @@ -305,7 +372,7 @@ func fixGardenerClusterLabels(kymaName, shootName string) map[string]string { labels := map[string]string{} labels["kyma-project.io/instance-id"] = "instanceID" - labels["kyma-project.io/runtime-id"] = "runtimeID" + labels["kyma-project.io/runtime-id"] = kymaName labels["kyma-project.io/broker-plan-id"] = "planID" labels["kyma-project.io/broker-plan-name"] = "planName" labels["kyma-project.io/global-account-id"] = "globalAccountID" diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index aa38d5e6..696bffaa 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -9,11 +9,12 @@ import ( ) const ( - runtimeIDKeyName = "runtimeId" - state = "state" - reason = "reason" - runtimeIDLabel = "kyma-project.io/runtime-id" - componentName = "infrastructure_manager" + runtimeIDKeyName = "runtimeId" + state = "state" + reason = "reason" + componentName = "infrastructure_manager" + RuntimeIDLabel = "kyma-project.io/runtime-id" + GardenerClusterStateMetricName = "im_gardener_clusters_state" ) type Metrics struct { @@ -25,7 +26,7 @@ func NewMetrics() Metrics { gardenerClustersStateGaugeVec: prometheus.NewGaugeVec( prometheus.GaugeOpts{ Subsystem: componentName, - Name: "im_gardener_clusters_state", + Name: GardenerClusterStateMetricName, Help: "Indicates the Status.state for GardenerCluster CRs", }, []string{runtimeIDKeyName, state, reason}), } @@ -34,7 +35,7 @@ func NewMetrics() Metrics { } func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { - var runtimeID = cluster.GetLabels()[runtimeIDLabel] + var runtimeID = cluster.GetLabels()[RuntimeIDLabel] if runtimeID != "" { if len(cluster.Status.Conditions) != 0 { @@ -43,6 +44,7 @@ func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { // first clean the old metric m.cleanUpGardenerClusterGauge(runtimeID) m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeID, string(cluster.Status.State), reason).Set(1) + fmt.Printf("\n++set metrics WithLabelValues(%v, %v, %v)", runtimeID, string(cluster.Status.State), reason) } } } @@ -55,18 +57,19 @@ func (m Metrics) cleanUpGardenerClusterGauge(runtimeID string) { var readyMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeID, "Ready") if readyMetric != nil { readyMetric.Set(0) + fmt.Printf("\n--cleanUpGardenerClusterGauge(set value to 0 for %v)", runtimeID) } var errorMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeID, "Error") if errorMetric != nil { errorMetric.Set(0) + fmt.Printf("\n--cleanUpGardenerClusterGauge(set value to 0 for %v)", runtimeID) } - fmt.Printf("GardenerClusterStates set value to 0 for %v", runtimeID) metricsDeleted := m.gardenerClustersStateGaugeVec.DeletePartialMatch(prometheus.Labels{ runtimeIDKeyName: runtimeID, }) if metricsDeleted > 0 { - fmt.Printf("gardenerClusterStateGauge deleted %d metrics for runtimeID %v", metricsDeleted, runtimeID) + fmt.Printf("\n--cleanUpGardenerClusterGauge(deleted %d metrics for runtimeID %v)", metricsDeleted, runtimeID) } } From 05494690806a1cdf61a62e07013bef2b0a1a4ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Wed, 6 Mar 2024 11:26:50 +0100 Subject: [PATCH 11/15] clean-ups metrics code --- .../gardener_cluster_controller_test.go | 20 ++++++++----------- internal/controller/metrics/metrics.go | 20 +------------------ 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/internal/controller/gardener_cluster_controller_test.go b/internal/controller/gardener_cluster_controller_test.go index ff186ae9..c2e25755 100644 --- a/internal/controller/gardener_cluster_controller_test.go +++ b/internal/controller/gardener_cluster_controller_test.go @@ -57,7 +57,7 @@ var _ = Describe("Gardener Cluster controller", func() { lastSyncTime := kubeconfigSecret.Annotations[lastKubeconfigSyncAnnotation] Expect(lastSyncTime).ToNot(BeEmpty()) - By("Metrics should be updated") + By("Metrics should be appended after creation") metricReason, metricState := getMetricsAttributes(kymaName) Expect(metricReason).To(Equal(string(imv1.ConditionReasonKubeconfigSecretCreated))) Expect(metricState).To(Equal(string(imv1.ReadyState))) @@ -93,9 +93,8 @@ var _ = Describe("Gardener Cluster controller", func() { return secretNotFound }, time.Minute*30, time.Second*3).Should(BeTrue()) - By("Metrics should be cleared") + By("Metrics should be cleared after deletion") Eventually(func() string { - fmt.Printf("\n=== Eventually->Metrics should be cleared!\n") metricReason, _ := getMetricsAttributes(kymaName) return metricReason }, time.Second*30, time.Second*3).Should(BeEmpty()) @@ -175,11 +174,6 @@ var _ = Describe("Gardener Cluster controller", func() { Expect(string(kubeconfigSecret.Data["config"])).To(Equal(expectedKubeconfig)) lastSyncTime := kubeconfigSecret.Annotations[lastKubeconfigSyncAnnotation] Expect(lastSyncTime).ToNot(BeEmpty()) - - By("Metrics should show SecretRotated reason") - metricReason, metricState := getMetricsAttributes(gardenerClusterKey.Name) - Expect(metricReason).To(Equal(string(imv1.ConditionReasonKubeconfigSecretRotated))) - Expect(metricState).To(Equal(string(imv1.ReadyState))) }, Entry("Rotate kubeconfig when rotation time passed", fixGardenerClusterCR("kymaname4", namespace, "shootName4", "secret-name4"), @@ -223,8 +217,6 @@ func getMetricsAttributes(runtimeID string) (outputReason, outputState string) { stringBody, _ := getMetricsBody() clusterStateMetricRegex := getGardenerClusterStateMetricRegex(runtimeID) matches := clusterStateMetricRegex.FindStringSubmatch(stringBody) - fmt.Printf("\n===matches:%v", matches) - //fmt.Printf("\n!!!!stringBody:%v", stringBody) if len(matches) > 0 { outputReason = matches[1] outputState = matches[2] @@ -233,9 +225,13 @@ func getMetricsAttributes(runtimeID string) (outputReason, outputState string) { return outputReason, outputState } +// getGardenerClusterStateMetricRegex returns regex that will find matches of gardener_cluster_state metrics +// and capture two groups for given `runtimeId` label value: +// 1) `reason` label value +// 2) `state` label value func getGardenerClusterStateMetricRegex(runtimeID string) *regexp.Regexp { - //infrastructure_manager_im_gardener_clusters_state{reason="KubeconfigSecretCreated",runtimeId="runtimeID",state="Ready"} 1 - return regexp.MustCompile(fmt.Sprintf("infrastructure_manager_im_gardener_clusters_state.*reason=\"(.*?)\",runtimeId=\"%v\",state=\"(.*?)\"", runtimeID)) + regexString := fmt.Sprintf("infrastructure_manager_im_gardener_clusters_state.*reason=\"(.*?)\",runtimeId=\"%v\",state=\"(.*?)\"", runtimeID) + return regexp.MustCompile(regexString) } func getMetricsBody() (string, error) { diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index 696bffaa..2f233f5c 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -1,8 +1,6 @@ package metrics import ( - "fmt" - v1 "github.com/kyma-project/infrastructure-manager/api/v1" "github.com/prometheus/client_golang/prometheus" ctrlMetrics "sigs.k8s.io/controller-runtime/pkg/metrics" @@ -44,7 +42,6 @@ func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { // first clean the old metric m.cleanUpGardenerClusterGauge(runtimeID) m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeID, string(cluster.Status.State), reason).Set(1) - fmt.Printf("\n++set metrics WithLabelValues(%v, %v, %v)", runtimeID, string(cluster.Status.State), reason) } } } @@ -54,22 +51,7 @@ func (m Metrics) UnSetGardenerClusterStates(runtimeID string) { } func (m Metrics) cleanUpGardenerClusterGauge(runtimeID string) { - var readyMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeID, "Ready") - if readyMetric != nil { - readyMetric.Set(0) - fmt.Printf("\n--cleanUpGardenerClusterGauge(set value to 0 for %v)", runtimeID) - } - var errorMetric, _ = m.gardenerClustersStateGaugeVec.GetMetricWithLabelValues(runtimeID, "Error") - if errorMetric != nil { - errorMetric.Set(0) - fmt.Printf("\n--cleanUpGardenerClusterGauge(set value to 0 for %v)", runtimeID) - } - - metricsDeleted := m.gardenerClustersStateGaugeVec.DeletePartialMatch(prometheus.Labels{ + m.gardenerClustersStateGaugeVec.DeletePartialMatch(prometheus.Labels{ runtimeIDKeyName: runtimeID, }) - - if metricsDeleted > 0 { - fmt.Printf("\n--cleanUpGardenerClusterGauge(deleted %d metrics for runtimeID %v)", metricsDeleted, runtimeID) - } } From e29481450d1baac5742be293626a4c0f26fdd6cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Wed, 6 Mar 2024 11:28:46 +0100 Subject: [PATCH 12/15] removes debug print --- .../gardener_cluster_controller_test.go | 1 - metrics-service.yaml | 42 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 metrics-service.yaml diff --git a/internal/controller/gardener_cluster_controller_test.go b/internal/controller/gardener_cluster_controller_test.go index c2e25755..5abbbd2e 100644 --- a/internal/controller/gardener_cluster_controller_test.go +++ b/internal/controller/gardener_cluster_controller_test.go @@ -89,7 +89,6 @@ var _ = Describe("Gardener Cluster controller", func() { Eventually(func() bool { err := k8sClient.Get(context.Background(), secretKey, &kubeconfigSecret) secretNotFound := err != nil && k8serrors.IsNotFound(err) - fmt.Printf("\nEventually->Wait for secret deletion->secretDeleted:%v", secretNotFound) return secretNotFound }, time.Minute*30, time.Second*3).Should(BeTrue()) diff --git a/metrics-service.yaml b/metrics-service.yaml new file mode 100644 index 00000000..8d47b669 --- /dev/null +++ b/metrics-service.yaml @@ -0,0 +1,42 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: | + {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app.kubernetes.io/component":"kube-rbac-proxy","app.kubernetes.io/created-by":"infrastructure-manager","app.kubernetes.io/instance":"infrastructure-manager-metrics-service","app.kubernetes.io/managed-by":"kustomize","app.kubernetes.io/name":"service","app.kubernetes.io/part-of":"infrastructure-manager","control-plane":"infrastructure-manager"},"name":"infrastructure-manager-infrastructure-manager-metrics-service","namespace":"kcp-system"},"spec":{"ports":[{"name":"https","port":8443,"protocol":"TCP","targetPort":"https"}],"selector":{"control-plane":"infrastructure-manager"}}} + creationTimestamp: "2024-02-29T13:34:21Z" + labels: + app.kubernetes.io/component: kube-rbac-proxy + app.kubernetes.io/created-by: infrastructure-manager + app.kubernetes.io/instance: infrastructure-manager-metrics-service + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/name: service + app.kubernetes.io/part-of: infrastructure-manager + control-plane: infrastructure-manager + name: infrastructure-manager-infrastructure-manager-metrics-service + namespace: kcp-system + resourceVersion: "1700" + uid: 71841a36-16cc-440c-a1eb-e7b298a7f16b +spec: + clusterIP: 10.43.37.224 + clusterIPs: + - 10.43.37.224 + internalTrafficPolicy: Cluster + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack + ports: + - name: https + port: 8443 + protocol: TCP + targetPort: https + - name: http + port: 8080 + protocol: TCP + targetPort: 8080 + selector: + control-plane: infrastructure-manager + sessionAffinity: None + type: ClusterIP +status: + loadBalancer: {} From 5e5f75b6d3ea59bdb15e9ded372f15b9a6504928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Wed, 6 Mar 2024 11:32:28 +0100 Subject: [PATCH 13/15] removes test service definition --- metrics-service.yaml | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 metrics-service.yaml diff --git a/metrics-service.yaml b/metrics-service.yaml deleted file mode 100644 index 8d47b669..00000000 --- a/metrics-service.yaml +++ /dev/null @@ -1,42 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - annotations: - kubectl.kubernetes.io/last-applied-configuration: | - {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app.kubernetes.io/component":"kube-rbac-proxy","app.kubernetes.io/created-by":"infrastructure-manager","app.kubernetes.io/instance":"infrastructure-manager-metrics-service","app.kubernetes.io/managed-by":"kustomize","app.kubernetes.io/name":"service","app.kubernetes.io/part-of":"infrastructure-manager","control-plane":"infrastructure-manager"},"name":"infrastructure-manager-infrastructure-manager-metrics-service","namespace":"kcp-system"},"spec":{"ports":[{"name":"https","port":8443,"protocol":"TCP","targetPort":"https"}],"selector":{"control-plane":"infrastructure-manager"}}} - creationTimestamp: "2024-02-29T13:34:21Z" - labels: - app.kubernetes.io/component: kube-rbac-proxy - app.kubernetes.io/created-by: infrastructure-manager - app.kubernetes.io/instance: infrastructure-manager-metrics-service - app.kubernetes.io/managed-by: kustomize - app.kubernetes.io/name: service - app.kubernetes.io/part-of: infrastructure-manager - control-plane: infrastructure-manager - name: infrastructure-manager-infrastructure-manager-metrics-service - namespace: kcp-system - resourceVersion: "1700" - uid: 71841a36-16cc-440c-a1eb-e7b298a7f16b -spec: - clusterIP: 10.43.37.224 - clusterIPs: - - 10.43.37.224 - internalTrafficPolicy: Cluster - ipFamilies: - - IPv4 - ipFamilyPolicy: SingleStack - ports: - - name: https - port: 8443 - protocol: TCP - targetPort: https - - name: http - port: 8080 - protocol: TCP - targetPort: 8080 - selector: - control-plane: infrastructure-manager - sessionAffinity: None - type: ClusterIP -status: - loadBalancer: {} From f10b26b6a999643147ea213c0f0b47178a682e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Thu, 7 Mar 2024 14:12:55 +0100 Subject: [PATCH 14/15] upgrades golang to 1.22.1 --- .github/workflows/run-vuln-check.yaml | 2 +- Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-vuln-check.yaml b/.github/workflows/run-vuln-check.yaml index af671ffe..df826356 100644 --- a/.github/workflows/run-vuln-check.yaml +++ b/.github/workflows/run-vuln-check.yaml @@ -15,5 +15,5 @@ jobs: - name: vulncheck uses: golang/govulncheck-action@v1 with: - go-version-input: 1.21.5 + go-version-input: 1.22.1 go-package: ./... diff --git a/Dockerfile b/Dockerfile index b22e4ec3..2c987475 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM golang:1.21.5 as builder +FROM golang:1.22.1 as builder ARG TARGETOS ARG TARGETARCH From dec8b6da44f55df94fde882ce90387de137da3a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Fri, 8 Mar 2024 09:22:18 +0100 Subject: [PATCH 15/15] removes redundant UnSetGardenerClusterStates fn --- internal/controller/gardener_cluster_controller.go | 2 +- internal/controller/metrics/metrics.go | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index d234b52a..a2ce9894 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -152,7 +152,7 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req } func (controller *GardenerClusterController) unsetStateMetric(req ctrl.Request) { - controller.metrics.UnSetGardenerClusterStates(req.NamespacedName.Name) + controller.metrics.CleanUpGardenerClusterGauge(req.NamespacedName.Name) } func loggingContextFromCluster(cluster *imv1.GardenerCluster) []any { diff --git a/internal/controller/metrics/metrics.go b/internal/controller/metrics/metrics.go index 2f233f5c..d1755731 100644 --- a/internal/controller/metrics/metrics.go +++ b/internal/controller/metrics/metrics.go @@ -40,17 +40,13 @@ func (m Metrics) SetGardenerClusterStates(cluster v1.GardenerCluster) { var reason = cluster.Status.Conditions[0].Reason // first clean the old metric - m.cleanUpGardenerClusterGauge(runtimeID) + m.CleanUpGardenerClusterGauge(runtimeID) m.gardenerClustersStateGaugeVec.WithLabelValues(runtimeID, string(cluster.Status.State), reason).Set(1) } } } -func (m Metrics) UnSetGardenerClusterStates(runtimeID string) { - m.cleanUpGardenerClusterGauge(runtimeID) -} - -func (m Metrics) cleanUpGardenerClusterGauge(runtimeID string) { +func (m Metrics) CleanUpGardenerClusterGauge(runtimeID string) { m.gardenerClustersStateGaugeVec.DeletePartialMatch(prometheus.Labels{ runtimeIDKeyName: runtimeID, })