-
Notifications
You must be signed in to change notification settings - Fork 9
/
runtimeconfig.go
111 lines (87 loc) · 2.97 KB
/
runtimeconfig.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
// 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 deploy
import (
"fmt"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/runtime"
)
func serverToRuntimeConfig(stack *planning.Stack, ps planning.PlannedServer, serverImage oci.ImageID) (*runtime.RuntimeConfig, error) {
srv := ps.Server
config := &runtime.RuntimeConfig{
Environment: makeEnv(srv.SealedContext().Environment()),
Current: makeServerConfig(stack, srv),
}
config.Current.ImageRef = serverImage.String()
for _, pkg := range ps.DeclaredStack.PackageNames() {
if pkg == ps.PackageName() {
continue
}
ref, ok := stack.Get(pkg)
if !ok {
return nil, fnerrors.InternalError("%s: missing in the stack", pkg)
}
config.StackEntry = append(config.StackEntry, makeServerConfig(stack, ref.Server))
}
return config, nil
}
func TestStackToRuntimeConfig(stack *planning.Stack, sutServers []string) (*runtime.RuntimeConfig, error) {
if len(sutServers) == 0 {
return nil, fnerrors.InternalError("no servers to test")
}
config := &runtime.RuntimeConfig{
Environment: makeEnv(stack.Servers[0].Server.SealedContext().Environment()),
}
for _, pkg := range sutServers {
ref, ok := stack.Get(schema.MakePackageName(pkg))
if !ok {
return nil, fnerrors.InternalError("%s: missing in the stack", pkg)
}
config.StackEntry = append(config.StackEntry, makeServerConfig(stack, ref.Server))
}
return config, nil
}
func makeEnv(env *schema.Environment) *runtime.RuntimeConfig_Environment {
res := &runtime.RuntimeConfig_Environment{
Ephemeral: env.Ephemeral,
Purpose: env.Purpose.String(),
}
// Ephemeral environments use generated names, that should not be depended on.
if !env.Ephemeral {
res.Name = env.Name
}
return res
}
func makeServerConfig(stack *planning.Stack, server planning.Server) *runtime.Server {
current := &runtime.Server{
PackageName: server.Proto().PackageName,
ModuleName: server.Proto().ModuleName,
}
for _, service := range server.Proto().Service {
current.Port = append(current.Port, makePort(service))
}
for _, service := range server.Proto().Ingress {
current.Port = append(current.Port, makePort(service))
}
for _, endpoint := range stack.Endpoints {
if endpoint.ServerOwner != server.Proto().PackageName {
continue
}
current.Service = append(current.Service, &runtime.Server_Service{
Owner: endpoint.EndpointOwner,
Name: endpoint.ServiceName,
Endpoint: fmt.Sprintf("%s:%d", endpoint.AllocatedName, endpoint.Port.ContainerPort),
})
}
return current
}
func makePort(service *schema.Server_ServiceSpec) *runtime.Server_Port {
return &runtime.Server_Port{
Name: service.Name,
Port: service.Port.ContainerPort,
}
}