-
Notifications
You must be signed in to change notification settings - Fork 8
/
cluster.go
573 lines (468 loc) · 18.3 KB
/
cluster.go
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package nscloud
import (
"context"
"encoding/json"
"io"
"time"
"github.com/bcicen/jstream"
"github.com/dustin/go-humanize"
"go.uber.org/atomic"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/internal/build/registry"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/environment"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/internal/runtime/kubernetes"
"namespacelabs.dev/foundation/internal/runtime/kubernetes/client"
"namespacelabs.dev/foundation/schema"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
"namespacelabs.dev/foundation/universe/nscloud/configuration"
)
const machineEndpoint = "https://grpc-gateway-84umfjt8rm05f5dimftg.prod-metal.namespacelabs.nscloud.dev"
const registryAddr = "registry-fgfo23t6gn9jd834s36g.prod-metal.namespacelabs.nscloud.dev"
var (
startCreateKubernetesCluster = fnapi.Call[CreateKubernetesClusterRequest]{
Endpoint: machineEndpoint,
Method: "nsl.vm.api.VMService/StartCreateKubernetesCluster",
PreAuthenticateRequest: func(user *fnapi.UserAuth, rt *CreateKubernetesClusterRequest) error {
rt.OpaqueUserAuth = user.Opaque
return nil
},
}
getKubernetesCluster = fnapi.Call[GetKubernetesClusterRequest]{
Endpoint: machineEndpoint,
Method: "nsl.vm.api.VMService/GetKubernetesCluster",
PreAuthenticateRequest: func(user *fnapi.UserAuth, rt *GetKubernetesClusterRequest) error {
rt.OpaqueUserAuth = user.Opaque
return nil
},
}
waitKubernetesCluster = fnapi.Call[WaitKubernetesClusterRequest]{
Endpoint: machineEndpoint,
Method: "nsl.vm.api.VMService/WaitKubernetesCluster",
PreAuthenticateRequest: func(user *fnapi.UserAuth, rt *WaitKubernetesClusterRequest) error {
rt.OpaqueUserAuth = user.Opaque
return nil
},
}
listKubernetesClusters = fnapi.Call[ListKubernetesClustersRequest]{
Endpoint: machineEndpoint,
Method: "nsl.vm.api.VMService/ListKubernetesClusters",
PreAuthenticateRequest: func(ua *fnapi.UserAuth, rt *ListKubernetesClustersRequest) error {
rt.OpaqueUserAuth = ua.Opaque
return nil
},
}
destroyKubernetesCluster = fnapi.Call[DestroyKubernetesClusterRequest]{
Endpoint: machineEndpoint,
Method: "nsl.vm.api.VMService/DestroyKubernetesCluster",
PreAuthenticateRequest: func(ua *fnapi.UserAuth, rt *DestroyKubernetesClusterRequest) error {
rt.OpaqueUserAuth = ua.Opaque
return nil
},
}
)
var clusterConfigType = cfg.DefineConfigType[*PrebuiltCluster]()
func RegisterClusterProvider() {
client.RegisterConfigurationProvider("nscloud", provideCluster)
kubernetes.RegisterOverrideClass("nscloud", provideClass)
cfg.RegisterConfigurationProvider(func(cluster *configuration.Cluster) ([]proto.Message, error) {
if cluster.ClusterId == "" {
return nil, fnerrors.BadInputError("cluster_id must be specified")
}
return []proto.Message{
&client.HostEnv{Provider: "nscloud"},
®istry.Provider{Provider: "nscloud"},
&PrebuiltCluster{ClusterId: cluster.ClusterId},
}, nil
}, "foundation.providers.nscloud.config.Cluster")
}
func provideCluster(ctx context.Context, cfg cfg.Configuration) (client.ClusterConfiguration, error) {
conf, ok := clusterConfigType.CheckGet(cfg)
if !ok {
return client.ClusterConfiguration{}, fnerrors.InternalError("missing configuration")
}
wres, err := WaitCluster(ctx, conf.ClusterId)
if err != nil {
return client.ClusterConfiguration{}, err
}
cluster := wres.Cluster
var p client.ClusterConfiguration
p.Ephemeral = conf.Ephemeral
p.Config = *client.MakeApiConfig(&client.StaticConfig{
EndpointAddress: cluster.EndpointAddress,
CertificateAuthorityData: cluster.CertificateAuthorityData,
ClientCertificateData: cluster.ClientCertificateData,
ClientKeyData: cluster.ClientKeyData,
})
for _, lbl := range cluster.Label {
p.Labels = append(p.Labels, &schema.Label{Name: lbl.Name, Value: lbl.Value})
}
return p, nil
}
type CreateClusterResult struct {
ClusterId string
Cluster *KubernetesCluster
Registry *ImageRegistry
Deadline *time.Time
}
func CreateCluster(ctx context.Context, machineType string, ephemeral bool, purpose string) (*KubernetesCluster, error) {
return tasks.Return(ctx, tasks.Action("nscloud.cluster-create"), func(ctx context.Context) (*KubernetesCluster, error) {
req := CreateKubernetesClusterRequest{
Ephemeral: ephemeral,
DocumentedPurpose: purpose,
MachineType: machineType,
}
if !environment.IsRunningInCI() {
keys, err := UserSSHKeys()
if err != nil {
return nil, err
}
if keys != nil {
actualKeys, err := compute.GetValue(ctx, keys)
if err != nil {
return nil, err
}
req.AuthorizedSshKeys = actualKeys
}
}
var response StartCreateKubernetesClusterResponse
if err := startCreateKubernetesCluster.Do(ctx, req, fnapi.DecodeJSONResponse(&response)); err != nil {
return nil, err
}
tasks.Attachments(ctx).AddResult("cluster_id", response.ClusterId)
if response.ClusterFragment != nil {
if shape := response.ClusterFragment.Shape; shape != nil {
tasks.Attachments(ctx).
AddResult("cluster_cpu", shape.VirtualCpu).
AddResult("cluster_ram", humanize.IBytes(uint64(shape.MemoryMegabytes)*humanize.MiByte))
}
}
if ephemeral {
compute.On(ctx).Cleanup(tasks.Action("nscloud.cluster-cleanup"), func(ctx context.Context) error {
if err := DestroyCluster(ctx, response.ClusterId); err != nil {
// The cluster being gone is an acceptable state (it could have
// been deleted by DeleteRecursively for example).
if status.Code(err) == codes.NotFound {
return nil
}
}
return nil
})
}
return response.ClusterFragment, nil
})
}
func CreateAndWaitCluster(ctx context.Context, machineType string, ephemeral bool, purpose string) (*CreateClusterResult, error) {
cluster, err := CreateCluster(ctx, machineType, ephemeral, purpose)
if err != nil {
return nil, err
}
return WaitCluster(ctx, cluster.ClusterId)
}
func WaitCluster(ctx context.Context, clusterId string) (*CreateClusterResult, error) {
ctx, done := context.WithTimeout(ctx, 15*time.Minute) // Wait for cluster creation up to 15 minutes.
defer done()
var cr *CreateKubernetesClusterResponse
if err := tasks.Action("nscloud.cluster-wait").Arg("cluster_id", clusterId).Run(ctx, func(ctx context.Context) error {
var progress clusterCreateProgress
progress.status.Store("CREATE_ACCEPTED_WAITING_FOR_ALLOCATION")
tasks.Attachments(ctx).SetProgress(&progress)
lastStatus := "<none>"
for cr == nil {
if err := ctx.Err(); err != nil {
return err // Check if we've been cancelled.
}
// We continue to wait for the cluster to become ready until we observe a READY.
if err := waitKubernetesCluster.Do(ctx, WaitKubernetesClusterRequest{ClusterId: clusterId}, func(body io.Reader) error {
decoder := jstream.NewDecoder(body, 1)
// jstream gives us the streamed array segmentation, however it
// returns map[string]interface{} rather than typed objects. We
// re-triggering parsing into the response type so the remainder
// of our codebase operates on types.
for mv := range decoder.Stream() {
var resp CreateKubernetesClusterResponse
if err := reparse(mv.Value, &resp); err != nil {
return fnerrors.InvocationError("nscloud", "failed to parse response: %w", err)
}
progress.set(resp.Status)
lastStatus = resp.Status
if resp.ClusterId != "" {
clusterId = resp.ClusterId
}
if resp.Status == "READY" {
cr = &resp
return nil
}
}
return nil
}); err != nil {
return fnerrors.InvocationError("cluster never became ready (last status was %q, cluster id: %s): %w", lastStatus, clusterId, err)
}
}
tasks.Attachments(ctx).
AddResult("cluster_address", cr.Cluster.EndpointAddress).
AddResult("deadline", cr.Cluster.Deadline)
return nil
}); err != nil {
return nil, err
}
result := &CreateClusterResult{
ClusterId: cr.ClusterId,
Cluster: cr.Cluster,
Registry: cr.Registry,
}
if cr.Deadline != "" {
t, err := time.Parse(time.RFC3339, cr.Deadline)
if err == nil {
result.Deadline = &t
}
}
return result, nil
}
func DestroyCluster(ctx context.Context, clusterId string) error {
return destroyKubernetesCluster.Do(ctx, DestroyKubernetesClusterRequest{
ClusterId: clusterId,
}, nil)
}
func GetCluster(ctx context.Context, clusterId string) (*GetKubernetesClusterResponse, error) {
return tasks.Return(ctx, tasks.Action("nscloud.get").Arg("id", clusterId), func(ctx context.Context) (*GetKubernetesClusterResponse, error) {
var response GetKubernetesClusterResponse
if err := getKubernetesCluster.Do(ctx, GetKubernetesClusterRequest{ClusterId: clusterId}, fnapi.DecodeJSONResponse(&response)); err != nil {
return nil, err
}
return &response, nil
})
}
func ListClusters(ctx context.Context) (*KubernetesClusterList, error) {
return tasks.Return(ctx, tasks.Action("nscloud.cluster-list"), func(ctx context.Context) (*KubernetesClusterList, error) {
var list KubernetesClusterList
if err := listKubernetesClusters.Do(ctx, ListKubernetesClustersRequest{}, fnapi.DecodeJSONResponse(&list)); err != nil {
return nil, err
}
return &list, nil
})
}
type clusterCreateProgress struct {
status atomic.String
}
func (crp *clusterCreateProgress) set(status string) { crp.status.Store(status) }
func (crp *clusterCreateProgress) FormatProgress() string { return crp.status.Load() }
func reparse(obj interface{}, target interface{}) error {
b, err := json.Marshal(obj)
if err != nil {
return err
}
return json.Unmarshal(b, target)
}
func provideClass(ctx context.Context, cfg cfg.Configuration) (runtime.Class, error) {
return runtimeClass{}, nil
}
type runtimeClass struct{}
var _ runtime.Class = runtimeClass{}
func (d runtimeClass) AttachToCluster(ctx context.Context, cfg cfg.Configuration) (runtime.Cluster, error) {
conf, ok := clusterConfigType.CheckGet(cfg)
if !ok {
return nil, fnerrors.BadInputError("%s: no cluster configured", cfg.EnvKey())
}
cluster, err := GetCluster(ctx, conf.ClusterId)
if err != nil {
return nil, err
}
return d.ensureCluster(ctx, cfg, cluster.Cluster)
}
func (d runtimeClass) EnsureCluster(ctx context.Context, srv cfg.Configuration, purpose string) (runtime.Cluster, error) {
if _, ok := clusterConfigType.CheckGet(srv); ok {
return d.AttachToCluster(ctx, srv)
}
ephemeral := true
result, err := CreateAndWaitCluster(ctx, "", ephemeral, purpose)
if err != nil {
return nil, err
}
srv = srv.Derive(srv.EnvKey(), func(previous cfg.ConfigurationSlice) cfg.ConfigurationSlice {
// Prepend to ensure that the prebuilt cluster is returned first.
previous.Configuration = append(protos.WrapAnysOrDie(
&PrebuiltCluster{ClusterId: result.ClusterId, Ephemeral: ephemeral},
), previous.Configuration...)
return previous
})
return d.ensureCluster(ctx, srv, result.Cluster)
}
func (d runtimeClass) ensureCluster(ctx context.Context, cfg cfg.Configuration, kc *KubernetesCluster) (runtime.Cluster, error) {
// XXX This is confusing. We can call NewCluster because the runtime class
// and cluster providers are registered with the same provider key. We
// should instead create the cluster here, when the CreateCluster intent is
// still clear.
unbound, err := kubernetes.ConnectToCluster(ctx, cfg)
if err != nil {
return nil, err
}
unbound.FetchSystemInfo = func(ctx context.Context) (*kubedef.SystemInfo, error) {
return &kubedef.SystemInfo{
NodePlatform: []string{"linux/amd64"},
DetectedDistribution: "k3s",
}, nil
}
return &cluster{cluster: unbound, config: kc}, nil
}
type cluster struct {
cluster *kubernetes.Cluster
config *KubernetesCluster
}
var _ runtime.Cluster = &cluster{}
var _ kubedef.KubeCluster = &cluster{}
func (d *cluster) Class() runtime.Class {
return runtimeClass{}
}
func (d *cluster) Bind(env cfg.Context) (runtime.ClusterNamespace, error) {
bound, err := d.cluster.Bind(env)
if err != nil {
return nil, err
}
return clusterNamespace{ClusterNamespace: bound.(*kubernetes.ClusterNamespace), Config: d.config}, nil
}
func (d *cluster) Planner(env cfg.Context) runtime.Planner {
base := kubernetes.NewPlanner(env, d.cluster.SystemInfo)
return planner{Planner: base, config: d.config}
}
func (d *cluster) FetchDiagnostics(ctx context.Context, cr *runtimepb.ContainerReference) (*runtimepb.Diagnostics, error) {
return d.cluster.FetchDiagnostics(ctx, cr)
}
func (d *cluster) FetchLogsTo(ctx context.Context, w io.Writer, cr *runtimepb.ContainerReference, opts runtime.FetchLogsOpts) error {
return d.cluster.FetchLogsTo(ctx, w, cr, opts)
}
func (d *cluster) AttachTerminal(ctx context.Context, container *runtimepb.ContainerReference, io runtime.TerminalIO) error {
return d.cluster.AttachTerminal(ctx, container, io)
}
func (d *cluster) ForwardIngress(ctx context.Context, localAddrs []string, localPort int, notify runtime.PortForwardedFunc) (io.Closer, error) {
return d.cluster.ForwardIngress(ctx, localAddrs, localPort, notify)
}
func (d *cluster) EnsureState(ctx context.Context, key string) (any, error) {
return d.cluster.EnsureState(ctx, key)
}
func (d *cluster) DeleteAllRecursively(ctx context.Context, wait bool, progress io.Writer) (bool, error) {
return d.cluster.DeleteAllRecursively(ctx, wait, progress)
}
func (d *cluster) PreparedClient() client.Prepared {
return d.cluster.PreparedClient()
}
type planner struct {
runtime.Planner
config *KubernetesCluster
}
func (d planner) ComputeBaseNaming(source *schema.Naming) (*schema.ComputedNaming, error) {
return &schema.ComputedNaming{
Source: source,
BaseDomain: d.config.IngressDomain,
TlsPassthroughBaseDomain: "int-" + d.config.IngressDomain, // XXX receive this value.
Managed: schema.Domain_CLOUD_TERMINATION,
UpstreamTlsTermination: true,
DomainFragmentSuffix: d.config.ClusterId, // XXX fetch ingress external IP to calculate domain.
UseShortAlias: true,
}, nil
}
type clusterNamespace struct {
*kubernetes.ClusterNamespace
Config *KubernetesCluster
}
var _ kubedef.KubeClusterNamespace = clusterNamespace{}
func (cr clusterNamespace) Planner() runtime.Planner {
base := cr.ClusterNamespace.Planner()
return planner{Planner: base, config: cr.Config}
}
func (cr clusterNamespace) DeleteRecursively(ctx context.Context, wait bool) (bool, error) {
return cr.deleteCluster(ctx)
}
func (cr clusterNamespace) DeleteAllRecursively(ctx context.Context, wait bool, progress io.Writer) (bool, error) {
return cr.deleteCluster(ctx)
}
func (cr clusterNamespace) deleteCluster(ctx context.Context) (bool, error) {
if err := DestroyCluster(ctx, cr.Config.ClusterId); err != nil {
if status.Code(err) == codes.NotFound {
return false, nil
}
return false, err
}
return true, nil
}
type CreateKubernetesClusterRequest struct {
OpaqueUserAuth []byte `json:"opaque_user_auth,omitempty"`
Ephemeral bool `json:"ephemeral,omitempty"`
DocumentedPurpose string `json:"documented_purpose,omitempty"`
AuthorizedSshKeys []string `json:"authorized_ssh_keys,omitempty"`
MachineType string `json:"machine_type,omitempty"`
}
type GetKubernetesClusterRequest struct {
OpaqueUserAuth []byte `json:"opaque_user_auth,omitempty"`
ClusterId string `json:"cluster_id,omitempty"`
}
type WaitKubernetesClusterRequest struct {
OpaqueUserAuth []byte `json:"opaque_user_auth,omitempty"`
ClusterId string `json:"cluster_id,omitempty"`
}
type CreateKubernetesClusterResponse struct {
Status string `json:"status,omitempty"`
ClusterId string `json:"cluster_id,omitempty"`
Cluster *KubernetesCluster `json:"cluster,omitempty"`
Registry *ImageRegistry `json:"registry,omitempty"`
Deadline string `json:"deadline,omitempty"`
}
type GetKubernetesClusterResponse struct {
Cluster *KubernetesCluster `json:"cluster,omitempty"`
Registry *ImageRegistry `json:"registry,omitempty"`
Deadline string `json:"deadline,omitempty"`
}
type StartCreateKubernetesClusterResponse struct {
ClusterId string `json:"cluster_id,omitempty"`
ClusterFragment *KubernetesCluster `json:"cluster_fragment,omitempty"`
}
type ListKubernetesClustersRequest struct {
OpaqueUserAuth []byte `json:"opaque_user_auth,omitempty"`
}
type KubernetesClusterList struct {
Clusters []KubernetesCluster `json:"cluster"`
}
type KubernetesCluster struct {
ClusterId string `json:"cluster_id,omitempty"`
Created string `json:"created,omitempty"`
Deadline string `json:"deadline,omitempty"`
SSHProxyEndpoint string `json:"ssh_proxy_endpoint,omitempty"`
SshPrivateKey []byte `json:"ssh_private_key,omitempty"`
DocumentedPurpose string `json:"documented_purpose,omitempty"`
Shape *ClusterShape `json:"shape,omitempty"`
EndpointAddress string `json:"endpoint_address,omitempty"`
CertificateAuthorityData []byte `json:"certificate_authority_data,omitempty"`
ClientCertificateData []byte `json:"client_certificate_data,omitempty"`
ClientKeyData []byte `json:"client_key_data,omitempty"`
KubernetesDistribution string `json:"kubernetes_distribution,omitempty"`
Platform []string `json:"platform,omitempty"`
IngressDomain string `json:"ingress_domain,omitempty"`
Label []*LabelEntry `json:"label,omitempty"`
}
type ImageRegistry struct {
EndpointAddress string `json:"endpoint_address,omitempty"`
}
type ClusterShape struct {
VirtualCpu int32 `json:"virtual_cpu,omitempty"`
MemoryMegabytes int32 `json:"memory_megabytes,omitempty"`
}
type DestroyKubernetesClusterRequest struct {
OpaqueUserAuth []byte `json:"opaque_user_auth,omitempty"`
ClusterId string `json:"cluster_id,omitempty"`
}
type LabelEntry struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}