Skip to content

Commit b7ce7cb

Browse files
djeebusjakubno
andauthored
Move orchestrator config into a model (#1295)
Co-authored-by: Jakub Novák <[email protected]>
1 parent 0d57199 commit b7ce7cb

File tree

26 files changed

+179
-117
lines changed

26 files changed

+179
-117
lines changed

iac/provider-gcp/nomad/jobs/orchestrator.hcl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ EOT
7070
TEMPLATE_BUCKET_NAME = "${template_bucket_name}"
7171
OTEL_COLLECTOR_GRPC_ENDPOINT = "${otel_collector_grpc_endpoint}"
7272
ALLOW_SANDBOX_INTERNET = "${allow_sandbox_internet}"
73-
SHARED_CHUNK_CACHE_PATH = "${shared_chunk_cache_path}"
73+
SHARED_CHUNK_CACHE_PATH = "${shared_chunk_cache_path}"
7474
CLICKHOUSE_CONNECTION_STRING = "${clickhouse_connection_string}"
7575
REDIS_URL = "${redis_url}"
7676
REDIS_CLUSTER_URL = "${redis_cluster_url}"
77+
GRPC_PORT = "${port}"
78+
PROXY_PORT = "${proxy_port}"
7779

7880
%{ if launch_darkly_api_key != "" }
7981
LAUNCH_DARKLY_API_KEY = "${launch_darkly_api_key}"
@@ -82,7 +84,7 @@ EOT
8284

8385
config {
8486
command = "/bin/bash"
85-
args = ["-c", " chmod +x local/orchestrator && local/orchestrator --port ${port} --proxy-port ${proxy_port}"]
87+
args = ["-c", " chmod +x local/orchestrator && local/orchestrator"]
8688
}
8789

8890
artifact {

iac/provider-gcp/nomad/jobs/template-manager.hcl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ job "template-manager-system" {
7777
SHARED_CHUNK_CACHE_PATH = "${shared_chunk_cache_path}"
7878
CLICKHOUSE_CONNECTION_STRING = "${clickhouse_connection_string}"
7979
DOCKERHUB_REMOTE_REPOSITORY_URL = "${dockerhub_remote_repository_url}"
80+
GRPC_PORT = "${port}"
8081
%{ if !update_stanza }
8182
FORCE_STOP = "true"
8283
%{ endif }
@@ -87,7 +88,7 @@ job "template-manager-system" {
8788

8889
config {
8990
command = "/bin/bash"
90-
args = ["-c", " chmod +x local/template-manager && local/template-manager --port ${port}"]
91+
args = ["-c", " chmod +x local/template-manager && local/template-manager"]
9192
}
9293

9394
artifact {

packages/client-proxy/internal/proxy/proxy.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ func catalogResolution(ctx context.Context, sandboxId string, c catalog.Sandboxe
9696
return s.OrchestratorIP, nil
9797
}
9898

99-
func NewClientProxy(meterProvider metric.MeterProvider, serviceName string, port uint, catalog catalog.SandboxesCatalog, useCatalogResolution bool, useDnsResolution bool) (*reverseproxy.Proxy, error) {
99+
func NewClientProxy(
100+
meterProvider metric.MeterProvider,
101+
serviceName string,
102+
port uint16,
103+
catalog catalog.SandboxesCatalog,
104+
useCatalogResolution, useDnsResolution bool,
105+
) (*reverseproxy.Proxy, error) {
100106
if !useCatalogResolution && !useDnsResolution {
101107
return nil, errors.New("catalog resolution and DNS resolution are both disabled, at least one must be enabled")
102108
}

packages/client-proxy/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log"
9+
"math"
910
"net"
1011
"net/http"
1112
"os"
@@ -104,6 +105,11 @@ func run() int {
104105
zap.ReplaceGlobals(logger)
105106

106107
proxyPort := internal.GetProxyServicePort()
108+
if proxyPort <= 0 || proxyPort > int(math.MaxUint16) {
109+
logger.Error("Proxy port is outside the valid uint16 range", zap.Int("value", proxyPort))
110+
return 1
111+
}
112+
107113
edgePort := internal.GetEdgeServicePort()
108114
edgeSecret := internal.GetEdgeServiceSecret()
109115
orchestratorPort := internal.GetOrchestratorServicePort()
@@ -155,7 +161,7 @@ func run() int {
155161
}
156162

157163
// Proxy sandbox http traffic to orchestrator nodes
158-
trafficProxy, err := e2bproxy.NewClientProxy(tel.MeterProvider, serviceName, uint(proxyPort), catalog, useProxyCatalogResolution, useDnsResolution)
164+
trafficProxy, err := e2bproxy.NewClientProxy(tel.MeterProvider, serviceName, uint16(proxyPort), catalog, useProxyCatalogResolution, useDnsResolution)
159165
if err != nil {
160166
logger.Error("Failed to create client proxy", zap.Error(err))
161167
return 1

packages/orchestrator/benchmark_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func BenchmarkBaseImageLaunch(b *testing.B) {
109109
b.Setenv("SNAPSHOT_CACHE_DIR", abs(filepath.Join(tempDir, "snapshot-cache")))
110110
b.Setenv("LOCAL_TEMPLATE_STORAGE_BASE_PATH", abs(filepath.Join(persistenceDir, "templates")))
111111

112+
networkConfig, err := network.ParseConfig()
113+
if err != nil {
114+
b.Fatalf("error parsing config: %v", err)
115+
}
116+
112117
// prep directories
113118
for _, subdir := range []string{"build", "build-templates" /*"fc-vm",*/, "sandbox", "snapshot-cache", "template"} {
114119
fullDirName := filepath.Join(tempDir, subdir)
@@ -123,7 +128,7 @@ func BenchmarkBaseImageLaunch(b *testing.B) {
123128
// sbxlogger.SetSandboxLoggerExternal(logger)
124129

125130
networkPool, err := network.NewPool(
126-
b.Context(), noop.MeterProvider{}, 8, 8, clientID,
131+
b.Context(), noop.MeterProvider{}, 8, 8, clientID, networkConfig,
127132
)
128133
require.NoError(b, err)
129134
defer func() {
@@ -197,7 +202,7 @@ func BenchmarkBaseImageLaunch(b *testing.B) {
197202
persistenceBuild, err := storage.GetBuildCacheStorageProvider(b.Context(), nil)
198203
require.NoError(b, err)
199204

200-
var proxyPort uint = 5007
205+
var proxyPort uint16 = 5007
201206

202207
sandboxes := smap.New[*sandbox.Sandbox]()
203208

packages/orchestrator/cmd/build-template/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ const (
3838
)
3939

4040
func main() {
41-
ctx, cancel := context.WithCancel(context.Background())
42-
defer cancel()
41+
ctx := context.Background()
4342

4443
templateID := flag.String("template", "", "template id")
4544
buildID := flag.String("build", "", "build id")
4645
kernelVersion := flag.String("kernel", "", "kernel version")
4746
fcVersion := flag.String("firecracker", "", "firecracker version")
4847
flag.Parse()
4948

50-
err := buildTemplate(ctx, *kernelVersion, *fcVersion, *templateID, *buildID)
49+
networkConfig, err := network.ParseConfig()
50+
if err != nil {
51+
log.Fatalf("error parsing config: %v", err)
52+
}
53+
54+
err = buildTemplate(ctx, *kernelVersion, *fcVersion, *templateID, *buildID, networkConfig)
5155
if err != nil {
52-
log.Fatalf("error building template: %v", err) //nolint:gocritic // probably fine to bail if we're done?
56+
log.Fatalf("error building template: %v", err)
5357
}
5458
}
5559

@@ -59,6 +63,7 @@ func buildTemplate(
5963
fcVersion,
6064
templateID,
6165
buildID string,
66+
networkConfig network.Config,
6267
) error {
6368
ctx, cancel := context.WithTimeout(parentCtx, time.Minute*5)
6469
defer cancel()
@@ -121,7 +126,7 @@ func buildTemplate(
121126
}
122127
}()
123128

124-
networkPool, err := network.NewPool(ctx, noop.MeterProvider{}, 8, 8, clientID)
129+
networkPool, err := network.NewPool(ctx, noop.MeterProvider{}, 8, 8, clientID, networkConfig)
125130
if err != nil {
126131
return fmt.Errorf("could not create network pool: %w", err)
127132
}

packages/orchestrator/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/aws/aws-sdk-go-v2/service/ecr v1.44.0
1919
github.com/bits-and-blooms/bitset v1.22.0
2020
github.com/bmatcuk/doublestar/v4 v4.9.1
21+
github.com/caarlos0/env/v11 v11.3.1
2122
github.com/containernetworking/plugins v1.6.0
2223
github.com/containers/storage v1.58.0
2324
github.com/coreos/go-iptables v0.8.0

packages/orchestrator/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cfg
2+
3+
import (
4+
"github.com/caarlos0/env/v11"
5+
6+
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/network"
7+
)
8+
9+
type Config struct {
10+
AllowSandboxInternet bool `env:"ALLOW_SANDBOX_INTERNET" envDefault:"true"`
11+
ClickhouseConnectionString string `env:"CLICKHOUSE_CONNECTION_STRING"`
12+
ForceStop bool `env:"FORCE_STOP"`
13+
GRPCPort uint16 `env:"GRPC_PORT" envDefault:"5008"`
14+
LaunchDarklyAPIKey string `env:"LAUNCH_DARKLY_API_KEY"`
15+
OrchestratorBasePath string `env:"ORCHESTRATOR_BASE_PATH" envDefault:"/orchestrator"`
16+
OrchestratorLockPath string `env:"ORCHESTRATOR_LOCK_PATH" envDefault:"/orchestrator.lock"`
17+
ProxyPort uint16 `env:"PROXY_PORT" envDefault:"5007"`
18+
RedisClusterURL string `env:"REDIS_CLUSTER_URL"`
19+
RedisURL string `env:"REDIS_URL"`
20+
Services []string `env:"ORCHESTRATOR_SERVICES" envDefault:"orchestrator"`
21+
22+
NetworkConfig network.Config
23+
}
24+
25+
func Parse() (Config, error) {
26+
var model Config
27+
err := env.Parse(&model)
28+
return model, err
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cfg
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestParse(t *testing.T) {
11+
t.Run("network config local flag defaults to false", func(t *testing.T) {
12+
config, err := Parse()
13+
require.NoError(t, err)
14+
15+
assert.False(t, config.NetworkConfig.UseLocalNamespaceStorage)
16+
})
17+
18+
t.Run("network config is parsed correctly", func(t *testing.T) {
19+
t.Setenv("USE_LOCAL_NAMESPACE_STORAGE", "true")
20+
21+
config, err := Parse()
22+
require.NoError(t, err)
23+
24+
assert.True(t, config.NetworkConfig.UseLocalNamespaceStorage)
25+
})
26+
27+
t.Run("multiple services parses correctly", func(t *testing.T) {
28+
t.Setenv("ORCHESTRATOR_SERVICES", "service1,service2")
29+
30+
config, err := Parse()
31+
require.NoError(t, err)
32+
33+
assert.Equal(t, []string{"service1", "service2"}, config.Services)
34+
})
35+
}

0 commit comments

Comments
 (0)