@@ -13,7 +13,6 @@ import (
1313 "go.opentelemetry.io/otel/trace"
1414 "go.uber.org/zap"
1515
16- globalconfig "github.com/e2b-dev/infra/packages/orchestrator/internal/config"
1716 "github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/block"
1817 "github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/build"
1918 "github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/fc"
@@ -24,6 +23,7 @@ import (
2423 "github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/uffd"
2524 "github.com/e2b-dev/infra/packages/orchestrator/internal/template/metadata"
2625 "github.com/e2b-dev/infra/packages/shared/pkg/env"
26+ featureflags "github.com/e2b-dev/infra/packages/shared/pkg/feature-flags"
2727 "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
2828 sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
2929 "github.com/e2b-dev/infra/packages/shared/pkg/storage"
@@ -117,12 +117,32 @@ type networkSlotRes struct {
117117 err error
118118}
119119
120+ type Factory struct {
121+ networkPool * network.Pool
122+ devicePool * nbd.DevicePool
123+ featureFlags * featureflags.Client
124+
125+ defaultAllowInternetAccess bool
126+ }
127+
128+ func NewFactory (
129+ networkPool * network.Pool ,
130+ devicePool * nbd.DevicePool ,
131+ featureFlags * featureflags.Client ,
132+ defaultAllowInternetAccess bool ,
133+ ) * Factory {
134+ return & Factory {
135+ networkPool : networkPool ,
136+ devicePool : devicePool ,
137+ featureFlags : featureFlags ,
138+ defaultAllowInternetAccess : defaultAllowInternetAccess ,
139+ }
140+ }
141+
120142// CreateSandbox creates the sandbox.
121143// IMPORTANT: You must Close() the sandbox after you are done with it.
122- func CreateSandbox (
144+ func ( f * Factory ) CreateSandbox (
123145 ctx context.Context ,
124- networkPool * network.Pool ,
125- devicePool * nbd.DevicePool ,
126146 config Config ,
127147 runtime RuntimeMetadata ,
128148 fcVersions fc.FirecrackerVersions ,
@@ -148,12 +168,13 @@ func CreateSandbox(
148168 }
149169 }()
150170
151- allowInternet := globalconfig .AllowSandboxInternet
171+ // TODO: Temporarily set this based on global config, should be removed later (it should be passed as a parameter in build)
172+ allowInternet := f .defaultAllowInternetAccess
152173 if config .AllowInternetAccess != nil {
153174 allowInternet = * config .AllowInternetAccess
154175 }
155176
156- ipsCh := getNetworkSlotAsync (ctx , networkPool , cleanup , allowInternet )
177+ ipsCh := getNetworkSlotAsync (ctx , f . networkPool , cleanup , allowInternet )
157178 defer func () {
158179 // Ensure the slot is received from chan so the slot is cleaned up properly in cleanup
159180 <- ipsCh
@@ -172,7 +193,7 @@ func CreateSandbox(
172193 rootfsProvider , err = rootfs .NewNBDProvider (
173194 rootFS ,
174195 sandboxFiles .SandboxCacheRootfsPath (),
175- devicePool ,
196+ f . devicePool ,
176197 )
177198 } else {
178199 rootfsProvider , err = rootfs .NewDirectProvider (
@@ -305,17 +326,14 @@ func endSpan(span trace.Span, err error) {
305326
306327// ResumeSandbox resumes the sandbox from already saved template or snapshot.
307328// IMPORTANT: You must Close() the sandbox after you are done with it.
308- func ResumeSandbox (
329+ func ( f * Factory ) ResumeSandbox (
309330 ctx context.Context ,
310- networkPool * network.Pool ,
311331 t template.Template ,
312332 config Config ,
313333 runtime RuntimeMetadata ,
314334 traceID string ,
315335 startedAt time.Time ,
316336 endAt time.Time ,
317- devicePool * nbd.DevicePool ,
318- useClickhouseMetrics bool ,
319337 apiConfigToStore * orchestrator.SandboxConfig ,
320338) (s * Sandbox , e error ) {
321339 ctx , span := tracer .Start (ctx , "resume sandbox" )
@@ -334,12 +352,14 @@ func ResumeSandbox(
334352 }
335353 }()
336354
337- allowInternet := globalconfig .AllowSandboxInternet
355+ // TODO: Temporarily set this based on global config, should be removed later
356+ // (it should be passed as a non nil parameter from API)
357+ allowInternet := f .defaultAllowInternetAccess
338358 if config .AllowInternetAccess != nil {
339359 allowInternet = * config .AllowInternetAccess
340360 }
341361
342- ipsCh := getNetworkSlotAsync (ctx , networkPool , cleanup , allowInternet )
362+ ipsCh := getNetworkSlotAsync (ctx , f . networkPool , cleanup , allowInternet )
343363 defer func () {
344364 // Ensure the slot is received from chan before ResumeSandbox returns so the slot is cleaned up properly in cleanup
345365 <- ipsCh
@@ -360,7 +380,7 @@ func ResumeSandbox(
360380 rootfsOverlay , err := rootfs .NewNBDProvider (
361381 readonlyRootfs ,
362382 sandboxFiles .SandboxCacheRootfsPath (),
363- devicePool ,
383+ f . devicePool ,
364384 )
365385 if err != nil {
366386 return nil , fmt .Errorf ("failed to create rootfs overlay: %w" , err )
@@ -508,6 +528,11 @@ func ResumeSandbox(
508528 exit : exit ,
509529 }
510530
531+ useClickhouseMetrics , flagErr := f .featureFlags .BoolFlag (ctx , featureflags .MetricsWriteFlagName )
532+ if flagErr != nil {
533+ zap .L ().Error ("soft failing during metrics write feature flag receive" , zap .Error (flagErr ))
534+ }
535+
511536 // Part of the sandbox as we need to stop Checks before pausing the sandbox
512537 // This is to prevent race condition of reporting unhealthy sandbox
513538 sbx .Checks = NewChecks (sbx , useClickhouseMetrics )
0 commit comments