Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/groundcontrol/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/container-registry/harbor-satellite/internal/env"
"github.com/container-registry/harbor-satellite/internal/groundcontrol/harborhealth"
"github.com/container-registry/harbor-satellite/internal/groundcontrol/harbor"
"github.com/container-registry/harbor-satellite/internal/groundcontrol/migrator"
"github.com/container-registry/harbor-satellite/internal/groundcontrol/server"
"github.com/joho/godotenv"
Expand All @@ -25,7 +25,7 @@ func main() {
log.Fatalf("failed to load environment: %v", err)
}

err := harborhealth.CheckHealth()
err := harbor.CheckHealth()
if err != nil {
log.Fatalf("health check failed: %v", err)
}
Expand Down
87 changes: 87 additions & 0 deletions internal/groundcontrol/harbor/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package harbor

import (
"context"
"fmt"
"log"
"net/url"
"time"

"github.com/container-registry/harbor-satellite/internal/env"
v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/health"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
)

const defaultHealthCheckTimeout = 5 * time.Second

var ignoredHealthComponents = map[string]struct{}{
"portal": {},
"trivy": {},
"registryctl": {},
"jobservice": {},
}

// CheckHealth verifies that all required Harbor components are healthy.
func CheckHealth() error {
if env.GC.Harbor.SkipHealthCheck {
log.Println("WARNING: Harbor health check skipped (SKIP_HARBOR_HEALTH_CHECK=true)")
return nil
}

client, err := newHealthClient(env.GC.Harbor.URL)
if err != nil {
return fmt.Errorf("create Harbor client: %w", err)
}
return checkHealth(client.Health)
}

func newHealthClient(rawURL string) (*v2client.HarborAPI, error) {
harborURL, err := url.Parse(rawURL)
if err != nil {
return nil, err
}

harborURL.Path = v2client.DefaultBasePath
Comment thread
vg006 marked this conversation as resolved.

return v2client.New(v2client.Config{URL: harborURL}), nil
}

func checkHealth(client health.API) error {
params := health.NewGetHealthParamsWithTimeout(defaultHealthCheckTimeout)
response, err := client.GetHealth(context.Background(), params)
if err != nil {
return fmt.Errorf("failed to get Harbor health: %w", err)
}
if response == nil || response.Payload == nil {
return fmt.Errorf("failed to get Harbor health: empty response")
}

unhealthy := getUnhealthyComponents(response.Payload.Components, ignoredHealthComponents)
if len(unhealthy) > 0 {
return fmt.Errorf("unhealthy components: %v", unhealthy)
}

return nil
}

func getUnhealthyComponents(components []*models.ComponentHealthStatus, ignored map[string]struct{}) []string {
var unhealthy []string
for _, component := range components {
if component == nil {
continue
}
if _, ignore := ignored[component.Name]; ignore {
continue
}
if component.Status != "healthy" {
entry := component.Name
if component.Error != "" {
entry += ": " + component.Error
}
unhealthy = append(unhealthy, entry)
}
}

return unhealthy
}
85 changes: 0 additions & 85 deletions internal/groundcontrol/harborhealth/check.go

This file was deleted.

29 changes: 0 additions & 29 deletions internal/groundcontrol/harborhealth/types.go

This file was deleted.

Loading