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
package childapplication
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"sync"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
ctlruntime "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
Name = "childapplication"
Export = "applications"
HostProcessExport = "hostprocesses"
Finalizer = "childapplication.socialweb.computer/workspace"
ConditionReady = "Ready"
ConditionProgressing = "Progressing"
ConditionFailed = "Failed"
ConditionOrphaned = "Orphaned"
AppWorkspaceLabel = "apps.socialweb.computer/application"
DevWorkspacePrefix = "root:dev"
ComponentWorkspace = "app"
)
type Workspace struct {
Name string
Phase string
Deleting bool
}
type Workspaces interface {
Get(ctx context.Context, parent, name string) (Workspace, bool, error)
ByLabel(ctx context.Context, parent, key, value string) (Workspace, bool, error)
}
type Lister interface {
List(ctx context.Context, workspace string, list client.ObjectList) error
}
type Options struct {
Log *slog.Logger
Workspaces Workspaces
Objects Lister
WorkspaceEvents <-chan string
Scope func(workspace string) bool
}
type networkKey struct {
cluster string
name string
}
type refs struct {
network networkKey
instance string
workspaces []string
}
type Controller struct {
log *slog.Logger
workspaces Workspaces
objects Lister
mu sync.Mutex
refs map[ctlruntime.Request]refs
}
func Register(m *ctlruntime.Manager, opts Options) (*Controller, error) {
if opts.Workspaces == nil {
return nil, errors.New("childapplication: a workspace reader is required")
}
if opts.Objects == nil {
return nil, errors.New("childapplication: a workspace object lister is required")
}
log := opts.Log
if log == nil {
log = slog.New(slog.DiscardHandler)
}
c := &Controller{log: log.With("component", Name+"-controller"), workspaces: opts.Workspaces, objects: opts.Objects,
refs: map[ctlruntime.Request]refs{}}
options := []ctlruntime.Option{ctlruntime.Export(Export), ctlruntime.Finalizer(Finalizer), ctlruntime.Gone(c.gone),
ctlruntime.Watches(func(_ context.Context, src ctlruntime.Request, obj *v1alpha1.MarketNetwork) []ctlruntime.Request {
return c.match(func(r refs) bool { return r.network == networkKey{cluster: src.Cluster, name: obj.Name} })
}),
ctlruntime.WatchesIn(HostProcessExport, func(_ context.Context, src ctlruntime.Request, _ *v1alpha1.HostProcess) []ctlruntime.Request {
instance, ok := strings.CutPrefix(src.Workspace, DevWorkspacePrefix+":")
if !ok || instance == "" {
return nil
}
return c.match(func(r refs) bool { return r.instance == instance })
}),
}
for _, kind := range componentKinds {
options = append(options, kind.watch(func(_ context.Context, src ctlruntime.Request) []ctlruntime.Request {
workspace, ok := strings.CutSuffix(src.Workspace, ":"+ComponentWorkspace)
if !ok {
return nil
}
return c.inWorkspace(workspace)
}))
}
if opts.Scope != nil {
options = append(options, ctlruntime.InWorkspaces(opts.Scope))
}
if opts.WorkspaceEvents != nil {
events, err := ctlruntime.Forward(m, Name+": workspaces", opts.WorkspaceEvents, c.inWorkspace)
if err != nil {
return nil, fmt.Errorf("childapplication: forwarding changes: %w", err)
}
options = append(options, ctlruntime.Events(events))
}
if err := ctlruntime.For[*v1alpha1.ChildApplication](m, Name, c, options...); err != nil {
return nil, err
}
return c, nil
}
func (c *Controller) inWorkspace(workspace string) []ctlruntime.Request {
return c.match(func(r refs) bool {
for _, w := range r.workspaces {
if w == workspace {
return true
}
}
return false
})
}
func (c *Controller) match(keep func(refs) bool) []ctlruntime.Request {
c.mu.Lock()
defer c.mu.Unlock()
var out []ctlruntime.Request
for req, r := range c.refs {
if keep(r) {
out = append(out, req)
}
}
return out
}
func (c *Controller) index(req ctlruntime.Request, r refs) {
c.mu.Lock()
defer c.mu.Unlock()
c.refs[ctlruntime.Request{Cluster: req.Cluster, Name: req.Name}] = r
}
func (c *Controller) gone(_ context.Context, req ctlruntime.Request) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.refs, ctlruntime.Request{Cluster: req.Cluster, Name: req.Name})
}