Skip to content

Commit

Permalink
refactor: opsrule with module framework (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan authored Mar 11, 2024
1 parent cbe9e71 commit 3891e96
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 195 deletions.
124 changes: 0 additions & 124 deletions models/generators/generator.go

This file was deleted.

20 changes: 10 additions & 10 deletions models/generators/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module kusion-modules

go 1.19
go 1.22

require (
github.com/hashicorp/go-plugin v1.6.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.27.2
kusionstack.io/kube-api v0.1.1
kusionstack.io/kusion v0.10.1-0.20240305085131-a947ad87dfa8
kusionstack.io/kusion v0.10.1-0.20240311030125-729b89bf8197
kusionstack.io/kusion-module-framework v0.0.0-20240311081739-579272fe615f
)

require (
Expand All @@ -17,13 +16,13 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/hashicorp/go-hclog v0.16.2 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
Expand All @@ -35,12 +34,13 @@ require (
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.61.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.27.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
Expand Down
39 changes: 15 additions & 24 deletions models/generators/opsrule/opsrule.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
package main

import (
"context"
"errors"
"strconv"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"kusionstack.io/kube-api/apps/v1alpha1"
"kusionstack.io/kusion-module-framework/pkg/module"
"kusionstack.io/kusion-module-framework/pkg/server"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/core/v1/workload"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/modules"
"kusionstack.io/kusion/pkg/modules/proto"
jsonutil "kusionstack.io/kusion/pkg/util/json"

generators "kusion-modules"
)

type OpsRuleModule struct{}

func (o *OpsRuleModule) Generate(r *proto.GeneratorRequest) (*proto.GeneratorResponse, error) {
emptyResponse := generators.EmptyResponse()
request, err := generators.NewGeneratorRequest(r)
if err != nil {
return nil, err
}
func (o *OpsRuleModule) Generate(_ context.Context, request *module.GeneratorRequest) (*module.GeneratorResponse, error) {

// opsRule does not exist in AppConfig and workspace config
if request.DevModuleConfig == nil && request.PlatformModuleConfig == nil {
log.Info("OpsRule does not exist in AppConfig and workspace config")
return emptyResponse, nil
return nil, nil
}

// Job does not support maxUnavailable
if request.Workload.Header.Type == workload.TypeJob {
log.Infof("Job does not support opsRule")
return emptyResponse, nil
return nil, nil
}

if request.Workload.Service.Type == workload.Collaset {
Expand All @@ -49,12 +42,12 @@ func (o *OpsRuleModule) Generate(r *proto.GeneratorRequest) (*proto.GeneratorRes
Kind: "PodTransitionRule",
},
ObjectMeta: metav1.ObjectMeta{
Name: modules.UniqueAppName(request.Project, request.Stack, request.App),
Namespace: request.App,
Name: module.UniqueAppName(request.Project, request.Stack, request.App),
Namespace: request.Project,
},
Spec: v1alpha1.PodTransitionRuleSpec{
Selector: &metav1.LabelSelector{
MatchLabels: modules.UniqueAppLabels(request.Project, request.App),
MatchLabels: module.UniqueAppLabels(request.Project, request.App),
},
Rules: []v1alpha1.TransitionRule{
{
Expand All @@ -68,18 +61,16 @@ func (o *OpsRuleModule) Generate(r *proto.GeneratorRequest) (*proto.GeneratorRes
},
},
}
resourceID := modules.KubernetesResourceID(ptr.TypeMeta, ptr.ObjectMeta)
resource, err := generators.WrapK8sResourceToKusionResource(resourceID, ptr)
resourceID := module.KubernetesResourceID(ptr.TypeMeta, ptr.ObjectMeta)
resource, err := module.WrapK8sResourceToKusionResource(resourceID, ptr)
if err != nil {
return nil, err
}
str := jsonutil.Marshal2String(resource)
b := []byte(str)
return &proto.GeneratorResponse{
Resources: [][]byte{b},
return &module.GeneratorResponse{
Resources: []v1.Resource{*resource},
}, nil
}
return emptyResponse, nil
return nil, nil
}

func GetMaxUnavailable(devConfig v1.Accessory, platformConfig v1.GenericConfig) (intstr.IntOrString, error) {
Expand Down Expand Up @@ -114,5 +105,5 @@ func GetMaxUnavailable(devConfig v1.Accessory, platformConfig v1.GenericConfig)
}

func main() {
modules.StartModule(&OpsRuleModule{})
server.Start(&OpsRuleModule{})
}
Loading

0 comments on commit 3891e96

Please sign in to comment.