-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
657eb1b
commit e09ff73
Showing
7 changed files
with
272 additions
and
11 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
pkg/generator/appconfiguration/generator/trait/ops_rule_generator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package trait | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"kusionstack.io/kube-api/apps/v1alpha1" | ||
"kusionstack.io/kusion/pkg/generator/appconfiguration" | ||
"kusionstack.io/kusion/pkg/models" | ||
appmodule "kusionstack.io/kusion/pkg/models/appconfiguration" | ||
"kusionstack.io/kusion/pkg/models/appconfiguration/workload" | ||
"kusionstack.io/kusion/pkg/projectstack" | ||
) | ||
|
||
type opsRuleGenerator struct { | ||
project *projectstack.Project | ||
stack *projectstack.Stack | ||
appName string | ||
app *appmodule.AppConfiguration | ||
} | ||
|
||
func NewOpsRuleGenerator( | ||
project *projectstack.Project, | ||
stack *projectstack.Stack, | ||
appName string, | ||
app *appmodule.AppConfiguration, | ||
) (appconfiguration.Generator, error) { | ||
return &opsRuleGenerator{ | ||
project: project, | ||
stack: stack, | ||
appName: appName, | ||
app: app, | ||
}, nil | ||
} | ||
|
||
func NewOpsRuleGeneratorFunc( | ||
project *projectstack.Project, | ||
stack *projectstack.Stack, | ||
appName string, | ||
app *appmodule.AppConfiguration, | ||
) appconfiguration.NewGeneratorFunc { | ||
return func() (appconfiguration.Generator, error) { | ||
return NewOpsRuleGenerator(project, stack, appName, app) | ||
} | ||
} | ||
|
||
func (g *opsRuleGenerator) Generate(spec *models.Spec) error { | ||
if g.app.Workload.Header.Type != workload.TypeService { | ||
return nil | ||
} | ||
|
||
switch g.app.Workload.Service.Type { | ||
case workload.TypeCollaset: | ||
resource := &v1alpha1.RuleSet{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: v1alpha1.GroupVersion.String(), | ||
Kind: "RuleSet", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: appconfiguration.UniqueAppName(g.project.Name, g.stack.Name, g.appName), | ||
Namespace: g.project.Name, | ||
}, | ||
Spec: v1alpha1.RuleSetSpec{ | ||
Selector: metav1.LabelSelector{ | ||
MatchLabels: appconfiguration.UniqueAppLabels(g.project.Name, g.appName), | ||
}, | ||
}, | ||
} | ||
return appconfiguration.AppendToSpec(models.Kubernetes, appconfiguration.KubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), spec, resource) | ||
case workload.TypeDeploy: | ||
// TODO: add maxUnavailable to deployment | ||
return nil | ||
} | ||
return nil | ||
} |
173 changes: 173 additions & 0 deletions
173
pkg/generator/appconfiguration/generator/trait/ops_rule_generator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package trait | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"kusionstack.io/kusion/pkg/models" | ||
appmodule "kusionstack.io/kusion/pkg/models/appconfiguration" | ||
"kusionstack.io/kusion/pkg/models/appconfiguration/trait" | ||
"kusionstack.io/kusion/pkg/models/appconfiguration/workload" | ||
"kusionstack.io/kusion/pkg/projectstack" | ||
) | ||
|
||
func Test_opsRuleGenerator_Generate(t *testing.T) { | ||
type fields struct { | ||
project *projectstack.Project | ||
stack *projectstack.Stack | ||
appName string | ||
app *appmodule.AppConfiguration | ||
} | ||
type args struct { | ||
spec *models.Spec | ||
} | ||
project := &projectstack.Project{ | ||
ProjectConfiguration: projectstack.ProjectConfiguration{ | ||
Name: "default", | ||
}, | ||
} | ||
stack := &projectstack.Stack{ | ||
StackConfiguration: projectstack.StackConfiguration{Name: "dev"}, | ||
} | ||
appName := "foo" | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr bool | ||
exp *models.Spec | ||
}{ | ||
{ | ||
name: "test Job", | ||
fields: fields{ | ||
project: project, | ||
stack: stack, | ||
appName: appName, | ||
app: &appmodule.AppConfiguration{ | ||
Workload: &workload.Workload{ | ||
Header: workload.Header{ | ||
Type: workload.TypeJob, | ||
}, | ||
}, | ||
OpsRule: &trait.OpsRule{ | ||
MaxUnavailable: "30%", | ||
}, | ||
}, | ||
}, | ||
args: args{ | ||
spec: &models.Spec{}, | ||
}, | ||
wantErr: false, | ||
exp: &models.Spec{}, | ||
}, | ||
{ | ||
name: "test CollaSet", | ||
fields: fields{ | ||
project: project, | ||
stack: stack, | ||
appName: appName, | ||
app: &appmodule.AppConfiguration{ | ||
Workload: &workload.Workload{ | ||
Header: workload.Header{ | ||
Type: workload.TypeService, | ||
}, | ||
Service: &workload.Service{ | ||
Type: workload.TypeCollaset, | ||
}, | ||
}, | ||
OpsRule: &trait.OpsRule{ | ||
MaxUnavailable: "30%", | ||
}, | ||
}, | ||
}, | ||
args: args{ | ||
spec: &models.Spec{}, | ||
}, | ||
wantErr: false, | ||
exp: &models.Spec{ | ||
Resources: []models.Resource{ | ||
{ | ||
ID: "apps.kusionstack.io/v1alpha1:RuleSet:default:default-dev-foo", | ||
Type: "Kubernetes", | ||
Attributes: map[string]interface{}{ | ||
"apiVersion": "apps.kusionstack.io/v1alpha1", | ||
"kind": "RuleSet", | ||
"metadata": map[string]interface{}{ | ||
"creationTimestamp": nil, | ||
"name": "default-dev-foo", | ||
"namespace": "default", | ||
}, | ||
"spec": map[string]interface{}{ | ||
"selector": map[string]interface{}{ | ||
"matchLabels": map[string]interface{}{ | ||
"app.kubernetes.io/name": "foo", | ||
"app.kubernetes.io/part-of": "default", | ||
}, | ||
}, | ||
}, | ||
"status": map[string]interface{}{}, | ||
}, | ||
DependsOn: nil, | ||
Extensions: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := &opsRuleGenerator{ | ||
project: tt.fields.project, | ||
stack: tt.fields.stack, | ||
appName: tt.fields.appName, | ||
app: tt.fields.app, | ||
} | ||
err := g.Generate(tt.args.spec) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.exp, tt.args.spec) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewOpsRuleGeneratorFunc(t *testing.T) { | ||
type args struct { | ||
project *projectstack.Project | ||
stack *projectstack.Stack | ||
appName string | ||
app *appmodule.AppConfiguration | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
want *opsRuleGenerator | ||
}{ | ||
{ | ||
name: "test1", | ||
args: args{ | ||
project: nil, | ||
stack: nil, | ||
appName: "", | ||
app: nil, | ||
}, | ||
wantErr: false, | ||
want: &opsRuleGenerator{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f := NewOpsRuleGeneratorFunc(tt.args.project, tt.args.stack, tt.args.appName, tt.args.app) | ||
g, err := f() | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, g) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package trait | ||
|
||
type OpsRule struct { | ||
MaxUnavailable string `json:"maxUnavailable,omitempty" yaml:"maxUnavailable,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters