diff --git a/components/workspace-rollout-job/cmd/root.go b/components/workspace-rollout-job/cmd/root.go index 3b0c9216c41044..92ac1fbe56f37d 100644 --- a/components/workspace-rollout-job/cmd/root.go +++ b/components/workspace-rollout-job/cmd/root.go @@ -99,6 +99,13 @@ var rootCmd = &cobra.Command{ return err } + // Check if prometheus is reachable + err = analysis.CheckPrometheusReachable(ctx, conf.prometheusURL) + if err != nil { + log.WithError(err).Fatal("init: prometheus is not reachable") + return err + } + prometheusAnalyzer, err := analysis.NewWorkspaceKeyMetricsAnalyzer(ctx, config, conf.prometheusURL, conf.targetPositivePercentage, 30305) if err != nil { log.WithError(err).Fatal("failed to create a prometheus client") diff --git a/components/workspace-rollout-job/pkg/analysis/analyzer.go b/components/workspace-rollout-job/pkg/analysis/analyzer.go index 0b4094f728f8c2..afad0ae6aa1859 100644 --- a/components/workspace-rollout-job/pkg/analysis/analyzer.go +++ b/components/workspace-rollout-job/pkg/analysis/analyzer.go @@ -6,6 +6,10 @@ package analysis import ( "context" + "time" + + "github.com/prometheus/client_golang/api" + v1 "github.com/prometheus/client_golang/api/prometheus/v1" ) type Decision int @@ -21,3 +25,24 @@ type Analyzer interface { // repeatedly to determine whether to move forward on the rollout or not. MoveForward(ctx context.Context, clusterName string) (Decision, error) } + +func CheckPrometheusReachable(ctx context.Context, prometheusURL string) error { + if prometheusURL == "" { + return nil + } + + client, err := api.NewClient(api.Config{ + Address: prometheusURL, + }) + if err != nil { + return err + } + + v1Client := v1.NewAPI(client) + _, _, err = v1Client.Query(ctx, "up", time.Now()) + if err != nil { + return err + } + + return nil +}