Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions cmd/agent-runtime/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"context"
"net/http"
"os"
"strconv"

"k8s.io/klog/v2"

flagOptions "github.com/openkruise/agents/cmd/agent-runtime/options"
agentruntime "github.com/openkruise/agents/pkg/agent-runtime"
"github.com/openkruise/agents/pkg/agent-runtime/host"
"github.com/openkruise/agents/pkg/agent-runtime/logs"
"github.com/openkruise/agents/pkg/agent-runtime/openapi/types"
"github.com/openkruise/agents/pkg/utils"
utilMap "github.com/openkruise/agents/pkg/utils/map"
)

const (
// This is the default user used in the container if not specified otherwise.
// It should be always overridden by the user in /init when building the template.
defaultUser = "root"
)

/*
Main function entry point for the sandboxRuntime,
serving as a standard service interface process that provides compatibility with e2b and envd environments.
It extends beyond basic compatibility by offering enhanced interface capabilities, including storage mounting functionality through CSI plugins.
*/
func main() {
klog.InitFlags(nil)
flagOptions.InitFlagOptions()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

logContext := logs.NewLoggerContext(ctx, agentruntime.SandboxRuntimeHttpServer, agentruntime.SandboxRuntimeHttpServerVersion)
logCollector := klog.FromContext(logContext)

if err := os.MkdirAll(host.E2BRunDir, 0o755); err != nil {
logCollector.Error(err, "error creating E2B run directory")
}

// To print the version
if flagOptions.VersionFlag {
logCollector.V(3).Info(agentruntime.SandboxRuntimeHttpServer, agentruntime.SandboxRuntimeHttpServerVersion)
}

// Start pprof server if enabled
if flagOptions.EnablePprof {
go func() {
klog.Info("starting pprof server", "addr", flagOptions.PprofAddr)
if err := http.ListenAndServe(flagOptions.PprofAddr, nil); err != nil {
logCollector.Error(err, "unable to start pprof server")
}
}()
}

defaults := &types.Defaults{
User: defaultUser,
EnvVars: utilMap.NewMap[string, string](),
}

// To set the default env var
isFCBoolStr := strconv.FormatBool(!flagOptions.IsNotFC)
defaults.EnvVars.Store("E2B_SANDBOX", isFCBoolStr)

config := agentruntime.ServerConfig{
Port: flagOptions.ServerPort,
Workspace: flagOptions.Workspace,
AuthConfig: agentruntime.AuthConfig{
ValidTokens: utils.StringToSlice(flagOptions.ValidTokens, ","),
AllowedPaths: utils.StringToSlice(flagOptions.AllowedPaths, ","),
EnableSigning: flagOptions.EnableSigning,
},
FlagConfig: agentruntime.FlagConfig{
VersionFlag: flagOptions.VersionFlag,
StartCmdFlag: flagOptions.StartCmdFlag,
},
Defaults: defaults,
}

// To config the cmd execution process
if config.FlagConfig.StartCmdFlag != "" {
// TODO: add the cmd execution process
}

// Create and start server and start
if err := agentruntime.NewHttpServer(config).Run(); err != nil {
logCollector.Error(err, "Failed to start sandboxRuntime")
os.Exit(1)
}
logCollector.Info("SandboxRuntime http stopped")
}
35 changes: 35 additions & 0 deletions cmd/agent-runtime/options/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package options

const (
defaultHttpServerPort = 49983
defaultLogLevel = 6
)

var (
// ServerPort defines the network port that the HTTP server binds to.
ServerPort int

// ServerLogLevel controls the logging level of the server.
ServerLogLevel int

Workspace string

// ValidTokens defines the valid token for authentication.
ValidTokens string

// AllowedPaths defines the allowed paths for skip authentication.
AllowedPaths string

// EnableSigning defines whether to enable signature validation.
EnableSigning bool

// Enable pprof config for debug
EnablePprof bool
PprofAddr string

// Some flags config for agent-runtime
VersionFlag bool
StartCmdFlag string

IsNotFC bool
)
43 changes: 43 additions & 0 deletions cmd/agent-runtime/options/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package options

import (
"flag"
"os"
"strconv"
)

