Skip to content

Commit

Permalink
Provide the API to check system readiness instead of health
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored May 3, 2024
2 parents fe60726 + dcadfe5 commit e6c54e2
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 116 deletions.
64 changes: 29 additions & 35 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,41 +215,6 @@ const docTemplate = `{
}
}
},
"/health": {
"get": {
"description": "Check Beetle is alive",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System management"
],
"summary": "Check Beetle is alive",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
}
},
"/httpVersion": {
"get": {
"description": "Checks and logs the HTTP version of the incoming request to the server console.",
Expand Down Expand Up @@ -735,6 +700,35 @@ const docTemplate = `{
}
}
},
"/readyz": {
"get": {
"description": "Check Beetle is ready",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System management"
],
"summary": "Check Beetle is ready",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"503": {
"description": "Service Unavailable",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
}
},
"/recommendation/infra": {
"post": {
"description": "It recommends a cloud infrastructure most similar to the input. Infrastructure includes network, storage, compute, and so on.",
Expand Down
64 changes: 29 additions & 35 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,41 +208,6 @@
}
}
},
"/health": {
"get": {
"description": "Check Beetle is alive",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System management"
],
"summary": "Check Beetle is alive",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
}
},
"/httpVersion": {
"get": {
"description": "Checks and logs the HTTP version of the incoming request to the server console.",
Expand Down Expand Up @@ -728,6 +693,35 @@
}
}
},
"/readyz": {
"get": {
"description": "Check Beetle is ready",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System management"
],
"summary": "Check Beetle is ready",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"503": {
"description": "Service Unavailable",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
}
},
"/recommendation/infra": {
"post": {
"description": "It recommends a cloud infrastructure most similar to the input. Infrastructure includes network, storage, compute, and so on.",
Expand Down
42 changes: 19 additions & 23 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -937,29 +937,6 @@ paths:
summary: Get config
tags:
- '[Admin] System environment'
/health:
get:
consumes:
- application/json
description: Check Beetle is alive
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/common.SimpleMsg'
"404":
description: Not Found
schema:
$ref: '#/definitions/common.SimpleMsg'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/common.SimpleMsg'
summary: Check Beetle is alive
tags:
- '[Admin] System management'
/httpVersion:
get:
consumes:
Expand Down Expand Up @@ -1283,6 +1260,25 @@ paths:
summary: Update namespace
tags:
- '[Namespace] Namespace management'
/readyz:
get:
consumes:
- application/json
description: Check Beetle is ready
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/common.SimpleMsg'
"503":
description: Service Unavailable
schema:
$ref: '#/definitions/common.SimpleMsg'
summary: Check Beetle is ready
tags:
- '[Admin] System management'
/recommendation/infra:
post:
consumes:
Expand Down
6 changes: 5 additions & 1 deletion cmd/cm-beetle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ import (
restServer "github.com/cloud-barista/cm-beetle/pkg/api/rest/server"
)

func init() {
common.SystemReady = false
}

func main() {

log.Info().Msg("starting CM-Beetle server")
log.Info().Msg("CM-Beetle server is starting...")

// Set the default port number "8056" for the REST API server to listen on
port := flag.String("port", "8056", "port number for the restapiserver to listen to")
Expand Down
26 changes: 14 additions & 12 deletions pkg/api/rest/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,24 @@ func Validate(c echo.Context, params []string) error {
return nil
}

// RestGetHealth func is for checking Beetle server health.
// RestGetHealth godoc
// @Summary Check Beetle is alive
// @Description Check Beetle is alive
// RestGetReadyz func check if CM-Beetle server is ready or not.
// RestGetReadyz godoc
// @Summary Check Beetle is ready
// @Description Check Beetle is ready
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /health [get]
func RestGetHealth(c echo.Context) error {
okMessage := common.SimpleMsg{}
okMessage.Message = "CM-Beetle API server is running"

return c.JSON(http.StatusOK, &okMessage)
// @Failure 503 {object} common.SimpleMsg
// @Router /readyz [get]
func RestGetReadyz(c echo.Context) error {
message := common.SimpleMsg{}
message.Message = "CM-Beetle is ready"
if !common.SystemReady {
message.Message = "CM-Beetle is NOT ready"
return c.JSON(http.StatusServiceUnavailable, &message)
}
return c.JSON(http.StatusOK, &message)
}

// RestCheckHTTPVersion godoc
Expand Down
30 changes: 20 additions & 10 deletions pkg/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
rest_common "github.com/cloud-barista/cm-beetle/pkg/api/rest/common"
"github.com/cloud-barista/cm-beetle/pkg/api/rest/middlewares"
"github.com/cloud-barista/cm-beetle/pkg/api/rest/route"
"github.com/cloud-barista/cm-beetle/pkg/core/common"
"github.com/spf13/viper"

"crypto/subtle"
Expand Down Expand Up @@ -96,7 +97,7 @@ const (
// @securityDefinitions.basic BasicAuth
func RunServer(port string) {

log.Info().Msg("Setting CM-Beetle REST API server")
log.Info().Msg("CM-Beetle REST API server is starting...")

e := echo.New()

Expand Down Expand Up @@ -134,7 +135,7 @@ func RunServer(port string) {
e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{
// Skip authentication for some routes that do not require authentication
Skipper: func(c echo.Context) bool {
if c.Path() == "/beetle/health" ||
if c.Path() == "/beetle/readyz" ||
c.Path() == "/beetle/httpVersion" {
return true
}
Expand All @@ -159,10 +160,16 @@ func RunServer(port string) {
fmt.Println("\n \n ")

// Route for system management
e.GET("/beetle/swagger/*", echoSwagger.WrapHandler)

// e.GET("/beetle/swagger/*", echoSwagger.WrapHandler)
// e.GET("/beetle/swaggerActive", rest_common.RestGetSwagger)
e.GET("/beetle/health", rest_common.RestGetHealth)
swaggerRedirect := func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "/beetle/api/index.html")
}
e.GET("/beetle/api", swaggerRedirect)
e.GET("/beetle/api/", swaggerRedirect)
e.GET("/beetle/api/*", echoSwagger.WrapHandler)

e.GET("/beetle/readyz", rest_common.RestGetReadyz)
e.GET("/beetle/httpVersion", rest_common.RestCheckHTTPVersion)

// Beetle API group which has /beetle as prefix
Expand Down Expand Up @@ -205,7 +212,7 @@ func RunServer(port string) {
// g.DELETE("/:nsId/mcis", rest_mcis.RestDelAllMcis)

selfEndpoint := viper.GetString("self.endpoint")
apidashboard := " http://" + selfEndpoint + "/beetle/swagger/index.html"
apidashboard := " http://" + selfEndpoint + "/beetle/api"

if enableAuth {
fmt.Println(" Access to API dashboard" + " (username: " + apiUser + " / password: " + apiPass + ")")
Expand Down Expand Up @@ -234,21 +241,24 @@ func RunServer(port string) {
// Block until a signal is triggered
<-gracefulShutdownContext.Done()

fmt.Println("\n[Stop] CM-Beetle REST API server")
log.Info().Msg("stopping CM-Beetle REST API server")
log.Info().Msg("Stopping CM-Beetle REST API server")
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()

if err := e.Shutdown(ctx); err != nil {
log.Error().Err(err).Msg("Error when graceful shutting down CM-Beetle API server")
e.Logger.Panic(err)
}
}(&wg)

log.Info().Msg("starting CM-Beetle REST API server")
port = fmt.Sprintf(":%s", port)
common.SystemReady = true
if err := e.Start(port); err != nil && err != http.ErrServerClosed {
e.Logger.Panic("shuttig down the server")
log.Error().Err(err).Msg("Error when starting CM-Beetle API server")
e.Logger.Panic("Shuttig down the server: ", err)
}

log.Info().Msg("CM-Beetle REST API server is started.")

wg.Wait()
}
3 changes: 3 additions & 0 deletions pkg/core/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type IdList struct {
IdList []string `json:"output"`
}

// SystemReady is global variable for checking SystemReady status
var SystemReady bool

// CB-Store
var CBLog *logrus.Logger
var CBStore icbs.Store
Expand Down

0 comments on commit e6c54e2

Please sign in to comment.