Skip to content

Commit

Permalink
Add Get Label Info and remove direct string usage
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Sep 19, 2024
2 parents a0bc69f + dc4bd86 commit ebfcd88
Show file tree
Hide file tree
Showing 19 changed files with 419 additions and 177 deletions.
47 changes: 47 additions & 0 deletions src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,36 @@ const docTemplate = `{
}
}
},
"/labelInfo": {
"get": {
"description": "Return LabelTypes and system defined label keys with example",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Infra Resource] Common Utility"
],
"summary": "Return LabelTypes and system defined label keys with example",
"operationId": "GetSystemLabelInfo",
"responses": {
"200": {
"description": "LabelTypes and System labels with example values",
"schema": {
"$ref": "#/definitions/model.SystemLabelInfo"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/model.SimpleMsg"
}
}
}
}
},
"/loadAssets": {
"get": {
"description": "Load Common Resources from internal asset files (Spec, Image)",
Expand Down Expand Up @@ -12020,6 +12050,23 @@ const docTemplate = `{
}
}
},
"model.SystemLabelInfo": {
"type": "object",
"properties": {
"labelTypes": {
"type": "array",
"items": {
"type": "string"
}
},
"systemLabels": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
"model.TbAttachDetachDataDiskReq": {
"type": "object",
"required": [
Expand Down
47 changes: 47 additions & 0 deletions src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,36 @@
}
}
},
"/labelInfo": {
"get": {
"description": "Return LabelTypes and system defined label keys with example",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Infra Resource] Common Utility"
],
"summary": "Return LabelTypes and system defined label keys with example",
"operationId": "GetSystemLabelInfo",
"responses": {
"200": {
"description": "LabelTypes and System labels with example values",
"schema": {
"$ref": "#/definitions/model.SystemLabelInfo"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/model.SimpleMsg"
}
}
}
}
},
"/loadAssets": {
"get": {
"description": "Load Common Resources from internal asset files (Spec, Image)",
Expand Down Expand Up @@ -12014,6 +12044,23 @@
}
}
},
"model.SystemLabelInfo": {
"type": "object",
"properties": {
"labelTypes": {
"type": "array",
"items": {
"type": "string"
}
},
"systemLabels": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
"model.TbAttachDetachDataDiskReq": {
"type": "object",
"required": [
Expand Down
31 changes: 31 additions & 0 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,26 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/model.SimpleMsg'
/labelInfo:
get:
tags:
- "[Infra Resource] Common Utility"
summary: Return LabelTypes and system defined label keys with example
description: Return LabelTypes and system defined label keys with example
operationId: GetSystemLabelInfo
responses:
"200":
description: LabelTypes and System labels with example values
content:
application/json:
schema:
$ref: '#/components/schemas/model.SystemLabelInfo'
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/model.SimpleMsg'
/loadAssets:
get:
tags:
Expand Down Expand Up @@ -8916,6 +8936,17 @@ components:
countUndefined:
type: integer
description: CountUndefined is for counting Undefined
model.SystemLabelInfo:
type: object
properties:
labelTypes:
type: array
items:
type: string
systemLabels:
type: object
additionalProperties:
type: string
model.TbAttachDetachDataDiskReq:
required:
- dataDiskId
Expand Down
29 changes: 29 additions & 0 deletions src/api/rest/server/common/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,32 @@ func RestGetResourcesByLabelSelector(c echo.Context) error {

return common.EndRequestWithLog(c, reqID, nil, response)
}

// RestGetSystemLabelInfo godoc
// @ID GetSystemLabelInfo
// @Summary Return LabelTypes and system defined label keys with example
// @Description Return LabelTypes and system defined label keys with example
// @Tags [Infra Resource] Common Utility
// @Accept json
// @Produce json
// @Success 200 {object} model.SystemLabelInfo "LabelTypes and System labels with example values"
// @Failure 500 {object} model.SimpleMsg "Internal Server Error"
// @Router /labelInfo [get]
func RestGetSystemLabelInfo(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"message": idErr.Error()})
}

// Use the GetLabelConstantsMap function to get the system label constants
systemLabels := model.GetLabelConstantsMap()
labelTypes := model.GetLabelTypes()

// Wrap the map in SystemLabelInfo struct
systemLabelInfo := model.SystemLabelInfo{
SystemLabels: systemLabels,
LabelTypes: labelTypes,
}

return common.EndRequestWithLog(c, reqID, nil, systemLabelInfo)
}
1 change: 1 addition & 0 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func RunServer(port string) {
e.DELETE("/tumblebug/label/:labelType/:uid/:key", rest_label.RestRemoveLabel)
e.GET("/tumblebug/label/:labelType/:uid", rest_label.RestGetLabels)
e.GET("/tumblebug/resources/:labelType", rest_label.RestGetResourcesByLabelSelector)
e.GET("/tumblebug/labelInfo", rest_label.RestGetSystemLabelInfo)

//MCI Management
g.POST("/:nsId/mci", rest_infra.RestPostMci)
Expand Down
14 changes: 7 additions & 7 deletions src/core/common/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func CreateNs(u *model.NsReq) (model.NsInfo, error) {

// Store label info using CreateOrUpdateLabel
labels := map[string]string{
"sys.manager": model.StrManager,
"sys.namespace": content.Id,
"sys.labelType": model.StrNamespace,
"sys.id": content.Id,
"sys.name": content.Name,
"sys.uid": content.Uid,
"sys.description": content.Description,
model.LabelManager: model.StrManager,
model.LabelNamespace: content.Id,
model.LabelLabelType: model.StrNamespace,
model.LabelId: content.Id,
model.LabelName: content.Name,
model.LabelUid: content.Uid,
model.LabelDescription: content.Description,
}
err = label.CreateOrUpdateLabel(model.StrNamespace, content.Uid, key, labels)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/core/infra/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func CreateMcSwNlb(nsId string, mciId string, req *model.TbNLBReq, option string

// create a special MCI for (SW)NLB
labels := map[string]string{
"sys.description": "MCI for Global-NLB",
model.LabelDescription: "MCI for Global-NLB",
}
mciDynamicReq := model.TbMciDynamicReq{Name: nlbMciId, InstallMonAgent: "no", Label: labels}

Expand Down
2 changes: 1 addition & 1 deletion src/core/infra/manageInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func ListVmId(nsId string, mciId string) ([]string, error) {
// ListVmByLabel is a function to list VM IDs by label
func ListVmByLabel(nsId string, mciId string, labelKey string) ([]string, error) {
// Construct the label selector
labelSelector := labelKey + " exists" + "," + "sys.nsId=" + nsId + "," + "sys.mciId=" + mciId
labelSelector := labelKey + " exists" + "," + model.LabelNamespace + "=" + nsId + "," + model.LabelMciId + "=" + mciId

// Call GetResourcesByLabelSelector (returns []interface{})
resources, err := label.GetResourcesByLabelSelector(model.StrVM, labelSelector)
Expand Down
4 changes: 2 additions & 2 deletions src/core/infra/orchestration.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func OrchestrationController() {
case autoAction.ActionType == model.AutoActionScaleOut:

labels := map[string]string{
"sys.deploymentType": model.LabelAutoGen,
model.LabelDeploymentType: model.StrAutoGen,
}
autoAction.VmDynamicReq.Label = labels
// append uid to given vm name to avoid duplicated vm ID.
Expand Down Expand Up @@ -289,7 +289,7 @@ func OrchestrationController() {

// ScaleIn MCI.
log.Debug().Msg("[Removing VM]")
vmList, vmListErr := ListVmByLabel(nsId, mciPolicyTmp.Id, model.LabelAutoGen)
vmList, vmListErr := ListVmByLabel(nsId, mciPolicyTmp.Id, model.StrAutoGen)
if vmListErr != nil {
mciPolicyTmp.Policy[policyIndex].Status = model.AutoStatusError
UpdateMciPolicyInfo(nsId, mciPolicyTmp)
Expand Down
60 changes: 30 additions & 30 deletions src/core/infra/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,13 @@ func CreateMci(nsId string, req *model.TbMciReq, option string) (*model.TbMciInf

// Store label info using CreateOrUpdateLabel
labels := map[string]string{
"sys.manager": model.StrManager,
"sys.namespace": nsId,
"sys.labelType": model.StrMCI,
"sys.id": mciId,
"sys.name": req.Name,
"sys.uid": uid,
"sys.description": req.Description,
model.LabelManager: model.StrManager,
model.LabelNamespace: nsId,
model.LabelLabelType: model.StrMCI,
model.LabelId: mciId,
model.LabelName: req.Name,
model.LabelUid: uid,
model.LabelDescription: req.Description,
}
for key, value := range req.Label {
labels[key] = value
Expand Down Expand Up @@ -596,16 +596,16 @@ func CreateMci(nsId string, req *model.TbMciReq, option string) (*model.TbMciInf

// Store label info using CreateOrUpdateLabel
labels := map[string]string{
"sys.manager": model.StrManager,
"sys.namespace": nsId,
"sys.labelType": model.StrSubGroup,
"sys.id": subGroupInfoData.Id,
"sys.name": subGroupInfoData.Name,
"sys.uid": subGroupInfoData.Uid,
"sys.mciId": mciId,
"sys.mciname": req.Name,
"sys.mciUid": uid,
"sys.mciDescription": req.Description,
model.LabelManager: model.StrManager,
model.LabelNamespace: nsId,
model.LabelLabelType: model.StrSubGroup,
model.LabelId: subGroupInfoData.Id,
model.LabelName: subGroupInfoData.Name,
model.LabelUid: subGroupInfoData.Uid,
model.LabelMciId: mciId,
model.LabelMciName: req.Name,
model.LabelMciUid: uid,
model.LabelMciDescription: req.Description,
}
err = label.CreateOrUpdateLabel(model.StrSubGroup, uid, key, labels)
if err != nil {
Expand Down Expand Up @@ -793,7 +793,7 @@ func CreateSystemMciDynamic(option string) (*model.TbMciInfo, error) {
// special purpose MCI
req.Name = option
labels := map[string]string{
"sys.purpose": option,
model.LabelPurpose: option,
}
req.Label = labels
req.SystemLabel = option
Expand Down Expand Up @@ -1209,18 +1209,18 @@ func AddVmToMci(wg *sync.WaitGroup, nsId string, mciId string, vmInfoData *model

// Store label info using CreateOrUpdateLabel
labels := map[string]string{
"sys.manager": model.StrManager,
"sys.namespace": nsId,
"sys.labelType": model.StrVM,
"sys.id": vmInfoData.Id,
"sys.name": vmInfoData.Name,
"sys.uid": vmInfoData.Uid,
"sys.cspResourceId": vmInfoData.CspResourceId,
"sys.cspResourceName": vmInfoData.CspResourceName,
"sys.subGroupId": vmInfoData.SubGroupId,
"sys.mciId": mciId,
"sys.createdTime": vmInfoData.CreatedTime,
"sys.connectionName": vmInfoData.ConnectionName,
model.LabelManager: model.StrManager,
model.LabelNamespace: nsId,
model.LabelLabelType: model.StrVM,
model.LabelId: vmInfoData.Id,
model.LabelName: vmInfoData.Name,
model.LabelUid: vmInfoData.Uid,
model.LabelCspResourceId: vmInfoData.CspResourceId,
model.LabelCspResourceName: vmInfoData.CspResourceName,
model.LabelSubGroupId: vmInfoData.SubGroupId,
model.LabelMciId: mciId,
model.LabelCreatedTime: vmInfoData.CreatedTime,
model.LabelConnectionName: vmInfoData.ConnectionName,
}
for key, value := range vmInfoData.Label {
labels[key] = value
Expand Down
2 changes: 1 addition & 1 deletion src/core/infra/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ func RegisterCspNativeResources(nsId string, connConfig string, mciId string, op
req.Name = vm.Name
}
labels := map[string]string{
"sys.registered": "true",
model.LabelRegistered: "true",
}
vm.Label = labels

Expand Down
Loading

0 comments on commit ebfcd88

Please sign in to comment.