From 638a48fbb6357e04f76b15404ca92675bcc3e20b Mon Sep 17 00:00:00 2001 From: QuantumEnigmaa Date: Mon, 20 May 2024 15:47:41 +0200 Subject: [PATCH] apply suggestions --- pkg/monitoring/mimir/ingress/ingress.go | 11 ++++++---- pkg/monitoring/mimir/service.go | 2 +- pkg/monitoring/mimir/utils.go | 5 ++--- pkg/monitoring/prometheusagent/secret.go | 25 +++++------------------ pkg/monitoring/prometheusagent/service.go | 7 +++---- pkg/monitoring/prometheusagent/types.go | 2 +- 6 files changed, 19 insertions(+), 33 deletions(-) diff --git a/pkg/monitoring/mimir/ingress/ingress.go b/pkg/monitoring/mimir/ingress/ingress.go index d19129e3..6f6b31c5 100644 --- a/pkg/monitoring/mimir/ingress/ingress.go +++ b/pkg/monitoring/mimir/ingress/ingress.go @@ -6,11 +6,14 @@ import ( "github.com/giantswarm/observability-operator/pkg/monitoring" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) - "github.com/giantswarm/observability-operator/pkg/monitoring/mimir" +const ( + secretName = "mimir-gateway-ingress" + secretNamespace = "mimir" ) -func (ms *MimirService) BuildIngressSecret(username string, password string) (*corev1.Secret, error) { +func BuildIngressSecret(username string, password string) (*corev1.Secret, error) { // Uses htpasswd to generate the password hash. secretData, err := exec.Command("htpasswd", "-bn", username, password).Output() if err != nil { @@ -20,8 +23,8 @@ func (ms *MimirService) BuildIngressSecret(username string, password string) (*c secret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ // The secret name is hard coded so that it's easier to use on other places. - Name: "mimir-gateway-ingress", - Namespace: "mimir", + Name: secretName, + Namespace: secretNamespace, Finalizers: []string{ monitoring.MonitoringFinalizer, }, diff --git a/pkg/monitoring/mimir/service.go b/pkg/monitoring/mimir/service.go index 894ed98e..7b7bd2bf 100644 --- a/pkg/monitoring/mimir/service.go +++ b/pkg/monitoring/mimir/service.go @@ -49,7 +49,7 @@ func (ms *MimirService) CreateOrUpdateIngressSecret(ctx context.Context, mc stri return errors.WithStack(err) } - secret, err := ms.BuildIngressSecret(mc, password) + secret, err := BuildIngressSecret(mc, password) if err != nil { return errors.WithStack(err) } diff --git a/pkg/monitoring/mimir/utils.go b/pkg/monitoring/mimir/utils.go index 72dc9429..0fef2c57 100644 --- a/pkg/monitoring/mimir/utils.go +++ b/pkg/monitoring/mimir/utils.go @@ -5,11 +5,10 @@ import ( "fmt" "log" + "github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent" corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" - - "github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent" ) // Checks whether Mimir is enabled in the cluster by listing the pods in the Mimir namespace. @@ -56,7 +55,7 @@ func GetMimirIngressPassword(ctx context.Context, mc string) (string, error) { return "", err } - mimirPassword, err := readRemoteWritePasswordFromSecret(*secret, true) + mimirPassword, err := prometheusagent.ReadRemoteWritePasswordFromSecret(*secret) return mimirPassword, err } diff --git a/pkg/monitoring/prometheusagent/secret.go b/pkg/monitoring/prometheusagent/secret.go index 6633a631..063b312d 100644 --- a/pkg/monitoring/prometheusagent/secret.go +++ b/pkg/monitoring/prometheusagent/secret.go @@ -13,23 +13,16 @@ import ( "github.com/giantswarm/observability-operator/pkg/monitoring" ) +const remoteWriteName = "mimir" + func getPrometheusAgentRemoteWriteSecretName(cluster *clusterv1.Cluster) string { return fmt.Sprintf("%s-remote-write-secret", cluster.Name) } // buildRemoteWriteSecret builds the secret that contains the remote write configuration for the Prometheus agent. func (pas PrometheusAgentService) buildRemoteWriteSecret( - cluster *clusterv1.Cluster, password string, mimirEnabled bool) (*corev1.Secret, error) { - var url string - var remoteWriteName string - - if mimirEnabled { - url = fmt.Sprintf(remoteWriteEndpointTemplateURL, "mimir", pas.ManagementCluster.BaseDomain, cluster.Name, "push") - remoteWriteName = "mimir" - } else { - url = fmt.Sprintf(remoteWriteEndpointTemplateURL, "prometheus", pas.ManagementCluster.BaseDomain, cluster.Name, "write") - remoteWriteName = "prometheus-meta-operator" - } + cluster *clusterv1.Cluster, password string) (*corev1.Secret, error) { + url := fmt.Sprintf(remoteWriteEndpointTemplateURL, pas.ManagementCluster.BaseDomain, cluster.Name) config := RemoteWriteConfig{ PrometheusAgentConfig: &PrometheusAgentConfig{ @@ -77,21 +70,13 @@ func (pas PrometheusAgentService) buildRemoteWriteSecret( }, nil } -func readRemoteWritePasswordFromSecret(secret corev1.Secret, mimirEnabled bool) (string, error) { +func ReadRemoteWritePasswordFromSecret(secret corev1.Secret) (string, error) { remoteWriteConfig := RemoteWriteConfig{} err := yaml.Unmarshal(secret.Data["values"], &remoteWriteConfig) if err != nil { return "", errors.WithStack(err) } - var remoteWriteName string - - if mimirEnabled { - remoteWriteName = "mimir" - } else { - remoteWriteName = "prometheus-meta-operator" - } - 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. diff --git a/pkg/monitoring/prometheusagent/service.go b/pkg/monitoring/prometheusagent/service.go index 2c30ff58..443a3ff1 100644 --- a/pkg/monitoring/prometheusagent/service.go +++ b/pkg/monitoring/prometheusagent/service.go @@ -103,7 +103,6 @@ func (pas PrometheusAgentService) createOrUpdateSecret(ctx context.Context, Name: getPrometheusAgentRemoteWriteSecretName(cluster), Namespace: cluster.GetNamespace(), } - mimirEnabled := IsMimirEnabled(ctx) current := &corev1.Secret{} // Get the current secret if it exists. @@ -117,7 +116,7 @@ func (pas PrometheusAgentService) createOrUpdateSecret(ctx context.Context, } logger.Info("generated password for the prometheus agent") - secret, err := pas.buildRemoteWriteSecret(cluster, password, mimirEnabled) + secret, err := pas.buildRemoteWriteSecret(cluster, password) if err != nil { return errors.WithStack(err) } @@ -131,12 +130,12 @@ func (pas PrometheusAgentService) createOrUpdateSecret(ctx context.Context, } // As it takes a long time to apply the new password to the agent due to a built-in delay in the app-platform, // we keep the already generated remote write password. - password, err := readRemoteWritePasswordFromSecret(*current, mimirEnabled) + password, err := ReadRemoteWritePasswordFromSecret(*current) if err != nil { return errors.WithStack(err) } - desired, err := pas.buildRemoteWriteSecret(cluster, password, mimirEnabled) + desired, err := pas.buildRemoteWriteSecret(cluster, password) if err != nil { return errors.WithStack(err) } diff --git a/pkg/monitoring/prometheusagent/types.go b/pkg/monitoring/prometheusagent/types.go index 69ec9b35..e388442c 100644 --- a/pkg/monitoring/prometheusagent/types.go +++ b/pkg/monitoring/prometheusagent/types.go @@ -13,7 +13,7 @@ const ( // servicePriorityLabel is the label used to determine the priority of a service. servicePriorityLabel string = "giantswarm.io/service-priority" - remoteWriteEndpointTemplateURL = "https://%s.%s/%s/api/v1/%s" + remoteWriteEndpointTemplateURL = "https://mimir.%s/%s/api/v1/push" ) type RemoteWriteConfig struct {