Skip to content

Commit

Permalink
use prometheus-operator lib instead of prometheus
Browse files Browse the repository at this point in the history
Signed-off-by: clyang82 <chuyang@redhat.com>
  • Loading branch information
clyang82 committed May 14, 2021
1 parent 3225923 commit 96a91fd
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 2,091 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -18,6 +18,7 @@ require (
github.com/prometheus/common v0.20.0
github.com/prometheus/prometheus v1.8.2-0.20210215121130-6f488061dfb4 // v1.8.2 is misleading as Prometheus does not have v2 module. This is pointing to v2.26.0, the same as in prometheus-operator v0.47.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.21.0
k8s.io/apiextensions-apiserver v0.21.0
k8s.io/apimachinery v0.21.0
Expand Down
37 changes: 23 additions & 14 deletions pkg/manifests/config.go
Expand Up @@ -22,11 +22,7 @@ import (

configv1 "github.com/openshift/api/config/v1"
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
promcmconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
promconfig "github.com/prometheus/prometheus/config"
promdiscovery "github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/pkg/relabel"
prommcconfig "github.com/prometheus/common/config"
v1 "k8s.io/api/core/v1"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)
Expand Down Expand Up @@ -102,19 +98,32 @@ type PrometheusK8sConfig struct {

type AdditionalAlertmanagerConfig struct {
// The URL scheme to use when talking to Alertmanagers.
Scheme string `yaml:"scheme,omitempty"`
Scheme string `json:"scheme,omitempty"`
// Path prefix to add in front of the push endpoint path.
PathPrefix string `yaml:"path_prefix,omitempty"`
PathPrefix string `json:"pathPrefix,omitempty"`
// The timeout used when sending alerts.
Timeout model.Duration `yaml:"timeout,omitempty"`
Timeout *string `json:"timeout,omitempty"`
// The api version of Alertmanager.
APIVersion promconfig.AlertmanagerAPIVersion `yaml:"api_version"`
// List of Alertmanager relabel configurations.
RelabelConfigs []*relabel.Config `yaml:"relabel_configs,omitempty"`
// HTTPClientConfig configures an HTTP client
HTTPClientConfig promcmconfig.HTTPClientConfig `yaml:",inline"`
APIVersion string `json:"apiVersion"`
// TLS Config to use for alertmanager connection.
TLSConfig TLSConfig `json:"tlsConfig,omitempty"`
// BearerTokenFile to read from filesystem to use when authenticating to
// Alertmanager.
BearerToken prommcconfig.Secret `json:"bearerToken,omitempty"`
// List of labeled statically configured Alertmanagers.
StaticConfigs promdiscovery.StaticConfig `yaml:"static_configs,omitempty"`
StaticConfigs []*monv1.ProbeTargetStaticConfig `json:"staticConfigs,omitempty"`
}

// TLSConfig configures the options for TLS connections.
type TLSConfig struct {
// Path to the CA cert in the Prometheus container to use for the targets.
CAFile string `json:"caFile,omitempty"`
// Path to the client cert file in the Prometheus container for the targets.
CertFile string `json:"certFile,omitempty"`
// Path to the client key file in the Prometheus container for the targets.
KeyFile string `json:"keyFile,omitempty"`
// Disable target certificate validation.
InsecureSkipVerify bool `json:"insecureSkipVerify"`
}

type AlertmanagerMainConfig struct {
Expand Down
116 changes: 96 additions & 20 deletions pkg/manifests/manifests.go
Expand Up @@ -28,14 +28,12 @@ import (
"strconv"
"strings"

gyaml "github.com/ghodss/yaml"
routev1 "github.com/openshift/api/route/v1"
securityv1 "github.com/openshift/api/security/v1"
"github.com/openshift/cluster-monitoring-operator/pkg/promqlgen"
"github.com/pkg/errors"
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
promconfig "github.com/prometheus/prometheus/config"
promdiscovery "github.com/prometheus/prometheus/discovery"
yaml2 "gopkg.in/yaml.v2"
admissionv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -248,7 +246,7 @@ var (
TrustedCABundleKey = "ca-bundle.crt"

AdditionalAlertmanagerConfigKey = "alertmanager-configs.yaml"
AdditionalAlertmanagerConfigName = "additional-alertmanager-configs"
AdditionalAlertmanagerConfigName = "prometheus-k8s-additional-alertmanager-configs"
)

type Factory struct {
Expand Down Expand Up @@ -1374,24 +1372,66 @@ func (f *Factory) PrometheusK8s(host string, grpcTLS *v1.Secret, trustedCABundle

func (f *Factory) AdditionalAlertManagerConfigs() (*v1.Secret, error) {
alertmanagerConfigs := f.config.ClusterMonitoringConfiguration.PrometheusK8sConfig.AlertmanagerConfigs
if alertmanagerConfigs == nil {
if len(alertmanagerConfigs) == 0 {
return nil, nil
}
amConfigList := []promconfig.AlertmanagerConfig{}

cfgs := []yaml2.MapSlice{}
for _, alertmanagerConfig := range alertmanagerConfigs {
amConfig := promconfig.AlertmanagerConfig{
APIVersion: alertmanagerConfig.APIVersion,
HTTPClientConfig: alertmanagerConfig.HTTPClientConfig,
Scheme: alertmanagerConfig.Scheme,
PathPrefix: alertmanagerConfig.PathPrefix,
Timeout: alertmanagerConfig.Timeout,
RelabelConfigs: alertmanagerConfig.RelabelConfigs,
ServiceDiscoveryConfigs: promdiscovery.Configs{alertmanagerConfig.StaticConfigs},
cfg := yaml2.MapSlice{}
cfg = append(cfg, yaml2.MapSlice{
{Key: "scheme", Value: alertmanagerConfig.Scheme},
{Key: "path_prefix", Value: alertmanagerConfig.PathPrefix},
{Key: "api_version", Value: alertmanagerConfig.APIVersion},
{Key: "bearer_token", Value: alertmanagerConfig.BearerToken},
}...)

if alertmanagerConfig.Timeout != nil {
cfg = append(cfg, yaml2.MapItem{
Key: "timeout", Value: alertmanagerConfig.Timeout,
})
}

cfg = append(cfg, yaml2.MapItem{
Key: "tls_config",
Value: yaml2.MapSlice{
{Key: "ca_file", Value: alertmanagerConfig.TLSConfig.CAFile},
{Key: "cert_file", Value: alertmanagerConfig.TLSConfig.CertFile},
{Key: "key_file", Value: alertmanagerConfig.TLSConfig.KeyFile},
{Key: "insecure_skip_verify", Value: alertmanagerConfig.TLSConfig.InsecureSkipVerify},
},
})

for _, staticConfig := range alertmanagerConfig.StaticConfigs {
sc := yaml2.MapSlice{
{Key: "targets", Value: staticConfig.Targets},
}

sc = append(sc, yaml2.MapSlice{
{Key: "labels", Value: staticConfig.Labels},
}...)

cfg = append(cfg, yaml2.MapItem{
Key: "static_configs",
Value: []yaml2.MapSlice{sc},
})

relabelings := yaml2.MapSlice{}
// Add configured relabelings.
if staticConfig.RelabelConfigs != nil {
for _, r := range staticConfig.RelabelConfigs {
relabelings = append(relabelings, generateRelabelConfig(r)...)
}
}
cfg = append(cfg, yaml2.MapItem{
Key: "relabel_configs",
Value: []yaml2.MapSlice{relabelings},
})
}
amConfigList = append(amConfigList, amConfig)
cfgs = append(cfgs, cfg)
}
amConfigs := &promconfig.AlertmanagerConfigs{}
amConfigYaml, err := gyaml.Marshal(amConfigs)

amConfigYaml, err := yaml2.Marshal(cfgs)
if err != nil {
return nil, err
}
Expand All @@ -1409,6 +1449,40 @@ func (f *Factory) AdditionalAlertManagerConfigs() (*v1.Secret, error) {
return additionalPromToAmConfigSecret, nil
}

func generateRelabelConfig(c *monv1.RelabelConfig) yaml2.MapSlice {
relabeling := yaml2.MapSlice{}

if len(c.SourceLabels) > 0 {
relabeling = append(relabeling, yaml2.MapItem{Key: "source_labels", Value: c.SourceLabels})
}

if c.Separator != "" {
relabeling = append(relabeling, yaml2.MapItem{Key: "separator", Value: c.Separator})
}

if c.TargetLabel != "" {
relabeling = append(relabeling, yaml2.MapItem{Key: "target_label", Value: c.TargetLabel})
}

if c.Regex != "" {
relabeling = append(relabeling, yaml2.MapItem{Key: "regex", Value: c.Regex})
}

if c.Modulus != uint64(0) {
relabeling = append(relabeling, yaml2.MapItem{Key: "modulus", Value: c.Modulus})
}

if c.Replacement != "" {
relabeling = append(relabeling, yaml2.MapItem{Key: "replacement", Value: c.Replacement})
}

if c.Action != "" {
relabeling = append(relabeling, yaml2.MapItem{Key: "action", Value: c.Action})
}

return relabeling
}

func (f *Factory) PrometheusUserWorkload(grpcTLS *v1.Secret) (*monv1.Prometheus, error) {
p, err := f.NewPrometheus(f.assets.MustNewAssetReader(PrometheusUserWorkload))
if err != nil {
Expand Down Expand Up @@ -3639,10 +3713,12 @@ func getConfigMapName(alertmanagerConfigs []AdditionalAlertmanagerConfig) []stri

caConfigmap := []string{}
for _, alertmanagerConfig := range alertmanagerConfigs {
if alertmanagerConfig.HTTPClientConfig.TLSConfig.CAFile != "" {
caFile := alertmanagerConfig.HTTPClientConfig.TLSConfig.CAFile
if alertmanagerConfig.TLSConfig.CAFile != "" {
caFile := alertmanagerConfig.TLSConfig.CAFile
caPathSlice := strings.Split(caFile, "/")
caConfigmap = append(caConfigmap, caPathSlice[len(caPathSlice)-1])
if len(caPathSlice) > 1 {
caConfigmap = append(caConfigmap, caPathSlice[len(caPathSlice)-2])
}
}
}
return caConfigmap
Expand Down
44 changes: 44 additions & 0 deletions pkg/manifests/manifests_test.go
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"gopkg.in/yaml.v2"

v1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
Expand Down Expand Up @@ -1070,6 +1071,49 @@ ingress:
}
}

func TestAdditionalAlertManagerConfigs(t *testing.T) {
c, err := NewConfigFromString(`prometheusK8s:
additionalAlertManagerConfigs:
- apiVersion: v2
bearerToken: xxx
scheme: https
tlsConfig:
caFile: /etc/prometheus/configmaps/router-ca/service-ca.crt
pathPrefix: /
staticConfigs:
- static:
- alertmanager-remote.com
- static:
- alertmanager-remotex.com
- apiVersion: v2
bearerToken: xxx
scheme: https
timeout: 60s
tlsConfig:
caFile: /etc/prometheus/configmaps/router-ca/service-ca.crt
pathPrefix: /
staticConfigs:
- static:
- alertmanager-remote.com
`)
if err != nil {
t.Fatal(err)
}

f := NewFactory("openshift-monitoring", "openshift-user-workload-monitoring", c, defaultInfrastructureReader(), &fakeProxyReader{}, NewAssets(assetsPath))

s, err := f.AdditionalAlertManagerConfigs()
if err != nil {
t.Fatal(err)
}

d := []AdditionalAlertmanagerConfig{}
err = yaml.Unmarshal(s.Data[AdditionalAlertmanagerConfigKey], &d)
if err != nil {
t.Fatal(err)
}
}

func TestK8sPrometheusAdapterConfiguration(t *testing.T) {
c, err := NewConfigFromString(`
k8sPrometheusAdapter:
Expand Down
2 changes: 1 addition & 1 deletion pkg/tasks/prometheus.go
Expand Up @@ -290,7 +290,7 @@ func (t *PrometheusTask) Run() error {
if err != nil {
return errors.Wrap(err, "initializing Prometheus additionalAlertManagerConfigs secret failed")
}
if err != nil && secret != nil {
if secret != nil {
klog.V(4).Info("initializing Prometheus additionalAlertManagerConfigs secret")
err = t.client.CreateOrUpdateSecret(secret)
if err != nil {
Expand Down

0 comments on commit 96a91fd

Please sign in to comment.