Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Prometheus support #490

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ require (
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/powerman/rpc-codec v1.2.2 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.67.1 // indirect
github.com/qri-io/jsonpointer v0.1.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/powerman/rpc-codec v1.2.2 h1:BK0JScZivljhwW/vLLhZLtUgqSxc/CD3sHEs8LiwwKw=
github.com/powerman/rpc-codec v1.2.2/go.mod h1:3Qr/y/+u3CwcSww9tfJMRn/95lB2qUdUeIQe7BYlLDo=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.67.1 h1:u1Mw9irznvsBPxQxjUmCel1ufP3UgzA1CILj7/2tpNw=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.67.1/go.mod h1:KZHvrby65G+rA4V/vMTUXDV22TI+GgLIrCigYClpjzk=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (g *appConfigurationGenerator) Generate(spec *models.Spec) error {

gfs := []appconfiguration.NewGeneratorFunc{
NewNamespaceGeneratorFunc(g.project.Name),
workload.NewWorkloadGeneratorFunc(g.project, g.stack, g.appName, g.app.Workload),
workload.NewWorkloadGeneratorFunc(g.project, g.stack, g.appName, g.app.Workload, g.app.Monitoring),
NewMonitoringGeneratorFunc(g.project, g.app.Monitoring, g.appName),
// The OrderedResourcesGenerator should be executed after all resources are generated.
NewOrderedResourcesGeneratorFunc(),
}
Expand Down
132 changes: 132 additions & 0 deletions pkg/generator/appconfiguration/generator/monitoring_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package generator

import (
"fmt"

prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kusionstack.io/kusion/pkg/generator/appconfiguration"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration/monitoring"
"kusionstack.io/kusion/pkg/projectstack"
)

type monitoringGenerator struct {
project *projectstack.Project
monitor *monitoring.Monitor
appName string
}

func NewMonitoringGenerator(project *projectstack.Project, monitor *monitoring.Monitor, appName string) (appconfiguration.Generator, error) {
if len(project.Name) == 0 {
return nil, fmt.Errorf("project name must not be empty")
}

if len(appName) == 0 {
return nil, fmt.Errorf("app name must not be empty")
}
return &monitoringGenerator{
project: project,
monitor: monitor,
appName: appName,
}, nil
}

func NewMonitoringGeneratorFunc(project *projectstack.Project, monitor *monitoring.Monitor, appName string) appconfiguration.NewGeneratorFunc {
return func() (appconfiguration.Generator, error) {
return NewMonitoringGenerator(project, monitor, appName)
}
}

func (g *monitoringGenerator) Generate(spec *models.Spec) error {
if spec.Resources == nil {
spec.Resources = make(models.Resources, 0)
}

// If Prometheus runs as an operator, it relies on Custom Resources to
// manage the scrape configs. CRs (ServiceMonitors and PodMonitors) rely on
// corresponding resources (Services and Pods) to have labels that can be
// used as part of the label selector for the CR to determine which
// service/pods to scrape from.
// Here we choose the label name kusion_monitoring_appname for two reasons:
// 1. Unlike the label validation in Kubernetes, the label name accepted by
// Prometheus cannot contain non-alphanumeric characters except underscore:
// https://github.com/prometheus/common/blob/main/model/labels.go#L94
// 2. The name should be unique enough that is only created by Kusion and
// used to identify a certain application
monitoringLabels := map[string]string{
"kusion_monitoring_appname": g.appName,
ffforest marked this conversation as resolved.
Show resolved Hide resolved
}

if g.project.ProjectConfiguration.Prometheus != nil && g.project.ProjectConfiguration.Prometheus.OperatorMode && g.monitor != nil {
if g.project.ProjectConfiguration.Prometheus.MonitorType == projectstack.ServiceMonitorType {
serviceEndpoint := prometheusv1.Endpoint{
Interval: g.monitor.Interval,
ScrapeTimeout: g.monitor.Timeout,
Port: g.monitor.Port,
Path: g.monitor.Path,
Scheme: g.monitor.Scheme,
}
serviceEndpointList := []prometheusv1.Endpoint{serviceEndpoint}
serviceMonitor := &prometheusv1.ServiceMonitor{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceMonitor",
APIVersion: prometheusv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("%s-service-monitor", g.appName), Namespace: g.project.Name},
Spec: prometheusv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: monitoringLabels,
},
Endpoints: serviceEndpointList,
},
}
err := appconfiguration.AppendToSpec(
models.Kubernetes,
appconfiguration.KubernetesResourceID(serviceMonitor.TypeMeta, serviceMonitor.ObjectMeta),
spec,
serviceMonitor,
)
if err != nil {
return err
}
} else if g.project.ProjectConfiguration.Prometheus.MonitorType == projectstack.PodMonitorType {
podMetricsEndpoint := prometheusv1.PodMetricsEndpoint{
Interval: g.monitor.Interval,
ScrapeTimeout: g.monitor.Timeout,
Port: g.monitor.Port,
Path: g.monitor.Path,
Scheme: g.monitor.Scheme,
}
podMetricsEndpointList := []prometheusv1.PodMetricsEndpoint{podMetricsEndpoint}

podMonitor := &prometheusv1.PodMonitor{
TypeMeta: metav1.TypeMeta{
Kind: "PodMonitor",
APIVersion: prometheusv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("%s-pod-monitor", g.appName), Namespace: g.project.Name},
Spec: prometheusv1.PodMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: monitoringLabels,
},
PodMetricsEndpoints: podMetricsEndpointList,
},
}

