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
package compose
import (
"cmp"
"context"
"fmt"
"log/slog"
"path/filepath"
"strings"
"sync"
"time"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
pdsv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/pds/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/kube"
"github.com/johnandersen777/socialweb-computer-kcp/internal/devinstance"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostsvc"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kcpserver"
)
const (
ManifestSet = "applications"
Finalizer = "apps.socialweb.computer/components"
ComponentWorkspace = "app"
DefaultWorkspace = "root"
ApplyTimeout = time.Minute
ComponentsDir = "components"
AppsDir = "apps"
)
type Config struct {
Kubeconfig string
Workspace string
Layout devinstance.Layout
Manager hostsvc.Manager
Record func() (devinstance.Record, error)
ReadyTimeout time.Duration
Listeners func(owner string) []v1alpha1.ApplicationListener
OnComponents func(owner string, listeners []v1alpha1.ApplicationListener)
StoreUsage func() *v1alpha1.StoreUsage
TTLReport func() *v1alpha1.TTLStatus
Kube *kube.Client
Bus *bus.Bus
RemotePDS *pdsv1alpha1.RemotePDS
Rest *rest.Config
Log *slog.Logger
}
type Composer struct {
cfg Config
restCfg *rest.Config
log *slog.Logger
mu sync.Mutex
clients map[string]*kube.Client
applied map[string]int64
forgotten map[string]bool
samples samples
adoptions adoptions
parking parking
roots rootIndex
drift map[string]*driftState
trusts Trusts
requesterSets sync.Map
requesters requesterIndex
applies applyCache
linkSets linkSets
}
func New(cfg Config) (*Composer, error) {
var restCfg *rest.Config
if cfg.Rest != nil {
restCfg = rest.CopyConfig(cfg.Rest)
if restCfg.QPS == 0 {
restCfg.QPS, restCfg.Burst = kcpserver.ClientQPS, kcpserver.ClientBurst
}
} else if cfg.Kube == nil {
var err error
restCfg, err = kcpserver.FrontRestConfig(cfg.Kubeconfig)
if err != nil {
return nil, err
}
}
log := cfg.Log
if log == nil {
log = slog.New(slog.DiscardHandler)
}
return &Composer{cfg: cfg, restCfg: restCfg, log: log.With("component", "app-compose"), clients: map[string]*kube.Client{},
applied: map[string]int64{}, forgotten: map[string]bool{}, roots: rootIndex{byCluster: map[string]map[string]map[string]bool{}}}, nil
}
func (c *Composer) Config() Config { return c.cfg }
func (c *Composer) Log() *slog.Logger { return c.log }
func (c *Composer) Bus() *bus.Bus { return c.cfg.Bus }
func (c *Composer) ProvideTrust(gk schema.GroupKind) { c.trusts.Provide(c.cfg.Bus, gk) }
func (c *Composer) PublishTrust(ref commonv1.ObjectRef, bundle []byte) { c.trusts.Publish(ref, bundle) }
func (c *Composer) Workspace() string {
return cmp.Or(strings.TrimSpace(c.cfg.Workspace), DefaultWorkspace)
}
func (c *Composer) StateRoot() string { return c.cfg.Layout.StateDir }
func (c *Composer) RestConfig() *rest.Config { return c.restCfg }
func (c *Composer) KcpServer() string {
if c.restCfg == nil {
return ""
}
server, _, _ := strings.Cut(c.restCfg.Host, "/clusters/")
return server
}
func (c *Composer) Kube(ws string) (*kube.Client, error) {
if c.cfg.Kube != nil {
return c.cfg.Kube, nil
}
ws = cmp.Or(strings.TrimSpace(ws), c.Workspace())
c.mu.Lock()
kc, ok := c.clients[ws]
c.mu.Unlock()
if !ok {
var err error
kc, err = kube.ForWorkspace(c.restCfg, ws)
if err != nil {
return nil, err
}
c.mu.Lock()
c.clients[ws] = kc
c.mu.Unlock()
}
return kc, nil
}
func (c *Composer) Client(ws string, gvr schema.GroupVersionResource) (kube.Resource, error) {
kc, err := c.Kube(ws)
if err != nil {
return kube.Resource{}, err
}
return kc.Resource(gvr), nil
}
func (c *Composer) Applied(r Root) bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.applied[string(r.UID)] == r.Generation
}
func (c *Composer) MarkApplied(r Root) {
c.mu.Lock()
defer c.mu.Unlock()
c.applied[string(r.UID)] = r.Generation
delete(c.forgotten, string(r.UID))
}
func (c *Composer) AppliedBefore(r Root, recorded *v1alpha1.AppliedSet, digest string) bool {
c.mu.Lock()
defer c.mu.Unlock()
return !c.forgotten[string(r.UID)] && recorded != nil && digest != "" &&
recorded.Generation == r.Generation && recorded.Digest == digest
}
func (c *Composer) Forget(r Root) {
c.mu.Lock()
delete(c.applied, string(r.UID))
c.forgotten[string(r.UID)] = true
c.mu.Unlock()
c.adoptions.forget(r)
}
func ComponentStateDir(instanceState, component string) string {
return filepath.Join(instanceState, ComponentsDir, component)
}
func ComponentCRStateDir(instanceState, kind, name string) string {
return filepath.Join(instanceState, ComponentsDir, kind+"-"+name)
}
func failf(format string, args ...any) error {
return fmt.Errorf("compose: "+format, args...)
}
type contextKey struct{}
func WithRoot(ctx context.Context, r Root) context.Context {
return context.WithValue(ctx, contextKey{}, r)
}
func RootFrom(ctx context.Context) (Root, bool) {
r, ok := ctx.Value(contextKey{}).(Root)
return r, ok
}