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
package remotepds
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/url"
"strings"
"sync"
"time"
"github.com/bluesky-social/indigo/atproto/syntax"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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/internal/bus"
ctlruntime "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime"
vaultctl "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/vault"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/binding"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/conds"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/opt"
"github.com/johnandersen777/socialweb-computer-kcp/internal/pds/remote"
)
const (
Name = "remotepds"
Export = "pds"
ConditionReady = "Ready"
ConditionFailed = "Failed"
ConditionSessionExpired = "SessionExpired"
DefaultTimeout = 10 * time.Second
DefaultCheckInterval = 30 * time.Second
)
type Options struct {
Log *slog.Logger
Bus *bus.Bus
Timeout time.Duration
CheckInterval time.Duration
}
type Controller struct {
log *slog.Logger
roots *vaultctl.Roots
bus *bus.Bus
mu sync.Mutex
sessions map[binding.Target]heldSession
timeout time.Duration
checkInterval time.Duration
hosts *binding.Table[*remote.Host]
}
type transientError struct {
err error
}
func (e transientError) Error() string { return e.err.Error() }
func (e transientError) Unwrap() error { return e.err }
func New(opts Options) *Controller {
log := opts.Log
if log == nil {
log = slog.New(slog.DiscardHandler)
}
timeout := opts.Timeout
timeout = opt.Positive(timeout, DefaultTimeout)
checkInterval := opts.CheckInterval
checkInterval = ctlruntime.Period(checkInterval, DefaultCheckInterval)
return &Controller{log: log.With("component", "remotepds-controller"), roots: vaultctl.NewRoots(opts.Bus, log), bus: opts.Bus,
sessions: map[binding.Target]heldSession{}, timeout: timeout, checkInterval: checkInterval, hosts: binding.New[*remote.Host]()}
}
func Register(m *ctlruntime.Manager, opts Options) (*Controller, error) {
ctl := New(opts)
ctl.Provide()
if err := ctlruntime.For[*pdsv1alpha1.RemotePDS](m, Name, ctl, ctlruntime.Export(Export)); err != nil {
return nil, fmt.Errorf("remotepds: registering the RemotePDS controller: %w", err)
}
return ctl, nil
}
func (c *Controller) Reconcile(ctx context.Context, req ctlruntime.Request, object *pdsv1alpha1.RemotePDS) (ctlruntime.Result, error) {
target := binding.Target{Cluster: req.Cluster, Workspace: req.Workspace, Name: req.Name}
if !object.DeletionTimestamp.IsZero() {
c.hosts.Set(target, nil)
c.roots.Drop(self(req, object))
c.mu.Lock()
delete(c.sessions, target)
c.mu.Unlock()
return ctlruntime.Result{}, nil
}
generation := object.Generation
wasReady := object.Status.Ready && object.Status.Phase == pdsv1alpha1.RemotePDSReady
status := object.Status
status.Phase = pdsv1alpha1.RemotePDSPending
status.Ready = false
status.Endpoint = ""
if object.Spec.Suspend {
conds.Set(&status.Conditions, generation, ConditionReady, false, "Suspended", "the RemotePDS is suspended")
conds.Set(&status.Conditions, generation, ConditionFailed, false, "Suspended", "the RemotePDS is suspended")
return ctlruntime.Result{}, c.apply(ctx, req, object, status)
}
checkedAt := metav1.NewTime(time.Now().UTC().Truncate(time.Second))
status.LastCheckedAt = &checkedAt
host, session, endpoint, reason, err := c.connect(ctx, req, target, object)
if err != nil {
c.hosts.Set(target, nil)
return c.fail(ctx, req, object, status, reason, err)
}
c.hosts.Set(target, &host)
info, err := host.Describe(ctx)
if err != nil {
return c.fail(ctx, req, object, status, "DescribeFailed", transientError{err: err})
}
status.Phase = pdsv1alpha1.RemotePDSReady
status.Ready = true
status.Endpoint = endpoint
message := fmt.Sprintf("the PDS at %s reports %s", endpoint, info.DID)
conds.Set(&status.Conditions, generation, ConditionReady, true, "Ready", message)
conds.Set(&status.Conditions, generation, ConditionFailed, false, "DescribeSucceeded", "the PDS answered describeServer")
sessionCondition(ctx, &status, generation, session)
if err := c.apply(ctx, req, object, status); err != nil {
return ctlruntime.Result{}, err
}
if !wasReady {
c.log.Info("the RemotePDS is ready", "cluster", req.Cluster, "workspace", req.Workspace, "name", req.Name,
"endpoint", endpoint, "fieldManager", req.FieldManager())
}
return ctlruntime.RefreshAfter(c.checkInterval), nil
}
func (c *Controller) fail(ctx context.Context, req ctlruntime.Request, object *pdsv1alpha1.RemotePDS, status pdsv1alpha1.RemotePDSStatus,
reason string, err error) (ctlruntime.Result, error) {
generation := object.Generation
status.Phase = pdsv1alpha1.RemotePDSFailed
conds.Set(&status.Conditions, generation, ConditionReady, false, reason, err.Error())
conds.Set(&status.Conditions, generation, ConditionFailed, true, reason, err.Error())
if applyErr := c.apply(ctx, req, object, status); applyErr != nil {
return ctlruntime.Result{}, errors.Join(err, applyErr)
}
var transient transientError
if errors.As(err, &transient) {
return ctlruntime.Result{}, fmt.Errorf("remotepds: %s: %w", reason, err)
}
c.log.Warn("the RemotePDS spec is invalid; it waits for a spec change", "workspace", req.Workspace, "name", req.Name,
"reason", reason, "err", err.Error())
return ctlruntime.Result{}, nil
}
func (c *Controller) apply(ctx context.Context, req ctlruntime.Request, object *pdsv1alpha1.RemotePDS, status pdsv1alpha1.RemotePDSStatus) error {
unchecked := status
unchecked.LastCheckedAt = object.Status.LastCheckedAt
if object.Status.LastCheckedAt != nil && apiequality.Semantic.DeepEqual(unchecked, object.Status) {
return nil
}
next := object.DeepCopy()
next.Status = status
return req.ApplyStatus(ctx, next)
}
func (c *Controller) connect(ctx context.Context, req ctlruntime.Request, target binding.Target,
object *pdsv1alpha1.RemotePDS) (*remote.Host, *remote.Session, string, string, error) {
baseURL, err := remoteURL(object.Spec.URL)
if err != nil {
return nil, nil, "", "InvalidURL", err
}
didText := strings.TrimSpace(object.Spec.DID)
if didText != "" {
if _, err := syntax.ParseDID(didText); err != nil {
return nil, nil, "", "InvalidDID", fmt.Errorf("the RemotePDS did %q is invalid", didText)
}
}
from := self(req, object)
tlsConfig, err := c.tlsConfig(ctx, from, object.Spec.TLS, baseURL)
if err != nil {
return nil, nil, "", "InvalidTLS", transientError{err: err}
}
endpoint := strings.TrimRight(baseURL.String(), "/")
session, err := c.session(ctx, target, from, object.Spec, endpoint, tlsConfig)
if err != nil {
return nil, nil, "", "InvalidAuth", transientError{err: err}
}
host, err := remote.New(remote.Config{URL: endpoint, DID: didText, TLS: tlsConfig, Auth: session.Auth(), Secrets: c.vaultSecrets(from),
Timeout: c.timeout})
if err != nil {
return nil, nil, "", "InvalidConfig", err
}
return host, session, endpoint, "", nil
}
type heldSession struct {
fingerprint string
session *remote.Session
}
func (c *Controller) session(ctx context.Context, target binding.Target, from commonv1.ObjectRef, spec pdsv1alpha1.RemotePDSSpec,
endpoint string, tlsConfig *tls.Config) (*remote.Session, error) {
raw, err := json.Marshal(struct {
Endpoint string
TLS pdsv1alpha1.RemotePDSTLS
Auth pdsv1alpha1.RemotePDSAuth
}{endpoint, spec.TLS, spec.Auth})
if err != nil {
return nil, err
}
fingerprint := string(raw)
c.mu.Lock()
held, ok := c.sessions[target]
c.mu.Unlock()
if ok && held.fingerprint == fingerprint {
return held.session, nil
}
session, err := remote.NewSession(ctx, remote.SessionConfig{Auth: spec.Auth, URL: endpoint, TLS: tlsConfig,
Read: c.vaultSecrets(from), Write: c.vaultWriter(from)})
if err != nil {
return nil, err
}
c.mu.Lock()
c.sessions[target] = heldSession{fingerprint: fingerprint, session: session}
c.mu.Unlock()
return session, nil
}
func sessionCondition(ctx context.Context, status *pdsv1alpha1.RemotePDSStatus, generation int64, session *remote.Session) {
if session == nil {
apimeta.RemoveStatusCondition(&status.Conditions, ConditionSessionExpired)
return
}
err := session.Check(ctx)
switch {
case err == nil:
conds.Set(&status.Conditions, generation, ConditionSessionExpired, false, "SessionLive", "the RemotePDS session holds a live access token")
case errors.Is(err, remote.ErrSessionExpired):
conds.Set(&status.Conditions, generation, ConditionSessionExpired, true, "RefreshRefused",
"the PDS refused the refresh token; the session secret needs a new sign-in: "+err.Error())
default:
conds.SetStatus(&status.Conditions, generation, ConditionSessionExpired, metav1.ConditionUnknown, "SessionUnchecked", err.Error())
}
}
func remoteURL(raw string) (*url.URL, error) {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
return nil, errors.New("the RemotePDS has no url")
}
parsed, err := url.Parse(trimmed)
if err != nil {
return nil, fmt.Errorf("the RemotePDS url does not parse: %w", err)
}
if parsed.Host == "" {
return nil, errors.New("the RemotePDS url has no host")
}
if parsed.User != nil {
return nil, errors.New("the RemotePDS url must not carry user info")
}
scheme := strings.ToLower(parsed.Scheme)
if scheme != "http" && scheme != "https" {
return nil, errors.New("the RemotePDS url needs an http or https scheme")
}
parsed.Scheme = scheme
parsed.RawQuery = ""
parsed.Fragment = ""
return parsed, nil
}
func (c *Controller) tlsConfig(ctx context.Context, from commonv1.ObjectRef, spec pdsv1alpha1.RemotePDSTLS, baseURL *url.URL) (*tls.Config, error) {
caSet := spec.CARef != nil || spec.CASecretRef != nil
serverName := strings.TrimSpace(spec.ServerName)
if !spec.Enabled {
if caSet || serverName != "" {
return nil, errors.New("tls fields are set but tls.enabled is false")
}
return nil, nil
}
if baseURL.Scheme != "https" {
return nil, errors.New("tls is enabled but the RemotePDS url is not https")
}
config := &tls.Config{MinVersion: tls.VersionTLS12}
if serverName != "" {
config.ServerName = serverName
}
pool, err := c.roots.Pool(ctx, from, spec.CARef, spec.CASecretRef)
if err != nil {
return nil, err
}
if pool != nil {
config = pool.PublicClientConfig(config)
}
return config, nil
}
func self(req ctlruntime.Request, object *pdsv1alpha1.RemotePDS) commonv1.ObjectRef {
return commonv1.ObjectRef{Group: pdsv1alpha1.GroupName, Kind: Kind, Name: object.Name, Workspace: req.Workspace}
}
func (c *Controller) vaultWriter(from commonv1.ObjectRef) remote.SecretWriter {
if c.bus == nil {
return nil
}
return func(ctx context.Context, ref commonv1.SecretRef, data map[string]any) error {
return vaultctl.WriteSecret(ctx, c.bus, from, ref, data)
}
}
func (c *Controller) vaultSecrets(from commonv1.ObjectRef) remote.Secrets {
if c.bus == nil {
return nil
}
return func(ctx context.Context, ref commonv1.SecretRef) (map[string]any, error) {
return vaultctl.ReadSecret(ctx, c.bus, from, ref)
}
}