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
package providermain
import (
"context"
"log/slog"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/clusterprovider"
"github.com/johnandersen777/socialweb-computer-kcp/internal/materialiser"
"github.com/johnandersen777/socialweb-computer-kcp/internal/portallocator"
)
func newRoller(units *clusterprovider.UnitsHost, stack clusterprovider.StackConfig, registry clusterprovider.Clusters,
alloc *portallocator.Allocator, purposes []portallocator.Purpose, name string, log *slog.Logger) *clusterprovider.Roller {
refs := func(ctx context.Context) (map[string]clusterprovider.Ref, error) {
list, err := registry.Refs(ctx)
if err != nil {
return nil, err
}
out := map[string]clusterprovider.Ref{}
for _, r := range list {
if key, err := r.Key(); err == nil {
out[key] = r
}
}
return out, nil
}
return &clusterprovider.Roller{
Units: units,
Stack: stack,
Clusters: func(ctx context.Context) ([]string, error) {
m, err := refs(ctx)
if err != nil {
return nil, err
}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys, nil
},
Ready: func(ctx context.Context, cluster string) (bool, error) {
m, err := refs(ctx)
if err != nil {
return false, err
}
ref, ok := m[cluster]
if !ok {
return false, nil
}
cl, err := registry.Read(ctx, ref)
if err != nil {
return false, err
}
return cl != nil && cl.Status.Phase == v1alpha1.ClusterReady, nil
},
Leases: func(cluster string) (materialiser.Leases, error) {
out := materialiser.Leases{}
for _, p := range purposes {
if l, ok := alloc.PortFor(cluster, p); ok {
out[p] = l
}
}
return out, nil
},
Relet: func(ctx context.Context, cluster string, p portallocator.Purpose) (portallocator.Lease, bool, error) {
return alloc.Relet(ctx, portallocator.Request{Cluster: cluster, Purpose: p})
},
Name: name,
Log: log,
}
}