Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

feat(runner): serve metadata endpoints on loopback only (#479)

Grant McCloskey22954f64bb29
diff --git a/cmd/ax-task-runner/main.go b/cmd/ax-task-runner/main.go
index cb278cb..262b0da 100644
--- a/cmd/ax-task-runner/main.go
+++ b/cmd/ax-task-runner/main.go
@@ -51,7 +51,8 @@ func main() {
taskFile string
wsFiles stringList
)
- flag.IntVar(&cfg.Port, "port", runner.DefaultPort, "Port for the metadata and guest server")
+ flag.IntVar(&cfg.Port, "port", runner.DefaultPort, "Port for the probe and guest server")
+ flag.IntVar(&cfg.MetadataPort, "metadata-port", runner.DefaultMetadataPort, "Loopback-only port for the metadata endpoints")
flag.StringVar(&taskFile, "task-file", "", "Read the Task YAML from this file instead of AX_TASK_YAML")
flag.Var(&wsFiles, "workspace-file", "Read Workspace YAML from this file instead of the environment; repeatable, and each file may hold several documents")
flag.Parse()
diff --git a/docs/networking.md b/docs/networking.md
index a2753fe..e4d83fd 100644
--- a/docs/networking.md
+++ b/docs/networking.md
@@ -10,7 +10,7 @@ Use the Service DNS name and add the header. This is exactly how AX polls a task
```bash
curl -H "ate-target-actor: default/task123" \
- http://atenet-router.ate-system.svc.cluster.local/metadata/v1alpha1/ax/task
+ http://atenet-router.ate-system.svc.cluster.local/readyz
```
## From your machine
diff --git a/docs/runner.md b/docs/runner.md
index 16a0357..e7328f0 100644
--- a/docs/runner.md
+++ b/docs/runner.md
@@ -33,12 +33,17 @@ The `/workspace` volume is what survives suspend and resume. Agent Substrate sna
|---|---|
| `/healthz` | Return `200` as soon as the runner is alive. |
| `/readyz` | Return `503` until the workspace is prepared, then `200`. AX polls this to set the task's `WorkspaceReady` condition, and `ax watch` shows the transition. |
+
+**Serve metadata on loopback.** These endpoints are for your command, so serve them on a loopback address (the default runner uses `127.0.0.1:8099`) and pass that base URL as `AX_METADATA_URL`. Don't serve them on port 80.
+
+| Path | Behavior |
+|---|---|
| `/metadata/v1alpha1/ax/task` | Return the `Task` as `application/yaml`. Optional, but your command and `ax` tooling may expect it. |
| `/metadata/v1alpha1/ax/workspaces` | Return every bound `Workspace` as a multi-document YAML stream. Optional, as above. |
**Prepare each workspace once.** A task binds workspaces through `spec.workspaces`. For each binding, at its path, clone the Git repos from `spec.git`, create the skills path, write any MCP configuration, and run any environment bootstrap the binding asks for through its `goal`. A binding without a path lands at `/workspace/<name>`. Record that setup happened somewhere on the durable volume or in a known location, per workspace, then skip the work on later boots. Resume restarts the container, and re-cloning into a restored workspace would destroy the agent's state. The default runner writes a marker file under `/ax` for each workspace path.
-**Run the command and supervise it.** Start `spec.command` as a child process with the first workspace as its working directory. Give it `AX_METADATA_URL` pointing at your own HTTP server plus every `spec.env` entry. Put it in its own process group so you can signal everything it spawns.
+**Run the command and supervise it.** Start `spec.command` as a child process with the first workspace as its working directory. Give it `AX_METADATA_URL` pointing at your loopback metadata server plus every `spec.env` entry. Put it in its own process group so you can signal everything it spawns.
**Stay up after the command exits.** The runner is PID 1, and the container lives as long as it does. If the runner exits when the command does, the metadata server goes with it and `ax ssh` stops working. Log the exit status and keep serving until you are told to stop. The control plane does not currently read the command's exit status back from the container.
@@ -155,11 +160,11 @@ AX provisions a dedicated Agent Substrate actor template for each distinct image
The default runner accepts its specs from files as well as the environment, which makes it easy to run outside a cluster. Your own runner should offer something similar:
```bash
-ax-task-runner --task-file task.yaml --workspace-file code.yaml --workspace-file tools.yaml --port 8080
+ax-task-runner --task-file task.yaml --workspace-file code.yaml --workspace-file tools.yaml --port 8080 --metadata-port 8099
# In another shell:
curl -i http://127.0.0.1:8080/readyz
-curl -s http://127.0.0.1:8080/metadata/v1alpha1/ax/task
+curl -s http://127.0.0.1:8099/metadata/v1alpha1/ax/task
```
Once the image is built, the fastest end-to-end check is a task with `debug: true` and `ax ssh` into it to confirm the workspace, the command, and the environment look the way you expect.
@@ -169,6 +174,7 @@ Once the image is built, the fastest end-to-end check is a task with `debug: tru
- Executable present at `/usr/local/bin/ax-task-runner`
- Reads `AX_TASK_YAML` and `AX_WORKSPACES_YAML`
- Serves `/healthz` and `/readyz` on port 80, with `/readyz` returning `503` until every workspace is ready
+- Serves the metadata endpoints on loopback only, at `AX_METADATA_URL`
- Prepares each workspace exactly once across restarts and resumes, at its own path
- Starts `spec.command` in the first workspace with `AX_METADATA_URL` and `spec.env`
- Keeps running after the command exits
diff --git a/docs/sandbox.md b/docs/sandbox.md
index adc4797..3da1d3e 100644
--- a/docs/sandbox.md
+++ b/docs/sandbox.md
@@ -3,20 +3,27 @@
Every task container starts with `ax-task-runner` as PID 1. On boot it:
1. Loads the `Task` and every bound `Workspace` spec.
-2. Starts a metadata and guest-management daemon on port 80.
+2. Starts its servers: probes and guest management on port 80, and the metadata endpoints on `127.0.0.1:8099`.
3. On the first run, prepares each workspace at its own path, in binding order: clones Git repos, sets up the skills path, and, if the binding has a goal, hands that goal to an Antigravity agent to finish environment setup. The agent needs `GEMINI_API_KEY` in the container and gets 10 minutes by default; set `AX_BOOTSTRAP_TIMEOUT` to a Go duration to change that. The task reports not-ready until every workspace, including any agent run, has finished.
4. Starts `spec.command` as a child process, with the first workspace as its working directory and `AX_METADATA_URL` plus `spec.env` in its environment, and supervises it.
The runner stays up as PID 1 whether or not the command is still running, so the metadata server keeps answering and `ax ssh` still works after the command has exited. Its exit code is logged. When the sandbox is stopped or suspended, the runner sends the command's process group `SIGTERM`, waits ten seconds, and then kills whatever is left.
-## Metadata server
+## Probes
-The daemon speaks HTTP/1.1 and `h2c` on the same port. Your agent can introspect its own configuration without any SDK:
+Port 80 speaks HTTP/1.1 and `h2c`. Agent Substrate and the AX server use it to check on the task:
| Endpoint | Method | Returns | Description |
|---|---|---|---|
| `/healthz` | `GET` | `text/plain` | Liveness. Always `200 OK`. |
| `/readyz` | `GET` | `text/plain` | Readiness. `503` while the workspace is initializing, `200` once clones, MCP config, and skills are in place. |
+
+## Metadata server
+
+Your agent can introspect its own configuration without any SDK. The metadata endpoints listen on loopback only, at the address in `AX_METADATA_URL`, so they're reachable from inside the task and nowhere else:
+
+| Endpoint | Method | Returns | Description |
+|---|---|---|---|
| `/metadata/v1alpha1/ax/task` | `GET` | `application/yaml` | Task launch configuration, excluding status. |
| `/metadata/v1alpha1/ax/workspaces` | `GET` | `application/yaml` | Every bound `Workspace`, as a multi-document stream in binding order. |
@@ -27,7 +34,7 @@ curl -s "$AX_METADATA_URL/metadata/v1alpha1/ax/task"
## Guest services
-When a task sets `spec.debug: true`, the same port also serves the [Agent Substrate guest services](https://github.com/agent-substrate/env) over gRPC. They are off by default because they allow arbitrary process execution and file access inside the sandbox, and `ax ssh` refuses to connect to a task that has not enabled them.
+When a task sets `spec.debug: true`, port 80 also serves the [Agent Substrate guest services](https://github.com/agent-substrate/env) over gRPC. They are off by default because they allow arbitrary process execution and file access inside the sandbox, and `ax ssh` refuses to connect to a task that has not enabled them.
- **Process service**: start, inspect, stream output from, and kill processes. This is what powers `ax ssh`.
- **File system service**: streaming file reads and writes inside the workspace.
@@ -36,4 +43,4 @@ When a task sets `spec.debug: true`, the same port also serves the [Agent Substr
| Variable | Value |
|---|---|
-| `AX_METADATA_URL` | Base URL of the metadata server, e.g. `http://127.0.0.1:80` |
+| `AX_METADATA_URL` | Base URL of the metadata server, `http://127.0.0.1:8099` by default |
diff --git a/internal/metadata/server.go b/internal/metadata/server.go
index fb4feba..803c00a 100644
--- a/internal/metadata/server.go
+++ b/internal/metadata/server.go
@@ -30,11 +30,16 @@ import (
"gopkg.in/yaml.v3"
)
-// Server is the cloud-style metadata server running inside the task actor container,
-// multiplexing HTTP metadata endpoints and guest gRPC daemon services on a single port.
+// Server is the cloud-style metadata server running inside the task actor container.
+// It runs two listeners. The probe listener on all interfaces serves /healthz and
+// /readyz, multiplexed with the guest gRPC services when debug is enabled. The
+// metadata listener serves the /metadata endpoints on loopback only, so they are
+// reachable from inside the container and nowhere else.
type Server struct {
- port int
server *http.Server
+ metadataServer *http.Server
+ probeAddr string // bound address of the probe listener, set by Start
+ metadataAddr string // bound address of the metadata listener, set by Start
grpcServer *grpc.Server
grpcCleanup func()
mu sync.RWMutex
@@ -47,25 +52,26 @@ type Server struct {
type ServerOptions struct {
WorkspacePath string
LogDir string
+ // MetadataPort is the loopback port for the /metadata endpoints. Zero picks
+ // a free port; read the result from MetadataURL after Start.
+ MetadataPort int
}
// NewServer creates a new metadata and guest server serving the task and its
// bound workspaces, in the task's declaration order. Nil workspaces are dropped.
+// port is the probe and guest port; the metadata endpoints use
+// ServerOptions.MetadataPort on loopback. A zero port picks a free one; read
+// the result from ProbeAddr or MetadataURL after Start.
func NewServer(port int, task *v1alpha1.Task, workspaces []*v1alpha1.Workspace, opts ...ServerOptions) *Server {
- if port <= 0 {
- port = 9999
+ var opt ServerOptions
+ if len(opts) > 0 {
+ opt = opts[0]
}
s := &Server{
- port: port,
task: task,
workspaces: compactWorkspaces(workspaces),
}
- var opt ServerOptions
- if len(opts) > 0 {
- opt = opts[0]
- }
-
// Guest services expose process execution and file access inside the container,
// so they are only served when the task opts in via spec.debug.
if task.GetSpec().GetDebug() {
@@ -89,22 +95,17 @@ func NewServer(port int, task *v1alpha1.Task, workspaces []*v1alpha1.Workspace,
slog.Info("guest services disabled; set spec.debug: true on the Task to enable ax ssh")
}
- mux := http.NewServeMux()
- mux.HandleFunc("/healthz", s.handleHealthz)
- mux.HandleFunc("/readyz", s.handleReadyz)
-
- // Metadata endpoints:
- // /metadata/v1alpha1/ax/task the Task
- // /metadata/v1alpha1/ax/workspaces every bound Workspace, as a YAML stream
- mux.HandleFunc("/metadata/v1alpha1/ax/task", s.handleTask)
- mux.HandleFunc("/metadata/v1alpha1/ax/workspaces", s.handleWorkspaces)
+ // Probe endpoints, reachable from outside the container.
+ probeMux := http.NewServeMux()
+ probeMux.HandleFunc("/healthz", s.handleHealthz)
+ probeMux.HandleFunc("/readyz", s.handleReadyz)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.grpcServer != nil && r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
s.grpcServer.ServeHTTP(w, r)
return
}
- mux.ServeHTTP(w, r)
+ probeMux.ServeHTTP(w, r)
})
s.server = &http.Server{
@@ -115,20 +116,60 @@ func NewServer(port int, task *v1alpha1.Task, workspaces []*v1alpha1.Workspace,
s.server.Protocols.SetHTTP1(true)
s.server.Protocols.SetUnencryptedHTTP2(true)
+ // Metadata endpoints, for the task command only:
+ // /metadata/v1alpha1/ax/task the Task
+ // /metadata/v1alpha1/ax/workspaces every bound Workspace, as a YAML stream
+ metadataMux := http.NewServeMux()
+ metadataMux.HandleFunc("/metadata/v1alpha1/ax/task", s.handleTask)
+ metadataMux.HandleFunc("/metadata/v1alpha1/ax/workspaces", s.handleWorkspaces)
+
+ s.metadataServer = &http.Server{
+ Addr: net.JoinHostPort("127.0.0.1", fmt.Sprint(opt.MetadataPort)),
+ Handler: metadataMux,
+ }
+
return s
}
-// Start begins serving the metadata HTTP and guest gRPC service in a background goroutine.
+// ProbeAddr is the address the probe and guest listener is bound to. It is
+// empty until Start succeeds.
+func (s *Server) ProbeAddr() string {
+ return s.probeAddr
+}
+
+// MetadataURL is the base URL of the metadata endpoints, as handed to the task
+// command in AX_METADATA_URL. It is empty until Start succeeds.
+func (s *Server) MetadataURL() string {
+ if s.metadataAddr == "" {
+ return ""
+ }
+ return "http://" + s.metadataAddr
+}
+
+// Start begins serving the probe, guest, and metadata endpoints in background goroutines.
func (s *Server) Start() error {
lis, err := net.Listen("tcp", s.server.Addr)
if err != nil {
- return fmt.Errorf("metadata server listening on %s: %w", s.server.Addr, err)
+ return fmt.Errorf("probe and guest server listening on %s: %w", s.server.Addr, err)
+ }
+ metadataLis, err := net.Listen("tcp", s.metadataServer.Addr)
+ if err != nil {
+ _ = lis.Close()
+ return fmt.Errorf("metadata server listening on %s: %w", s.metadataServer.Addr, err)
}
- slog.Info("metadata and guest server started", "addr", s.server.Addr)
+ s.probeAddr = lis.Addr().String()
+ s.metadataAddr = metadataLis.Addr().String()
+ slog.Info("probe and guest server started", "addr", s.probeAddr)
+ slog.Info("metadata server started", "addr", s.metadataAddr)
go func() {
if err := s.server.Serve(lis); err != nil && !errors.Is(err, http.ErrServerClosed) {
+ slog.Error("probe and guest server error", "error", err)
+ }
+ }()
+ go func() {
+ if err := s.metadataServer.Serve(metadataLis); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("metadata server error", "error", err)
}
}()
@@ -136,12 +177,19 @@ func (s *Server) Start() error {
return nil
}
-// Stop gracefully stops the metadata and guest server.
+// Stop gracefully stops the probe, guest, and metadata servers.
func (s *Server) Stop(ctx context.Context) error {
if s.grpcCleanup != nil {
s.grpcCleanup()
}
- return s.server.Shutdown(ctx)
+ // Shut both servers down concurrently so one draining its connections does
+ // not hold the other open or use up ctx's deadline.
+ var wg sync.WaitGroup
+ var probeErr, metadataErr error
+ wg.Go(func() { probeErr = s.server.Shutdown(ctx) })
+ wg.Go(func() { metadataErr = s.metadataServer.Shutdown(ctx) })
+ wg.Wait()
+ return errors.Join(probeErr, metadataErr)
}
// UpdateState allows dynamic updates to the current Task and Workspaces.
diff --git a/internal/metadata/server_test.go b/internal/metadata/server_test.go
index 7564d4d..209925b 100644
--- a/internal/metadata/server_test.go
+++ b/internal/metadata/server_test.go
@@ -17,7 +17,9 @@ package metadata_test
import (
"context"
"io"
+ "net"
"net/http"
+ "strings"
"testing"
"time"
@@ -64,7 +66,7 @@ func TestMetadataServer(t *testing.T) {
Spec: &v1alpha1.WorkspaceSpec{},
}
- srv := metadata.NewServer(9999, task, []*v1alpha1.Workspace{ws, ws2})
+ srv := metadata.NewServer(0, task, []*v1alpha1.Workspace{ws, ws2})
if err := srv.Start(); err != nil {
t.Fatalf("failed to start server: %v", err)
}
@@ -74,7 +76,7 @@ func TestMetadataServer(t *testing.T) {
time.Sleep(50 * time.Millisecond)
// Test /healthz
- resp, err := http.Get("http://127.0.0.1:9999/healthz")
+ resp, err := http.Get("http://" + srv.ProbeAddr() + "/healthz")
if err != nil {
t.Fatalf("healthz request failed: %v", err)
}
@@ -83,7 +85,7 @@ func TestMetadataServer(t *testing.T) {
}
// Test /readyz before workspace ready
- resp, err = http.Get("http://127.0.0.1:9999/readyz")
+ resp, err = http.Get("http://" + srv.ProbeAddr() + "/readyz")
if err != nil {
t.Fatalf("readyz request failed: %v", err)
}
@@ -95,7 +97,7 @@ func TestMetadataServer(t *testing.T) {
srv.SetWorkspaceReady(true)
// Test /readyz after workspace ready
- resp, err = http.Get("http://127.0.0.1:9999/readyz")
+ resp, err = http.Get("http://" + srv.ProbeAddr() + "/readyz")
if err != nil {
t.Fatalf("readyz request failed: %v", err)
}
@@ -104,7 +106,7 @@ func TestMetadataServer(t *testing.T) {
}
// Test /metadata/v1alpha1/ax/task
- resp, err = http.Get("http://127.0.0.1:9999/metadata/v1alpha1/ax/task")
+ resp, err = http.Get(srv.MetadataURL() + "/metadata/v1alpha1/ax/task")
if err != nil {
t.Fatalf("get task metadata failed: %v", err)
}
@@ -120,7 +122,7 @@ func TestMetadataServer(t *testing.T) {
}
// Test /metadata/v1alpha1/ax/workspaces returns every bound workspace in order
- resp, err = http.Get("http://127.0.0.1:9999/metadata/v1alpha1/ax/workspaces")
+ resp, err = http.Get(srv.MetadataURL() + "/metadata/v1alpha1/ax/workspaces")
if err != nil {
t.Fatalf("get workspaces metadata failed: %v", err)
}
@@ -142,7 +144,7 @@ func TestMetadataServer(t *testing.T) {
}
// Test multiplexed gRPC ProcessService on the same port
- grpcConn, err := grpc.NewClient("127.0.0.1:9999", grpc.WithTransportCredentials(insecure.NewCredentials()))
+ grpcConn, err := grpc.NewClient(srv.ProbeAddr(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("dialing gRPC: %v", err)
}
@@ -169,7 +171,7 @@ func TestMetadataServer_GuestServicesRequireDebug(t *testing.T) {
Spec: &v1alpha1.TaskSpec{Image: "ghcr.io/test/img"},
}
- srv := metadata.NewServer(9998, task, nil)
+ srv := metadata.NewServer(0, task, nil)
if err := srv.Start(); err != nil {
t.Fatalf("failed to start server: %v", err)
}
@@ -177,7 +179,7 @@ func TestMetadataServer_GuestServicesRequireDebug(t *testing.T) {
time.Sleep(100 * time.Millisecond)
// HTTP metadata still works.
- resp, err := http.Get("http://127.0.0.1:9998/metadata/v1alpha1/ax/task")
+ resp, err := http.Get(srv.MetadataURL() + "/metadata/v1alpha1/ax/task")
if err != nil {
t.Fatalf("task metadata request failed: %v", err)
}
@@ -187,7 +189,7 @@ func TestMetadataServer_GuestServicesRequireDebug(t *testing.T) {
}
// Guest gRPC is not served.
- grpcConn, err := grpc.NewClient("127.0.0.1:9998", grpc.WithTransportCredentials(insecure.NewCredentials()))
+ grpcConn, err := grpc.NewClient(srv.ProbeAddr(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("failed to create grpc client: %v", err)
}
@@ -202,3 +204,59 @@ func TestMetadataServer_GuestServicesRequireDebug(t *testing.T) {
t.Fatalf("expected StartProcess to fail when spec.debug is false")
}
}
+
+func TestMetadataServer_MetadataOnlyOnLoopback(t *testing.T) {
+ task := &v1alpha1.Task{
+ Metadata: &v1alpha1.ObjectMeta{Name: "loopback", Atespace: "default"},
+ Spec: &v1alpha1.TaskSpec{Image: "ghcr.io/test/img"},
+ }
+
+ srv := metadata.NewServer(0, task, nil)
+ if err := srv.Start(); err != nil {
+ t.Fatalf("failed to start server: %v", err)
+ }
+ defer srv.Stop(context.Background())
+ time.Sleep(100 * time.Millisecond)
+
+ metadataHost, metadataPort, err := net.SplitHostPort(strings.TrimPrefix(srv.MetadataURL(), "http://"))
+ if err != nil {
+ t.Fatalf("parsing MetadataURL %q: %v", srv.MetadataURL(), err)
+ }
+ if metadataHost != "127.0.0.1" {
+ t.Errorf("metadata listener bound to %q, want 127.0.0.1", metadataHost)
+ }
+
+ // The metadata endpoints are not served on the probe port.
+ for _, path := range []string{"/metadata/v1alpha1/ax/task", "/metadata/v1alpha1/ax/workspaces"} {
+ resp, err := http.Get("http://" + srv.ProbeAddr() + path)
+ if err != nil {
+ t.Fatalf("request to probe port failed: %v", err)
+ }
+ resp.Body.Close()
+ if resp.StatusCode != http.StatusNotFound {
+ t.Errorf("GET %s on the probe port: expected 404, got %d", path, resp.StatusCode)
+ }
+ }
+
+ // The metadata port does not accept connections on non-loopback addresses.
+ addrs, err := net.InterfaceAddrs()
+ if err != nil {
+ t.Fatalf("listing interface addresses: %v", err)
+ }
+ checked := false
+ for _, a := range addrs {
+ ipNet, ok := a.(*net.IPNet)
+ if !ok || ipNet.IP.IsLoopback() || ipNet.IP.IsLinkLocalUnicast() || ipNet.IP.To4() == nil {
+ continue
+ }
+ checked = true
+ conn, err := net.DialTimeout("tcp", net.JoinHostPort(ipNet.IP.String(), metadataPort), time.Second)
+ if err == nil {
+ conn.Close()
+ t.Errorf("metadata port accepted a connection on %s", ipNet.IP)
+ }
+ }
+ if !checked {
+ t.Log("no non-loopback IPv4 address found; skipped the external address check")
+ }
+}
diff --git a/runner/runner.go b/runner/runner.go
index 4eaadb0..cd1fb78 100644
--- a/runner/runner.go
+++ b/runner/runner.go
@@ -37,9 +37,12 @@ import (
)
const (
- // DefaultPort is where the metadata and guest server listens when Config.Port
+ // DefaultPort is where the probe and guest server listens when Config.Port
// is zero. It matches the port Agent Substrate probes for readiness.
DefaultPort = 80
+ // DefaultMetadataPort is the loopback port for the metadata endpoints when
+ // Config.MetadataPort is zero.
+ DefaultMetadataPort = 8099
// DefaultWorkspacePath is the command's working directory when the task
// binds no workspaces.
DefaultWorkspacePath = v1alpha1.DefaultWorkspacePath
@@ -51,9 +54,12 @@ const (
// Config controls a single Run.
type Config struct {
- // Port is where the metadata HTTP endpoints and guest gRPC services listen.
+ // Port is where the probe endpoints and guest gRPC services listen.
// Zero means DefaultPort.
Port int
+ // MetadataPort is the loopback-only port for the metadata endpoints the task
+ // command reads through AX_METADATA_URL. Zero means DefaultMetadataPort.
+ MetadataPort int
// Task is the task to run. It supplies the command, its environment, and
// the workspace bindings. Nil runs with no command and default settings.
Task *v1alpha1.Task
@@ -121,6 +127,10 @@ func Run(ctx context.Context, cfg Config) error {
if port <= 0 {
port = DefaultPort
}
+ metadataPort := cfg.MetadataPort
+ if metadataPort <= 0 {
+ metadataPort = DefaultMetadataPort
+ }
mounts := resolveMounts(cfg)
if len(mounts) == 0 {
// Nothing bound: still provide an empty working directory at the default path.
@@ -137,16 +147,18 @@ func Run(ctx context.Context, cfg Config) error {
}
slog.Info("initializing AX task runner", "port", port, "wsPath", wsPath, "workspaces", paths)
- metadataURL := fmt.Sprintf("http://127.0.0.1:%d", port)
+ metaServer := metadata.NewServer(port, cfg.Task, workspaces, metadata.ServerOptions{
+ WorkspacePath: wsPath,
+ MetadataPort: metadataPort,
+ })
+ if err := metaServer.Start(); err != nil {
+ return fmt.Errorf("starting metadata server: %w", err)
+ }
+ metadataURL := metaServer.MetadataURL()
_ = os.Setenv("AX_METADATA_URL", metadataURL)
for _, e := range cfg.Task.GetSpec().GetEnv() {
_ = os.Setenv(e.GetName(), e.GetValue())
}
-
- metaServer := metadata.NewServer(port, cfg.Task, workspaces, metadata.ServerOptions{WorkspacePath: wsPath})
- if err := metaServer.Start(); err != nil {
- return fmt.Errorf("starting metadata server: %w", err)
- }
defer func() {
slog.Info("shutting down metadata server")
_ = metaServer.Stop(context.Background())
@@ -176,7 +188,7 @@ func Run(ctx context.Context, cfg Config) error {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
- cmd.Env = commandEnv(cfg.Task, port)
+ cmd.Env = commandEnv(cfg.Task, metadataURL)
// A dedicated process group lets shutdown signal the command together with
// anything it spawned.
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
@@ -237,8 +249,8 @@ func reportExit(cfg Config, cmd *exec.Cmd, err error) {
// commandEnv builds the environment for the task command: the runner's own
// environment, the metadata URL, and the Task's spec.env entries.
-func commandEnv(task *v1alpha1.Task, port int) []string {
- env := append(os.Environ(), fmt.Sprintf("AX_METADATA_URL=http://127.0.0.1:%d", port))
+func commandEnv(task *v1alpha1.Task, metadataURL string) []string {
+ env := append(os.Environ(), "AX_METADATA_URL="+metadataURL)
for _, e := range task.GetSpec().GetEnv() {
env = append(env, fmt.Sprintf("%s=%s", e.GetName(), e.GetValue()))
}
diff --git a/runner/runner_test.go b/runner/runner_test.go
index e44cea1..2810adc 100644
--- a/runner/runner_test.go
+++ b/runner/runner_test.go
@@ -47,7 +47,7 @@ func TestRun_StartsTaskCommandWithInjectedEnv(t *testing.T) {
t.Fatalf("command did not write its output: %v", err)
}
fields := strings.Fields(string(out))
- if len(fields) != 3 || fields[0] != "from-task" || fields[1] != fmt.Sprintf("http://127.0.0.1:%d", h.cfg.Port) {
+ if len(fields) != 3 || fields[0] != "from-task" || fields[1] != fmt.Sprintf("http://127.0.0.1:%d", h.cfg.MetadataPort) {
t.Errorf("unexpected env in command: %q", out)
}
// The working directory is the workspace path; compare resolved paths since
@@ -178,6 +178,7 @@ func newHarness(t *testing.T) *harness {
}
h.cfg = runner.Config{
Port: freePort(t),
+ MetadataPort: freePort(t),
Task: h.task,
OnCommandExit: func(e runner.CommandExit) { h.exited <- e },
}
@@ -292,7 +293,7 @@ func TestRun_SetsUpEveryWorkspace(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Errorf("readyz = %d, want 200", resp.StatusCode)
}
- resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/metadata/v1alpha1/ax/workspaces", h.cfg.Port))
+ resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/metadata/v1alpha1/ax/workspaces", h.cfg.MetadataPort))
if err != nil {
t.Fatal(err)
}