Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some monitoring constants and functions to common/monitoring package #65

Merged
merged 8 commits into from
Aug 13, 2024
76 changes: 76 additions & 0 deletions pkg/common/monitoring/monitoring.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,86 @@
package monitoring

import (
"context"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/yaml"
)

const (
// DefaultServicePriority is the default service priority if not set.
defaultServicePriority = "highest"
mimirApiKey = "mimir-basic-auth" // #nosec G101
mimirNamespace = "mimir"
// ServicePriorityLabel is the label used to determine the priority of a service.
servicePriorityLabel = "giantswarm.io/service-priority"

// DefaultShards is the default number of shards to use.
DefaultShards = 1

// Values accepted by the monitoring-agent flag
MonitoringAgentPrometheus = "prometheus-agent"
MonitoringAgentAlloy = "alloy"
// Applications name in the observability-bundle
MonitoringPrometheusAgentAppName = "prometheusAgent"
MonitoringAlloyAppName = "alloyMetrics"

QueueConfigCapacity = 30000
QueueConfigMaxSamplesPerSend = 150000
QueueConfigMaxShards = 10

RemoteWriteName = "mimir"
RemoteWriteEndpointTemplateURL = "https://mimir.%s/api/v1/push"
RemoteWriteTimeout = "60s"
)

func GetServicePriority(cluster *clusterv1.Cluster) string {
if servicePriority, ok := cluster.GetLabels()[servicePriorityLabel]; ok && servicePriority != "" {
return servicePriority
}
return defaultServicePriority
}

func GetMimirIngressPassword(ctx context.Context) (string, error) {
cfg, err := config.GetConfig()
if err != nil {
return "", err
}

c, err := client.New(cfg, client.Options{})
if err != nil {
return "", err
}

secret := &corev1.Secret{}

err = c.Get(ctx, client.ObjectKey{
Name: mimirApiKey,
Namespace: mimirNamespace,
}, secret)
if err != nil {
return "", err
}

mimirPassword, err := readMimirAuthPasswordFromSecret(*secret)

return mimirPassword, err
}

