-
Notifications
You must be signed in to change notification settings - Fork 9
/
nscloud.go
99 lines (83 loc) · 2.91 KB
/
nscloud.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
// 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 prepare
import (
"context"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/internal/executor"
"namespacelabs.dev/foundation/internal/parsing/devhost"
"namespacelabs.dev/foundation/internal/providers/nscloud"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/orchestration"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/universe/nscloud/configuration"
)
func NamespaceCluster(machineType string, ephemeral, withBuild bool) Stage {
return Stage{
Pre: func(ch chan *orchestration.Event) {
ch <- &orchestration.Event{
Category: "Namespace Cloud",
ResourceId: "new-cluster",
ResourceLabel: "New cluster",
}
if withBuild {
ch <- &orchestration.Event{
Category: "Namespace Cloud",
ResourceId: "build-cluster",
ResourceLabel: "Setup build cluster",
}
}
},
Run: func(ctx context.Context, env cfg.Context, ch chan *orchestration.Event) (*schema.DevHost_ConfigureEnvironment, error) {
return PrepareNewNamespaceCluster(ctx, env, machineType, ephemeral, withBuild, ch)
},
}
}
func PrepareNewNamespaceCluster(ctx context.Context, env cfg.Context, machineType string, ephemeral, withBuild bool, ch chan *orchestration.Event) (*schema.DevHost_ConfigureEnvironment, error) {
eg := executor.New(ctx, "prepare-new-cluster")
var mainMessages, buildMessages []proto.Message
eg.Go(func(ctx context.Context) error {
cfg, err := api.CreateAndWaitCluster(ctx, api.Endpoint, api.CreateClusterOpts{
MachineType: machineType,
Ephemeral: ephemeral,
KeepAtExit: true,
Purpose: env.Environment().Name,
WaitClusterOpts: api.WaitClusterOpts{WaitKind: "kubernetes"},
})
if err != nil {
return err
}
mainMessages = append(mainMessages, &configuration.Cluster{ClusterId: cfg.ClusterId})
return nil
})
if withBuild {
eg.Go(func(ctx context.Context) error {
msg, err := nscloud.EnsureBuildCluster(ctx, api.Endpoint)
if err != nil {
return err
}
ch <- &orchestration.Event{
Category: "Namespace Cloud",
ResourceId: "build-cluster",
ResourceLabel: "Setup build cluster",
Ready: orchestration.Event_READY,
Stage: orchestration.Event_DONE,
}
buildMessages = append(buildMessages, msg)
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
ch <- &orchestration.Event{
Category: "Namespace Cloud",
ResourceId: "new-cluster",
ResourceLabel: "New cluster",
Ready: orchestration.Event_READY,
Stage: orchestration.Event_DONE,
}
return devhost.MakeConfiguration(append(mainMessages, buildMessages...)...)
}