-
Notifications
You must be signed in to change notification settings - Fork 9
/
prepare.go
105 lines (85 loc) · 3.13 KB
/
prepare.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
// 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 orchestration
import (
"context"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/planning/deploy"
"namespacelabs.dev/foundation/internal/planning/secrets"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/orchestration/client"
"namespacelabs.dev/foundation/orchestration/server/constants"
"namespacelabs.dev/foundation/schema"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/tasks"
)
var (
RenderOrchestratorDeployment = false
stateless = &runtimepb.Deployable{
PackageRef: schema.MakePackageSingleRef(constants.ServerPkg),
Id: constants.ServerId,
Name: constants.ServerName,
DeployableClass: string(schema.DeployableClass_STATELESS),
}
)
func RegisterPrepare() {
client.RegisterOrchestrator(func(ctx context.Context, target cfg.Configuration, cluster runtime.Cluster) (any, error) {
return tasks.Return(ctx, tasks.Action("orchestrator.prepare"), func(ctx context.Context) (any, error) {
return PrepareOrchestrator(ctx, target, cluster, true)
})
})
}
func PrepareOrchestrator(ctx context.Context, targetEnv cfg.Configuration, cluster runtime.Cluster, wait bool) (any, error) {
env, err := MakeOrchestratorContext(ctx, targetEnv)
if err != nil {
return nil, err
}
boundCluster, err := cluster.Bind(ctx, env)
if err != nil {
return nil, err
}
if err := deployOrchestrator(ctx, env, boundCluster, wait); err != nil {
return nil, err
}
return client.RemoteOrchestrator(boundCluster, stateless), nil
}
func deployOrchestrator(ctx context.Context, env cfg.Context, boundCluster runtime.ClusterNamespace, wait bool) error {
focus, err := planning.RequireServer(ctx, env, constants.ServerPkg)
if err != nil {
return err
}
planner, err := runtime.PlannerFor(ctx, env)
if err != nil {
return err
}
p := planning.Planner{
Context: env,
Runtime: planner,
Registry: planner.Registry(),
Secrets: secrets.NoSecrets,
}
plan, err := deploy.PrepareDeployServers(ctx, p, focus)
if err != nil {
return err
}
computed, err := compute.GetValue(ctx, plan)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, client.ConnTimeout)
defer cancel()
return execute(ctx, env, boundCluster, computed.Deployer, wait)
}
func execute(ctx context.Context, env cfg.Context, boundCluster runtime.ClusterNamespace, plan *execution.Plan, wait bool) error {
if wait {
return execution.Execute(ctx, "orchestrator.deploy", plan,
deploy.MaybeRenderBlock(env, boundCluster, RenderOrchestratorDeployment),
execution.FromContext(env), runtime.InjectCluster(boundCluster))
}
return execution.RawExecute(ctx, "orchestrator.deploy", execution.ExecuteOpts{ContinueOnErrors: true},
plan, execution.FromContext(env), runtime.InjectCluster(boundCluster))
}