From 2d49d2905d3b04a39522117bbe0d33c3c52118bf Mon Sep 17 00:00:00 2001
From: QuentinBisson <quentin@giantswarm.io>
Date: Wed, 10 Apr 2024 14:17:47 +0200
Subject: [PATCH] Address comments

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
---
 .../controller/cluster_monitoring_controller.go     |  4 ++--
 pkg/common/types.go                                 |  7 +++++++
 pkg/monitoring/prometheusagent/config.go            | 13 +++++++------
 pkg/monitoring/prometheusagent/secret.go            |  6 +++---
 pkg/monitoring/prometheusagent/service.go           | 12 +++++++-----
 5 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/internal/controller/cluster_monitoring_controller.go b/internal/controller/cluster_monitoring_controller.go
index 4bbd449c..ba96b33b 100644
--- a/internal/controller/cluster_monitoring_controller.go
+++ b/internal/controller/cluster_monitoring_controller.go
@@ -118,7 +118,7 @@ func (r *ClusterMonitoringReconciler) reconcile(ctx context.Context, cluster *cl
 	}
 
 	// Create or update PrometheusAgent remote write configuration.
-	err := r.PrometheusAgentService.ReconcileRemoteWriteConfig(ctx, cluster)
+	err := r.PrometheusAgentService.ReconcileRemoteWriteConfiguration(ctx, cluster)
 	if err != nil {
 		logger.Error(err, "failed to create or update prometheus agent remote write config")
 		return ctrl.Result{RequeueAfter: 5 * time.Minute}, errors.WithStack(err)
@@ -139,7 +139,7 @@ func (r *ClusterMonitoringReconciler) reconcileDelete(ctx context.Context, clust
 			}
 		}
 
