Feat: support automatically creating scaler trait for webservice component (#2908)

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
This commit is contained in:
barnettZQG
2021-12-10 15:04:31 +08:00
committed by GitHub
parent b622cbdb7f
commit f47ae0e498
3 changed files with 111 additions and 85 deletions
+19
View File
@@ -991,6 +991,9 @@ func (c *applicationUsecaseImpl) AddComponent(ctx context.Context, app *model.Ap
return nil, bcode.ErrInvalidProperties
}
componentModel.Properties = properties
// create default triat for component
c.initCreateDefaultTrait(&componentModel)
if err := c.ds.Add(ctx, &componentModel); err != nil {
if errors.Is(err, datastore.ErrRecordExist) {
return nil, bcode.ErrApplicationComponetExist
@@ -1001,6 +1004,22 @@ func (c *applicationUsecaseImpl) AddComponent(ctx context.Context, app *model.Ap
return converComponentModelToBase(&componentModel), nil
}
func (c *applicationUsecaseImpl) initCreateDefaultTrait(component *model.ApplicationComponent) {
replicationTrait := model.ApplicationTrait{
Alias: "Replication",
Type: "scaler",
Description: "Adjust the number of application replication.",
Properties: &model.JSONStruct{
"replicas": 1,
},
}
var initTraits = []model.ApplicationTrait{}
if component.Type == "webservice" && len(component.Traits) == 0 {
initTraits = append(initTraits, replicationTrait)
}
component.Traits = initTraits
}
func converComponentModelToBase(componentModel *model.ApplicationComponent) *apisv1.ComponentBase {
if componentModel == nil {
return nil
@@ -89,10 +89,17 @@ var _ = Describe("Test application usecase function", func() {
Description: "test env",
TargetNames: []string{"test-target"},
}},
Component: &v1.CreateComponentRequest{
Name: "component-name",
ComponentType: "webservice",
},
}
base, err := appUsecase.CreateApplication(context.TODO(), req)
Expect(err).Should(BeNil())
Expect(cmp.Diff(base.Description, req.Description)).Should(BeEmpty())
detail, err := appUsecase.DetailComponent(context.TODO(), &model.Application{Name: "test-app", Project: testProject}, "component-name")
Expect(err).Should(BeNil())
Expect(cmp.Diff(len(detail.Traits), 1)).Should(BeEmpty())
_, err = appUsecase.CreateApplication(context.TODO(), req)
equal := cmp.Equal(err, bcode.ErrApplicationExist, cmpopts.EquateErrors())
+85 -85
View File
@@ -51,6 +51,12 @@
- valueFrom
label: Add By Secret
subParameters:
- description: The value of the environment variable
jsonKey: value
label: Value
sort: 100
uiType: Input
validate: {}
- description: Specifies a source the value of this var should come from
jsonKey: valueFrom
label: Secret Selector
@@ -88,12 +94,6 @@
uiType: Input
validate:
required: true
- description: The value of the environment variable
jsonKey: value
label: Value
sort: 100
uiType: Input
validate: {}
uiType: Structs
validate: {}
- description: Instructions for assessing whether the container is in a suitable state
@@ -102,6 +102,70 @@
label: ReadinessProbe
sort: 13
subParameters:
- description: Instructions for assessing container health by executing an HTTP
GET request. Either this attribute or the exec attribute or the tcpSocket attribute
MUST be specified. This attribute is mutually exclusive with both the exec attribute
and the tcpSocket attribute.
jsonKey: httpGet
label: HttpGet
sort: 100
subParameters:
- description: ""
jsonKey: httpHeaders
label: HttpHeaders
sort: 100
subParameters:
- description: ""
jsonKey: value
label: Value
sort: 100
uiType: Input
validate:
required: true
- description: ""
jsonKey: name
label: Name
sort: 100
uiType: Input
validate:
required: true
uiType: Structs
validate: {}
- description: The endpoint, relative to the port, to which the HTTP GET request
should be directed.
jsonKey: path
label: Path
sort: 100
uiType: Input
validate:
required: true
- description: The TCP socket within the container to which the HTTP GET request
should be directed.
jsonKey: port
label: Port
sort: 100
uiType: Number
validate:
required: true
uiType: Group
validate: {}
- description: Number of seconds after the container is started before the first
probe is initiated.
jsonKey: initialDelaySeconds
label: InitialDelaySeconds
sort: 100
uiType: Number
validate:
defaultValue: 0
required: true
- description: How often, in seconds, to execute the probe.
jsonKey: periodSeconds
label: PeriodSeconds
sort: 100
uiType: Number
validate:
defaultValue: 10
required: true
- description: Minimum consecutive successes for the probe to be considered successful
after having failed.
jsonKey: successThreshold
@@ -166,70 +230,6 @@
validate:
defaultValue: 3
required: true
- description: Instructions for assessing container health by executing an HTTP
GET request. Either this attribute or the exec attribute or the tcpSocket attribute
MUST be specified. This attribute is mutually exclusive with both the exec attribute
and the tcpSocket attribute.
jsonKey: httpGet
label: HttpGet
sort: 100
subParameters:
- description: ""
jsonKey: httpHeaders
label: HttpHeaders
sort: 100
subParameters:
- description: ""
jsonKey: name
label: Name
sort: 100
uiType: Input
validate:
required: true
- description: ""
jsonKey: value
label: Value
sort: 100
uiType: Input
validate:
required: true
uiType: Structs
validate: {}
- description: The endpoint, relative to the port, to which the HTTP GET request
should be directed.
jsonKey: path
label: Path
sort: 100
uiType: Input
validate:
required: true
- description: The TCP socket within the container to which the HTTP GET request
should be directed.
jsonKey: port
label: Port
sort: 100
uiType: Number
validate:
required: true
uiType: Group
validate: {}
- description: Number of seconds after the container is started before the first
probe is initiated.
jsonKey: initialDelaySeconds
label: InitialDelaySeconds
sort: 100
uiType: Number
validate:
defaultValue: 0
required: true
- description: How often, in seconds, to execute the probe.
jsonKey: periodSeconds
label: PeriodSeconds
sort: 100
uiType: Number
validate:
defaultValue: 10
required: true
uiType: Group
validate: {}
- description: Instructions for assessing whether the container is alive.
@@ -367,21 +367,6 @@
required: true
uiType: Group
validate: {}
- description: Which port do you want customer traffic sent to
disable: true
jsonKey: port
label: Port
sort: 100
uiType: Number
validate:
defaultValue: 80
required: true
- description: Specify image pull secrets for your service
jsonKey: imagePullSecrets
label: ImagePullSecrets
sort: 100
uiType: Strings
validate: {}
- description: Declare volumes and volumeMounts
disable: true
jsonKey: volumes
@@ -430,3 +415,18 @@
validate:
defaultValue: false
required: true
- description: Specify image pull secrets for your service
jsonKey: imagePullSecrets
label: ImagePullSecrets
sort: 100
uiType: Strings
validate: {}
- description: Which port do you want customer traffic sent to
disable: true
jsonKey: port
label: Port
sort: 100
uiType: Number
validate:
defaultValue: 80
required: true