Skip to content

Commit

Permalink
refactor: update monitoring module generator to properly generate pat…
Browse files Browse the repository at this point in the history
…cher resources when operator mode is false
  • Loading branch information
ffforest committed Mar 20, 2024
1 parent 3db7d1b commit 4c445c8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 74 deletions.
4 changes: 2 additions & 2 deletions models/generators/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TEST?=$$(go list ./... | grep -v 'vendor')
###### chang variables below according to your own modules ###
NAMESPACE=kusionstack
NAME=opsrule
NAME=monitoring
VERSION=v0.1.0
BINARY=../bin/kusion-module-${NAME}_${VERSION}

Expand All @@ -17,7 +17,7 @@ export OS_ARCH ?= $(GOARCH_LOCAL)
default: install

build-darwin:
GOOS=darwin GOARCH=arm64 go build -o ${BINARY} ./${NAME}
GOOS=darwin GOARCH=arm64 go build -v -o ${BINARY} ./${NAME}

install: build-darwin
# copy module binary to $KUSION_HOME. e.g. ~/.kusion/modules/kusionstack/opsrule/v0.1.0/darwin/arm64/kusion-module-opsrule_v0.1.0
Expand Down
10 changes: 6 additions & 4 deletions models/generators/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ require (
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
kusionstack.io/kube-api v0.2.0
kusionstack.io/kusion v0.10.1-0.20240319121724-1726f1070bfe
kusionstack.io/kusion v0.10.1-0.20240320025236-b97b074139a9
kusionstack.io/kusion-module-framework v0.0.0-20240319125223-dc8cc739460b
)

require (
github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-github/v50 v50.0.0 // indirect
github.com/google/go-github/v50 v50.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20230323073829-e72429f035bd // indirect
Expand All @@ -47,12 +49,12 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
20 changes: 12 additions & 8 deletions models/generators/go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton h1:HKz85FwoXx86kVtTvFke7rgHvq/HoloSUvW5semjFWs=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
github.com/bytedance/mockey v1.2.10 h1:4JlMpkm7HMXmTUtItid+iCu2tm61wvq+ca1X2u7ymzE=
github.com/bytedance/mockey v1.2.10/go.mod h1:bNrUnI1u7+pAc0TYDgPATM+wF2yzHxmNH+iDXg4AOCU=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand All @@ -30,8 +34,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v50 v50.0.0 h1:gdO1AeuSZZK4iYWwVbjni7zg8PIQhp7QfmPunr016Jk=
github.com/google/go-github/v50 v50.0.0/go.mod h1:Ev4Tre8QoKiolvbpOSG3FIi4Mlon3S2Nt9W5JYqKiwA=
github.com/google/go-github/v50 v50.2.0 h1:j2FyongEHlO9nxXLc+LP3wuBSVU9mVxfpdYUexMpIfk=
github.com/google/go-github/v50 v50.2.0/go.mod h1:VBY8FB6yPIjrtKhozXv4FQupxKLS6H4m6xFZlT43q8Q=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -133,8 +137,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ=
golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o=
golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -176,8 +180,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c h1:lfpJ/2rWPa/kJgxyyXM8PrNnfCzcmxJ265mADgwmvLI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down Expand Up @@ -208,8 +212,8 @@ kcl-lang.io/kcl-plugin v0.5.0 h1:eoh6y4l81rwA8yhJXU4hN7YmJeTUNB1nfYCP9OffSxc=
kcl-lang.io/kcl-plugin v0.5.0/go.mod h1:QnZ5OLcyBw5nOnHpChRHtvBq8wvjwiHu/ZZ8j1dfz48=
kusionstack.io/kube-api v0.2.0 h1:40SHCpm9RdabTUTVjhsHWoX+h7djy4jMYYTcbnJ9SQc=
kusionstack.io/kube-api v0.2.0/go.mod h1:fYwuojoLs71ox8uyvyKNsJU4CtPtptSfJ3PUSt/3Hgg=
kusionstack.io/kusion v0.10.1-0.20240319121724-1726f1070bfe h1:S+x5hb6NkMOTVIP7PeLL1lBOXBTNacLTk8WWvgJHIdA=
kusionstack.io/kusion v0.10.1-0.20240319121724-1726f1070bfe/go.mod h1:iV48aL8BhadpB5ZaFssmrgteYydBE2W4YAbc5KbNf4Y=
kusionstack.io/kusion v0.10.1-0.20240320025236-b97b074139a9 h1:V30Dc5xHpELxhtww5TNYyFgG8v1AJWNfXv4OgAquUOM=
kusionstack.io/kusion v0.10.1-0.20240320025236-b97b074139a9/go.mod h1:iV48aL8BhadpB5ZaFssmrgteYydBE2W4YAbc5KbNf4Y=
kusionstack.io/kusion-module-framework v0.0.0-20240319125223-dc8cc739460b h1:EqJxYoJtjBb9T+v8VfYQ4HvxidmbLsrYhnHtZvNJrLg=
kusionstack.io/kusion-module-framework v0.0.0-20240319125223-dc8cc739460b/go.mod h1:CE3ZbocFjQmLPpuUVaEIpgybrYNQ4smJmkuKHU86xCo=
sigs.k8s.io/controller-runtime v0.17.2 h1:FwHwD1CTUemg0pW2otk7/U5/i5m2ymzvOXdbeGOUvw0=
Expand Down
49 changes: 16 additions & 33 deletions models/generators/monitoring/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/modules"
"kusionstack.io/kusion/pkg/workspace"
)

