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