()
| 175 | } |
| 176 | |
| 177 | func (p *Probe) httpProbe() error { |
| 178 | // to mimic k8s probe functionality |
| 179 | targetHost := p.HTTPGet.Host |
| 180 | if p.HTTPGet.Host == "" { |
| 181 | targetHost = "localhost" |
| 182 | } |
| 183 | |
| 184 | targetURL := s.EnsurePrefix( |
| 185 | net.JoinHostPort(targetHost, p.HTTPGet.Port.String())+s.EnsurePrefix(p.HTTPGet.Path, "/"), |
| 186 | "http://", |
| 187 | ) |
| 188 | |
| 189 | httpClient := &http.Client{ |
| 190 | Timeout: time.Duration(p.TimeoutSeconds) * time.Second, |
| 191 | } |
| 192 | req, err := http.NewRequest(http.MethodGet, targetURL, nil) |
| 193 | if err != nil { |
| 194 | return err |
| 195 | } |
| 196 | |
| 197 | req.Header.Add(consts.UserAgentKey, consts.KubeProbeUserAgentPrefix) |
| 198 | |
| 199 | for _, header := range p.HTTPGet.HTTPHeaders { |
| 200 | req.Header.Add(header.Name, header.Value) |
| 201 | } |
| 202 | |
| 203 | res, err := httpClient.Do(req) |
| 204 | if err != nil { |
| 205 | return err |
| 206 | } |
| 207 | |
| 208 | defer func() { |
| 209 | // Ensure body is both read _and_ closed so it can be reused for keep-alive. |
| 210 | // No point handling errors, connection just won't be reused. |
| 211 | _, _ = io.Copy(ioutil.Discard, res.Body) |
| 212 | _ = res.Body.Close() |
| 213 | }() |
| 214 | |
| 215 | // response status code between 200-399 indicates success |
| 216 | if !(res.StatusCode >= 200 && res.StatusCode < 400) { |
| 217 | return fmt.Errorf("HTTP probe did not respond Ready, got status code: %d", res.StatusCode) |
| 218 | } |
| 219 | |
| 220 | return nil |
| 221 | } |
| 222 | |
| 223 | func (p *Probe) tcpProbe() error { |
| 224 | // to mimic k8s probe functionality |
no test coverage detected