| 21 | } |
| 22 | |
| 23 | func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLReportOptions) { |
| 24 | ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 25 | defer cancel() |
| 26 | |
| 27 | r.Severity = health.SeverityOK |
| 28 | r.Warnings = []health.Message{} |
| 29 | r.Dismissed = opts.Dismissed |
| 30 | |
| 31 | if opts.AccessURL == nil { |
| 32 | r.Error = health.Errorf(health.CodeAccessURLNotSet, "Access URL not set") |
| 33 | r.Severity = health.SeverityError |
| 34 | return |
| 35 | } |
| 36 | r.AccessURL = opts.AccessURL.String() |
| 37 | |
| 38 | if opts.Client == nil { |
| 39 | opts.Client = http.DefaultClient |
| 40 | } |
| 41 | |
| 42 | accessURL, err := opts.AccessURL.Parse("/healthz") |
| 43 | if err != nil { |
| 44 | r.Error = health.Errorf(health.CodeAccessURLInvalid, "parse healthz endpoint: %s", err) |
| 45 | r.Severity = health.SeverityError |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | req, err := http.NewRequestWithContext(ctx, "GET", accessURL.String(), nil) |
| 50 | if err != nil { |
| 51 | r.Error = health.Errorf(health.CodeAccessURLFetch, "create healthz request: %s", err) |
| 52 | r.Severity = health.SeverityError |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | res, err := opts.Client.Do(req) |
| 57 | if err != nil { |
| 58 | r.Error = health.Errorf(health.CodeAccessURLFetch, "get healthz endpoint: %s", err) |
| 59 | r.Severity = health.SeverityError |
| 60 | return |
| 61 | } |
| 62 | defer res.Body.Close() |
| 63 | |
| 64 | body, err := io.ReadAll(res.Body) |
| 65 | if err != nil { |
| 66 | r.Error = health.Errorf(health.CodeAccessURLFetch, "read healthz response: %s", err) |
| 67 | r.Severity = health.SeverityError |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | r.Reachable = true |
| 72 | r.Healthy = res.StatusCode == http.StatusOK |
| 73 | r.StatusCode = res.StatusCode |
| 74 | if res.StatusCode != http.StatusOK { |
| 75 | r.Severity = health.SeverityWarning |
| 76 | r.Warnings = append(r.Warnings, health.Messagef(health.CodeAccessURLNotOK, "/healthz did not return 200 OK")) |
| 77 | } |
| 78 | r.HealthzResponse = string(body) |
| 79 | } |