err := appconfiguration.AppendToSpec(
models.Kubernetes,
appconfiguration.KubernetesResourceID(podMonitor.TypeMeta, podMonitor.ObjectMeta),
spec,
podMonitor,
)
if err != nil {
return err
}
} else {
return fmt.Errorf("MonitorType should either be service or pod %s", g.project.ProjectConfiguration.Prometheus.MonitorType)
}
}

return nil
}
143 changes: 143 additions & 0 deletions pkg/generator/appconfiguration/generator/monitoring_generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package generator

import (
"fmt"
"strings"
"testing"

prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/require"

"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration/monitoring"
"kusionstack.io/kusion/pkg/projectstack"
)

type Fields struct {
project *projectstack.Project
monitor *monitoring.Monitor
appName string
}

type Args struct {
spec *models.Spec
}

type TestCase struct {
name string
fields Fields
args Args
want *models.Spec
wantErr bool
}

func BuildMonitoringTestCase(
projectName, appName string,
interval, timeout prometheusv1.Duration,
path, port, scheme string,
monitorType projectstack.MonitorType,
operatorMode bool,
) *TestCase {
var endpointType string
var monitorKind projectstack.MonitorType
if monitorType == "Service" {
monitorKind = "ServiceMonitor"
endpointType = "endpoints"
} 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, strings.ToLower(string(monitorType))),
Type: "Kubernetes",
Attributes: map[string]interface{}{
"apiVersion": "monitoring.coreos.com/v1",
"kind": string(monitorKind),
"metadata": map[string]interface{}{
"creationTimestamp": nil,
"name": fmt.Sprintf("%s-%s-monitor", appName, strings.ToLower(string(monitorType))),
"namespace": projectName,
},
"spec": map[string]interface{}{
endpointType: []interface{}{
map[string]interface{}{
"bearerTokenSecret": map[string]interface{}{
"key": "",
},
"interval": string(interval),
"scrapeTimeout": string(timeout),
"path": path,
"port": port,
"scheme": scheme,
},
},
"namespaceSelector": make(map[string]interface{}),
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"kusion_monitoring_appname": appName,
},
},
},
},
DependsOn: nil,
Extensions: nil,
},
}
}
testCase := &TestCase{
name: fmt.Sprintf("%s-%s", projectName, appName),
fields: Fields{
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,
},
appName: appName,
},
args: Args{
spec: &models.Spec{},
},
want: &models.Spec{
Resources: expectedResources,
},
wantErr: false,
}
return testCase
}

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),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := &monitoringGenerator{
project: tt.fields.project,
monitor: tt.fields.monitor,
appName: tt.fields.appName,
}
if err := g.Generate(tt.args.spec); (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
}
require.Equal(t, tt.want, tt.args.spec)
})
}
}
Loading
Loading