/
healthchecksyncer.go
278 lines (257 loc) · 9.88 KB
/
healthchecksyncer.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2017 Google Inc.
//
// 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 healthcheck
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"time"
"github.com/golang/glog"
multierror "github.com/hashicorp/go-multierror"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/client-go/kubernetes"
ingressbe "k8s.io/ingress-gce/pkg/backends"
ingresshc "k8s.io/ingress-gce/pkg/healthchecks"
"k8s.io/ingress-gce/pkg/utils"
utilsnamer "github.com/GoogleCloudPlatform/k8s-multicluster-ingress/app/kubemci/pkg/gcp/namer"
"github.com/GoogleCloudPlatform/k8s-multicluster-ingress/app/kubemci/pkg/kubeutils"
)
const (
// DefaultHealthCheckInterval defines how frequently a probe runs
// TODO(nikhiljindal): Share them with kubernetes/ingress.
// These values set a low health threshold and a high failure threshold.
// We're just trying to detect if the node networking is
// borked, service level outages will get detected sooner
// by kube-proxy.
DefaultHealthCheckInterval = 1 * time.Minute
// DefaultHealthyThreshold defines the threshold of success probes that declare a backend "healthy"
DefaultHealthyThreshold = 1
// DefaultUnhealthyThreshold defines the threshold of failure probes that declare a backend "unhealthy"
DefaultUnhealthyThreshold = 10
// DefaultTimeout defines the timeout of each probe
DefaultTimeout = 1 * time.Minute
)
// Syncer manages GCP health checks for multicluster GCP L7 load balancers.
type Syncer struct {
namer *utilsnamer.Namer
hcp ingresshc.HealthCheckProvider
}
// NewHealthCheckSyncer returns a new instance of the syncer.
func NewHealthCheckSyncer(namer *utilsnamer.Namer, hcp ingresshc.HealthCheckProvider) SyncerInterface {
return &Syncer{
namer: namer,
hcp: hcp,
}
}
// Ensure this implements SyncerInterface.
var _ SyncerInterface = &Syncer{}
// EnsureHealthCheck ensures that the required health check exists.
// Does nothing if it exists already, else creates a new one.
// Returns a map of the ensured health checks keyed by the corresponding port.
func (h *Syncer) EnsureHealthCheck(lbName string, ports []ingressbe.ServicePort, clients map[string]kubernetes.Interface, forceUpdate bool) (HealthChecksMap, error) {
fmt.Println("Ensuring health checks")
var err error
ensuredHealthChecks := HealthChecksMap{}
for _, p := range ports {
path, pathErr := getHealthCheckPath(p, clients)
if pathErr != nil {
err = multierror.Append(err, pathErr)
continue
}
fmt.Println("Path for healthcheck is", path)
hc, hcErr := h.ensureHealthCheck(lbName, p, path, forceUpdate)
if hcErr != nil {
hcErr = fmt.Errorf("Error %s in ensuring health check for port %v", hcErr, p)
// Try ensuring health checks for all ports and return all errors at once.
err = multierror.Append(err, hcErr)
continue
}
ensuredHealthChecks[p.NodePort] = hc
}
return ensuredHealthChecks, err
}
func getHealthCheckPath(port ingressbe.ServicePort, clients map[string]kubernetes.Interface) (string, error) {
var err error
path := ""
for cName, c := range clients {
probe, prErr := kubeutils.GetProbe(c, port)
if prErr != nil {
prErr = fmt.Errorf("Error %s when getting readiness probe from pods for service %s", prErr, port.SvcName)
err = multierror.Append(err, prErr)
continue
}
if probe != nil && probe.HTTPGet != nil {
if path != "" {
// Ensure that all probes have the same health check path
if path != probe.HTTPGet.Path {
pathErr := fmt.Errorf("readiness probe path '%s' from pod spec in cluster '%s' does not match already extracted health check path '%s'", probe.HTTPGet.Path, cName, path)
err = multierror.Append(err, pathErr)
return path, err
}
} else {
// TODO share code with `applyProbeSettingsToHC` method from ingress-gce/backend to get probe settings
path = probe.HTTPGet.Path
}
}
}
// GCE requires a leading "/" for health check urls.
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return path, err
}
// DeleteHealthChecks deletes the health checks that EnsureHealthCheck would have created.
// See interface for more details.
func (h *Syncer) DeleteHealthChecks(ports []ingressbe.ServicePort) error {
fmt.Println("Deleting health checks")
var err error
for _, p := range ports {
if hcErr := h.deleteHealthCheck(p); hcErr != nil {
err = multierror.Append(err, hcErr)
}
}
if err != nil {
fmt.Println("Errors in deleting health checks:", err)
return err
}
fmt.Println("Successfully deleted all health checks")
return nil
}
func (h *Syncer) deleteHealthCheck(port ingressbe.ServicePort) error {
name := h.namer.HealthCheckName(port.NodePort)
glog.V(2).Infof("Deleting health check %s", name)
err := h.hcp.DeleteHealthCheck(name)
if err != nil {
if utils.IsHTTPErrorCode(err, http.StatusNotFound) {
fmt.Println("Health check", name, "does not exist. Nothing to delete")
return nil
}
fmt.Println("Error in deleting health check", name, ":", err)
return err
}
glog.V(2).Infof("Successfully deleted health check %s", name)
return nil
}
func getJSONIgnoreErr(v interface{}) string {
output, err := json.Marshal(v)
if err != nil {
glog.Warningf("Marshalling error: %v", err)
}
return string(output)
}
func (h *Syncer) ensureHealthCheck(lbName string, port ingressbe.ServicePort, path string, forceUpdate bool) (*compute.HealthCheck, error) {
fmt.Printf("Ensuring health check for port: %+v\n", port)
desiredHC, err := h.desiredHealthCheck(lbName, port, path)
if err != nil {
return nil, fmt.Errorf("error %s in computing desired health check", err)
}
name := desiredHC.Name
// Check if hc already exists.
existingHC, err := h.hcp.GetHealthCheck(name)
if err == nil {
fmt.Println("Health check", name, "exists already. Checking if it matches our desired health check")
glog.V(5).Infof("Existing health check:\n%v\nDesired health check:\n%v\n", getJSONIgnoreErr(existingHC), getJSONIgnoreErr(desiredHC))
// Health check with that name exists already. Check if it matches what we want.
if healthCheckMatches(desiredHC, *existingHC) {
// Nothing to do. Desired health check exists already.
fmt.Println("Desired health check exists already")
return existingHC, nil
}
if forceUpdate {
return h.updateHealthCheck(&desiredHC)
}
// TODO(G-Harmon): prompt yes/no for overwriting.
fmt.Println("Will not overwrite this differing health check without the --force flag.")
return nil, fmt.Errorf("will not overwrite healthcheck without --force")
}
glog.V(5).Infof("Got error %s while trying to get existing health check %s", err, name)
// TODO(nikhiljindal): Handle non NotFound errors. We should create only if the error is NotFound.
// Create the health check.
return h.createHealthCheck(&desiredHC)
}
// updateHealthCheck updates the health check and returns the updated health check.
func (h *Syncer) updateHealthCheck(desiredHC *compute.HealthCheck) (*compute.HealthCheck, error) {
name := desiredHC.Name
fmt.Println("Updating existing health check", name, "to match the desired state")
err := h.hcp.UpdateHealthCheck(desiredHC)
if err != nil {
return nil, err
}
fmt.Println("Health check", name, "updated successfully")
return h.hcp.GetHealthCheck(name)
}
// createHealthCheck creates the health check and returns the created health check.
func (h *Syncer) createHealthCheck(desiredHC *compute.HealthCheck) (*compute.HealthCheck, error) {
name := desiredHC.Name
fmt.Println("Creating health check", name)
glog.V(5).Infof("Creating health check %v", desiredHC)
err := h.hcp.CreateHealthCheck(desiredHC)
if err != nil {
return nil, err
}
fmt.Println("Health check", name, "created successfully")
return h.hcp.GetHealthCheck(name)
}
func healthCheckMatches(desiredHC, existingHC compute.HealthCheck) bool {
// Clear fields we don't care about to make the printout easier to read.
existingHC.CreationTimestamp = ""
existingHC.Id = 0
existingHC.SelfLink = ""
existingHC.ServerResponse = googleapi.ServerResponse{}
equal := reflect.DeepEqual(existingHC, desiredHC)
if !equal {
glog.Infof("Diff:\n%v", diff.ObjectDiff(desiredHC, existingHC))
}
return equal
}
func (h *Syncer) desiredHealthCheck(lbName string, port ingressbe.ServicePort, path string) (compute.HealthCheck, error) {
// Compute the desired health check.
hc := compute.HealthCheck{
Name: h.namer.HealthCheckName(port.NodePort),
Description: fmt.Sprintf("Health check for service %s as part of kubernetes multicluster loadbalancer %s", port.Description(), lbName),
// How often to health check.
CheckIntervalSec: int64(DefaultHealthCheckInterval.Seconds()),
// How long to wait before claiming failure of a health check.
TimeoutSec: int64(DefaultTimeout.Seconds()),
// Number of healthchecks to pass for a vm to be deemed healthy.
HealthyThreshold: DefaultHealthyThreshold,
// Number of healthchecks to fail before the vm is deemed unhealthy.
UnhealthyThreshold: DefaultUnhealthyThreshold,
Type: string(port.Protocol),
Kind: "compute#healthCheck",
}
switch port.Protocol {
case "HTTP":
hc.HttpHealthCheck = &compute.HTTPHealthCheck{
Port: port.NodePort,
RequestPath: path,
ProxyHeader: "NONE",
}
break
case "HTTPS":
hc.HttpsHealthCheck = &compute.HTTPSHealthCheck{
Port: port.NodePort,
RequestPath: path,
ProxyHeader: "NONE",
}
break
default:
return compute.HealthCheck{}, fmt.Errorf("Unexpected port protocol: %s", port.Protocol)
}
return hc, nil
}