/
backend.go
236 lines (188 loc) · 5.69 KB
/
backend.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
package healthcheck
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/gardener/gardener/extensions/pkg/controller/healthcheck"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
discoveryv1 "k8s.io/api/discovery/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/types"
)
// retryMap contains outputRetries using the namespace as a key
type retryMap map[string]outputRetries
// outputRetries contains retries per output plugin
type outputRetries map[string]int
type BackendHealthChecker struct {
logger logr.Logger
httpClient *http.Client
retries retryMap
mutex sync.Mutex
backoff map[string]time.Time
syncPeriod time.Duration
seedClient client.Client
}
func backendHealth(syncPeriod time.Duration) healthcheck.HealthCheck {
return &BackendHealthChecker{
httpClient: http.DefaultClient,
retries: retryMap{},
backoff: map[string]time.Time{},
syncPeriod: syncPeriod,
}
}
// InjectSeedClient injects the seed client
func (h *BackendHealthChecker) InjectSeedClient(seedClient client.Client) {
h.seedClient = seedClient
}
func (h *BackendHealthChecker) SetLoggerSuffix(provider, extension string) {
h.logger = h.logger.WithName(fmt.Sprintf("%s-%s-healthcheck-backend", provider, extension))
}
func (h *BackendHealthChecker) DeepCopy() healthcheck.HealthCheck {
return &BackendHealthChecker{
logger: h.logger,
httpClient: h.httpClient,
retries: h.retries,
mutex: sync.Mutex{},
backoff: h.backoff,
syncPeriod: h.syncPeriod,
seedClient: h.seedClient,
}
}
func (h *BackendHealthChecker) Check(ctx context.Context, request types.NamespacedName) (*healthcheck.SingleCheckResult, error) {
err := h.check(ctx, request.Namespace)
if err != nil {
return &healthcheck.SingleCheckResult{ // nolint:nilerr
Status: gardencorev1beta1.ConditionFalse,
Detail: err.Error(),
}, nil
}
return &healthcheck.SingleCheckResult{
Status: gardencorev1beta1.ConditionTrue,
}, nil
}
func (h *BackendHealthChecker) check(ctx context.Context, namespace string) error {
if err := h.checkHealthEndpoint(ctx, namespace); err != nil {
return err
}
if err := h.checkRetries(ctx, namespace); err != nil {
return err
}
return nil
}
func (h *BackendHealthChecker) checkHealthEndpoint(ctx context.Context, namespace string) error {
url := fmt.Sprintf("http://audit-webhook-backend.%s.svc.cluster.local:2020/api/v1/health", namespace)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("unable to create http request: %w", err)
}
resp, err := h.httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to do http request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
return fmt.Errorf("backend is unhealthy since errors or failures have occurred in the last minute time frame")
}
func (h *BackendHealthChecker) checkRetries(ctx context.Context, namespace string) error {
// as retries are set to no_limits, fluent-bit does not count unreachable backends as errors or retry_errors
// therefore, we need to check if there were any retries during the last health check
endpointSliceList := &discoveryv1.EndpointSliceList{}
err := h.seedClient.List(ctx, endpointSliceList, client.MatchingLabels{
"kubernetes.io/service-name": "audit-webhook-backend",
}, client.InNamespace(namespace))
if err != nil {
return err
}
if len(endpointSliceList.Items) == 0 {
return fmt.Errorf("no endpoints found for audit backend service")
}
h.mutex.Lock()
defer h.mutex.Unlock()
defer func() {
h.backoff[namespace] = time.Now()
}()
lastCheck, ok := h.backoff[namespace]
if ok {
if time.Since(lastCheck) < h.syncPeriod-1*time.Second {
// we only need a check once every sync period, otherwise retries might be too low such that health flickers
return nil
}
}
type metrics struct {
Output map[string]struct {
Retries int `json:"retries"`
RetriedRecords int `json:"retried_records"`
} `json:"output"`
}
var (
ms []metrics
addresses []string
)
for _, endpoints := range endpointSliceList.Items {
for _, endpoint := range endpoints.Endpoints {
addresses = append(addresses, endpoint.Addresses...)
}
}
for _, address := range addresses {
url := fmt.Sprintf("http://%s:2020/api/v1/metrics", address)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("unable to create http request: %w", err)
}
resp, err := h.httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to do http request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("metrics endpoint return code was %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unable to read http body: %w", err)
}
var m metrics
err = json.Unmarshal(body, &m)
if err != nil {
return fmt.Errorf("unable to unmarshal metrics: %w", err)
}
ms = append(ms, m)
}
plugins, ok := h.retries[namespace]
if !ok {
plugins = outputRetries{}
}
var (
errs []error
sums = outputRetries{}
)
for _, m := range ms {
m := m
for name, output := range m.Output {
output := output
sums[name] += output.Retries
}
}
for name, sum := range sums {
lastCount, ok := plugins[name]
if !ok {
plugins[name] = sum
continue
}
diff := sum - lastCount
plugins[name] = sum
if diff > 0 {
errs = append(errs, fmt.Errorf("%d retries (%d in total) have occurred in the last minute time frame for output %q", diff, sum, name))
}
}
h.retries[namespace] = plugins
return errors.Join(errs...)
}