| 306 | } |
| 307 | |
| 308 | func (p *HTTPReadinessProbe) Ready(runnable Runnable) (err error) { |
| 309 | endpoint := runnable.Endpoint(p.portName) |
| 310 | if endpoint == "" { |
| 311 | return errors.Newf("cannot get service endpoint for port %s", p.portName) |
| 312 | } |
| 313 | if endpoint == "stopped" { |
| 314 | return errors.New("service has stopped") |
| 315 | } |
| 316 | |
| 317 | httpClient := &http.Client{Timeout: 1 * time.Second} |
| 318 | if p.scheme == "HTTPS" { |
| 319 | httpClient.Transport = &http.Transport{ |
| 320 | TLSClientConfig: &tls.Config{ |
| 321 | InsecureSkipVerify: true, |
| 322 | }, |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | res, err := httpClient.Get(p.scheme + "://" + endpoint + p.path) |
| 327 | if err != nil { |
| 328 | return err |
| 329 | } |
| 330 | defer errcapture.ExhaustClose(&err, res.Body, "response readiness") |
| 331 | |
| 332 | body, _ := io.ReadAll(res.Body) |
| 333 | if res.StatusCode < p.expectedStatusRangeStart || res.StatusCode > p.expectedStatusRangeEnd { |
| 334 | return errors.Newf("expected code in range: [%v, %v], got status code: %v and body: %v", p.expectedStatusRangeStart, p.expectedStatusRangeEnd, res.StatusCode, string(body)) |
| 335 | } |
| 336 | |
| 337 | for _, expected := range p.expectedContent { |
| 338 | if !strings.Contains(string(body), expected) { |
| 339 | return errors.Newf("expected body containing %s, got: %v", expected, string(body)) |
| 340 | } |
| 341 | } |
| 342 | return nil |
| 343 | } |
| 344 | |
| 345 | // TCPReadinessProbe checks readiness by ensure a TCP connection can be established. |
| 346 | type TCPReadinessProbe struct { |