func (g *MonitoringModule) Generate(_ context.Context, request *module.GeneratorRequest) (*module.GeneratorResponse, error) {
Expand All @@ -25,6 +24,8 @@ func (g *MonitoringModule) Generate(_ context.Context, request *module.Generator
return nil, nil
}

log.Debug("request.DevModuleConfig: ", request.DevModuleConfig)
log.Debug("request.PlatformModuleConfig: ", request.PlatformModuleConfig)
// Parse workspace configurations for monitoring generator.
if err := g.parseWorkspaceConfig(request.DevModuleConfig, request.PlatformModuleConfig); err != nil {
return nil, err
Expand Down Expand Up @@ -62,29 +63,19 @@ func (g *MonitoringModule) Generate(_ context.Context, request *module.Generator
return nil, fmt.Errorf("MonitorType should either be service or pod %s", g.MonitorType)
}
} else {
fmt.Println("Operator mode is disabled. Patching workload annotations...")
log.Info("Operator mode is disabled. Patching workload annotations...")
// Patch workload annotations
annotations := map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/path": g.Path,
"prometheus.io/port": g.Port,
"prometheus.io/scheme": g.Scheme,
}
resource := &v1.Resource{
ID: "",
Type: v1.Kubernetes,
Attributes: nil,
DependsOn: nil,
Patcher: &v1.Patcher{
Annotations: annotations,
},
Extensions: map[string]any{
v1.ResourceExtensionGVK: "",
},
patchers := &v1.Patcher{
Annotations: annotations,
}
//resource.
return &module.GeneratorResponse{
Resources: []v1.Resource{*resource},
Patchers: []v1.Patcher{*patchers},
}, nil
}
}
Expand All @@ -94,12 +85,12 @@ func main() {
}

