Skip to content

Commit

Permalink
add /tumblebug/recommendK8sClusterSpec[POST] API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sooyoung Kim committed May 23, 2024
1 parent 85cc6e7 commit fb4f126
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8079,6 +8079,54 @@ const docTemplate = `{
}
}
},
"/recommendK8sClusterSpec": {
"post": {
"description": "Recommend K8s Cluster plan (filter and priority) Find details from https://github.com/cloud-barista/cb-tumblebug/discussions/1234",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Infra service] Cluster Provisioning Management"
],
"summary": "Recommend K8s Cluster plan (filter and priority)",
"parameters": [
{
"description": "Recommend K8s Cluster plan (filter and priority)",
"name": "deploymentPlan",
"in": "body",
"schema": {
"$ref": "#/definitions/mcis.DeploymentPlan"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/mcir.TbSpecInfo"
}
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
}
},
"/region": {
"get": {
"description": "List all registered regions",
Expand Down
48 changes: 48 additions & 0 deletions src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -8072,6 +8072,54 @@
}
}
},
"/recommendK8sClusterSpec": {
"post": {
"description": "Recommend K8s Cluster plan (filter and priority) Find details from https://github.com/cloud-barista/cb-tumblebug/discussions/1234",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Infra service] Cluster Provisioning Management"
],
"summary": "Recommend K8s Cluster plan (filter and priority)",
"parameters": [
{
"description": "Recommend K8s Cluster plan (filter and priority)",
"name": "deploymentPlan",
"in": "body",
"schema": {
"$ref": "#/definitions/mcis.DeploymentPlan"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/mcir.TbSpecInfo"
}
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
}
},
"/region": {
"get": {
"description": "List all registered regions",
Expand Down
32 changes: 32 additions & 0 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8408,6 +8408,38 @@ paths:
summary: Check Tumblebug is ready
tags:
- '[Admin] System management'
/recommendK8sClusterSpec:
post:
consumes:
- application/json
description: Recommend K8s Cluster plan (filter and priority) Find details from
https://github.com/cloud-barista/cb-tumblebug/discussions/1234
parameters:
- description: Recommend K8s Cluster plan (filter and priority)
in: body
name: deploymentPlan
schema:
$ref: '#/definitions/mcis.DeploymentPlan'
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/mcir.TbSpecInfo'
type: array
"404":
description: Not Found
schema:
$ref: '#/definitions/common.SimpleMsg'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/common.SimpleMsg'
summary: Recommend K8s Cluster plan (filter and priority)
tags:
- '[Infra service] Cluster Provisioning Management'
/region:
get:
consumes:
Expand Down
27 changes: 27 additions & 0 deletions src/api/rest/server/mcis/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ func RestRecommendVm(c echo.Context) error {
return common.EndRequestWithLog(c, reqID, err, content)
}

// RestRecommendK8sClusterSpec godoc
// @Summary Recommend K8s Cluster plan (filter and priority)
// @Description Recommend K8s Cluster plan (filter and priority) Find details from https://github.com/cloud-barista/cb-tumblebug/discussions/1234
// @Tags [Infra service] Cluster Provisioning Management
// @Accept json
// @Produce json
// @Param deploymentPlan body mcis.DeploymentPlan false "Recommend K8s Cluster plan (filter and priority)"
// @Success 200 {object} []mcir.TbSpecInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /recommendK8sClusterSpec [post]
func RestRecommendK8sClusterSpec(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}
nsId := common.SystemCommonNs

u := &mcis.DeploymentPlan{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
}

content, err := mcis.RecommendK8sClusterSpec(nsId, *u)
return common.EndRequestWithLog(c, reqID, err, content)
}

type RestPostMcisRecommendResponse struct {
//VmReq []TbVmRecommendReq `json:"vmReq"`
VmRecommend []mcis.TbVmRecommendInfo `json:"vmRecommend"`
Expand Down
3 changes: 3 additions & 0 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ func RunServer(port string) {
g.POST("/:nsId/mcisDynamic", rest_mcis.RestPostMcisDynamic)
g.POST("/:nsId/mcis/:mcisId/vmDynamic", rest_mcis.RestPostMcisVmDynamic)

// @Tags [Infra] Cluster Provisioning Management
e.POST("/tumblebug/recommendK8sClusterSpec", rest_mcis.RestRecommendK8sClusterSpec)

//g.GET("/:nsId/mcis/:mcisId", rest_mcis.RestGetMcis, middleware.TimeoutWithConfig(middleware.TimeoutConfig{Timeout: 20 * time.Second}), middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(1)))
//g.GET("/:nsId/mcis", rest_mcis.RestGetAllMcis, middleware.TimeoutWithConfig(middleware.TimeoutConfig{Timeout: 20 * time.Second}), middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(1)))
// path specific timeout and ratelimit
Expand Down
131 changes: 131 additions & 0 deletions src/core/mcis/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,134 @@ func CorePostMcisRecommend(nsId string, req *McisRecommendReq) ([]TbVmRecommendI

return VmRecommend, nil
}

func RecommendK8sClusterSpec(nsId string, plan DeploymentPlan) ([]mcir.TbSpecInfo, error) {
//
// Get Recommend Specs in NodeGroup Level
//
limitK8s := plan.Limit
plan.Limit = strconv.Itoa(math.MaxInt)
rcmVmSpecs, err := RecommendVm(nsId, plan)
if err != nil {
log.Error().Err(err).Msg("")
return []mcir.TbSpecInfo{}, err
}

//
// Validate k8sclusterinfo.yaml
//
result := []mcir.TbSpecInfo{}
limitNum, err := strconv.Atoi(limitK8s)
if err != nil {
limitNum = math.MaxInt
}

log.Debug().Msg("[Filtering specs for K8sClusterInfo]")

count := 0
for _, spec := range rcmVmSpecs {
// check k8sclusterinfo.yaml
valid := isValidSpecInK8sClusterInfo(&spec)
if valid == false {
continue
}

result = append(result, spec)
if count == (limitNum - 1) {
break
}
count = count + 1
}

log.Debug().Msgf("RecommendedK8sClusterspec: %v", result)

return result, nil
}

func isValidSpecInK8sClusterInfo(spec *mcir.TbSpecInfo) bool {
//
// Check for Provider
//

providerName := strings.ToLower(spec.ProviderName)

var k8sClusterDetail *common.K8sClusterDetail = nil
for provider, detail := range common.RuntimeK8sClusterInfo.CSPs {
provider = strings.ToLower(provider)
if provider == providerName {
k8sClusterDetail = &detail
break
}
}
if k8sClusterDetail == nil {
return false
}

//
// Check for Region
//

regionName := strings.ToLower(spec.RegionName)

// Check for Version
isExist := false
for _, versionDetail := range k8sClusterDetail.Version {
for _, region := range versionDetail.Region {
region = strings.ToLower(region)
if region == "all" || region == regionName {
if len(versionDetail.Available) > 0 {
isExist = true
break
}
}
}
if isExist == true {
break
}
}
if isExist == false {
return false
}

// Check for NodeImage
isExist = false
for _, nodeImageDetail := range k8sClusterDetail.NodeImage {
for _, region := range nodeImageDetail.Region {
region = strings.ToLower(region)
if region == "all" || region == regionName {
if len(nodeImageDetail.Available) > 0 {
isExist = true
break
}
}
}
if isExist == true {
break
}
}
if isExist == false {
return false
}

// Check for RootDisk
isExist = false
for _, rootDiskDetail := range k8sClusterDetail.RootDisk {
for _, region := range rootDiskDetail.Region {
region = strings.ToLower(region)
if region == "all" || region == regionName {
if len(rootDiskDetail.Type) > 0 {
isExist = true
break
}
}
}
if isExist == true {
break
}
}
if isExist == false {
return false
}

return true
}

0 comments on commit fb4f126

Please sign in to comment.