/
health_probers.go
217 lines (192 loc) · 5.74 KB
/
health_probers.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package health
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"os/exec"
"strconv"
"time"
"istio.io/api/networking/v1alpha3"
"istio.io/istio/pilot/cmd/pilot-agent/status"
"istio.io/istio/pilot/cmd/pilot-agent/status/ready"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/test/echo/common/scheme"
)
var healthCheckLog = log.RegisterScope("healthcheck", "Health Checks performed by Istio-Agent")
type Prober interface {
// Probe will healthcheck and return whether or not the target is healthy.
// If an error returned is not nil, it is assumed that the process could
// not complete, and Probe() was unable to determine whether or not the
// target was healthy.
Probe(timeout time.Duration) (ProbeResult, error)
}
type ProbeResult string
const (
Healthy ProbeResult = "HEALTHY"
Unhealthy ProbeResult = "UNHEALTHY"
Unknown ProbeResult = "UNKNOWN"
)
func (p *ProbeResult) IsHealthy() bool {
return *p == Healthy
}
type HTTPProber struct {
Config *v1alpha3.HTTPHealthCheckConfig
Transport *http.Transport
}
var _ Prober = &HTTPProber{}
func NewHTTPProber(cfg *v1alpha3.HTTPHealthCheckConfig, ipv6 bool) *HTTPProber {
h := new(HTTPProber)
h.Config = cfg
// Create an http.Transport with TLSClientConfig for HTTPProber if the scheme is https,
// otherwise set up an empty one.
if cfg.Scheme == string(scheme.HTTPS) {
// nolint: gosec
// This is matching Kubernetes. It is a reasonable usage of this, as it is just a health check over localhost.
h.Transport = &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
} else {
h.Transport = &http.Transport{
DisableKeepAlives: true,
}
}
d := status.ProbeDialer()
d.LocalAddr = status.UpstreamLocalAddressIPv4
if ipv6 {
d.LocalAddr = status.UpstreamLocalAddressIPv6
}
h.Transport.DialContext = d.DialContext
return h
}
// HttpProber_Probe will return whether or not the target is healthy (true -> healthy)
// by making an HTTP Get response.
func (h *HTTPProber) Probe(timeout time.Duration) (ProbeResult, error) {
client := &http.Client{
Timeout: timeout,
Transport: h.Transport,
}
// transform crd into net http header
headers := make(http.Header)
for _, val := range h.Config.HttpHeaders {
// net.httpHeaders value is a []string but uses only index 0
headers[val.Name] = append(headers[val.Name], val.Value)
}
targetURL, err := url.Parse(h.Config.Path)
// Something is busted with the path, but it's too late to reject it. Pass it along as is.
if err != nil {
targetURL = &url.URL{
Path: h.Config.Path,
}
}
targetURL.Scheme = h.Config.Scheme
targetURL.Host = net.JoinHostPort(h.Config.Host, strconv.Itoa(int(h.Config.Port)))
if err != nil {
healthCheckLog.Errorf("unable to parse url: %v", err)
return Unknown, err
}
req, err := http.NewRequest(http.MethodGet, targetURL.String(), nil)
if err != nil {
return Unknown, err
}
req.Header = headers
if headers.Get("Host") != "" {
req.Host = headers.Get("Host")
}
if _, ok := headers["User-Agent"]; !ok {
// explicitly set User-Agent so it's not set to default Go value. K8s use kube-probe.
headers.Set("User-Agent", "istio-probe/1.0")
}
res, err := client.Do(req)
// if we were unable to connect, count as failure
if err != nil {
return Unhealthy, err
}
defer func() {
err = res.Body.Close()
if err != nil {
healthCheckLog.Error(err)
}
}()
// from [200,400)
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
return Healthy, nil
}
return Unhealthy, fmt.Errorf("status code was not from [200,400), bad code %v", res.StatusCode)
}
type TCPProber struct {
Config *v1alpha3.TCPHealthCheckConfig
}
var _ Prober = &TCPProber{}
func (t *TCPProber) Probe(timeout time.Duration) (ProbeResult, error) {
// if we cant connect, count as fail
d := status.ProbeDialer()
d.Timeout = timeout
hostPort := net.JoinHostPort(t.Config.Host, strconv.Itoa(int(t.Config.Port)))
conn, err := d.Dial("tcp", hostPort)
if err != nil {
return Unhealthy, err
}
err = conn.Close()
if err != nil {
healthCheckLog.Errorf("Unable to close TCP Socket: %v", err)
}
return Healthy, nil
}
type ExecProber struct {
Config *v1alpha3.ExecHealthCheckConfig
}
var _ Prober = &ExecProber{}
func (e *ExecProber) Probe(timeout time.Duration) (ProbeResult, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, e.Config.Command[0], e.Config.Command[1:]...)
if err := cmd.Run(); err != nil {
select {
case <-ctx.Done():
return Unhealthy, fmt.Errorf("command timeout exceeded: %v", err)
default:
}
return Unhealthy, err
}
return Healthy, nil
}
type EnvoyProber struct {
Config ready.Prober
}
var _ Prober = &EnvoyProber{}
func (a EnvoyProber) Probe(time.Duration) (ProbeResult, error) {
if err := a.Config.Check(); err != nil {
return Unhealthy, err
}
return Healthy, nil
}
type AggregateProber struct {
Probes []Prober
}
var _ Prober = &AggregateProber{}
func (a AggregateProber) Probe(timeout time.Duration) (ProbeResult, error) {
for _, probe := range a.Probes {
res, err := probe.Probe(timeout)
if err != nil || !res.IsHealthy() {
return res, err
}
}
return Healthy, nil
}