Skip to content

Commit

Permalink
feat: moved operatorMode and monitorType to project.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ffforest committed Aug 29, 2023
1 parent 8c26fc5 commit 0aad251
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"fmt"
"strings"
"testing"

Prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
Expand Down Expand Up @@ -33,29 +34,31 @@ 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"
}
expectedResources := make([]models.Resource, 0)
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{}{
Expand Down Expand Up @@ -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,
},
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
15 changes: 8 additions & 7 deletions pkg/models/appconfiguration/monitoring/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
16 changes: 15 additions & 1 deletion pkg/projectstack/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,27 @@ 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 {
Type GeneratorType `json:"type"`
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
Expand All @@ -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"`
}
Expand Down

0 comments on commit 0aad251

Please sign in to comment.