Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lilic committed Jun 10, 2020
1 parent 503a852 commit dbef134
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
5 changes: 5 additions & 0 deletions examples/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ prometheusK8s:
cpu: 200m
memory: 2Gi
alertmanagerMain:
volumeClaimTemplate:
spec:
resources:
requests:
storage: 15Gi
baseImage: quay.io/prometheus/alertmanager
externalUrl: https://monitoring-demo.staging.core-os.net/alertmanager
resources:
Expand Down
7 changes: 5 additions & 2 deletions pkg/manifests/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The Cluster Monitoring Operator Authors
/// Copyright 2018 The Cluster Monitoring Operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -184,13 +184,14 @@ func (cfg *TelemeterClientConfig) IsEnabled() bool {
func NewConfig(content io.Reader) (*Config, error) {
c := Config{}
cmc := ClusterMonitoringConfiguration{}
err := k8syaml.NewYAMLOrJSONDecoder(content, 100).Decode(&cmc)
err := k8syaml.NewYAMLOrJSONDecoder(content, 4096).Decode(&cmc)
if err != nil {
return nil, err
}
c.ClusterMonitoringConfiguration = &cmc
res := &c
res.applyDefaults()
c.UserWorkloadConfiguration = NewDefaultUserWorkloadMonitoringConfig()

return res, nil
}
Expand Down Expand Up @@ -384,6 +385,8 @@ type UserWorkloadConfiguration struct {
}

