From 4c12c8f45734eb2f7609717ef74c31aaa81ccad7 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 3 May 2024 15:56:08 +0900 Subject: [PATCH 1/2] Add `readyz` pkg --- pkg/readyz/readyz.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 pkg/readyz/readyz.go diff --git a/pkg/readyz/readyz.go b/pkg/readyz/readyz.go new file mode 100644 index 0000000..c4a4c90 --- /dev/null +++ b/pkg/readyz/readyz.go @@ -0,0 +1,23 @@ +package readyz + +import ( + "sync/atomic" +) + +// isSystemReady is declared as an atomic.Value to ensure thread safety. +var isSystemReady atomic.Value + +// init function initializes isSystemReady with its default value. +func init() { + isSystemReady.Store(false) // Initialize as the system is not ready. +} + +// IsReady returns the current readiness status of the system. +func IsReady() bool { + return isSystemReady.Load().(bool) +} + +// SetReady sets the readiness status of the system. +func SetReady(ready bool) { + isSystemReady.Store(ready) +} From 487f40d65c3ee9de63f5f3f789cb839113ac81f0 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 3 May 2024 15:56:17 +0900 Subject: [PATCH 2/2] Provide the API to check system readiness instead of health --- cmd/poc-mc-net-tf/main.go | 6 ++++++ pkg/api/rest/docs/docs.go | 32 ++++++++++++++++---------------- pkg/api/rest/docs/swagger.json | 32 ++++++++++++++++---------------- pkg/api/rest/docs/swagger.yaml | 30 +++++++++++++++--------------- pkg/api/rest/handlers/utility.go | 22 ++++++++++++++-------- pkg/api/rest/server/server.go | 19 ++++++++++++++----- 6 files changed, 81 insertions(+), 60 deletions(-) diff --git a/cmd/poc-mc-net-tf/main.go b/cmd/poc-mc-net-tf/main.go index 72d51c5..0c6b872 100644 --- a/cmd/poc-mc-net-tf/main.go +++ b/cmd/poc-mc-net-tf/main.go @@ -29,8 +29,14 @@ import ( // _ "github.com/mattn/go-sqlite3" restServer "github.com/cloud-barista/poc-mc-net-tf/pkg/api/rest/server" + + "github.com/cloud-barista/poc-mc-net-tf/pkg/readyz" ) +func init() { + readyz.SetReady(false) +} + func main() { log.Info().Msg("starting POC-MC-Net-TF server") diff --git a/pkg/api/rest/docs/docs.go b/pkg/api/rest/docs/docs.go index 8ee6a25..82dde47 100644 --- a/pkg/api/rest/docs/docs.go +++ b/pkg/api/rest/docs/docs.go @@ -23,9 +23,9 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { - "/health": { + "/httpVersion": { "get": { - "description": "Check API server is running", + "description": "Checks and logs the HTTP version of the incoming request to the server console.", "consumes": [ "application/json" ], @@ -35,7 +35,7 @@ const docTemplate = `{ "tags": [ "[System] Utility" ], - "summary": "Check API server is running", + "summary": "Check HTTP version of incoming request", "responses": { "200": { "description": "OK", @@ -43,8 +43,14 @@ const docTemplate = `{ "$ref": "#/definitions/models.ResponseText" } }, - "503": { - "description": "Service Unavailable", + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/models.ResponseText" + } + }, + "500": { + "description": "Internal Server Error", "schema": { "$ref": "#/definitions/models.ResponseText" } @@ -52,9 +58,9 @@ const docTemplate = `{ } } }, - "/httpVersion": { + "/readyz": { "get": { - "description": "Checks and logs the HTTP version of the incoming request to the server console.", + "description": "Check mc-net server is ready", "consumes": [ "application/json" ], @@ -64,7 +70,7 @@ const docTemplate = `{ "tags": [ "[System] Utility" ], - "summary": "Check HTTP version of incoming request", + "summary": "Check mc-net server is ready", "responses": { "200": { "description": "OK", @@ -72,14 +78,8 @@ const docTemplate = `{ "$ref": "#/definitions/models.ResponseText" } }, - "404": { - "description": "Not Found", - "schema": { - "$ref": "#/definitions/models.ResponseText" - } - }, - "500": { - "description": "Internal Server Error", + "503": { + "description": "Service Unavailable", "schema": { "$ref": "#/definitions/models.ResponseText" } diff --git a/pkg/api/rest/docs/swagger.json b/pkg/api/rest/docs/swagger.json index 070bab4..5a96914 100644 --- a/pkg/api/rest/docs/swagger.json +++ b/pkg/api/rest/docs/swagger.json @@ -16,9 +16,9 @@ }, "basePath": "/mc-net", "paths": { - "/health": { + "/httpVersion": { "get": { - "description": "Check API server is running", + "description": "Checks and logs the HTTP version of the incoming request to the server console.", "consumes": [ "application/json" ], @@ -28,7 +28,7 @@ "tags": [ "[System] Utility" ], - "summary": "Check API server is running", + "summary": "Check HTTP version of incoming request", "responses": { "200": { "description": "OK", @@ -36,8 +36,14 @@ "$ref": "#/definitions/models.ResponseText" } }, - "503": { - "description": "Service Unavailable", + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/models.ResponseText" + } + }, + "500": { + "description": "Internal Server Error", "schema": { "$ref": "#/definitions/models.ResponseText" } @@ -45,9 +51,9 @@ } } }, - "/httpVersion": { + "/readyz": { "get": { - "description": "Checks and logs the HTTP version of the incoming request to the server console.", + "description": "Check mc-net server is ready", "consumes": [ "application/json" ], @@ -57,7 +63,7 @@ "tags": [ "[System] Utility" ], - "summary": "Check HTTP version of incoming request", + "summary": "Check mc-net server is ready", "responses": { "200": { "description": "OK", @@ -65,14 +71,8 @@ "$ref": "#/definitions/models.ResponseText" } }, - "404": { - "description": "Not Found", - "schema": { - "$ref": "#/definitions/models.ResponseText" - } - }, - "500": { - "description": "Internal Server Error", + "503": { + "description": "Service Unavailable", "schema": { "$ref": "#/definitions/models.ResponseText" } diff --git a/pkg/api/rest/docs/swagger.yaml b/pkg/api/rest/docs/swagger.yaml index 61589b7..c9a6a24 100644 --- a/pkg/api/rest/docs/swagger.yaml +++ b/pkg/api/rest/docs/swagger.yaml @@ -197,11 +197,12 @@ info: title: POC-MC-Net-TF REST API version: latest paths: - /health: + /httpVersion: get: consumes: - application/json - description: Check API server is running + description: Checks and logs the HTTP version of the incoming request to the + server console. produces: - application/json responses: @@ -209,19 +210,22 @@ paths: description: OK schema: $ref: '#/definitions/models.ResponseText' - "503": - description: Service Unavailable + "404": + description: Not Found + schema: + $ref: '#/definitions/models.ResponseText' + "500": + description: Internal Server Error schema: $ref: '#/definitions/models.ResponseText' - summary: Check API server is running + summary: Check HTTP version of incoming request tags: - '[System] Utility' - /httpVersion: + /readyz: get: consumes: - application/json - description: Checks and logs the HTTP version of the incoming request to the - server console. + description: Check mc-net server is ready produces: - application/json responses: @@ -229,15 +233,11 @@ paths: description: OK schema: $ref: '#/definitions/models.ResponseText' - "404": - description: Not Found - schema: - $ref: '#/definitions/models.ResponseText' - "500": - description: Internal Server Error + "503": + description: Service Unavailable schema: $ref: '#/definitions/models.ResponseText' - summary: Check HTTP version of incoming request + summary: Check mc-net server is ready tags: - '[System] Utility' /rg/{resourceGroupId}: diff --git a/pkg/api/rest/handlers/utility.go b/pkg/api/rest/handlers/utility.go index 8af4ad2..2069d9f 100644 --- a/pkg/api/rest/handlers/utility.go +++ b/pkg/api/rest/handlers/utility.go @@ -17,24 +17,30 @@ import ( "net/http" "github.com/cloud-barista/poc-mc-net-tf/pkg/api/rest/models" + "github.com/cloud-barista/poc-mc-net-tf/pkg/readyz" "github.com/cloud-barista/poc-mc-net-tf/pkg/tofu" "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" ) -// Health godoc -// @Summary Check API server is running -// @Description Check API server is running +// Readyz func is for checking mc-net server is ready. +// Readyz godoc +// @Summary Check mc-net server is ready +// @Description Check mc-net server is ready // @Tags [System] Utility // @Accept json // @Produce json // @Success 200 {object} models.ResponseText // @Failure 503 {object} models.ResponseText -// @Router /health [get] -func Health(c echo.Context) error { - res := models.ResponseText{Success: true, Text: "POC-MC-Net-TF API server is running"} - - return c.JSON(http.StatusOK, res) +// @Router /readyz [get] +func Readyz(c echo.Context) error { + res := models.ResponseText{} + res.Text = "mc-net server is ready" + if !readyz.IsReady() { + res.Text = "mc-net server is NOT ready" + return c.JSON(http.StatusServiceUnavailable, &res) + } + return c.JSON(http.StatusOK, &res) } // HTTPVersion godoc diff --git a/pkg/api/rest/server/server.go b/pkg/api/rest/server/server.go index 8b1be9b..b45b90c 100644 --- a/pkg/api/rest/server/server.go +++ b/pkg/api/rest/server/server.go @@ -46,6 +46,8 @@ import ( // echo-swagger middleware _ "github.com/cloud-barista/poc-mc-net-tf/pkg/api/rest/docs" echoSwagger "github.com/swaggo/echo-swagger" + + "github.com/cloud-barista/poc-mc-net-tf/pkg/readyz" ) //var masterConfigInfos confighandler.MASTERCONFIGTYPE @@ -145,7 +147,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() == "/mc-net/health" || + if c.Path() == "/mc-net/readyz" || c.Path() == "/mc-net/httpVersion" { return true } @@ -170,10 +172,16 @@ func RunServer(port string) { fmt.Println("\n \n ") // Route for system management - e.GET("/mc-net/swagger/*", echoSwagger.WrapHandler) - + swaggerRedirect := func(c echo.Context) error { + return c.Redirect(http.StatusMovedPermanently, "/mc-net/api/index.html") + } + e.GET("/mc-net/api", swaggerRedirect) + e.GET("/mc-net/api/", swaggerRedirect) + e.GET("/mc-net/api/*", echoSwagger.WrapHandler) + // e.GET("/mc-net/swagger/*", echoSwagger.WrapHandler) // e.GET("/mc-net/swaggerActive", rest_common.RestGetSwagger) - e.GET("/mc-net/health", handlers.Health) + + e.GET("/mc-net/readyz", handlers.Readyz) e.GET("/mc-net/httpVersion", handlers.HTTPVersion) e.GET("/mc-net/tofuVersion", handlers.TofuVersion) @@ -189,7 +197,7 @@ func RunServer(port string) { route.RegisterSampleRoutes(groupSample) selfEndpoint := viper.GetString("self.endpoint") - apidashboard := " http://" + selfEndpoint + "/mc-net/swagger/index.html" + apidashboard := " http://" + selfEndpoint + "/mc-net/api" if enableAuth { fmt.Println(" Access to API dashboard" + " (username: " + apiUser + " / password: " + apiPass + ")") @@ -230,6 +238,7 @@ func RunServer(port string) { log.Info().Msg("starting POC-MC-Net-TF REST API server") port = fmt.Sprintf(":%s", port) + readyz.SetReady(true) if err := e.Start(port); err != nil && err != http.ErrServerClosed { e.Logger.Panic("shuttig down the server") }