From ff9b51563044e9018b7d5321e6ac4ba0ade8cf43 Mon Sep 17 00:00:00 2001 From: He Weiwei Date: Sat, 23 Jan 2021 05:44:41 +0000 Subject: [PATCH] feat: add health checker for trivy when it's enabled Closes #14055 Signed-off-by: He Weiwei --- src/core/api/health.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/core/api/health.go b/src/core/api/health.go index 2e19bc950..adb12095a 100644 --- a/src/core/api/health.go +++ b/src/core/api/health.go @@ -19,6 +19,8 @@ import ( "fmt" "io/ioutil" "net/http" + "sort" + "strings" "sync" "time" @@ -77,6 +79,9 @@ func (h *HealthAPI) CheckHealth() { } components = append(components, componentStatus) } + + sort.Slice(components, func(i, j int) bool { return components[i].Name < components[j].Name }) + status := &overallHealthStatus{} status.Status = isHealthy.String() status.Components = components @@ -269,6 +274,14 @@ func redisHealthChecker() health.Checker { return PeriodicHealthChecker(checker, period) } +func trivyHealthChecker() health.Checker { + url := strings.TrimSuffix(config.TrivyAdapterURL(), "/") + "/probe/healthy" + timeout := 60 * time.Second + period := 10 * time.Second + checker := HTTPStatusCodeHealthChecker(http.MethodGet, url, nil, timeout, http.StatusOK) + return PeriodicHealthChecker(checker, period) +} + func registerHealthCheckers() { HealthCheckerRegistry["core"] = coreHealthChecker() HealthCheckerRegistry["portal"] = portalHealthChecker() @@ -283,6 +296,9 @@ func registerHealthCheckers() { if config.WithNotary() { HealthCheckerRegistry["notary"] = notaryHealthChecker() } + if config.WithTrivy() { + HealthCheckerRegistry["trivy"] = trivyHealthChecker() + } } func getRegistryURL() string {