-
Notifications
You must be signed in to change notification settings - Fork 9
/
runtimeconfig.go
142 lines (116 loc) · 4.28 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
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
// 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"
internalruntime "namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/runtime"
)
func serverToRuntimeConfig(stack serverStack, ps planning.PlannedServer, serverImage oci.ImageID) (*runtime.RuntimeConfig, error) {
srv := ps.Server
env := srv.SealedContext().Environment()
config := &runtime.RuntimeConfig{
Environment: makeEnv(env),
Current: makeServerConfig(stack, srv.Proto(), env),
}
config.Current.ImageRef = serverImage.String()
for _, pkg := range ps.DeclaredStack.PackageNames() {
if pkg == ps.PackageName() {
continue
}
ref, ok := stack.GetServerProto(pkg)
if !ok {
return nil, fnerrors.InternalError("%s: missing in the stack", pkg)
}
config.StackEntry = append(config.StackEntry, makeServerConfig(stack, ref, env))
}
return config, nil
}
func TestStackToRuntimeConfig(stack *planning.StackWithIngress, sutServers []string) (*runtime.RuntimeConfig, error) {
if len(sutServers) == 0 {
return nil, fnerrors.InternalError("no servers to test")
}
env := stack.Servers[0].Server.SealedContext().Environment()
config := &runtime.RuntimeConfig{
Environment: makeEnv(env),
}
for _, pkg := range sutServers {
ref, ok := stack.GetServerProto(schema.MakePackageName(pkg))
if !ok {
return nil, fnerrors.InternalError("%s: missing in the stack", pkg)
}
config.StackEntry = append(config.StackEntry, makeServerConfig(stack, ref, env))
}
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 serverStack, server *schema.Server, env *schema.Environment) *runtime.Server {
current := &runtime.Server{
PackageName: server.PackageName,
ModuleName: server.ModuleName,
}
for _, service := range server.Service {
current.Port = append(current.Port, makePort(service))
}
for _, service := range server.Ingress {
current.Port = append(current.Port, makePort(service))
}
endpoints, _ := stack.GetEndpoints(schema.PackageName(server.PackageName))
for _, endpoint := range endpoints {
current.Service = append(current.Service, &runtime.Server_Service{
Owner: endpoint.EndpointOwner,
Name: endpoint.ServiceName,
Endpoint: fmt.Sprintf("%s:%d", endpoint.AllocatedName, endpoint.ExportedPort),
Ingress: makeServiceIngress(stack, endpoint, env),
})
}
return current
}
func makePort(service *schema.Server_ServiceSpec) *runtime.Server_Port {
return &runtime.Server_Port{
Name: service.Name,
Port: service.Port.ContainerPort,
}
}
// TODO: consolidate with "resolveBackend" from Node.js build
func makeServiceIngress(stack serverStack, endpoint *schema.Endpoint, env *schema.Environment) *runtime.Server_Ingress {
// There is often no ingress in tests so we use the in-cluster address.
// In the future we could allow the user to annotate domains which would
// be accessible from the test environment.
if env.Purpose == schema.Environment_TESTING {
return &runtime.Server_Ingress{
Domain: []*runtime.Server_Ingress_Domain{{
BaseUrl: fmt.Sprintf("http://%s:%d", endpoint.AllocatedName, endpoint.ExportedPort),
}},
}
}
ingress := &runtime.Server_Ingress{}
for _, fragment := range stack.GetIngressesForService(endpoint.EndpointOwner, endpoint.ServiceName) {
domain := &runtime.Server_Ingress_Domain{}
d := fragment.Domain
if d.Managed == schema.Domain_LOCAL_MANAGED {
domain.BaseUrl = fmt.Sprintf("http://%s:%d", d.Fqdn, internalruntime.LocalIngressPort)
} else if d.TlsFrontend {
domain.BaseUrl = fmt.Sprintf("https://%s", d.Fqdn)
} else {
domain.BaseUrl = fmt.Sprintf("http://%s", d.Fqdn)
}
ingress.Domain = append(ingress.Domain, domain)
}
return ingress
}