Skip to content

Commit

Permalink
agent: Improve health check API
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed May 24, 2024
1 parent a58962c commit fa6aab5
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 15 deletions.
5 changes: 3 additions & 2 deletions agent/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ windows: lint swag ## Build the binary file for Windows
@echo Build finished!

run: ## Run the built binary
@sudo killall ${MODULE_NAME} | true
@git diff > .diff_current
@STATUS=`diff .diff_last_build .diff_current 2>&1 > /dev/null; echo $$?` && \
GIT_HASH_MINE=`git rev-parse HEAD` && \
Expand All @@ -113,9 +114,9 @@ run: ## Run the built binary
@@kernel_name=`uname -s` && \
cp -RpPf conf cmd/${MODULE_NAME}/ && \
if [[ $$kernel_name == "CYGWIN"* ]] || [[ $$kernel_name == "MINGW"* ]]; then \
./cmd/${MODULE_NAME}/${MODULE_NAME}.exe; \
./cmd/${MODULE_NAME}/${MODULE_NAME}.exe & \
else \
./cmd/${MODULE_NAME}/${MODULE_NAME} || echo "Trying with sudo..." && sudo ./cmd/${MODULE_NAME}/${MODULE_NAME}; \
./cmd/${MODULE_NAME}/${MODULE_NAME} || echo "Trying with sudo..." && sudo ./cmd/${MODULE_NAME}/${MODULE_NAME} & \
fi

run_docker: ## Run the built binary within Docker
Expand Down
17 changes: 16 additions & 1 deletion agent/cmd/cm-honeybee-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"github.com/cloud-barista/cm-honeybee/agent/common"
"github.com/cloud-barista/cm-honeybee/agent/lib/config"
"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/controller"
"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/server"
"github.com/jollaman999/utils/logger"
"github.com/jollaman999/utils/syscheck"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)

Expand All @@ -36,7 +38,20 @@ func init() {

logger.Println(logger.INFO, false, "Agent UUID: "+common.AgentUUID)

server.Init()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer func() {
wg.Done()
}()
controller.OkMessage.Message = "API server is not ready"
server.Init()
}()

controller.OkMessage.Message = "CM-Honeybee Agent is ready"
controller.IsReady = true

wg.Wait()
}

func end() {
Expand Down
31 changes: 20 additions & 11 deletions agent/pkg/api/rest/controller/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@ type SimpleMsg struct {
Message string `json:"message"`
}

// GetHealth func is for checking Honeybee Agent health.
// @Summary Check Honeybee Agent is alive
// @Description Check Honeybee Agent is alive
var OkMessage = SimpleMsg{}
var IsReady = false

// CheckReady func is for checking Honeybee Agent health.
// @Summary Check Ready
// @Description Check Honeybee Agent is ready
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Success 200 {object} SimpleMsg "Successfully get heath state."
// @Failure 500 {object} common.ErrorResponse "Failed to check health."
func GetHealth(c echo.Context) error {
okMessage := SimpleMsg{}
okMessage.Message = "CM-Honeybee Agent is running"
return c.JSONPretty(http.StatusOK, &okMessage, " ")
// @Accept json
// @Produce json
// @Success 200 {object} SimpleMsg "Successfully get ready state."
// @Failure 500 {object} common.ErrorResponse "Failed to check ready state."
//
// @Router /honeybee-agent/readyz [get]
func CheckReady(c echo.Context) error {
status := http.StatusOK

if !IsReady {
status = http.StatusServiceUnavailable
}

return c.JSONPretty(status, &OkMessage, " ")
}
29 changes: 29 additions & 0 deletions agent/pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/honeybee-agent/readyz": {
"get": {
"description": "Check Honeybee Agent is ready",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System management"
],
"summary": "Check Ready",
"responses": {
"200": {
"description": "Successfully get ready state.",
"schema": {
"$ref": "#/definitions/pkg_api_rest_controller.SimpleMsg"
}
},
"500": {
"description": "Failed to check ready state.",
"schema": {
"$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_agent_pkg_api_rest_common.ErrorResponse"
}
}
}
}
},
"/infra": {
"get": {
"description": "Get infra information.",
Expand Down
29 changes: 29 additions & 0 deletions agent/pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@
"contact": {}
},
"paths": {
"/honeybee-agent/readyz": {
"get": {
"description": "Check Honeybee Agent is ready",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System management"
],
"summary": "Check Ready",
"responses": {
"200": {
"description": "Successfully get ready state.",
"schema": {
"$ref": "#/definitions/pkg_api_rest_controller.SimpleMsg"
}
},
"500": {
"description": "Failed to check ready state.",
"schema": {
"$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_agent_pkg_api_rest_common.ErrorResponse"
}
}
}
}
},
"/infra": {
"get": {
"description": "Get infra information.",
Expand Down
19 changes: 19 additions & 0 deletions agent/pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,25 @@ definitions:
info:
contact: {}
paths:
/honeybee-agent/readyz:
get:
consumes:
- application/json
description: Check Honeybee Agent is ready
produces:
- application/json
responses:
"200":
description: Successfully get ready state.
schema:
$ref: '#/definitions/pkg_api_rest_controller.SimpleMsg'
"500":
description: Failed to check ready state.
schema:
$ref: '#/definitions/github_com_cloud-barista_cm-honeybee_agent_pkg_api_rest_common.ErrorResponse'
summary: Check Ready
tags:
- '[Admin] System management'
/infra:
get:
consumes:
Expand Down
2 changes: 1 addition & 1 deletion agent/pkg/api/rest/route/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
)

func RegisterUtility(e *echo.Echo) {
e.GET("/"+strings.ToLower(common.ShortModuleName)+"/health", controller.GetHealth)
e.GET("/"+strings.ToLower(common.ShortModuleName)+"/readyz", controller.CheckReady)
}

0 comments on commit fa6aab5

Please sign in to comment.