func InitFlagOptions() {
// Set default values
if portEnv, err := strconv.Atoi(os.Getenv("SERVER_PORT")); err == nil {
ServerPort = portEnv
} else {
ServerPort = defaultHttpServerPort
}

if logLevelEnv, err := strconv.Atoi(os.Getenv("LOG_LEVEL")); err == nil {
ServerLogLevel = logLevelEnv
} else {
ServerLogLevel = defaultLogLevel
}

flag.IntVar(&ServerPort, "port", ServerPort, "SandboxRuntime Http Server listening port (default: 9527)")
flag.IntVar(&ServerLogLevel, "log-level", ServerLogLevel,
"Server log level — Specifies the logging verbosity as an integer from 0 to 7, where 0=LevelEmergency, 1=LevelAlert, 2=LevelCritical, 3=LevelError, 4=LevelWarning, 5=LevelNotice, 6=LevelInformational, and 7=LevelDebug; defaults to 6 (LevelInformational).)")
flag.String("workspace", "",
"Root directory for file operations — Specifies the base path used for file system operations; defaults to the current working directory if not provided.\n\n")
flag.BoolVar(&EnablePprof, "enable-pprof", false, "Enable pprof profiling")
flag.StringVar(&PprofAddr, "pprof-addr", ":6060", "The address the pprof debug maps to.")

flag.StringVar(&ValidTokens, "valid-tokens", "", "The valid tokens for authentication")
flag.StringVar(&AllowedPaths, "allowed-paths", "", "The allowed paths for authentication")
flag.BoolVar(&EnableSigning, "enable-signing", true, "Enable signing for authentication")

// flag config
flag.BoolVar(&VersionFlag, "version", false, "To print envd version")
flag.StringVar(&StartCmdFlag, "cmd", "", "A command to run on the daemon start")

flag.BoolVar(&IsNotFC, "isnotfc", false, "isNotFCmode prints all logs to stdout")

// Parse flags - these will override environment variables if provided
flag.Parse()
}
39 changes: 39 additions & 0 deletions dockerfiles/agent-runtime_v1.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Build stage
FROM golang:1.24 AS builder

WORKDIR /app

# Copy go mod and sum files
COPY ../go.mod go.sum ./

# Copy the source code
COPY ../cmd/agent-runtime ./cmd/agent-runtime
COPY ../pkg ./pkg
COPY ../api ./api
COPY ../client ./client

# Build the binary
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o sandbox-agent-runtime ./cmd/agent-runtime

# Final stage
FROM alpine:3.20 as runtime

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk --no-cache add ca-certificates && \
rm -rf /usr/local/sbin/* && \
rm -rf /usr/local/bin/* && \
rm -rf /usr/sbin/* && \
rm -rf /usr/bin/* && \
rm -rf /sbin/* && \
rm -rf /bin/*

WORKDIR /
# Copy the binary from builder stage
COPY --from=builder /app/sandbox-agent-runtime .
USER 65532:65532

# Expose port
EXPOSE 49983

# Run the binary
CMD ["/sandbox-agent-runtime"]
29 changes: 28 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ require (
github.com/container-storage-interface/spec v1.9.0
github.com/distribution/reference v0.6.0
github.com/envoyproxy/go-control-plane/envoy v1.32.4
github.com/gin-contrib/cors v1.7.6
github.com/gin-gonic/gin v1.10.1
github.com/go-logr/logr v1.4.3
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.27.3
github.com/onsi/gomega v1.38.2
github.com/prometheus/client_golang v1.22.0
github.com/shirou/gopsutil/v4 v4.26.1
github.com/spf13/pflag v1.0.6
github.com/stretchr/testify v1.11.1
golang.org/x/sys v0.40.0
google.golang.org/grpc v1.75.1
google.golang.org/protobuf v1.36.9
k8s.io/api v0.33.0
Expand All @@ -35,22 +39,33 @@ require (
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/bytedance/sonic v1.13.3 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/ebitengine/purego v0.9.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.9 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.26.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/cel-go v0.23.2 // indirect
Expand All @@ -61,20 +76,31 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.16.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/tklauser/go-sysconf v0.3.16 // indirect
github.com/tklauser/numcpus v0.11.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.2.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.63.0 // indirect
go.opentelemetry.io/otel v1.38.0 // indirect
Expand All @@ -87,12 +113,13 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/arch v0.18.0 // indirect
golang.org/x/crypto v0.42.0 // indirect
golang.org/x/exp v0.0.0-20250531010427-b6e5de432a8b // indirect
golang.org/x/mod v0.27.0 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/oauth2 v0.30.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/term v0.35.0 // indirect
golang.org/x/text v0.29.0 // indirect
golang.org/x/time v0.11.0 // indirect
Expand Down
Loading
Loading