func readMimirAuthPasswordFromSecret(secret corev1.Secret) (string, error) {
if credentials, ok := secret.Data["credentials"]; !ok {
return "", errors.New("credentials key not found in secret")
} else {
var secretData string

err := yaml.Unmarshal(credentials, &secretData)
if err != nil {
return "", errors.WithStack(err)
}
return secretData, nil
}
}
3 changes: 2 additions & 1 deletion pkg/monitoring/mimir/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/giantswarm/observability-operator/pkg/common"
commonmonitoring "github.com/giantswarm/observability-operator/pkg/common/monitoring"
"github.com/giantswarm/observability-operator/pkg/common/password"
"github.com/giantswarm/observability-operator/pkg/common/secret"
"github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (ms *MimirService) CreateIngressAuthenticationSecret(ctx context.Context, l
if apierrors.IsNotFound(err) {
logger.Info("building ingress secret")

password, err := prometheusagent.GetMimirIngressPassword(ctx)
password, err := commonmonitoring.GetMimirIngressPassword(ctx)
if err != nil {
return errors.WithStack(err)
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/monitoring/prometheusagent/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/giantswarm/observability-operator/pkg/common"
commonmonitoring "github.com/giantswarm/observability-operator/pkg/common/monitoring"
"github.com/giantswarm/observability-operator/pkg/metrics"
"github.com/giantswarm/observability-operator/pkg/monitoring/mimir/querier"
"github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent/sharding"
Expand Down Expand Up @@ -42,7 +43,7 @@ func (pas PrometheusAgentService) buildRemoteWriteConfig(ctx context.Context,
"pipeline": pas.ManagementCluster.Pipeline,
"provider": provider,
"region": pas.ManagementCluster.Region,
"service_priority": getServicePriority(cluster),
"service_priority": commonmonitoring.GetServicePriority(cluster),
}

// Compute the number of shards based on the number of series.
Expand Down Expand Up @@ -95,13 +96,6 @@ func getPrometheusAgentRemoteWriteConfigName(cluster *clusterv1.Cluster) string
return fmt.Sprintf("%s-remote-write-config", cluster.Name)
}

func getServicePriority(cluster *clusterv1.Cluster) string {
if servicePriority, ok := cluster.GetLabels()[servicePriorityLabel]; ok && servicePriority != "" {
return servicePriority
}
return defaultServicePriority
}

func getClusterShardingStrategy(cluster metav1.Object) (*sharding.Strategy, error) {
var err error
var scaleUpSeriesCount, scaleDownPercentage float64
Expand Down
61 changes: 8 additions & 53 deletions pkg/monitoring/prometheusagent/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,20 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/yaml"
)

const (
mimirApiKey = "mimir-basic-auth" // #nosec G101
mimirNamespace = "mimir"
commonmonitoring "github.com/giantswarm/observability-operator/pkg/common/monitoring"
)

func GetMimirIngressPassword(ctx context.Context) (string, error) {
cfg, err := config.GetConfig()
if err != nil {
return "", err
}

c, err := client.New(cfg, client.Options{})
if err != nil {
return "", err
}

secret := &corev1.Secret{}

err = c.Get(ctx, client.ObjectKey{
Name: mimirApiKey,
Namespace: mimirNamespace,
}, secret)
if err != nil {
return "", err
}

mimirPassword, err := readMimirAuthPasswordFromSecret(*secret)

return mimirPassword, err
}

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(ctx context.Context,
cluster *clusterv1.Cluster) (*corev1.Secret, error) {
url := fmt.Sprintf(remoteWriteEndpointTemplateURL, pas.ManagementCluster.BaseDomain)
password, err := GetMimirIngressPassword(ctx)
url := fmt.Sprintf(commonmonitoring.RemoteWriteEndpointTemplateURL, pas.ManagementCluster.BaseDomain)
password, err := commonmonitoring.GetMimirIngressPassword(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -64,12 +33,12 @@ func (pas PrometheusAgentService) buildRemoteWriteSecret(ctx context.Context,
{
RemoteWriteSpec: promv1.RemoteWriteSpec{
URL: url,
Name: remoteWriteName,
RemoteTimeout: "60s",
Name: commonmonitoring.RemoteWriteName,
RemoteTimeout: commonmonitoring.RemoteWriteTimeout,
QueueConfig: &promv1.QueueConfig{
Capacity: 30000,
MaxSamplesPerSend: 150000,
MaxShards: 10,
Capacity: commonmonitoring.QueueConfigCapacity,
MaxSamplesPerSend: commonmonitoring.QueueConfigMaxSamplesPerSend,
MaxShards: commonmonitoring.QueueConfigMaxShards,
},
TLSConfig: &promv1.TLSConfig{
SafeTLSConfig: promv1.SafeTLSConfig{
Expand Down Expand Up @@ -100,17 +69,3 @@ func (pas PrometheusAgentService) buildRemoteWriteSecret(ctx context.Context,
Type: "Opaque",
}, nil
}

func readMimirAuthPasswordFromSecret(secret corev1.Secret) (string, error) {
if credentials, ok := secret.Data["credentials"]; !ok {
return "", errors.New("credentials key not found in secret")
} else {
var secretData string

err := yaml.Unmarshal(credentials, &secretData)
if err != nil {
return "", errors.WithStack(err)
}
return secretData, nil
}
}
3 changes: 2 additions & 1 deletion pkg/monitoring/prometheusagent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/giantswarm/observability-operator/pkg/common"
commonmonitoring "github.com/giantswarm/observability-operator/pkg/common/monitoring"
"github.com/giantswarm/observability-operator/pkg/common/organization"
"github.com/giantswarm/observability-operator/pkg/common/password"
"github.com/giantswarm/observability-operator/pkg/monitoring"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (pas PrometheusAgentService) createOrUpdateConfigMap(ctx context.Context,
// Get the current configmap if it exists.
err := pas.Client.Get(ctx, objectKey, current)
if apierrors.IsNotFound(err) {
configMap, err := pas.buildRemoteWriteConfig(ctx, cluster, logger, defaultShards)
configMap, err := pas.buildRemoteWriteConfig(ctx, cluster, logger, commonmonitoring.DefaultShards)
if err != nil {
return errors.WithStack(err)
}
Expand Down
13 changes: 0 additions & 13 deletions pkg/monitoring/prometheusagent/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ import (
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
)

const (
// defaultServicePriority is the default service priority if not set.
defaultServicePriority string = "highest"
// defaultShards is the default number of shards to use.
defaultShards = 1

// servicePriorityLabel is the label used to determine the priority of a service.
servicePriorityLabel string = "giantswarm.io/service-priority"

remoteWriteName = "mimir"
remoteWriteEndpointTemplateURL = "https://mimir.%s/api/v1/push"
)

type RemoteWriteConfig struct {
PrometheusAgentConfig *PrometheusAgentConfig `yaml:"prometheus-agent,omitempty" json:"prometheus-agent,omitempty"`
}
Expand Down