Skip to content

Commit

Permalink
fix: add maxUnavailable in OpsRule (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Sep 6, 2023
1 parent f9158e6 commit 8a13100
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package trait

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"kusionstack.io/kube-api/apps/v1alpha1"

"kusionstack.io/kusion/pkg/generator/appconfiguration"
"kusionstack.io/kusion/pkg/models"
appmodule "kusionstack.io/kusion/pkg/models/appconfiguration"
Expand Down Expand Up @@ -47,27 +49,35 @@ func (g *opsRuleGenerator) Generate(spec *models.Spec) error {
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,
if g.app.Workload.Service.Type != workload.TypeCollaset {
return nil
}

maxUnavailable := intstr.Parse(g.app.OpsRule.MaxUnavailable)
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),
},
Spec: v1alpha1.RuleSetSpec{
Selector: metav1.LabelSelector{
MatchLabels: appconfiguration.UniqueAppLabels(g.project.Name, g.appName),
Rules: []v1alpha1.RuleSetRule{
{
Name: "maxUnavailable",
RuleSetRuleDefinition: v1alpha1.RuleSetRuleDefinition{
AvailablePolicy: &v1alpha1.AvailableRule{
MaxUnavailableValue: &maxUnavailable,
},
},
},
},
}
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
return appconfiguration.AppendToSpec(models.Kubernetes, appconfiguration.KubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), spec, resource)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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"
Expand Down Expand Up @@ -85,30 +86,32 @@ func Test_opsRuleGenerator_Generate(t *testing.T) {
},
wantErr: false,
exp: &models.Spec{
Resources: []models.Resource{
{
Resources: models.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,
"creationTimestamp": interface{}(nil),
"name": "default-dev-foo",
"namespace": "default",
},
"spec": map[string]interface{}{
"rules": []interface{}{map[string]interface{}{
"availablePolicy": map[string]interface{}{
"maxUnavailableValue": "30%",
},
"name": "maxUnavailable",
}},
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"app.kubernetes.io/name": "foo",
"app.kubernetes.io/part-of": "default",
"app.kubernetes.io/name": "foo", "app.kubernetes.io/part-of": "default",
},
},
},
"status": map[string]interface{}{},
},
DependsOn: nil,
Extensions: nil,
}, "status": map[string]interface{}{},
}, DependsOn: []string(nil), Extensions: map[string]interface{}(nil),
},
},
},
Expand Down

0 comments on commit 8a13100

Please sign in to comment.