From 0aad251e2700db459c1d766676bb025263ea16d2 Mon Sep 17 00:00:00 2001 From: ffforest Date: Tue, 29 Aug 2023 16:23:10 +0800 Subject: [PATCH] feat: moved operatorMode and monitorType to project.yaml --- .../generator/monitoring_generator.go | 8 ++-- .../generator/monitoring_generator_test.go | 43 +++++++++++-------- .../workload/workload_generator_test.go | 4 ++ .../appconfiguration/monitoring/monitoring.go | 15 ++++--- pkg/projectstack/types.go | 16 ++++++- 5 files changed, 55 insertions(+), 31 deletions(-) diff --git a/pkg/generator/appconfiguration/generator/monitoring_generator.go b/pkg/generator/appconfiguration/generator/monitoring_generator.go index 6cbb81b10..7701f4b31 100644 --- a/pkg/generator/appconfiguration/generator/monitoring_generator.go +++ b/pkg/generator/appconfiguration/generator/monitoring_generator.go @@ -47,8 +47,8 @@ func (g *monitoringGenerator) Generate(spec *models.Spec) error { "kusion_monitoring_appname": g.appName, } - if g.monitor != nil && g.monitor.OperatorMode { - if g.monitor.MonitorType == "service" { + if g.project.ProjectConfiguration.Prometheus != nil && g.project.ProjectConfiguration.Prometheus.OperatorMode { + if g.project.ProjectConfiguration.Prometheus.MonitorType == projectstack.ServiceMonitorType { serviceEndpoint := prometheusV1.Endpoint{ Interval: g.monitor.Interval, ScrapeTimeout: g.monitor.Timeout, @@ -79,7 +79,7 @@ func (g *monitoringGenerator) Generate(spec *models.Spec) error { if err != nil { return err } - } else if g.monitor != nil && g.monitor.MonitorType == "pod" { + } else if g.project.ProjectConfiguration.Prometheus.MonitorType == projectstack.PodMonitorType { podMetricsEndpoint := prometheusV1.PodMetricsEndpoint{ Interval: g.monitor.Interval, ScrapeTimeout: g.monitor.Timeout, @@ -113,7 +113,7 @@ func (g *monitoringGenerator) Generate(spec *models.Spec) error { return err } } else { - return fmt.Errorf("MonitorType should either be service or pod %s", g.monitor.MonitorType) + return fmt.Errorf("MonitorType should either be service or pod %s", g.project.ProjectConfiguration.Prometheus.MonitorType) } } diff --git a/pkg/generator/appconfiguration/generator/monitoring_generator_test.go b/pkg/generator/appconfiguration/generator/monitoring_generator_test.go index cc0918393..f855e94c8 100644 --- a/pkg/generator/appconfiguration/generator/monitoring_generator_test.go +++ b/pkg/generator/appconfiguration/generator/monitoring_generator_test.go @@ -2,6 +2,7 @@ package generator import ( "fmt" + "strings" "testing" Prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" @@ -33,14 +34,16 @@ type TestCase struct { func BuildMonitoringTestCase( projectName, appName string, interval, timeout Prometheusv1.Duration, - path, port, scheme, monitorType string, + path, port, scheme string, + monitorType projectstack.MonitorType, operatorMode bool, ) *TestCase { - var monitorKind, endpointType string - if monitorType == "service" { + var endpointType string + var monitorKind projectstack.MonitorType + if monitorType == "Service" { monitorKind = "ServiceMonitor" endpointType = "endpoints" - } else if monitorType == "pod" { + } else if monitorType == "Pod" { monitorKind = "PodMonitor" endpointType = "podMetricsEndpoints" } @@ -48,14 +51,14 @@ func BuildMonitoringTestCase( if operatorMode { expectedResources = []models.Resource{ { - ID: fmt.Sprintf("monitoring.coreos.com/v1:%s:%s:%s-%s-monitor", monitorKind, projectName, appName, monitorType), + ID: fmt.Sprintf("monitoring.coreos.com/v1:%s:%s:%s-%s-monitor", monitorKind, projectName, appName, strings.ToLower(string(monitorType))), Type: "Kubernetes", Attributes: map[string]interface{}{ "apiVersion": "monitoring.coreos.com/v1", - "kind": monitorKind, + "kind": string(monitorKind), "metadata": map[string]interface{}{ "creationTimestamp": nil, - "name": fmt.Sprintf("%s-%s-monitor", appName, monitorType), + "name": fmt.Sprintf("%s-%s-monitor", appName, strings.ToLower(string(monitorType))), "namespace": projectName, }, "spec": map[string]interface{}{ @@ -90,17 +93,19 @@ func BuildMonitoringTestCase( project: &projectstack.Project{ ProjectConfiguration: projectstack.ProjectConfiguration{ Name: projectName, + Prometheus: &projectstack.PrometheusConfig{ + OperatorMode: operatorMode, + MonitorType: monitorType, + }, }, Path: "/test-project", }, monitor: &monitoring.Monitor{ - Interval: interval, - Timeout: timeout, - Path: path, - Port: port, - Scheme: scheme, - OperatorMode: operatorMode, - MonitorType: monitorType, + Interval: interval, + Timeout: timeout, + Path: path, + Port: port, + Scheme: scheme, }, appName: appName, }, @@ -115,12 +120,12 @@ func BuildMonitoringTestCase( return testCase } -func Test_monitoringGenerator_Generate(t *testing.T) { +func TestMonitoringGenerator_Generate(t *testing.T) { tests := []TestCase{ - *BuildMonitoringTestCase("test-project", "test-app", "15s", "5s", "/metrics", "web", "http", "service", true), - *BuildMonitoringTestCase("test-project", "test-app", "15s", "5s", "/metrics", "web", "http", "pod", true), - *BuildMonitoringTestCase("test-project", "test-app", "30s", "15s", "/metrics", "8080", "http", "service", false), - *BuildMonitoringTestCase("test-project", "test-app", "30s", "15s", "/metrics", "8080", "http", "pod", false), + *BuildMonitoringTestCase("test-project", "test-app", "15s", "5s", "/metrics", "web", "http", "Service", true), + *BuildMonitoringTestCase("test-project", "test-app", "15s", "5s", "/metrics", "web", "http", "Pod", true), + *BuildMonitoringTestCase("test-project", "test-app", "30s", "15s", "/metrics", "8080", "http", "Service", false), + *BuildMonitoringTestCase("test-project", "test-app", "30s", "15s", "/metrics", "8080", "http", "Pod", false), } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/generator/appconfiguration/generator/workload/workload_generator_test.go b/pkg/generator/appconfiguration/generator/workload/workload_generator_test.go index 1dccbcefe..dfb5b8d72 100644 --- a/pkg/generator/appconfiguration/generator/workload/workload_generator_test.go +++ b/pkg/generator/appconfiguration/generator/workload/workload_generator_test.go @@ -96,6 +96,10 @@ func TestWorkloadGenerator_Generate(t *testing.T) { expectedProject := &projectstack.Project{ ProjectConfiguration: projectstack.ProjectConfiguration{ Name: "test", + Prometheus: &projectstack.PrometheusConfig{ + OperatorMode: false, + MonitorType: "Pod", + }, }, } expectedStack := &projectstack.Stack{} diff --git a/pkg/models/appconfiguration/monitoring/monitoring.go b/pkg/models/appconfiguration/monitoring/monitoring.go index 16b5158ce..95fdbf02b 100644 --- a/pkg/models/appconfiguration/monitoring/monitoring.go +++ b/pkg/models/appconfiguration/monitoring/monitoring.go @@ -5,11 +5,12 @@ import ( ) type Monitor struct { - Interval prometheusV1.Duration `yaml:"interval,omitempty" json:"interval,omitempty"` - Timeout prometheusV1.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"` - Path string `yaml:"path,omitempty" json:"path,omitempty"` - Port string `yaml:"port,omitempty" json:"port,omitempty"` - Scheme string `yaml:"scheme,omitempty" json:"scheme,omitempty"` - OperatorMode bool `yaml:"operatorMode,omitempty" json:"operatorMode,omitempty"` - MonitorType string `yaml:"monitorType,omitempty" json:"monitorType,omitempty"` + Interval prometheusV1.Duration `yaml:"interval,omitempty" json:"interval,omitempty"` + Timeout prometheusV1.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"` + Path string `yaml:"path,omitempty" json:"path,omitempty"` + // Despite what the name suggests, PodMonitor and ServiceMonitor actually + // only accept port names as the input. So in operator mode, this port field + // need to be the user-provided port name. + Port string `yaml:"port,omitempty" json:"port,omitempty"` + Scheme string `yaml:"scheme,omitempty" json:"scheme,omitempty"` } diff --git a/pkg/projectstack/types.go b/pkg/projectstack/types.go index 0972e018a..eb07adc99 100644 --- a/pkg/projectstack/types.go +++ b/pkg/projectstack/types.go @@ -27,9 +27,14 @@ const ( KclFile = "kcl.yaml" KCLGenerator GeneratorType = "KCL" AppConfigurationGenerator GeneratorType = "AppConfiguration" + PodMonitorType MonitorType = "Pod" + ServiceMonitorType MonitorType = "Service" ) -type GeneratorType string +type ( + GeneratorType string + MonitorType string +) // GeneratorConfig represent Generator configs saved in project.yaml type GeneratorConfig struct { @@ -37,6 +42,12 @@ type GeneratorConfig struct { Configs map[string]interface{} `json:"configs,omitempty"` } +// PrometheusConfig represent Prometheus configs saved in project.yaml +type PrometheusConfig struct { + OperatorMode bool `yaml:"operatorMode,omitempty" json:"operatorMode,omitempty"` + MonitorType MonitorType `yaml:"monitorType,omitempty" json:"monitorType,omitempty"` +} + // ProjectConfiguration is the project configuration type ProjectConfiguration struct { // Project name @@ -51,6 +62,9 @@ type ProjectConfiguration struct { // SpecGenerator configs Generator *GeneratorConfig `json:"generator,omitempty" yaml:"generator,omitempty"` + // Prometheus configs + Prometheus *PrometheusConfig `json:"prometheus,omitempty" yaml:"prometheus,omitempty"` + // Secret stores SecretStores *vals.SecretStores `json:"secret_stores,omitempty" yaml:"secret_stores,omitempty"` }