func (u *UserWorkloadConfiguration) applyDefaults() {
fmt.Println("apply some defaults")
fmt.Println(u.PrometheusOperator)
if u.PrometheusOperator == nil {
u.PrometheusOperator = &PrometheusOperatorConfig{}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/manifests/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ func TestConfigParsing(t *testing.T) {
if err != nil {
t.Fatal(err)
}

_, err = NewConfig(f)
c, err := NewConfig(f)
if err != nil {
t.Fatal(err)
}

if c.ClusterMonitoringConfiguration.AlertmanagerMainConfig.VolumeClaimTemplate == nil {
t.Fatal("config parsing failed: AlertmanagerMainConfig VolumeClaimTemplate was not parsed correctly")
}
}

func TestEmptyConfigIsValid(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,8 +1455,10 @@ func (f *Factory) PrometheusUserWorkload(grpcTLS *v1.Secret) (*monv1.Prometheus,
if f.config.ClusterMonitoringConfiguration.PrometheusUserWorkloadConfig.ExternalLabels != nil {
p.Spec.ExternalLabels = f.config.ClusterMonitoringConfiguration.PrometheusUserWorkloadConfig.ExternalLabels
}
fmt.Println(f.config.UserWorkloadConfiguration.Prometheus.VolumeClaimTemplate)

if f.config.UserWorkloadConfiguration.Prometheus.VolumeClaimTemplate != nil {
fmt.Println("yes===")
p.Spec.Storage = &monv1.StorageSpec{
VolumeClaimTemplate: *f.config.UserWorkloadConfiguration.Prometheus.VolumeClaimTemplate,
}
Expand All @@ -1467,6 +1469,8 @@ func (f *Factory) PrometheusUserWorkload(grpcTLS *v1.Secret) (*monv1.Prometheus,
VolumeClaimTemplate: *f.config.ClusterMonitoringConfiguration.PrometheusUserWorkloadConfig.VolumeClaimTemplate,
}
}
fmt.Println("====")
fmt.Println(p.Spec.Storage)

if len(f.config.UserWorkloadConfiguration.Prometheus.RemoteWrite) > 0 {
p.Spec.RemoteWrite = f.config.UserWorkloadConfiguration.Prometheus.RemoteWrite
Expand Down Expand Up @@ -1829,7 +1833,6 @@ func (f *Factory) PrometheusOperatorDeployment(namespaces []string) (*appsv1.Dep
if err != nil {
return nil, err
}
fmt.Printf("%#+v\n", f.config.ClusterMonitoringConfiguration.PrometheusOperatorConfig)
if len(f.config.ClusterMonitoringConfiguration.PrometheusOperatorConfig.NodeSelector) > 0 {

d.Spec.Template.Spec.NodeSelector = f.config.ClusterMonitoringConfiguration.PrometheusOperatorConfig.NodeSelector
Expand Down Expand Up @@ -1875,8 +1878,7 @@ func (f *Factory) PrometheusOperatorUserWorkloadDeployment(denyNamespaces []stri
if err != nil {
return nil, err
}
fmt.Println("====")
fmt.Printf("%#+v\n", f.config.UserWorkloadConfiguration)
fmt.Println(f.config.UserWorkloadConfiguration)
if len(f.config.UserWorkloadConfiguration.PrometheusOperator.NodeSelector) > 0 {
d.Spec.Template.Spec.NodeSelector = f.config.UserWorkloadConfiguration.PrometheusOperator.NodeSelector
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package operator

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -361,21 +362,24 @@ func (o *Operator) sync(key string) error {
}

func (o *Operator) loadUserWorkloadConfig() (*manifests.UserWorkloadConfiguration, error) {
userCM, err := o.client.GetConfigmap(o.userWorkloadConfigMapName, o.namespaceUserWorkload)
userCM, err := o.client.GetConfigmap(o.namespaceUserWorkload, o.userWorkloadConfigMapName)
if err != nil {
if apierrors.IsNotFound(err) {
fmt.Println("not found?")
return manifests.NewDefaultUserWorkloadMonitoringConfig(), nil
} else {

klog.Warningf("Error loading user workload monitoring ConfigMap. Error: %v", err)
return nil, err
}
}

configContent, found := userCM.Data["config.yaml"]
if found {
fmt.Println("---load user workload config---")
uwc, err := manifests.NewUserConfigFromString(configContent)
if err != nil {
klog.Warningf("Error filling user workload confiugration from CofigMap. Error: %v", err)
klog.Warningf("Error filling user workload configuration from ConfigMap. Error: %v", err)
return nil, err
}
return uwc, nil
Expand Down
85 changes: 84 additions & 1 deletion test/e2e/user_workload_monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
)

// The namespace where to deploy the test application.
const userWorkloadTestNs = "user-workload-test"
const (
userWorkloadTestNs = "user-workload-test"
userWorkloadNs = "openshift-user-workload-monitoring"
)

func TestUserWorkloadMonitoring(t *testing.T) {
cm := &v1.ConfigMap{
Expand Down Expand Up @@ -67,6 +71,85 @@ func TestUserWorkloadMonitoring(t *testing.T) {
}
}

func TestUserWorkloadMonitoringNewConfig(t *testing.T) {
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-monitoring-config",
Namespace: f.Ns,
},
Data: map[string]string{
"config.yaml": `enableUserWorkload: true
`,
},
}

uwmCM := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "user-workload-monitoring-config",
Namespace: userWorkloadNs,
},
Data: map[string]string{
"config.yaml": `prometheus:
volumeClaimTemplate:
spec:
storageClassName: gp2
resources:
requests:
storage: 2Gi
`,
},
}

for _, scenario := range []struct {
name string
f func(*testing.T)
}{
{"enable user workload monitoring, assert prometheus rollout", createUserWorkloadAssets(cm)},
{"set VolumeClaimTemplate for prometheus CR, assert that it is created", assertPrometheusVCConfig(uwmCM)},
{"assert assets are deleted when user workload monitoring is disabled", assertDeletedUserWorkloadAssets(cm)},
} {
if ok := t.Run(scenario.name, scenario.f); !ok {
t.Fatalf("scenario %q failed", scenario.name)
}
}
}

func assertPrometheusVCConfig(cm *v1.ConfigMap) func(*testing.T) {
return func(t *testing.T) {
if err := f.OperatorClient.CreateOrUpdateConfigMap(cm); err != nil {
t.Fatal(err)
}

var lastErr error
// Wait for persistent volume claim
err := wait.Poll(time.Second, 5*time.Minute, func() (bool, error) {
_, err := f.KubeClient.CoreV1().PersistentVolumeClaims(userWorkloadNs).Get("prometheus-user-workload-db-prometheus-user-workload-0", metav1.GetOptions{})
lastErr = errors.Wrap(err, "getting prometheus persistent volume claim failed")
if err != nil {
return false, nil
}
return true, nil
})
if err != nil {
if err == wait.ErrWaitTimeout && lastErr != nil {
err = lastErr
}
t.Fatal(err)
}

err = f.OperatorClient.WaitForStatefulsetRollout(&appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "prometheus-user-workload",
Namespace: userWorkloadNs,
},
})
if err != nil {
t.Fatal(err)
}
}

}

func createUserWorkloadAssets(cm *v1.ConfigMap) func(*testing.T) {
return func(t *testing.T) {
if err := f.OperatorClient.CreateOrUpdateConfigMap(cm); err != nil {
Expand Down

0 comments on commit dbef134

Please sign in to comment.