-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathprobes.go
74 lines (61 loc) · 1.82 KB
/
probes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package kubernetes
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/gitpod-io/gitpod/common-go/log"
)
func NetworkIsReachableProbe(url string) func() error {
log.Infof("creating network check using URL %v", url)
return func() error {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyFromEnvironment,
}
client := &http.Client{
Transport: tr,
Timeout: 5 * time.Second,
// never follow redirects
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get(url)
if err != nil {
log.Errorf("unexpected error checking URL %v: %v", url, err)
return err
}
resp.Body.Close()
if resp.StatusCode > 499 {
log.WithField("url", url).WithField("statusCode", resp.StatusCode).WithError(err).Error("NetworkIsReachableProbe: unexpected status code checking URL")
return fmt.Errorf("returned status %d", resp.StatusCode)
}
return nil
}
}
func DNSCanResolveProbe(host string, timeout time.Duration) func() error {
log.Infof("creating DNS check for host %v", host)
// remove port if there is one
host = strings.Split(host, ":")[0]
resolver := net.Resolver{}
return func() error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
addrs, err := resolver.LookupHost(ctx, host)
if err != nil {
log.WithField("host", host).WithError(err).Error("NetworkIsReachableProbe: unexpected error resolving host")
return err
}
if len(addrs) < 1 {
return fmt.Errorf("could not resolve host")
}
return nil
}
}