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
package pds
import (
"context"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
atpds "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pds"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/atprotorepo"
pdscontract "github.com/johnandersen777/socialweb-computer-kcp/internal/contract/pds"
"github.com/johnandersen777/socialweb-computer-kcp/internal/pds/local"
)
const Kind = "Pds"
var GroupKind = schema.GroupKind{Group: v1alpha1.GroupName, Kind: Kind}
type Lookup func(workspace, name string) (*atpds.Server, bool)
type Await func(ctx context.Context, workspace, name string) (*atpds.Server, bool)
type Secrets func(ctx context.Context, workspace string, ref commonv1.SecretRef) (map[string]any, error)
type Options struct {
Bus *bus.Bus
Servers Await
Secrets Secrets
}
func Provide(opts Options) {
server := func(ctx context.Context, to commonv1.ObjectRef) (*atpds.Server, error) {
if opts.Servers == nil {
return nil, fmt.Errorf("%w: no in-process Pds lookup is configured", pdscontract.ErrUnsupported)
}
srv, ok := opts.Servers(ctx, strings.TrimSpace(to.Workspace), strings.TrimSpace(to.Name))
if !ok || srv == nil {
return nil, fmt.Errorf("%w: the Pds %s is not running in this process", pdscontract.ErrNotFound, to.String())
}
return srv, nil
}
secrets := func(workspace string) local.Secrets {
return func(ctx context.Context, ref commonv1.SecretRef) (map[string]any, error) {
if opts.Secrets == nil {
return nil, fmt.Errorf("%w: no secret resolver is configured", pdscontract.ErrUnsupported)
}
return opts.Secrets(ctx, workspace, ref)
}
}
bus.Provide(opts.Bus, GroupKind, pdscontract.CapabilityAccount, func(ctx context.Context, to commonv1.ObjectRef, scope []string) (atprotorepo.Repo, error) {
srv, err := server(ctx, to)
if err != nil {
return nil, err
}
return local.NewRepo(ctx, srv, scope)
})
bus.Provide(opts.Bus, GroupKind, pdscontract.CapabilityAccount, func(ctx context.Context, to commonv1.ObjectRef, scope []string) (pdscontract.Accounts, error) {
srv, err := server(ctx, to)
if err != nil {
return nil, err
}
return local.NewAccounts(srv, secrets(to.Workspace), scope), nil
})
bus.Provide(opts.Bus, GroupKind, pdscontract.CapabilitySession, func(ctx context.Context, to commonv1.ObjectRef, scope []string) (pdscontract.Sessions, error) {
srv, err := server(ctx, to)
if err != nil {
return nil, err
}
return local.NewSessions(srv, secrets(to.Workspace), scope), nil
})
bus.Provide(opts.Bus, GroupKind, pdscontract.CapabilityServiceAuth, func(ctx context.Context, to commonv1.ObjectRef, scope []string) (pdscontract.ServiceAuth, error) {
srv, err := server(ctx, to)
if err != nil {
return nil, err
}
return local.NewServiceAuth(srv, scope), nil
})
bus.Provide(opts.Bus, GroupKind, pdscontract.CapabilitySync, func(ctx context.Context, to commonv1.ObjectRef, _ []string) (pdscontract.Sync, error) {
srv, err := server(ctx, to)
if err != nil {
return nil, err
}
return local.NewSync(srv), nil
})
}