-
Notifications
You must be signed in to change notification settings - Fork 453
/
kubeprism.go
284 lines (233 loc) · 6.84 KB
/
kubeprism.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
279
280
281
282
283
284
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package k8s
import (
"context"
"fmt"
"net"
"strconv"
"time"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-loadbalancer/controlplane"
"github.com/siderolabs/go-loadbalancer/upstream"
"go.uber.org/zap"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
)
// KubePrismController creates KubePrism load balancer based on KubePrismEndpointsType resource.
type KubePrismController struct {
balancerHost string
balancerPort int
lb *controlplane.LoadBalancer
ticker *time.Ticker
upstreamCh chan []string
}
// Name implements controller.Controller interface.
func (ctrl *KubePrismController) Name() string {
return "k8s.KubePrismController"
}
// Inputs implements controller.Controll
// er interface.
func (ctrl *KubePrismController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: k8s.NamespaceName,
Type: k8s.KubePrismConfigType,
ID: optional.Some(k8s.KubePrismConfigID),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *KubePrismController) Outputs() []controller.Output {
return []controller.Output{
{
Type: k8s.KubePrismStatusesType,
Kind: controller.OutputExclusive,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo,cyclop
func (ctrl *KubePrismController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
logger = logger.Named("kubeprism")
defer func() {
if ctrl.lb == nil {
return
}
ctrl.stopKubePrism(logger) //nolint:errcheck
}()
for {
select {
case <-ctx.Done():
return nil
case <-ctrl.takeTickerC():
err := ctrl.writeKubePrismStatus(ctx, r)
if err != nil {
return err
}
continue
case <-r.EventCh():
}
lbCfg, err := safe.ReaderGetByID[*k8s.KubePrismConfig](ctx, r, k8s.KubePrismConfigID)
if err != nil && !state.IsNotFoundError(err) {
return err
}
switch {
case ctrl.lb == nil && lbCfg != nil:
err = ctrl.startKubePrism(lbCfg, logger)
if err != nil {
return err
}
case ctrl.lb != nil && lbCfg == nil:
err = ctrl.stopKubePrism(logger)
if err != nil {
return err
}
case ctrl.lb != nil && lbCfg != nil:
if lbCfg.TypedSpec().Host != ctrl.balancerHost || lbCfg.TypedSpec().Port != ctrl.balancerPort {
err = ctrl.stopKubePrism(logger)
if err != nil {
return err
}
err = ctrl.startKubePrism(lbCfg, logger)
if err != nil {
return err
}
} else {
ctrl.upstreamChan() <- makeEndpoints(lbCfg.TypedSpec())
}
}
err = ctrl.writeKubePrismStatus(ctx, r)
if err != nil {
return err
}
r.ResetRestartBackoff()
}
}
//nolint:gocyclo
func (ctrl *KubePrismController) writeKubePrismStatus(
ctx context.Context,
r controller.Runtime,
) error {
if ctrl.lb != nil && ctrl.endpoint() != "" {
healthy, err := ctrl.lb.Healthy()
if err != nil {
return fmt.Errorf("failed to check KubePrism health: %w", err)
}
got, err := safe.ReaderGetByID[*k8s.KubePrismStatuses](ctx, r, k8s.KubePrismStatusesID)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("failed to get KubePrism status: %w", err)
}
if got != nil && got.TypedSpec().Healthy == healthy {
return nil
}
err = safe.WriterModify(
ctx,
r,
k8s.NewKubePrismStatuses(k8s.NamespaceName, k8s.KubePrismStatusesID),
func(res *k8s.KubePrismStatuses) error {
res.TypedSpec().Host = ctrl.endpoint()
res.TypedSpec().Healthy = healthy
return nil
},
)
if err != nil {
return fmt.Errorf("failed to write KubePrism status: %w", err)
}
}
// list keys for cleanup
list, err := safe.ReaderListAll[*k8s.KubePrismStatuses](ctx, r)
if err != nil {
return fmt.Errorf("error listing KubePrism resources: %w", err)
}
for it := list.Iterator(); it.Next(); {
res := it.Value()
if ctrl.lb == nil || res.Metadata().ID() != k8s.KubePrismStatusesID {
if err = r.Destroy(ctx, res.Metadata()); err != nil {
return fmt.Errorf("error cleaning up KubePrism specs: %w", err)
}
}
}
return nil
}
func (ctrl *KubePrismController) startKubePrism(lbCfg *k8s.KubePrismConfig, logger *zap.Logger) error {
spec := lbCfg.TypedSpec()
ctrl.balancerHost = spec.Host
ctrl.balancerPort = spec.Port
lb, err := controlplane.NewLoadBalancer(ctrl.balancerHost, ctrl.balancerPort,
logger.WithOptions(zap.IncreaseLevel(zap.ErrorLevel)), // silence the load balancer logs
controlplane.WithDialTimeout(constants.KubePrismDialTimeout),
controlplane.WithKeepAlivePeriod(constants.KubePrismKeepAlivePeriod),
controlplane.WithTCPUserTimeout(constants.KubePrismTCPUserTimeout),
controlplane.WithHealthCheckOptions(
upstream.WithHealthcheckInterval(constants.KubePrismHealthCheckInterval),
upstream.WithHealthcheckTimeout(constants.KubePrismHealthCheckTimeout),
),
)
if err != nil {
return fmt.Errorf("failed to create KubePrism: %w", err)
}
err = lb.Start(ctrl.upstreamChan())
if err != nil {
return fmt.Errorf("failed to start KubePrism: %w", err)
}
logger.Info("KubePrism is enabled", zap.String("endpoint", ctrl.endpoint()))
ctrl.upstreamChan() <- makeEndpoints(spec)
ctrl.lb = lb
return nil
}
func makeEndpoints(spec *k8s.KubePrismConfigSpec) []string {
return xslices.Map(spec.Endpoints, func(e k8s.KubePrismEndpoint) string {
return net.JoinHostPort(e.Host, strconv.FormatUint(uint64(e.Port), 10))
})
}
func (ctrl *KubePrismController) takeTickerC() <-chan time.Time {
switch {
case ctrl.lb == nil && ctrl.ticker == nil:
return nil
case ctrl.lb != nil && ctrl.ticker == nil:
ctrl.ticker = time.NewTicker(5 * time.Second)
return ctrl.ticker.C
case ctrl.lb == nil:
ticker := replaceWithZero(&ctrl.ticker)
if ticker != nil {
ticker.Stop()
}
return nil
default:
return ctrl.ticker.C
}
}
func (ctrl *KubePrismController) endpoint() string {
return net.JoinHostPort(ctrl.balancerHost, strconv.FormatUint(uint64(ctrl.balancerPort), 10))
}
func (ctrl *KubePrismController) upstreamChan() chan []string {
if ctrl.upstreamCh == nil {
ctrl.upstreamCh = make(chan []string)
}
return ctrl.upstreamCh
}
func (ctrl *KubePrismController) stopKubePrism(logger *zap.Logger) error {
replaceWithZero(&ctrl.upstreamCh)
lb := replaceWithZero(&ctrl.lb)
err := lb.Shutdown()
if err != nil {
logger.Error("failed to shutdown KubePrism", zap.Error(err))
return err
}
logger.Info("KubePrism is disabled")
return nil
}
func replaceWithZero[T any](v *T) T {
var zero T
result := *v
*v = zero
return result
}