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
package bidder
import (
"cmp"
"context"
"errors"
"fmt"
"os/user"
"regexp"
"slices"
"strings"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/app/compose"
"github.com/johnandersen777/socialweb-computer-kcp/internal/app/marketbidder"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/kube"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostcontract"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostsvc"
"github.com/johnandersen777/socialweb-computer-kcp/internal/install"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kcpclient"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kcpserver"
)
const (
RootName = compose.ChildRootName
FieldManager = "socialweb-computer"
Poll = time.Second
PublicPlc = "https://plc.directory"
PublicIngress = "xrpc.fedproxy.com"
InstancePrefix = "b-"
maxInstanceID = 40
)
var (
handleRE = regexp.MustCompile(`^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]([a-z0-9-]*[a-z0-9])?$`)
nonLabel = regexp.MustCompile(`[^a-z0-9-]+`)
ErrNoQrID = errors.New("the bidder became ready without a sign-in naming its account")
)
func CheckHandle(handle string) error {
if !handleRE.MatchString(handle) || slices.ContainsFunc(strings.Split(handle, "."), func(l string) bool { return len(l) > 63 }) {
return fmt.Errorf("bidder: handle %q is not a lowercase atproto handle", handle)
}
return nil
}
func InstanceID(handle string) string {
id := InstancePrefix + strings.Trim(nonLabel.ReplaceAllString(strings.ReplaceAll(strings.ToLower(handle), ".", "-"), "-"), "-")
if len(id) > maxInstanceID {
id = strings.TrimRight(id[:maxInstanceID], "-")
}
return id
}
func AppName(handle string) string {
first, _, _ := strings.Cut(strings.ToLower(handle), ".")
name := strings.Trim(nonLabel.ReplaceAllString(first, "-"), "-")
name = cmp.Or(name, "bidder")
return name
}
type SpecInput struct {
Profile string
Handle string
Policy string
Network *v1alpha1.MarketNetworkValue
PdsUrl string
Mode v1alpha1.MarketIdentityMode
Kcp *v1alpha1.KcpProvider
Ingress *v1alpha1.IngressConfig
Comms *v1alpha1.MarketComms
Options []v1alpha1.ApplicationOption
GuestNetwork *v1alpha1.GuestNetworkOptions
ComputeKinds []string
}
func Spec(in SpecInput) (v1alpha1.MarketBidderSpec, error) {
mode := in.Mode
mode = cmp.Or(mode, v1alpha1.MarketIdentityModeLocalRepo)
if in.Profile == marketbidder.BidderProfileProdQr && in.Handle == "" {
return v1alpha1.MarketBidderSpec{}, errors.New("bidder: profile prod-qr needs --handle: the QR sign-in is refused for any other account")
}
if in.Handle != "" {
if err := CheckHandle(in.Handle); err != nil {
return v1alpha1.MarketBidderSpec{}, err
}
}
kinds := computeKinds(in.ComputeKinds)
if in.Kcp == nil && slices.Contains(kinds, string(v1alpha1.ComputeKindVM)) {
return v1alpha1.MarketBidderSpec{}, errors.New("bidder: the bidder's provider is compute-provider-kcp and needs its guest image source (--guest-image-source)")
}
n := in.Network
if n == nil {
n = &v1alpha1.MarketNetworkValue{}
}
n.PLCDirectoryURL = cmp.Or(n.PLCDirectoryURL, PublicPlc)
n.IngressProxyHost = cmp.Or(n.IngressProxyHost, PublicIngress)
if in.Profile == marketbidder.BidderProfileNetwork && (in.PdsUrl == "" || n.CABundle == "" || n.GuestTLSURL() == "") {
return v1alpha1.MarketBidderSpec{}, errors.New("bidder: profile market-network needs the market network's value and its bidders' PDS (--network, or --market-network-file)")
}
ingress := in.Ingress
if ingress == nil && (in.Profile == marketbidder.BidderProfileProdQr || in.Profile == marketbidder.BidderProfileNetwork) {
ingress = compose.DefaultIngressConfig()
if n.IngressProxyHost != "" {
ingress.Xrpc.IngressProxyHost = n.IngressProxyHost
}
}
return v1alpha1.MarketBidderSpec{Profile: in.Profile, Network: n, Ingress: ingress, Comms: in.Comms, Policy: in.Policy, Kcp: in.Kcp, Options: in.Options,
GuestNetwork: in.GuestNetwork, ComputeKinds: kinds, Identity: v1alpha1.ApplicationIdentity{Mode: mode, Handle: in.Handle, PdsURL: in.PdsUrl}}, nil
}
func computeKinds(in []string) []string {
var out []string
for _, k := range in {
switch k = strings.TrimSpace(k); k {
case string(v1alpha1.ComputeKindVM), string(v1alpha1.ComputeKindDeno):
if !slices.Contains(out, k) {
out = append(out, k)
}
}
}
if len(out) == 0 {
return []string{string(v1alpha1.ComputeKindVM)}
}
return out
}
func CheckSignIn(spec v1alpha1.MarketBidderSpec, id *v1alpha1.MarketIdentity) error {
if spec.Identity.Mode != v1alpha1.MarketIdentityModeOAuthQr || id == nil || id.Status.Phase != v1alpha1.MarketIdentityPhaseReady {
return nil
}
want := strings.ToLower(spec.Identity.Handle)
got := strings.ToLower(id.Status.Handle)
if got == "" {
return ErrNoQrID
}
if want != "" && got != want {
return fmt.Errorf("the session belongs to %s; scan the QR with %s", got, want)
}
return nil
}
func CheckAccount(backend hostsvc.Backend, handle, root string) error {
if backend != hostsvc.BackendSystemdUser {
return nil
}
u, err := user.Current()
if err != nil {
return err
}
rec, err := install.ReadRecord(root, u.Username)
if err != nil {
return fmt.Errorf("bidder: host-backend=systemd-user runs only as an account install bidder made, and %s has no install record: %w", u.Username, err)
}
if rec.Role != hostcontract.RoleBidder || rec.Account != u.Username {
return fmt.Errorf("bidder: host-backend=systemd-user: %s was installed as %s %s, not as a bidder account", u.Username, rec.Role, rec.Account)
}
if slices.Contains(rec.Handles, handle) {
return nil
}
return fmt.Errorf("bidder: install bidder did not install handle %s (installed: %s)", handle, strings.Join(rec.Handles, ", "))
}
type Kcp struct {
rest *rest.Config
dyn *kube.Client
}
func OpenKcp(kubeconfig string) (*Kcp, error) {
cfg, err := kcpserver.FrontRestConfig(kubeconfig)
if err != nil {
return nil, err
}
root := rest.CopyConfig(cfg)
root.Host = strings.TrimSuffix(cfg.Host, "/") + "/clusters/" + compose.DefaultWorkspace
dc, err := kube.New(root)
if err != nil {
return nil, err
}
return &Kcp{rest: cfg, dyn: dc}, nil
}
func (k *Kcp) ApplyRoot(ctx context.Context, spec v1alpha1.MarketBidderSpec) error {
if err := compose.AwaitBound(ctx, k.dyn.Resource(compose.APIBindingGVR), compose.RootBindTimeout, compose.RootExports...); err != nil {
return err
}
obj := &v1alpha1.MarketBidder{TypeMeta: metav1.TypeMeta{APIVersion: v1alpha1.GroupVersion.String(), Kind: string(v1alpha1.ApplicationKindMarketBidder)},
ObjectMeta: metav1.ObjectMeta{Name: RootName}, Spec: spec}
_, err := kube.Apply[kube.Doc](ctx, k.dyn.Resource(compose.MarketBidderGVR), obj, kube.ApplyOptions{FieldManager: FieldManager, Force: true})
return err
}
func (k *Kcp) Root(ctx context.Context) (*v1alpha1.MarketBidder, error) {
return kube.Get[v1alpha1.MarketBidder](ctx, k.dyn.Resource(compose.MarketBidderGVR), RootName)
}
func (k *Kcp) Identities() (*kcpclient.MarketIdentities, error) {
c, err := kcpclient.New(kcpclient.Options{Host: k.rest.Host, RestConfig: k.rest})
if err != nil {
return nil, err
}
return c.MarketIdentities(compose.DefaultWorkspace)
}
func (k *Kcp) WaitIdentityObject(ctx context.Context, timeout time.Duration) error {
_, err := kube.Await(ctx, k.dyn.Resource(compose.MarketIdentityGVR), marketbidder.BidderIdentity, timeout, kube.Present[kube.Doc])
if errors.Is(err, kube.ErrAwaitTimeout) {
missing := apierrors.NewNotFound(compose.MarketIdentityGVR.GroupResource(), marketbidder.BidderIdentity)
return fmt.Errorf("bidder: the core created no MarketIdentity %s within %s: %w", marketbidder.BidderIdentity, timeout, missing)
}
return err
}
func (k *Kcp) WaitReady(ctx context.Context, timeout time.Duration, progress func(v1alpha1.ApplicationStatus)) (*v1alpha1.MarketBidder, error) {
last := ""
o, err := kube.Await(ctx, k.dyn.Resource(compose.MarketBidderGVR), RootName, timeout, func(o *v1alpha1.MarketBidder) (bool, error) {
if o == nil {
return false, nil
}
if key := string(o.Status.Phase) + o.Status.Message; key != last && progress != nil {
progress(o.Status)
last = key
}
return o.Status.Phase == v1alpha1.ApplicationPhaseReady || o.Status.Phase == v1alpha1.ApplicationPhaseFailed, nil
})
switch {
case errors.Is(err, kube.ErrAwaitTimeout):
return o, fmt.Errorf("bidder: the MarketBidder was not Ready within %s", timeout)
case err != nil:
return o, err
case o.Status.Phase == v1alpha1.ApplicationPhaseFailed:
return o, fmt.Errorf("bidder: the MarketBidder failed: %s", o.Status.Message)
}
return o, nil
}
func (k *Kcp) AwaitIdentity(ctx context.Context, name string, each func(*v1alpha1.MarketIdentity) (bool, error)) (*v1alpha1.MarketIdentity, error) {
return kube.Await(ctx, k.dyn.Resource(compose.MarketIdentityGVR), name, 0, each)
}