Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumEnigmaa committed May 20, 2024
1 parent e3ad381 commit 638a48f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 33 deletions.
11 changes: 7 additions & 4 deletions pkg/monitoring/mimir/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/monitoring/mimir/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/monitoring/mimir/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
25 changes: 5 additions & 20 deletions pkg/monitoring/prometheusagent/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions pkg/monitoring/prometheusagent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/monitoring/prometheusagent/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 638a48f

Please sign in to comment.