-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathnodeport_healthcheck.go
185 lines (156 loc) · 4.98 KB
/
nodeport_healthcheck.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
package proxy
import (
"context"
"fmt"
"net/http"
"strconv"
"sync"
"time"
"k8s.io/klog/v2"
)
type nodePortHealthCheckController struct {
nphcServicesInfo
activeNPHC map[int](chan<- struct{})
wg *sync.WaitGroup
stopCh chan struct{}
}
type serviceHealthCheck struct {
serviceID string
nodePort int
}
type nphcServicesInfo struct {
serviceInfoMap serviceInfoMap
endpointsInfoMap endpointSliceInfoMap
}
type nphcHandler struct {
svcHC *serviceHealthCheck
nphc *nodePortHealthCheckController
}
func (nphc *nodePortHealthCheckController) UpdateServicesInfo(serviceInfoMap serviceInfoMap,
endpointsInfoMap endpointSliceInfoMap) error {
klog.V(1).Info("Running UpdateServicesInfo for NodePort health check")
nphc.serviceInfoMap = serviceInfoMap
nphc.endpointsInfoMap = endpointsInfoMap
newActiveServices := make(map[int]bool)
for svcID, svc := range serviceInfoMap {
if svc.healthCheckNodePort != 0 {
newActiveServices[svc.healthCheckNodePort] = true
svcHC := serviceHealthCheck{
serviceID: svcID,
nodePort: svc.healthCheckNodePort,
}
if nphc.healthCheckExists(svcHC) {
continue
}
err := nphc.addHealthCheck(svcHC)
if err != nil {
return err
}
}
}
for np := range nphc.activeNPHC {
if !newActiveServices[np] {
err := nphc.stopHealthCheck(np)
if err != nil {
klog.Errorf("error stopping the NodePort healthcheck on NodePort %d: %v", np, err)
}
}
}
klog.V(1).Info("Finished UpdateServicesInfo for NodePort health check")
return nil
}
func (nphc *nodePortHealthCheckController) healthCheckExists(svcHC serviceHealthCheck) bool {
if _, ok := nphc.activeNPHC[svcHC.nodePort]; ok {
return true
}
return false
}
func (nphc *nodePortHealthCheckController) addHealthCheck(svcHC serviceHealthCheck) error {
klog.V(1).Infof("Adding NodePort health check for port: %d with svcid: %s", svcHC.nodePort, svcHC.serviceID)
if nphc.healthCheckExists(svcHC) {
return fmt.Errorf("unable to add healthcheck for NodePort %d as it is already taken", svcHC.nodePort)
}
closingChan := make(chan struct{})
nphc.activeNPHC[svcHC.nodePort] = closingChan
nphc.wg.Add(1)
go func(nphc *nodePortHealthCheckController, svcHC serviceHealthCheck, closingChan <-chan struct{}) {
defer nphc.wg.Done()
mux := http.NewServeMux()
srv := &http.Server{
Addr: ":" + strconv.Itoa(svcHC.nodePort),
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
}
npHandler := nphcHandler{
svcHC: &svcHC,
nphc: nphc,
}
mux.HandleFunc("/healthz", npHandler.Handler)
nphc.wg.Add(1)
go func(svcHC serviceHealthCheck) {
defer nphc.wg.Done()
klog.Infof("starting NodePort health controller on NodePort: %d", svcHC.nodePort)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
// cannot panic, because this probably is an intentional close
klog.Errorf("could not start NodePort health controller on NodePort %d: %s", svcHC.nodePort, err)
}
}(svcHC)
// block until we receive a shut down signal on either our private channel or the global channel
select {
case <-closingChan:
case <-nphc.stopCh:
}
klog.Infof("shutting down NodePort health controller on NodePort: %d", svcHC.nodePort)
if err := srv.Shutdown(context.Background()); err != nil {
klog.Errorf("could not shutdown NodePort health controller on NodePort %d: %v", svcHC.nodePort, err)
}
}(nphc, svcHC, closingChan)
return nil
}
func (nphc *nodePortHealthCheckController) stopHealthCheck(nodePort int) error {
if _, ok := nphc.activeNPHC[nodePort]; !ok {
return fmt.Errorf("no NodePort health check currently exists for NodePort: %d", nodePort)
}
svcStopCh := nphc.activeNPHC[nodePort]
close(svcStopCh)
delete(nphc.activeNPHC, nodePort)
return nil
}
func (npHandler *nphcHandler) Handler(w http.ResponseWriter, r *http.Request) {
eps := npHandler.nphc.endpointsInfoMap[npHandler.svcHC.serviceID]
endpointsOnNode := hasActiveEndpoints(eps)
var numActiveEndpoints int8
for _, endpoint := range eps {
if endpoint.isLocal && !endpoint.isTerminating {
numActiveEndpoints++
}
}
if endpointsOnNode && numActiveEndpoints > 0 {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, "%d Service Endpoints found\n", numActiveEndpoints)
if err != nil {
klog.Errorf("failed to write body: %s", err)
}
} else {
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte("No Service Endpoints Found\n"))
if err != nil {
klog.Errorf("Failed to write body: %s", err)
}
}
}
func (nphc *nodePortHealthCheckController) StopAll() {
klog.Info("Stopping all NodePort health checks")
close(nphc.stopCh)
klog.Info("Waiting for all NodePort health checks to finish shutting down")
nphc.wg.Wait()
klog.Info("All NodePort health checks are completely shut down, all done!")
}
func NewNodePortHealthCheck() *nodePortHealthCheckController {
nphc := nodePortHealthCheckController{
activeNPHC: make(map[int]chan<- struct{}),
wg: &sync.WaitGroup{},
stopCh: make(chan struct{}),
}
return &nphc
}