-
Notifications
You must be signed in to change notification settings - Fork 82
/
health.go
232 lines (213 loc) · 6.19 KB
/
health.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package eks
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/eks"
"go.uber.org/zap"
"k8s.io/utils/exec"
)
func (ts *Tester) checkHealth() (err error) {
defer func() {
if err == nil {
ts.cfg.RecordStatus(eks.ClusterStatusActive)
}
}()
// might take several minutes for DNS to propagate
waitDur := 5 * time.Minute
retryStart := time.Now()
for time.Now().Sub(retryStart) < waitDur {
select {
case <-ts.stopCreationCh:
return errors.New("health check aborted")
case <-ts.interruptSig:
return errors.New("health check aborted")
case <-time.After(5 * time.Second):
}
err = ts.health()
if err == nil {
break
}
ts.lg.Warn("health check failed", zap.Error(err))
ts.cfg.RecordStatus(fmt.Sprintf("health check failed (%v)", err))
}
ts.lg.Info("health check success")
return err
}
func (ts *Tester) health() error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
output, err := exec.New().CommandContext(
ctx,
ts.cfg.KubectlPath,
"--kubeconfig="+ts.cfg.KubeConfigPath,
"version",
).CombinedOutput()
cancel()
out := string(output)
if err != nil {
return fmt.Errorf("'kubectl version' failed %v (output %q)", err, out)
}
fmt.Printf("\n\"kubectl version\" output:\n%s\n", out)
ep := ts.cfg.Status.ClusterAPIServerEndpoint + "/version"
buf := bytes.NewBuffer(nil)
if err = httpReadInsecure(ts.lg, ep, buf); err != nil {
return err
}
out = buf.String()
if !strings.Contains(out, fmt.Sprintf(`"gitVersion": "v%s`, ts.cfg.Parameters.Version)) {
return fmt.Errorf("%q does not contain version %q", out, ts.cfg.Parameters.Version)
}
fmt.Printf("\n\n\"%s\" output:\n%s\n", ep, out)
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
output, err = exec.New().CommandContext(
ctx,
ts.cfg.KubectlPath,
"--kubeconfig="+ts.cfg.KubeConfigPath,
"cluster-info",
).CombinedOutput()
cancel()
out = string(output)
if err != nil {
return fmt.Errorf("'kubectl cluster-info' failed %v (output %q)", err, out)
}
if !strings.Contains(out, "is running at") {
return fmt.Errorf("'kubectl cluster-info' not ready (output %q)", out)
}
fmt.Printf("\n\"kubectl cluster-info\" output:\n%s\n", out)
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
output, err = exec.New().CommandContext(
ctx,
ts.cfg.KubectlPath,
"--kubeconfig="+ts.cfg.KubeConfigPath,
"get",
"cs",
).CombinedOutput()
cancel()
out = string(output)
if err != nil {
return fmt.Errorf("'kubectl get cs' failed %v (output %q)", err, out)
}
fmt.Printf("\n\"kubectl get cs\" output:\n%s\n", out)
ep = ts.cfg.Status.ClusterAPIServerEndpoint + "/healthz?verbose"
buf.Reset()
if err := httpReadInsecure(ts.lg, ep, buf); err != nil {
return err
}
out = buf.String()
if !strings.Contains(out, "healthz check passed") {
return fmt.Errorf("%q does not contain 'healthz check passed'", out)
}
fmt.Printf("\n\n\"%s\" output:\n%s\n", ep, out)
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
output, err = exec.New().CommandContext(
ctx,
ts.cfg.KubectlPath,
"--kubeconfig="+ts.cfg.KubeConfigPath,
"--namespace=kube-system",
"get",
"all",
).CombinedOutput()
cancel()
out = string(output)
if err != nil {
return fmt.Errorf("'kubectl get all -n=kube-system' failed %v (output %q)", err, out)
}
fmt.Printf("\n\"kubectl all -n=kube-system\" output:\n%s\n", out)
fmt.Printf("\n\"kubectl get pods -n=kube-system\" output:\n")
pods, err := ts.getPods("kube-system")
if err != nil {
return fmt.Errorf("failed to get pods %v", err)
}
for _, v := range pods.Items {
fmt.Printf("kube-system Pod: %q\n", v.Name)
}
println()
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
output, err = exec.New().CommandContext(
ctx,
ts.cfg.KubectlPath,
"--kubeconfig="+ts.cfg.KubeConfigPath,
"get",
"configmaps",
"--all-namespaces",
).CombinedOutput()
cancel()
out = string(output)
if err != nil {
return fmt.Errorf("'kubectl get configmaps --all-namespaces' failed %v (output %q)", err, out)
}
fmt.Printf("\n\"kubectl get configmaps --all-namespaces\" output:\n%s\n", out)
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
output, err = exec.New().CommandContext(
ctx,
ts.cfg.KubectlPath,
"--kubeconfig="+ts.cfg.KubeConfigPath,
"get",
"namespaces",
).CombinedOutput()
cancel()
out = string(output)
if err != nil {
return fmt.Errorf("'kubectl get namespaces' failed %v (output %q)", err, out)
}
fmt.Printf("\n\"kubectl get namespaces\" output:\n%s\n", out)
fmt.Printf("\n\"curl -sL http://localhost:8080/metrics | grep storage_\" output:\n")
output, err = ts.k8sClientSet.
CoreV1().
RESTClient().
Get().
RequestURI("/metrics").
Do().
Raw()
if err != nil {
return fmt.Errorf("failed to fetch /metrics (%v)", err)
}
const (
metricDEKGen = "apiserver_storage_data_key_generation_latencies_microseconds_count"
metricEnvelopeCacheMiss = "apiserver_storage_envelope_transformation_cache_misses_total"
)
dekGenCnt, cacheMissCnt := int64(0), int64(0)
scanner := bufio.NewScanner(bytes.NewReader(output))
for scanner.Scan() {
line := scanner.Text()
switch {
case strings.HasPrefix(line, "# "):
continue
case strings.HasPrefix(line, metricDEKGen+" "):
vs := strings.TrimSpace(strings.Replace(line, metricDEKGen, "", -1))
dekGenCnt, err = strconv.ParseInt(vs, 10, 64)
if err != nil {
ts.lg.Warn("failed to parse",
zap.String("line", line),
zap.Error(err),
)
}
case strings.HasPrefix(line, metricEnvelopeCacheMiss+" "):
vs := strings.TrimSpace(strings.Replace(line, metricEnvelopeCacheMiss, "", -1))
cacheMissCnt, err = strconv.ParseInt(vs, 10, 64)
if err != nil {
ts.lg.Warn("failed to parse",
zap.String("line", line),
zap.Error(err),
)
}
}
if dekGenCnt > 0 || cacheMissCnt > 0 {
break
}
}
ts.lg.Info("encryption metrics",
zap.Int64("dek-gen-count", dekGenCnt),
zap.Int64("cache-miss-count", cacheMissCnt),
)
if ts.cfg.Parameters.EncryptionCMKARN != "" && dekGenCnt <= 0 && cacheMissCnt <= 0 {
return errors.New("encrypted enabled, unexpected /metrics")
}
ts.lg.Info("checked /metrics")
return nil
}