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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package nodemarket
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"slices"
"strings"
"sync"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/opt"
"github.com/johnandersen777/socialweb-computer-kcp/internal/scale"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
)
type Claims interface {
WriteNodeClaim(ctx context.Context, logicalCluster string, claim v1alpha1.NodeClaim) error
ReadNodeClaim(ctx context.Context, logicalCluster, name string) (*v1alpha1.NodeClaim, error)
DeleteNodeClaim(ctx context.Context, logicalCluster, name string) error
ListNodeClaims(ctx context.Context, logicalCluster string) ([]v1alpha1.NodeClaim, error)
AwaitNodeClaimGone(ctx context.Context, logicalCluster, name string) error
}
type Cluster struct {
Workspace string
OwnerDid string
Name string
UID types.UID
}
type ClusterSource func(ctx context.Context) (Cluster, error)
type Options struct {
Cluster string
NodeName string
AdvertiseAddress string
ApiserverPort int
TokenFile string
ServerCAFile string
DataDir string
DenyBidderDids []string
DenyMore func() []string
Resolve ClusterSource
Claims Claims
Record func(ctx context.Context, nodeName string, phase v1alpha1.NodePhase) error
Log *slog.Logger
ReleaseTimeout time.Duration
Secrets NodeSecrets
Links Links
}
const DefaultReleaseTimeout = 2 * time.Minute
const fatalReadTimeout = 2 * time.Second
var ErrAwaitingAdoption = errors.New("nodemarket: the claim waits for the door to match the joined node to its receipt")
const claimGoneMessage = "the claim this node was rented under no longer exists: it was written " +
"before this node was counted, and the door releases a claim's machine before the claim can go, " +
"so a counted node with no claim is a machine already released"
var ErrIncomplete = errors.New(
"nodemarket: a market node is a claim written into the cluster's own home workspace and " +
"fulfilled by a door in another process, and this one is missing an input that cannot be " +
"derived: the workspace and owner DID it is claimed under, the store the claim is written " +
"through, the paths the door reads the join material from, the address and port the node " +
"dials its apiserver on, or the Node CR its phase is recorded in")
type Host struct {
mu sync.Mutex
opts Options
provisioned bool
policyWritten bool
secretWritten bool
heldBack string
granted *v1alpha1.NodeClaimGrant
}
var _ scale.Nodes = (*Host)(nil)
func New(opts Options) (*Host, error) {
opts = opts.withDefaults()
if err := opts.Validate(); err != nil {
return nil, err
}
return &Host{opts: opts}, nil
}
func (o Options) withDefaults() Options {
o.ReleaseTimeout = opt.Positive(o.ReleaseTimeout, DefaultReleaseTimeout)
return o
}
func (o Options) Validate() error {
var missing []string
for _, req := range []struct{ name, value string }{
{"Cluster", o.Cluster},
{"NodeName", o.NodeName},
{"AdvertiseAddress", o.AdvertiseAddress},
{"TokenFile", o.TokenFile},
{"ServerCAFile", o.ServerCAFile},
{"DataDir", o.DataDir},
} {
if req.value == "" {
missing = append(missing, req.name)
}
}
if o.ApiserverPort <= 0 || o.ApiserverPort > 65535 {
missing = append(missing, "ApiserverPort")
}
if o.Resolve == nil {
missing = append(missing, "Resolve")
}
if o.Claims == nil {
missing = append(missing, "Claims")
}
if o.Record == nil {
missing = append(missing, "Record")
}
if o.Secrets != nil && o.Links == nil {
missing = append(missing, "Links")
}
if len(missing) > 0 {
return fmt.Errorf("%w (missing %s)", ErrIncomplete, strings.Join(missing, ", "))
}
return nil
}
func (h *Host) log() *slog.Logger {
if h.opts.Log == nil {
return slog.Default()
}
return h.opts.Log
}
func (h *Host) Provision(ctx context.Context) error {
h.mu.Lock()
defer h.mu.Unlock()
if err := h.opts.Validate(); err != nil {
return fmt.Errorf("nodemarket: node %s of %s: %w", h.opts.NodeName, h.opts.Cluster, err)
}
if err := os.MkdirAll(h.opts.DataDir, 0o700); err != nil {
return fmt.Errorf("nodemarket: creating %s: %w", h.opts.DataDir, err)
}
passwordFile := NodePasswordPath(h.opts.DataDir)
if _, err := MintOrReadNodePassword(passwordFile); err != nil {
return fmt.Errorf("nodemarket: node %s of %s: %w -- the claim names this file as the path "+
"the door reads the per-node password from, so a password that is not on disk is a "+
"claim the door can only fail on", h.opts.NodeName, h.opts.Cluster, err)
}
target, err := h.opts.Resolve(ctx)
if err != nil {
return fmt.Errorf("nodemarket: the workspace and owner DID node %s of %s is claimed under: %w",
h.opts.NodeName, h.opts.Cluster, err)
}
if h.opts.Secrets != nil {
if why := h.linksNotListening(ctx, target); why != "" {
return fmt.Errorf("%w: node %s of %s: %s", ErrLinksNotListening, h.opts.NodeName,
h.opts.Cluster, why)
}
}
if err := h.opts.Record(ctx, h.opts.NodeName, v1alpha1.NodePending); err != nil {
return fmt.Errorf("nodemarket: recording node %s of %s as Pending before its claim is "+
"written: %w -- the phase is written first because the claim is about to exist, and a "+
"gateway that died between the two would leave no record at all of a node it rented",
h.opts.NodeName, h.opts.Cluster, err)
}
if err := h.writeSecretsLocked(ctx, target); err != nil {
return fmt.Errorf("%w -- nothing was rented: the claim is written only once the node's "+
"policy is in the cluster OpenBao", err)
}
claim := h.claimLocked(target, passwordFile)
if err := h.opts.Claims.WriteNodeClaim(ctx, target.Workspace, claim); err != nil {
return fmt.Errorf("nodemarket: writing the claim for node %s of %s into %s: %w -- the claim "+
"is the whole request, so nothing was rented and the next pass may ask again",
h.opts.NodeName, h.opts.Cluster, target.Workspace, err)
}
h.provisioned = true
h.log().Info("nodemarket: node claimed from the market",
"cluster", h.opts.Cluster, "node", h.opts.NodeName, "workspace", target.Workspace,
"ownerDid", target.OwnerDid, "advertiseAddress", h.opts.AdvertiseAddress,
"apiserverPort", h.opts.ApiserverPort, "phase", v1alpha1.NodePending,
"next", "ConfirmJoined, once the door reports the claim Running")
return nil
}
func (h *Host) claimLocked(target Cluster, passwordFile string) v1alpha1.NodeClaim {
return v1alpha1.NodeClaim{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha1.GroupVersion.String(),
Kind: "NodeClaim",
},
ObjectMeta: metav1.ObjectMeta{Name: h.opts.NodeName, OwnerReferences: clusterOwner(target)},
Spec: v1alpha1.NodeClaimSpec{
ClusterRef: commonv1.LocalObjectRef{Name: h.opts.Cluster},
NodeName: h.opts.NodeName,
OwnerDid: target.OwnerDid,
ApiserverPort: h.opts.ApiserverPort,
AdvertiseAddress: h.opts.AdvertiseAddress,
TokenFile: h.opts.TokenFile,
NodePasswordFile: passwordFile,
ServerCAFile: h.opts.ServerCAFile,
DenyBidderDids: h.denied(),
},
}
}
func clusterOwner(target Cluster) []metav1.OwnerReference {
if target.UID == "" || target.Name == "" {
return nil
}
return []metav1.OwnerReference{{APIVersion: v1alpha1.GroupVersion.String(), Kind: "Cluster", Name: target.Name, UID: target.UID}}
}
func (h *Host) denied() []string {
out := append([]string(nil), h.opts.DenyBidderDids...)
if h.opts.DenyMore != nil {
for _, did := range h.opts.DenyMore() {
if !slices.Contains(out, did) {
out = append(out, did)
}
}
}
return out
}
func (h *Host) WriteAdoptionClaim(ctx context.Context) error {
h.mu.Lock()
defer h.mu.Unlock()
if err := h.opts.Validate(); err != nil {
return fmt.Errorf("nodemarket: node %s of %s: %w", h.opts.NodeName, h.opts.Cluster, err)
}
target, err := h.opts.Resolve(ctx)
if err != nil {
return fmt.Errorf("nodemarket: the workspace and owner DID node %s of %s is claimed under: %w",
h.opts.NodeName, h.opts.Cluster, err)
}
claim := h.claimLocked(target, NodePasswordPath(h.opts.DataDir))
claim.Spec.AdoptJoinedNode = true
if err := h.opts.Claims.WriteNodeClaim(ctx, target.Workspace, claim); err != nil {
return fmt.Errorf("nodemarket: writing the adoption claim for node %s of %s into %s: %w",
h.opts.NodeName, h.opts.Cluster, target.Workspace, err)
}
h.provisioned = true
h.policyWritten = true
h.secretWritten = true
h.log().Info("nodemarket: a joined Node with no claim is claimed again for adoption",
"cluster", h.opts.Cluster, "node", h.opts.NodeName, "workspace", target.Workspace,
"next", "the door matches it to its receipt and moves the claim from Adopting to Running, or fails it")
return nil
}
func (h *Host) ConfirmJoined(ctx context.Context) error {
h.mu.Lock()
defer h.mu.Unlock()
claim, err := h.read(ctx)
if err != nil {
return err
}
if claim == nil {
return fmt.Errorf("nodemarket: node %s of %s is reported by its cluster and its claim is "+
"gone: the door releases the claim only once the machine is deleted, so a cluster still "+
"naming this node is a node nothing here can account for",
h.opts.NodeName, h.opts.Cluster)
}
switch claim.Status.Phase {
case v1alpha1.NodeClaimJoining, v1alpha1.NodeClaimRunning:
case v1alpha1.NodeClaimAdopting:
return ErrAwaitingAdoption
case "", v1alpha1.NodeClaimPending:
if claim.Spec.AdoptJoinedNode {
return ErrAwaitingAdoption
}
return fmt.Errorf("nodemarket: node %s of %s is reported by its cluster while its claim is "+
"%q, not Joining", h.opts.NodeName, h.opts.Cluster, claim.Status.Phase)
case v1alpha1.NodeClaimFailed, v1alpha1.NodeClaimLost:
return fmt.Errorf("nodemarket: node %s of %s: %s",
h.opts.NodeName, h.opts.Cluster, endedMessage(claim))
default:
return fmt.Errorf("nodemarket: node %s of %s is reported by its cluster while its claim is "+
"%q, not Joining. The door writes Joining only once the guest fetched this claim's "+
"secrets, and only that guest holds the node password the agent registers with, so a "+
"cluster reporting the node before that is a node something else registered under "+
"this name", h.opts.NodeName, h.opts.Cluster, claim.Status.Phase)
}
if err := h.opts.Record(ctx, h.opts.NodeName, v1alpha1.NodeRunning); err != nil {
return fmt.Errorf("nodemarket: recording node %s of %s as Running after the door reported "+
"it: %w", h.opts.NodeName, h.opts.Cluster, err)
}
if claim.Status.Phase == v1alpha1.NodeClaimJoining && claim.Status.JoinedAt == "" {
if m, ok := h.opts.Claims.(claimJoinReporter); ok {
if target, err := h.opts.Resolve(ctx); err == nil {
if err := m.ReportNodeClaimJoined(ctx, target.Workspace, h.opts.NodeName); err != nil {
return fmt.Errorf("nodemarket: reporting the claim for node %s of %s joined: %w",
h.opts.NodeName, h.opts.Cluster, err)
}
}
}
}
return nil
}
type claimJoinReporter interface {
ReportNodeClaimJoined(ctx context.Context, logicalCluster, name string) error
}
type End struct {
Message string
Declined bool
Lost bool
Bidder string
}
func MarketDeclined(claim *v1alpha1.NodeClaim) bool {
if claim == nil || claim.Status.Phase != v1alpha1.NodeClaimFailed {
return false
}
return claim.Status.Contract.RFPURI != "" && claim.Status.Contract.AcceptURI == ""
}
func (h *Host) End(ctx context.Context) End {
ctx, cancel := context.WithTimeout(ctx, fatalReadTimeout)
defer cancel()
h.mu.Lock()
defer h.mu.Unlock()
claim, err := h.read(ctx)
if err != nil {
return End{}
}
if claim == nil {
if !h.provisioned {
return End{}
}
return End{Message: claimGoneMessage}
}
if !EndedClaimPhase(claim.Status.Phase) {
return End{}
}
end := End{Message: endedMessage(claim), Declined: MarketDeclined(claim),
Lost: claim.Status.Phase == v1alpha1.NodeClaimLost}
if wn := claim.Status.Contract.Winner; wn != nil {
end.Bidder = wn.DID
}
return end
}
func (h *Host) Terminal(ctx context.Context) string {
return h.End(ctx).Message
}
func (h *Host) Teardown(ctx context.Context) error {
return h.release(ctx)
}
func (h *Host) Reap(ctx context.Context) error {
return h.release(ctx)
}
func (h *Host) release(ctx context.Context) error {
h.mu.Lock()
defer h.mu.Unlock()
if err := h.opts.Validate(); err != nil {
return fmt.Errorf("nodemarket: node %s of %s: %w", h.opts.NodeName, h.opts.Cluster, err)
}
target, err := h.opts.Resolve(ctx)
if err != nil {
return fmt.Errorf("nodemarket: the workspace the claim for node %s of %s lives in: %w",
h.opts.NodeName, h.opts.Cluster, err)
}
var problems []error
if err := h.opts.Record(ctx, h.opts.NodeName, v1alpha1.NodeDraining); err != nil {
problems = append(problems, fmt.Errorf("nodemarket: recording node %s of %s as Draining: %w",
h.opts.NodeName, h.opts.Cluster, err))
}
h.log().Info("nodemarket: releasing the node", "cluster", h.opts.Cluster, "node", h.opts.NodeName)
if err := h.opts.Claims.DeleteNodeClaim(ctx, target.Workspace, h.opts.NodeName); err != nil {
problems = append(problems, fmt.Errorf("nodemarket: deleting the claim for node %s of %s: "+
"%w -- the claim is the only handle on the rented machine, and a delete that did not "+
"land leaves the door holding a session for a node nothing counts",
h.opts.NodeName, h.opts.Cluster, err))
return errors.Join(problems...)
}
h.revokeSecretsLocked(ctx)
if err := h.awaitRelease(ctx, target.Workspace); err != nil {
problems = append(problems, err)
}
return errors.Join(problems...)
}
func (h *Host) awaitRelease(ctx context.Context, workspace string) error {
wctx, cancel := context.WithTimeout(ctx, h.opts.ReleaseTimeout)
defer cancel()
err := h.opts.Claims.AwaitNodeClaimGone(wctx, workspace, h.opts.NodeName)
switch {
case err == nil:
return nil
case ctx.Err() != nil:
return fmt.Errorf("nodemarket: waiting for the door to release node %s of %s: %w",
h.opts.NodeName, h.opts.Cluster, ctx.Err())
case errors.Is(err, context.DeadlineExceeded):
return fmt.Errorf("nodemarket: the claim for node %s of %s is still there %s after it "+
"was deleted. The door removes its finalizer only once the market VM is gone, so "+
"this is a machine that has not been destroyed: closing is not destroying, and "+
"reporting this as a completed teardown would leave the rent running",
h.opts.NodeName, h.opts.Cluster, h.opts.ReleaseTimeout)
}
return fmt.Errorf("nodemarket: watching the claim for node %s of %s while waiting "+
"for the door to release it: %w -- a watch that failed is not a claim that is gone, "+
"and reporting a release nobody observed is how a machine that is still being paid "+
"for reads as closed", h.opts.NodeName, h.opts.Cluster, err)
}
func (h *Host) read(ctx context.Context) (*v1alpha1.NodeClaim, error) {
if err := h.opts.Validate(); err != nil {
return nil, fmt.Errorf("nodemarket: node %s of %s: %w", h.opts.NodeName, h.opts.Cluster, err)
}
target, err := h.opts.Resolve(ctx)
if err != nil {
return nil, fmt.Errorf("nodemarket: the workspace the claim for node %s of %s lives in: %w",
h.opts.NodeName, h.opts.Cluster, err)
}
claim, err := h.opts.Claims.ReadNodeClaim(ctx, target.Workspace, h.opts.NodeName)
if err != nil {
return nil, fmt.Errorf("nodemarket: reading the claim for node %s of %s from %s: %w",
h.opts.NodeName, h.opts.Cluster, target.Workspace, err)
}
return claim, nil
}
func EndedClaimPhase(phase v1alpha1.NodeClaimPhase) bool {
return phase == v1alpha1.NodeClaimFailed || phase == v1alpha1.NodeClaimReleased ||
phase == v1alpha1.NodeClaimLost
}
func endedMessage(claim *v1alpha1.NodeClaim) string {
if claim.Status.Message != "" {
return claim.Status.Message
}
return "the door ended this claim and recorded no reason"
}