Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: QuentinBisson <[email protected]>
  • Loading branch information
QuentinBisson committed Apr 10, 2024
1 parent be81015 commit 2d49d29
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions internal/controller/cluster_monitoring_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 7 additions & 6 deletions pkg/monitoring/prometheusagent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions pkg/monitoring/prometheusagent/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/monitoring/prometheusagent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 2d49d29

Please sign in to comment.