-		err := r.PrometheusAgentService.DeleteRemoteWriteConfig(ctx, cluster)
+		err := r.PrometheusAgentService.DeleteRemoteWriteConfiguration(ctx, cluster)
 		if err != nil {
 			logger.Error(err, "failed to delete prometheus agent remote write config")
 			return ctrl.Result{RequeueAfter: 5 * time.Minute}, errors.WithStack(err)
diff --git a/pkg/common/types.go b/pkg/common/types.go
index 96fd8a95..91a25407 100644
--- a/pkg/common/types.go
+++ b/pkg/common/types.go
@@ -47,6 +47,13 @@ type ManagementCluster struct {
 	Region string
 }
 
+func GetClusterType(cluster *clusterv1.Cluster, mc ManagementCluster) string {
+	if cluster.Name == mc.Name {
+		return "management_cluster"
+	}
+	return "workload_cluster"
+}
+
 func GetClusterProvider(cluster *clusterv1.Cluster) (string, error) {
 	switch cluster.Spec.InfrastructureRef.Kind {
 	case AWSClusterKind:
diff --git a/pkg/monitoring/prometheusagent/config.go b/pkg/monitoring/prometheusagent/config.go
index b38fcc21..d6890521 100644
--- a/pkg/monitoring/prometheusagent/config.go
+++ b/pkg/monitoring/prometheusagent/config.go
@@ -33,14 +33,9 @@ func (pas PrometheusAgentService) buildRemoteWriteConfig(ctx context.Context,
 		return nil, errors.WithStack(err)
 	}
 
-	clusterType := "workload_cluster"
-	if val, ok := cluster.Labels["cluster.x-k8s.io/cluster-name"]; ok && val == pas.ManagementCluster.Name {
-		clusterType = "management_cluster"
-	}
-
 	externalLabels := map[string]string{
 		"cluster_id":       cluster.Name,
-		"cluster_type":     clusterType,
+		"cluster_type":     common.GetClusterType(cluster, pas.ManagementCluster),
 		"customer":         pas.ManagementCluster.Customer,
 		"installation":     pas.ManagementCluster.Name,
 		"organization":     organization,
@@ -69,6 +64,12 @@ func (pas PrometheusAgentService) buildRemoteWriteConfig(ctx context.Context,
 		return nil, errors.WithStack(err)
 	}
 
+	if currentShards < shards {
+		logger.Info("scaling up shards", "old", currentShards, "new", shards)
+	} else if currentShards > shards {
+		logger.Info("scaling down shards", "old", currentShards, "new", shards)
+	}
+
 	return &corev1.ConfigMap{
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      getPrometheusAgentRemoteWriteConfigName(cluster),
diff --git a/pkg/monitoring/prometheusagent/secret.go b/pkg/monitoring/prometheusagent/secret.go
index 0e2b6a37..3ee949b9 100644
--- a/pkg/monitoring/prometheusagent/secret.go
+++ b/pkg/monitoring/prometheusagent/secret.go
@@ -76,9 +76,9 @@ func readRemoteWritePasswordFromSecret(secret corev1.Secret) (string, error) {
 	}
 
 	for _, rw := range remoteWriteConfig.PrometheusAgentConfig.RemoteWrite {
-                // We read the secret from the remote write configuration named `prometheus-meta-operator` only
-                // as this secret is generated per cluster. 
-                // This will eventually be taken care of by the multi-tenancy contoller
+		// We read the secret from the remote write configuration named `prometheus-meta-operator` only
+		// as this secret is generated per cluster.
+		// This will eventually be taken care of by the multi-tenancy contoller
 		if rw.Name == remoteWriteName {
 			return rw.Password, nil
 		}
diff --git a/pkg/monitoring/prometheusagent/service.go b/pkg/monitoring/prometheusagent/service.go
index 4c14ce90..4eb5b9a4 100644
--- a/pkg/monitoring/prometheusagent/service.go
+++ b/pkg/monitoring/prometheusagent/service.go
@@ -27,14 +27,14 @@ type PrometheusAgentService struct {
 	PrometheusVersion string
 }
 
-// ReconcileRemoteWriteConfig ensures that the prometheus remote write config is present in the cluster.
-func (pas *PrometheusAgentService) ReconcileRemoteWriteConfig(
+// ReconcileRemoteWriteConfiguration ensures that the prometheus remote write config is present in the cluster.
+func (pas *PrometheusAgentService) ReconcileRemoteWriteConfiguration(
 	ctx context.Context, cluster *clusterv1.Cluster) error {
 
 	logger := log.FromContext(ctx).WithValues("cluster", cluster.Name)
 	logger.Info("ensuring prometheus agent remote write configmap and secret")
 
-	err := pas.createOrUpdateConfig(ctx, cluster, logger)
+	err := pas.createOrUpdateConfigMap(ctx, cluster, logger)
 	if err != nil {
 		logger.Error(err, "failed to create or update prometheus agent remote write configmap")
 		return errors.WithStack(err)
@@ -51,7 +51,7 @@ func (pas *PrometheusAgentService) ReconcileRemoteWriteConfig(
 	return nil
 }
 
-func (pas PrometheusAgentService) createOrUpdateConfig(ctx context.Context,
+func (pas PrometheusAgentService) createOrUpdateConfigMap(ctx context.Context,
 	cluster *clusterv1.Cluster, logger logr.Logger) error {
 
 	objectKey := client.ObjectKey{
@@ -150,7 +150,9 @@ func (pas PrometheusAgentService) createOrUpdateSecret(ctx context.Context,
 	return nil
 }
 
-func (pas *PrometheusAgentService) DeleteRemoteWriteConfig(ctx context.Context, cluster *clusterv1.Cluster) error {
+func (pas *PrometheusAgentService) DeleteRemoteWriteConfiguration(
+	ctx context.Context, cluster *clusterv1.Cluster) error {
+
 	logger := log.FromContext(ctx).WithValues("cluster", cluster.Name)
 	logger.Info("deleting prometheus agent remote write configmap and secret")