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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package clusterprovider
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/apiserverfront"
"github.com/johnandersen777/socialweb-computer-kcp/internal/clusterstack"
"github.com/johnandersen777/socialweb-computer-kcp/internal/materialiser"
"github.com/johnandersen777/socialweb-computer-kcp/internal/portallocator"
"github.com/johnandersen777/socialweb-computer-kcp/internal/procgroup"
)
const GateTimeout = 60 * time.Second
const PollInterval = 250 * time.Millisecond
const (
EnvCloudControllerManagerKubeconfig = "SOCIALWEB_CLOUDCONTROLLERMANAGER_TENANT_KUBECONFIG"
EnvCloudControllerManagerCluster = "SOCIALWEB_CLOUDCONTROLLERMANAGER_CLUSTER"
EnvCloudControllerManagerInstances = "SOCIALWEB_CLOUDCONTROLLERMANAGER_INSTANCES_FILE"
)
const cloudControllerManagerReadyMark = `"phase":"Ready"`
var ErrNoLease = errors.New("clusterprovider: the cluster holds no lease for this component")
var ErrNoTenantClient = errors.New("clusterprovider: no client for the cluster's own apiserver was supplied")
var ErrNoVaultStore = errors.New(
"clusterprovider: the vault is in this Stack and no per-cluster postgres DSN was supplied " +
"for it. The vault's store IS the cluster's own embedded-postgres, so without the DSN " +
"the vault cannot be rendered and the Stack would lease a vault port and start nothing")
type Stack []procgroup.Component
var DesignedStack = Stack{procgroup.ComponentOpenBao, procgroup.ComponentKine, procgroup.ComponentK3s}
var ProvenStack = Stack{procgroup.ComponentK3s}
func (s Stack) Has(c procgroup.Component) bool {
return slices.Contains(s, c)
}
func (s Stack) NeedsLocalPostgres(localStore bool) bool {
return localStore || s.Has(procgroup.ComponentOpenBao)
}
func componentLog(dataDir string, c procgroup.Component) (string, error) {
if err := os.MkdirAll(LogDir(dataDir), 0o700); err != nil {
return "", fmt.Errorf("clusterprovider: the log directory for this cluster's components: %w", err)
}
return filepath.Join(LogDir(dataDir), string(c)+".log"), nil
}
func LogDir(dataDir string) string { return filepath.Join(dataDir, "logs") }
func (cfg StackConfig) StepsFrom(spec clusterstack.Spec, leases materialiser.Leases) ([]procgroup.Step, error) {
stack := cfg.Stack
if err := spec.Validate(); err != nil {
return nil, err
}
if len(stack) == 0 {
return nil, errors.New("clusterprovider: the Stack is empty; a group with no components is not a cluster")
}
in := make(map[procgroup.Component]bool, len(stack))
for _, c := range stack {
if c == procgroup.ComponentPostgres {
return nil, errors.New(
"clusterprovider: ComponentPostgres cannot be a Step (clusterstack.ErrNotAProcess): " +
"embedded-postgres is a library and is started by the component that links it")
}
in[c] = true
}
var bao portallocator.Lease
if in[procgroup.ComponentOpenBao] {
var ok bool
if bao, ok = leases[portallocator.PurposeOpenBao]; !ok {
return nil, fmt.Errorf("%w: openbao", ErrNoLease)
}
}
api := leases[portallocator.PurposeK3sSecure]
if in[procgroup.ComponentK3s] && !in[procgroup.ComponentApiserverFront] {
return nil, fmt.Errorf("%w: this Stack runs k3s and no apiserver front (data dir %s)", clusterstack.ErrMissingFront, spec.DataDir)
}
steps := make([]procgroup.Step, 0, len(stack))
for _, c := range procgroup.StackOrder {
if !in[c] {
continue
}
if c == procgroup.ComponentApiserverFront {
st, err := frontStep(spec)
if err != nil {
return nil, err
}
steps = append(steps, st)
continue
}
argv, err := spec.Argv(c)
if err != nil {
return nil, err
}
if len(argv) == 0 {
return nil, fmt.Errorf("clusterprovider: %s has an empty argv", c)
}
log, err := componentLog(spec.DataDir, c)
if err != nil {
return nil, err
}
slot := ""
if c == procgroup.ComponentKine || c == procgroup.ComponentK3s {
slot = spec.Slot
}
if slot != "" && slot != ActiveSlot {
log = strings.TrimSuffix(log, ".log") + "@" + slot + ".log"
}
ps := procgroup.Spec{Argv: argv, Env: stepEnv(spec, c), Log: log}
if c == procgroup.ComponentOpenBao {
ps.Listen = net.JoinHostPort(portallocator.LoopbackAdvertiseAddress, strconv.Itoa(bao.Port))
}
st := procgroup.Step{
Name: c,
Slot: slot,
Spec: ps,
Ready: cfg.gate(c, spec, bao, api),
}
if c == procgroup.ComponentK3s {
st.Prepare = repointApiserverClients(spec)
}
steps = append(steps, st)
}
return steps, nil
}
func stepEnv(spec clusterstack.Spec, c procgroup.Component) []string {
if c == procgroup.ComponentExternalSecrets || c == procgroup.ComponentExternalSecretsWebhook {
return externalSecretsEnv(spec)
}
if c != procgroup.ComponentCloudControllerManager {
return nil
}
return []string{
EnvCloudControllerManagerKubeconfig + "=" + clusterstack.CloudControllerManagerKubeconfig(spec.DataDir),
EnvCloudControllerManagerCluster + "=" + spec.Cluster,
EnvCloudControllerManagerInstances + "=" + clusterstack.NodeSnapshotPath(spec.DataDir),
}
}
func (cfg StackConfig) gate(c procgroup.Component, spec clusterstack.Spec, bao, api portallocator.Lease) func(context.Context, procgroup.Member) error {
return func(ctx context.Context, p procgroup.Member) error {
ctx, cancel := context.WithTimeout(ctx, GateTimeout)
defer cancel()
switch c {
case procgroup.ComponentOpenBao:
return waitOpenBao(ctx, spec.DataDir, bao.Port)
case procgroup.ComponentKine:
return waitDial(ctx, spec.KineListen)
case procgroup.ComponentApiserverFront:
return waitFront(ctx, spec.FrontControl)
case procgroup.ComponentK3s:
if api.Purpose != portallocator.PurposeK3sSecure {
return fmt.Errorf("%w: the apiserver front, so this k3s step has no leased port to hold it to (data dir %s)", ErrNoLease, spec.DataDir)
}
if err := portallocator.AssertMarketAdvertiseInvariant(portallocator.Advertisement{Address: spec.AdvertiseAddress,
Port: spec.AdvertisePort, LinkTarget: spec.AdvertisePort, Front: api, SlotListen: []int{spec.K3sSecurePort}}); err != nil {
return err
}
if err := cfg.waitSlotUp(ctx, spec); err != nil {
return err
}
if _, err := (apiserverfront.Client{Path: spec.FrontControl}).Set(ctx, SlotBackend(spec)); err != nil {
return fmt.Errorf("clusterprovider: pointing the front of %s at slot %s: %w", spec.Cluster, spec.Slot, err)
}
if err := waitK3s(ctx, spec.BindAddress, spec.AdvertisePort); err != nil {
return err
}
return cfg.CheckAdvertisedEndpoints(ctx, spec)
case procgroup.ComponentCloudControllerManager:
return waitCloudControllerManager(ctx, p)
case procgroup.ComponentExternalSecretsWebhook:
return cfg.waitExternalSecretsWebhook(ctx, spec, p)
case procgroup.ComponentExternalSecrets:
return waitExternalSecretsController(ctx, spec, p)
}
return fmt.Errorf("clusterprovider: no readiness gate for %q", c)
}
}
type errNotReady struct {
what string
last string
}
func (e *errNotReady) Error() string {
return fmt.Sprintf("%s never became ready (last observation: %s)", e.what, e.last)
}
func waitOpenBao(ctx context.Context, dataDir string, port int) error {
pool, err := vaultCA(dataDir)
if err != nil {
return err
}
return waitOpenBaoTLS(ctx, VaultAddr(port), pool)
}
func waitOpenBaoTLS(ctx context.Context, addr string, pool *x509.CertPool) error {
url := addr + "/v1/sys/health"
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}},
}
last := "no answer"
return poll(ctx, func(ctx context.Context) (bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return false, err
}
resp, err := client.Do(req)
if err != nil {
last = err.Error()
return false, nil
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK, http.StatusTooManyRequests, 473:
return true, nil
}
last = fmt.Sprintf("HTTP %d", resp.StatusCode)
return false, nil
}, func() error { return &errNotReady{what: "openbao", last: last} })
}
func waitDial(ctx context.Context, addr string) error {
last := "no answer"
return poll(ctx, func(context.Context) (bool, error) {
conn, err := net.DialTimeout("tcp", addr, 2*time.Second)
if err != nil {
last = err.Error()
return false, nil
}
_ = conn.Close()
return true, nil
}, func() error { return &errNotReady{what: "kine at " + addr, last: last} })
}
func waitK3s(ctx context.Context, host string, port int) error {
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}
url := fmt.Sprintf("https://%s:%d/readyz", host, port)
last := "no answer"
return poll(ctx, func(ctx context.Context) (bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return false, err
}
resp, err := client.Do(req)
if err != nil {
last = err.Error()
return false, nil
}
defer resp.Body.Close()
if apiserverIsAnswering(resp.StatusCode) {
return true, nil
}
last = fmt.Sprintf("HTTP %d", resp.StatusCode)
return false, nil
}, func() error { return &errNotReady{what: "k3s server at " + url, last: last} })
}
const readyzStreak = 5
func (cfg StackConfig) open(kubeconfig string, o OpenOptions) (Tenant, error) {
if cfg.Open == nil {
return nil, ErrNoTenantClient
}
return cfg.Open(kubeconfig, o)
}
func (cfg StackConfig) waitK3sReadyz(ctx context.Context, kubeconfig string) error {
last := "no answer"
streak := 0
return poll(ctx, func(ctx context.Context) (bool, error) {
core, err := cfg.open(kubeconfig, OpenOptions{Timeout: 3 * time.Second})
if err != nil {
last = err.Error()
return false, nil
}
if err := core.Readyz(ctx); err != nil {
streak = 0
last = err.Error()
return false, nil
}
streak++
last = fmt.Sprintf("/readyz answered 200 %d of %d times in a row", streak, readyzStreak)
return streak >= readyzStreak, nil
}, func() error {
return &errNotReady{
what: "the k3s server whose admin kubeconfig is " + kubeconfig + " answering /readyz 200 " + strconv.Itoa(readyzStreak) + " times in a row",
last: last,
}
})
}
func (cfg StackConfig) adminClient(ctx context.Context, kubeconfig string, last *string) (Tenant, error) {
var core Tenant
err := untilOutput(ctx, unitMember{path: kubeconfig}, func(string) (bool, error) {
var err error
if core, err = cfg.open(kubeconfig, OpenOptions{}); err != nil {
*last = err.Error()
return false, nil
}
return true, nil
}, ctx.Err)
return core, err
}
func (cfg StackConfig) waitK3sControllers(ctx context.Context, kubeconfig string) error {
last := "no answer"
notReady := func() error {
return &errNotReady{
what: "the k3s controllers of the server whose admin kubeconfig is " + kubeconfig +
". The apiserver answers /readyz before they start, and k3s registers the " +
"node-password validator that answers /v1-k3s/*.crt for a REMOTE node on the line " +
"before it sets Runtime.Core: a node given its guest in that window asks for its " +
"serving certificate, is refused with 503 Service Unavailable, and retries on a " +
"backoff of five seconds jittered by the same five, which is the difference between " +
"a node that joins at five seconds and one that joins at twelve to twenty. The " +
"controllers' own first write is an addon Deployment in kube-system and that is what " +
"this waits for: /v1-k3s/readyz is the endpoint that flips exactly with them, and it " +
"takes a node's credentials, which this process does not hold",
last: last,
}
}
core, err := cfg.adminClient(ctx, kubeconfig, &last)
if err != nil {
if ctx.Err() != nil {
return notReady()
}
return err
}
last = "the apiserver answered and " + metav1.NamespaceSystem + " holds no Deployment yet"
err = core.AwaitDeployments(ctx, metav1.NamespaceSystem, 0, func(addons []*appsv1.Deployment) (bool, error) {
return len(addons) > 0, nil
})
if err != nil && ctx.Err() != nil {
return notReady()
}
return err
}
func waitCloudControllerManager(ctx context.Context, p procgroup.Member) error {
last := "no output yet"
return untilOutput(ctx, p, func(out string) (bool, error) {
if strings.Contains(out, cloudControllerManagerReadyMark) {
return true, nil
}
last = fmt.Sprintf("the process has written %d bytes and no readiness line", len(out))
return false, nil
}, func() error {
return &errNotReady{
what: "the cloud controller manager's informer sync. It logs phase Ready once its " +
"Node informer has synced, which is the first moment it can act; before that it has " +
"observed nothing and would write nothing. It listens on no port, so this line is the " +
"only readiness signal there is",
last: last,
}
})
}
func apiserverIsAnswering(code int) bool {
switch code {
case http.StatusOK, http.StatusUnauthorized:
return true
default:
return false
}
}
func poll(ctx context.Context, up func(context.Context) (bool, error), timeout func() error) error {
for {
ok, err := up(ctx)
if err != nil {
return err
}
if ok {
return nil
}
select {
case <-ctx.Done():
return timeout()
case <-time.After(PollInterval):
}
}
}
func Probe(ctx context.Context, url string) (code int, reachable bool) {
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url+"/readyz", nil)
if err != nil {
return 0, false
}
resp, err := client.Do(req)
if err != nil {
return 0, false
}
defer resp.Body.Close()
code = resp.StatusCode
return code, apiserverIsAnswering(code)
}