/
tf_controller_health_checks.go
229 lines (202 loc) · 7.35 KB
/
tf_controller_health_checks.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
package controllers
import (
"bytes"
"context"
"fmt"
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"io"
"net"
"net/http"
"net/url"
"text/template"
"time"
"github.com/fluxcd/pkg/runtime/logger"
infrav1 "github.com/weaveworks/tf-controller/api/v1alpha2"
"github.com/weaveworks/tf-controller/runner"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
)
func (r *TerraformReconciler) shouldDoHealthChecks(terraform infrav1.Terraform) bool {
if terraform.Spec.HealthChecks == nil || len(terraform.Spec.HealthChecks) < 1 {
return false
}
var applyCondition metav1.Condition
var hcCondition metav1.Condition
for _, c := range terraform.Status.Conditions {
if c.Type == infrav1.ConditionTypeApply {
applyCondition = c
} else if c.Type == infrav1.ConditionTypeHealthCheck {
hcCondition = c
}
}
// health checks were previously performed but failed
// do health check again
if hcCondition.Reason == infrav1.HealthChecksFailedReason {
return true
}
// terraform was applied and no health check performed yet
// do health check
if applyCondition.Reason == infrav1.TFExecApplySucceedReason &&
hcCondition.Reason == "" {
return true
}
return false
}
func (r *TerraformReconciler) doHealthChecks(ctx context.Context, terraform infrav1.Terraform, revision string, runnerClient runner.RunnerClient) (infrav1.Terraform, error) {
log := ctrl.LoggerFrom(ctx)
traceLog := log.V(logger.TraceLevel).WithValues("function", "TerraformReconciler.doHealthChecks")
log.Info("calling doHealthChecks ...")
// get terraform output data for health check urls
traceLog.Info("Create a map for outputs")
outputs := make(map[string]string)
traceLog.Info("Check for a name for our outputs secret")
if terraform.Spec.WriteOutputsToSecret != nil && terraform.Spec.WriteOutputsToSecret.Name != "" {
traceLog.Info("Get outputs from the runner")
getOutputsReply, err := runnerClient.GetOutputs(ctx, &runner.GetOutputsRequest{
Namespace: terraform.Namespace,
SecretName: terraform.Spec.WriteOutputsToSecret.Name,
})
traceLog.Info("Check for an error")
if err != nil {
err = fmt.Errorf("error getting terraform output for health checks: %s", err)
traceLog.Error(err, "Hit an error")
return infrav1.TerraformHealthCheckFailed(
terraform,
err.Error(),
), err
}
traceLog.Info("Set outputs")
outputs = getOutputsReply.Outputs
}
traceLog.Info("Loop over the health checks")
for _, hc := range terraform.Spec.HealthChecks {
// perform health check based on type
traceLog.Info("Check the health check type")
switch hc.Type {
case infrav1.HealthCheckTypeTCP:
traceLog = traceLog.WithValues("health-check-type", infrav1.HealthCheckTypeTCP)
traceLog.Info("Parse Address and outputs into a template")
parsed, err := r.parseHealthCheckTemplate(outputs, hc.Address)
traceLog.Info("Check for an error")
if err != nil {
err = fmt.Errorf("error getting terraform output for health checks: %s", err)
traceLog.Error(err, "Hit an error")
return infrav1.TerraformHealthCheckFailed(
terraform,
err.Error(),
), err
}
traceLog.Info("Run TCP health check and check for an error")
if err := r.doTCPHealthCheck(ctx, hc.Name, parsed, hc.GetTimeout()); err != nil {
traceLog.Error(err, "Hit an error")
msg := fmt.Sprintf("TCP health check error: %s, url: %s", hc.Name, hc.Address)
traceLog.Info("Record an event")
r.event(ctx, terraform, revision, eventv1.EventSeverityError, msg, nil)
traceLog.Info("Return failed health check")
return infrav1.TerraformHealthCheckFailed(
terraform,
err.Error(),
), err
}
case infrav1.HealthCheckTypeHttpGet:
traceLog = traceLog.WithValues("health-check-type", infrav1.HealthCheckTypeHttpGet)
traceLog.Info("Parse Address and outputs into a template")
parsed, err := r.parseHealthCheckTemplate(outputs, hc.URL)
traceLog.Info("Check for an error")
if err != nil {
err = fmt.Errorf("error getting terraform output for health checks: %s", err)
traceLog.Error(err, "Hit an error")
return infrav1.TerraformHealthCheckFailed(
terraform,
err.Error(),
), err
}
traceLog.Info("Run HTTP health check and check for an error")
if err := r.doHTTPHealthCheck(ctx, hc.Name, parsed, hc.GetTimeout()); err != nil {
traceLog.Error(err, "Hit an error")
msg := fmt.Sprintf("HTTP health check error: %s, url: %s", hc.Name, hc.URL)
traceLog.Info("Record an event")
r.event(ctx, terraform, revision, eventv1.EventSeverityError, msg, nil)
traceLog.Info("Return failed health check")
return infrav1.TerraformHealthCheckFailed(
terraform,
err.Error(),
), err
}
}
}
traceLog.Info("Health Check successful")
terraform = infrav1.TerraformHealthCheckSucceeded(terraform, "Health checks succeeded")
return terraform, nil
}
func (r *TerraformReconciler) doTCPHealthCheck(ctx context.Context, name string, address string, timeout time.Duration) error {
log := ctrl.LoggerFrom(ctx)
// validate tcp address
_, err := url.ParseRequestURI(address)
if err != nil {
return fmt.Errorf("invalid url for http health check: %s, %s", address, err)
}
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
return fmt.Errorf("failed to perform tcp health check for %s on %s: %s", name, address, err.Error())
}
err = conn.Close()
if err != nil {
log.Error(err, "Unexpected error closing TCP health check socket")
}
return nil
}
func (r *TerraformReconciler) doHTTPHealthCheck(ctx context.Context, name string, urlString string, timeout time.Duration) error {
log := ctrl.LoggerFrom(ctx)
// validate url
_, err := url.ParseRequestURI(urlString)
if err != nil {
return fmt.Errorf("invalid url for http health check: %s, %s", urlString, err)
}
req, err := http.NewRequest("GET", urlString, nil)
if err != nil {
log.Error(err, "Unexpected error creating HTTP request")
return err
}
ctxt, cancel := context.WithTimeout(req.Context(), timeout)
defer cancel()
re, err := http.DefaultClient.Do(req.WithContext(ctxt))
if err != nil {
return fmt.Errorf("failed to perform http health check for %s on %s: %s", name, urlString, err.Error())
}
defer func() {
if rerr := re.Body.Close(); rerr != nil {
log.Error(err, "Unexpected error closing HTTP health check socket")
}
}()
// read http body
b, err := io.ReadAll(re.Body)
if err != nil {
return fmt.Errorf("failed to perform http health check for %s on %s, error reading body: %s", name, urlString, err.Error())
}
// check http status code
if re.StatusCode >= http.StatusOK && re.StatusCode < http.StatusBadRequest {
log.Info("HTTP health check succeeded for %s on %s, response: %v", name, urlString, *re)
return nil
}
err = fmt.Errorf("failed to perform http health check for %s on %s, response body: %v", name, urlString, string(b))
log.Error(err, "failed to perform http health check for %s on %s, response body: %v", name, urlString, string(b))
return err
}
// parse template string from map[string]string
func (r *TerraformReconciler) parseHealthCheckTemplate(content map[string]string, text string) (string, error) {
var b bytes.Buffer
tmpl, err := template.
New("tmpl").
Delims("${{", "}}").
Parse(text)
if err != nil {
return "", err
}
err = tmpl.Execute(&b, content)
if err != nil {
err = fmt.Errorf("error getting terraform output for health checks: %s", err)
return "", err
}
return b.String(), nil
}