// parseWorkspaceConfig parses the config items for monitoring generator in workspace configurations.
func (g *MonitoringModule) parseWorkspaceConfig(accessories v1.Accessory, workspaceConfig v1.GenericConfig) error {
func (g *MonitoringModule) parseWorkspaceConfig(devConfig v1.Accessory, workspaceConfig v1.GenericConfig) error {
// Get dev config and check if it is empty
devConfig, ok := accessories[ModuleName].(map[string]any)
if !ok {
return ErrEmptyMonitoringConfigBlock
}
// devConfig, ok := accessories[ModuleName].(map[string]any)
// if !ok {
// return ErrEmptyMonitoringConfigBlock
// }

// get path and port
if path, ok := devConfig[PathKey]; ok {
Expand All @@ -109,37 +100,29 @@ func (g *MonitoringModule) parseWorkspaceConfig(accessories v1.Accessory, worksp
g.Port = port.(string)
}

// Get workspace config and check if it is empty
wsConfig, ok := workspaceConfig[ModuleName]
// If AppConfiguration contains monitoring config but workspace does not,
// respond with the error ErrEmptyModuleConfigBlock
if !ok {
return workspace.ErrEmptyModuleConfigBlock
}

if operatorMode, ok := wsConfig.(map[string]any)[OperatorModeKey]; ok {
if operatorMode, ok := workspaceConfig[OperatorModeKey]; ok {
g.OperatorMode = operatorMode.(bool)
}

if monitorType, ok := wsConfig.(map[string]any)[MonitorTypeKey]; ok {
if monitorType, ok := workspaceConfig[MonitorTypeKey]; ok {
g.MonitorType = MonitorType(monitorType.(string))
} else {
g.MonitorType = DefaultMonitorType
}

if interval, ok := wsConfig.(map[string]any)[IntervalKey]; ok {
if interval, ok := workspaceConfig[IntervalKey]; ok {
g.Interval = prometheusv1.Duration(interval.(string))
} else {
g.Interval = DefaultInterval
}

if timeout, ok := wsConfig.(map[string]any)[TimeoutKey]; ok {
if timeout, ok := workspaceConfig[TimeoutKey]; ok {
g.Timeout = prometheusv1.Duration(timeout.(string))
} else {
g.Timeout = DefaultTimeout
}

if scheme, ok := wsConfig.(map[string]any)[SchemeKey]; ok {
if scheme, ok := workspaceConfig[SchemeKey]; ok {
g.Scheme = scheme.(string)
} else {
g.Scheme = DefaultScheme
Expand Down
42 changes: 16 additions & 26 deletions models/generators/monitoring/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func BuildMonitoringTestCase(
endpointType = "podMetricsEndpoints"
}
var expectedResources []apiv1.Resource
var expectedPatchers []apiv1.Patcher
uniqueName := modules.UniqueAppName(projectName, stackName, appName)
if operatorMode {
expectedResources = []apiv1.Resource{
Expand Down Expand Up @@ -80,22 +81,13 @@ func BuildMonitoringTestCase(
}

} else {
expectedResources = []apiv1.Resource{
expectedPatchers = []apiv1.Patcher{
{
ID: "",
Type: "Kubernetes",
Attributes: nil,
DependsOn: nil,
Patcher: &apiv1.Patcher{
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/path": path,
"prometheus.io/port": port,
"prometheus.io/scheme": scheme,
},
},
Extensions: map[string]interface{}{
"GVK": "",
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/path": path,
"prometheus.io/port": port,
"prometheus.io/scheme": scheme,
},
},
}
Expand All @@ -107,23 +99,20 @@ func BuildMonitoringTestCase(
Stack: stackName,
App: appName,
PlatformModuleConfig: apiv1.GenericConfig{
ModuleName: map[string]interface{}{
TimeoutKey: timeout,
IntervalKey: interval,
SchemeKey: scheme,
OperatorModeKey: operatorMode,
MonitorTypeKey: monitorType,
},
TimeoutKey: timeout,
IntervalKey: interval,
SchemeKey: scheme,
OperatorModeKey: operatorMode,
MonitorTypeKey: monitorType,
},
DevModuleConfig: apiv1.Accessory{
ModuleName: map[string]interface{}{
PathKey: path,
PortKey: port,
},
PathKey: path,
PortKey: port,
},
},
want: &module.GeneratorResponse{
Resources: expectedResources,
Patchers: expectedPatchers,
},
wantErr: wantErr,
}
Expand All @@ -134,6 +123,7 @@ func TestMonitoringGenerator_Generate(t *testing.T) {
ctx := context.TODO()
tests := []TestCase{
*BuildMonitoringTestCase("ServiceMonitorTest", "test-project", "test-stack", "test-app", "15s", "5s", "/metrics", "web", "http", "Service", true, false),
*BuildMonitoringTestCase("ServiceMonitorTest2", "test-project-2", "test-stack-2", "test-app-2", "15s", "5s", "/metrics", "web", "http", "Service", true, false),
*BuildMonitoringTestCase("PodMonitorTest", "test-project", "test-stack", "test-app", "15s", "5s", "/metrics", "web", "http", "Pod", true, false),
*BuildMonitoringTestCase("ServiceAnnotationTest", "test-project", "test-stack", "test-app", "30s", "15s", "/metrics", "8080", "http", "Service", false, false),
*BuildMonitoringTestCase("PodAnnotationTest", "test-project", "test-stack", "test-app", "30s", "15s", "/metrics", "8080", "http", "Pod", false, false),
Expand Down
1 change: 0 additions & 1 deletion models/generators/monitoring/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

const (
ModuleName = "monitoring"
PathKey = "path"
PortKey = "port"
OperatorModeKey = "operatorMode"
Expand Down

0 comments on commit 4c445c8

Please sign in to comment.