generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Take over prometheus agent remote write config from PMO.
- Loading branch information
1 parent
2f6d694
commit 7031bbe
Showing
13 changed files
with
407 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package password | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/hex" | ||
) | ||
|
||
type Manager interface { | ||
GeneratePassword(length int) (string, error) | ||
} | ||
|
||
type SimpleManager struct { | ||
} | ||
|
||
func (m SimpleManager) GeneratePassword(length int) (string, error) { | ||
bytes := make([]byte, length) | ||
if _, err := rand.Read(bytes); err != nil { | ||
return "", err | ||
} | ||
return hex.EncodeToString(bytes), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package prometheusagent | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"gopkg.in/yaml.v2" | ||
"github.com/go-logr/logr" | ||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/giantswarm/observability-operator/pkg/common" | ||
"github.com/giantswarm/observability-operator/pkg/monitoring" | ||
"github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent/remotewrite" | ||
"github.com/giantswarm/observability-operator/pkg/monitoring/mimir/querier" | ||
"github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent/shards" | ||
) | ||
|
||
|
||
|
||
func (pas PrometheusAgentService) buildRemoteWriteConfig(ctx context.Context, | ||
cluster *clusterv1.Cluster, logger logr.Logger, currentShards int) (*corev1.ConfigMap, error) { | ||
|
||
organization, err := pas.OrganizationRepository.Read(ctx, cluster) | ||
if err != nil { | ||
logger.Error(err, "failed to get cluster organization") | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
provider, err := common.GetClusterProvider(cluster) | ||
if err != nil { | ||
logger.Error(err, "failed to get cluster provider") | ||
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, | ||
"customer": pas.ManagementCluster.Customer, | ||
"installation": pas.ManagementCluster.Name, | ||
"organization": organization, | ||
"pipeline": pas.ManagementCluster.Pipeline, | ||
"provider": provider, | ||
"region": pas.ManagementCluster.Region, | ||
"service_priority": getServicePriority(cluster), | ||
} | ||
|
||
shards, err := getShardsCountForCluster(ctx, cluster, currentShards) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
config, err := yaml.Marshal(remotewrite.RemoteWriteConfig{ | ||
PrometheusAgentConfig: remotewrite.PrometheusAgentConfig{ | ||
ExternalLabels: externalLabels, | ||
Image: remotewrite.PrometheusAgentImage{ | ||
Tag: pas.PrometheusVersion, | ||
}, | ||
Shards: shards, | ||
Version: pas.PrometheusVersion, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
return &corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: getPrometheusAgentRemoteWriteConfigName(cluster), | ||
Namespace: cluster.Namespace, | ||
Finalizers: []string{ | ||
monitoring.MonitoringFinalizer, | ||
}, | ||
}, | ||
Data: map[string]string{ | ||
"values": string(config), | ||
}, | ||
}, nil | ||
} | ||
|
||
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 | ||
} | ||
|
||
// We want to compute the number of shards based on the number of nodes. | ||
func getShardsCountForCluster(ctx context.Context, cluster *clusterv1.Cluster, currentShardCount int) (int, error) { | ||
headSeries, err := querier.QueryTSDBHeadSeries(ctx, cluster.Name) | ||
if err != nil { | ||
// Verify that Prometheus is accessible. If not, return the default number of shards. | ||
var dnsError *net.DNSError | ||
if errors.As(err, &dnsError) { | ||
return shards.ComputeShards(currentShardCount, defaultShards), nil | ||
} | ||
return 0, errors.WithStack(err) | ||
} | ||
return shards.ComputeShards(currentShardCount, headSeries), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package prometheusagent | ||
|
||
import ( | ||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/giantswarm/observability-operator/pkg/monitoring/prometheusagent/remotewrite" | ||
) | ||
|
||
func readCurrentShardsFromConfig(configMap corev1.ConfigMap) (int, error) { | ||
remoteWriteConfig := remotewrite.RemoteWriteConfig{} | ||
err := yaml.Unmarshal([]byte(configMap.Data["values"]), &remoteWriteConfig) | ||
if err != nil { | ||
return 0, errors.WithStack(err) | ||
} | ||
|
||
return remoteWriteConfig.PrometheusAgentConfig.Shards, nil | ||
} | ||
|
||
func readRemoteWritePasswordFromSecret(secret corev1.Secret) (string, error) { | ||
remoteWriteConfig := remotewrite.RemoteWriteConfig{} | ||
err := yaml.Unmarshal(secret.Data["values"], &remoteWriteConfig) | ||
if err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
|
||
for _, rw := range remoteWriteConfig.PrometheusAgentConfig.RemoteWrite { | ||
if rw.Name == "prometheus-meta-operator" { | ||
return rw.Password, nil | ||
} | ||
} | ||
|
||
return "", errors.New("remote write password not found in secret") | ||
} |
Oops, something went wrong.