-
Notifications
You must be signed in to change notification settings - Fork 91
/
healthchecks.go
150 lines (118 loc) · 3.34 KB
/
healthchecks.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
/*
Copyright 2022 The Kubernetes 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 healthchecks
import (
"fmt"
"net"
"net/http"
"strconv"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/kpng/client"
"sigs.k8s.io/kpng/client/diffstore"
"sigs.k8s.io/kpng/client/localsink/fullstate"
)
type hcStatus struct {
nsn types.NamespacedName
port int
count int
}
type leaf = diffstore.AnyLeaf[hcStatus]
var _ fullstate.Callback = (&backend{}).Callback
func (b *backend) Callback(ch <-chan *client.ServiceEndpoints) {
defer b.statuses.Reset()
for seps := range ch {
port := int(seps.Service.HealthCheckNodePort)
if port == 0 {
continue // skip services without health check port
}
nsn := types.NamespacedName{
Namespace: seps.Service.Namespace,
Name: seps.Service.Name,
}
key := nsn.Namespace + "/" + nsn.Name + ":" + strconv.Itoa(port)
// count is the number of local endpoints
count := 0
for _, ep := range seps.Endpoints {
if ep.Local {
count++
}
}
// check the leaf and act accordingly
b.statuses.Get(key).Set(hcStatus{
nsn: nsn,
port: port,
count: count,
})
}
b.statuses.Done()
for _, item := range b.statuses.Deleted() {
key := item.Key()
b.instances[key].stop()
delete(b.instances, key)
}
for _, item := range b.statuses.Changed() {
key := item.Key()
status := item.Value().Get()
if instance, ok := b.instances[key]; ok {
instance.status = status
} else {
instance = &hcInstance{status: status}
if instance.start(b) {
b.instances[key] = instance
}
}
}
}
type hcInstance struct {
status hcStatus
listener net.Listener
}
func (hc *hcInstance) start(b *backend) (ok bool) {
nsn := hc.status.nsn
listener, err := net.Listen("tcp", b.ip+":"+strconv.Itoa(hc.status.port))
if err != nil {
// TODO send event, see events.EventRecorder and k8s.io/kubernetes/pkg/proxy/healthcheck.server#SyncServices
klog.ErrorS(err, "Failed to start healthcheck", "node", b.cfg.NodeName, "service", nsn, "port", hc.status.port)
return
}
ok = true
hc.listener = listener
go func() {
klog.V(3).InfoS("Starting goroutine for healthcheck", "service", nsn, "address", hc.listener.Addr())
err := http.Serve(hc.listener, hc)
// reminder: err is always non-nil
klog.ErrorS(err, "Healthcheck closed", "service", nsn, "address", hc.listener.Addr())
}()
return
}
func (hc *hcInstance) stop() {
hc.listener.Close()
}
func (hc *hcInstance) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
s := hc.status
resp.Header().Set("Content-Type", "application/json")
resp.Header().Set("X-Content-Type-Options", "nosniff")
if s.count == 0 {
resp.WriteHeader(http.StatusServiceUnavailable)
} else {
resp.WriteHeader(http.StatusOK)
}
fmt.Fprintf(resp, `{
"service": {
"namespace": %q,
"name": %q
},
"localEndpoints": %d
}`, s.nsn.Namespace, s.nsn.Name, s.count)
}