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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package v1alpha1
import (
"cmp"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
)
type ComputeVM struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ComputeVMSpec `json:"spec,omitempty"`
Status ComputeVMStatus `json:"status,omitempty"`
}
type ComputeVMList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ComputeVM `json:"items"`
}
type ComputeVMSpec struct {
BackendRef commonv1.ObjectRef `json:"backendRef"`
VM ComputeVMShape `json:"vm"`
Owner ComputeVMOwner `json:"owner"`
UserData ComputeVMUserData `json:"userData"`
Requester ComputeVMRequester `json:"requester"`
Contract *ComputeVMContract `json:"contract,omitempty"`
Image *ComputeVMImageRequest `json:"image,omitempty"`
StopGraceSeconds int32 `json:"stopGraceSeconds,omitempty"`
GuestLogin *ComputeVMGuestLogin `json:"guestLogin,omitempty"`
Accept *commonv1.StrongRef `json:"accept,omitempty"`
Release *ComputeVMRelease `json:"release,omitempty"`
AssociationExpiresAt string `json:"associationExpiresAt,omitempty"`
HostPorts []int32 `json:"hostPorts,omitempty" apigen:"listType=set"`
Sandbox *ComputeVMSandbox `json:"sandbox,omitempty"`
GuestNetwork *GuestNetworkOptions `json:"guestNetwork,omitempty"`
}
type ComputeVMSandbox struct {
Source string `json:"source"`
TimeoutSeconds int32 `json:"timeoutSeconds,omitempty"`
}
type GuestNetworkOptions struct {
LANUDP *bool `json:"lanUDP,omitempty"`
HostUDP *bool `json:"hostUDP,omitempty"`
WebRTCUDPPorts *GuestUDPPortRange `json:"webrtcUDPPorts,omitempty"`
WebRTCUDPPort int32 `json:"webrtcUDPPort,omitempty" apigen:"minimum=0,maximum=65535"`
}
type GuestUDPPortRange struct {
Min int32 `json:"min" apigen:"minimum=1024,maximum=65535"`
Max int32 `json:"max" apigen:"minimum=1024,maximum=65535"`
}
const (
DefaultWebRTCUDPPortMin int32 = 31000
DefaultWebRTCUDPPortMax int32 = 31999
)
const (
BidderOnlyMePolicy = "bidder-only-me"
BidderTangledVouchPolicy = "bidder-tangled-vouch"
BidderDefaultPolicy = BidderTangledVouchPolicy
RequesterTangledVouchPolicy = "requester-tangled-vouch"
RequesterDefaultPolicy = RequesterTangledVouchPolicy
HostUDPReasonSpec = "spec"
HostUDPReasonOnlyMe = "policy only-me"
HostUDPReasonNotOnlyMe = "policy not only-me"
)
type GuestNetworkStatus struct {
LANUDP bool `json:"lanUDP"`
HostUDP bool `json:"hostUDP"`
HostUDPReason string `json:"hostUDPReason,omitempty"`
WebRTCUDPPorts *GuestUDPPortRange `json:"webrtcUDPPorts,omitempty"`
}
func (g *GuestNetworkOptions) LANUDPAllowed() bool {
return g == nil || g.LANUDP == nil || *g.LANUDP
}
func (g *GuestNetworkOptions) HostUDPAllowed() bool {
return g != nil && g.HostUDP != nil && *g.HostUDP
}
func (g *GuestNetworkOptions) WebRTCUDPPortRange() GuestUDPPortRange {
if g == nil || g.WebRTCUDPPorts == nil {
return GuestUDPPortRange{Min: DefaultWebRTCUDPPortMin, Max: DefaultWebRTCUDPPortMax}
}
return *g.WebRTCUDPPorts
}
func (g *GuestNetworkOptions) WebRTCUDPPortOf() int32 {
if g == nil {
return 0
}
return g.WebRTCUDPPort
}
func BidderPolicyOnlyMe(policy string) bool {
return strings.TrimSpace(policy) == BidderOnlyMePolicy
}
func (g *GuestNetworkOptions) EffectiveFor(policy string) GuestNetworkStatus {
ports := g.WebRTCUDPPortRange()
st := GuestNetworkStatus{LANUDP: g.LANUDPAllowed(), WebRTCUDPPorts: &ports}
switch {
case g != nil && g.HostUDP != nil:
st.HostUDP, st.HostUDPReason = *g.HostUDP, HostUDPReasonSpec
case BidderPolicyOnlyMe(policy):
st.HostUDP, st.HostUDPReason = true, HostUDPReasonOnlyMe
default:
st.HostUDPReason = HostUDPReasonNotOnlyMe
}
return st
}
func (g *GuestNetworkOptions) WithDefaults(d *GuestNetworkOptions) *GuestNetworkOptions {
out := GuestNetworkOptions{}
if g != nil {
out = *g
}
if d != nil {
out.LANUDP = cmp.Or(out.LANUDP, d.LANUDP)
out.HostUDP = cmp.Or(out.HostUDP, d.HostUDP)
out.WebRTCUDPPorts = cmp.Or(out.WebRTCUDPPorts, d.WebRTCUDPPorts)
}
return &out
}
func GuestNetworkWith(lanUDP, hostUDP bool, webrtcUDPPort int32) *GuestNetworkOptions {
return &GuestNetworkOptions{LANUDP: &lanUDP, HostUDP: &hostUDP, WebRTCUDPPort: webrtcUDPPort}
}
type ComputeVMRelease struct {
RequestedAt string `json:"requestedAt"`
Request *commonv1.StrongRef `json:"request,omitempty"`
}
type ComputeVMGuestLogin struct {
Mount string `json:"mount"`
BindSecretSha256 string `json:"bindSecretSha256"`
}
type ComputeVMUserData struct {
Vault commonv1.SecretRef `json:"vault"`
}
type ComputeVMShape struct {
Cpus int32 `json:"cpus"`
Mem string `json:"mem"`
Disk string `json:"disk"`
Role string `json:"role"`
}
type ComputeVMOwner struct {
Handle string `json:"handle"`
}
type ComputeVMRequester struct {
Did string `json:"did"`
Plc string `json:"plc,omitempty"`
}
type ComputeVMContract struct {
Receipt commonv1.StrongRef `json:"receipt,omitempty"`
Accept commonv1.StrongRef `json:"accept,omitempty"`
SubmitEventURL string `json:"submitEventUrl,omitempty" apigen:"userEndpoint"`
AcceptedAt string `json:"acceptedAt,omitempty"`
}
type ComputeVMImageRequest struct {
ReuseStale bool `json:"reuseStale,omitempty"`
}
type ComputeVMPhase string
const (
ComputeVMPhasePending ComputeVMPhase = "Pending"
ComputeVMPhasePreparing ComputeVMPhase = "Preparing"
ComputeVMPhaseBooting ComputeVMPhase = "Booting"
ComputeVMPhaseRunning ComputeVMPhase = "Running"
ComputeVMPhaseReleasing ComputeVMPhase = "Releasing"
ComputeVMPhaseLost ComputeVMPhase = "Lost"
ComputeVMPhaseFailed ComputeVMPhase = "Failed"
)
type ComputeVMLostReason string
const (
ComputeVMLostHostRebooted ComputeVMLostReason = "HostRebooted"
ComputeVMLostUnitGone ComputeVMLostReason = "UnitGone"
ComputeVMLostUnitFailed ComputeVMLostReason = "UnitFailed"
ComputeVMLostOOMKilled ComputeVMLostReason = "OOMKilled"
ComputeVMLostVMMExited ComputeVMLostReason = "VMMExited"
ComputeVMLostGuestPoweroff ComputeVMLostReason = "GuestPoweroff"
ComputeVMLostIdentityMismatch ComputeVMLostReason = "IdentityMismatch"
)
type ComputeVMAddresses struct {
Guest string `json:"guest,omitempty"`
Host string `json:"host,omitempty"`
}
type ComputeVMHostKey struct {
Type string `json:"type"`
PublicKey string `json:"publicKey"`
Fingerprint string `json:"fingerprint,omitempty"`
}
type ComputeVMMarket struct {
Guest *ComputeVMGuestReport `json:"guest,omitempty"`
Published *ComputeVMPublished `json:"published,omitempty"`
}
type ComputeVMGuestReport struct {
Address string `json:"address,omitempty"`
ReportedAt string `json:"reportedAt,omitempty"`
HostKeys []ComputeVMHostKey `json:"hostKeys,omitempty"`
Networks []ComputeVMGuestNetwork `json:"networks,omitempty"`
}
type ComputeVMGuestNetwork struct {
Tag string `json:"tag"`
Kind NetworkKind `json:"kind,omitempty" apigen:"enum=iroh|https|webrtc"`
Address string `json:"address"`
ReportedAt string `json:"reportedAt,omitempty"`
}
type ComputeVMPublished struct {
OnNetworkURI string `json:"onNetworkUri,omitempty"`
HostKeysURI string `json:"hostKeysUri,omitempty"`
Networks []ComputeVMPublishedNetwork `json:"networks,omitempty"`
}
type ComputeVMPublishedNetwork struct {
Tag string `json:"tag"`
Kind NetworkKind `json:"kind,omitempty" apigen:"enum=iroh|https|webrtc"`
URI string `json:"uri"`
}
type ComputeVMTermination struct {
Reason string `json:"reason,omitempty"`
ObservedAt string `json:"observedAt,omitempty"`
ExitCode *int `json:"exitCode,omitempty"`
Request *commonv1.StrongRef `json:"request,omitempty"`
PayloadURI string `json:"payloadUri,omitempty"`
PayloadCid string `json:"payloadCid,omitempty"`
EventURI string `json:"eventUri,omitempty"`
EventCid string `json:"eventCid,omitempty"`
}
type ComputeVMStatus struct {
Phase ComputeVMPhase `json:"phase,omitempty"`
LostReason ComputeVMLostReason `json:"lostReason,omitempty"`
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
Addresses *ComputeVMAddresses `json:"addresses,omitempty"`
BootedAt string `json:"bootedAt,omitempty"`
Message string `json:"message,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
Market *ComputeVMMarket `json:"market,omitempty"`
}
const (
ComputeVMConditionReady = commonv1.ConditionReady
ComputeVMConditionBackendBound = commonv1.ConditionBackendBound
)
const BackendKindFirecrackerVM = "FirecrackerVM"
const BackendKindDenoSandbox = "DenoSandbox"
const FinalizerComputeVM = "computevm.socialweb.computer/release"
const (
FieldManagerComputeVMCreator = "socialweb.computer/bidder"
FieldManagerComputeVMController = "socialweb.computer/computevm"
FieldManagerComputeVMMarketGuest = "socialweb.computer/guestcontrolplane"
FieldManagerComputeVMMarketPublished = "socialweb.computer/bidder"
)
const GuestHostAddress = "192.0.2.1"
const (
GuestVaultPrefix = "guests"
GuestUserDataEntry = "user-data"
GuestUserDataKey = "userData"
GuestProvisioningEntry = "provisioning"
)