Agent Sandbox
Go SDK Reference
import "sigs.k8s.io/agent-sandbox/clients/go/sandbox"
Index
- Constants
- Variables
- func NewTracerProvider(ctx context.Context, serviceName string) (*sdktrace.TracerProvider, error)
- type CallOption
- type Client
- func NewClient(_ context.Context, opts Options) (*Client, error)
- func (c *Client) CreateSandbox(ctx context.Context, warmPoolName, namespace string) (*Sandbox, error)
- func (c *Client) DeleteAll(ctx context.Context)
- func (c *Client) DeleteSandbox(ctx context.Context, claimName, namespace string) error
- func (c *Client) EnableAutoCleanup() (stop func())
- func (c *Client) GetSandbox(ctx context.Context, claimName, namespace string) (*Sandbox, error)
- func (c *Client) ListActiveSandboxes() []Key
- func (c *Client) ListAllSandboxes(ctx context.Context, namespace string) ([]string, error)
- type Commands
- type ConnectionStrategy
- type DirectStrategy
- type ExecutionResult
- type FileEntry
- type FileType
- type Files
- func (f *Files) Exists(ctx context.Context, path string, opts …CallOption) (bool, error)
- func (f *Files) List(ctx context.Context, path string, opts …CallOption) ([]FileEntry, error)
- func (f *Files) Read(ctx context.Context, path string, opts …CallOption) ([]byte, error)
- func (f *Files) Write(ctx context.Context, path string, content []byte, opts …CallOption) error
- type HTTPError
- type Handle
- type Info
- type K8sHelper
- type Key
- type Options
- type Sandbox
- func New(_ context.Context, opts Options) (*Sandbox, error)
- func (s *Sandbox) Annotations() map[string]string
- func (s *Sandbox) ClaimName() string
- func (s *Sandbox) Close(ctx context.Context) error
- func (s *Sandbox) Commands() *Commands
- func (s *Sandbox) Disconnect(ctx context.Context) error
- func (s *Sandbox) Exists(ctx context.Context, path string, opts …CallOption) (bool, error)
- func (s *Sandbox) Files() *Files
- func (s *Sandbox) IsReady() bool
- func (s *Sandbox) List(ctx context.Context, path string, opts …CallOption) ([]FileEntry, error)
- func (s *Sandbox) Open(ctx context.Context) (retErr error)
- func (s *Sandbox) PodIP() string
- func (s *Sandbox) PodName() string
- func (s *Sandbox) Read(ctx context.Context, path string, opts …CallOption) ([]byte, error)
- func (s *Sandbox) Run(ctx context.Context, command string, opts …CallOption) (*ExecutionResult, error)
- func (s *Sandbox) SandboxName() string
- func (s *Sandbox) Write(ctx context.Context, path string, content []byte, opts …CallOption) error
Constants
const (
// PodNameAnnotation is the annotation key on a Sandbox resource that
// identifies the name of the underlying pod.
PodNameAnnotation = "agents.x-k8s.io/pod-name"
)
Variables
Span attribute keys in the sandbox.* namespace.
var (
AttrClaimName = attribute.Key("sandbox.claim.name")
AttrCommandExecutable = attribute.Key("sandbox.command.executable")
AttrExitCode = attribute.Key("sandbox.exit_code")
AttrFilePath = attribute.Key("sandbox.file.path")
AttrFileSize = attribute.Key("sandbox.file.size")
AttrFileCount = attribute.Key("sandbox.file.count")
AttrFileExists = attribute.Key("sandbox.file.exists")
AttrGatewayName = attribute.Key("sandbox.gateway.name")
AttrGatewayNamespace = attribute.Key("sandbox.gateway.namespace")
AttrRequestID = attribute.Key("sandbox.request_id")
)
Sentinel errors returned by the SDK.
var (
ErrNotReady = errors.New("sandbox is not ready")
ErrTimeout = errors.New("operation timed out")
ErrClaimFailed = errors.New("claim creation failed")
ErrPortForwardDied = errors.New("port-forward connection lost")
ErrAlreadyOpen = errors.New("sandbox is already open; call Close first")
ErrOrphanedClaim = errors.New("orphaned claim; call Close() to retry deletion")
ErrRetriesExhausted = errors.New("retries exhausted")
ErrSandboxDeleted = errors.New("sandbox was deleted before becoming ready")
ErrGatewayDeleted = errors.New("gateway was deleted during address discovery")
ErrResponseTooLarge = errors.New("response exceeded 16 MB limit")
)
func NewTracerProvider
func NewTracerProvider(ctx context.Context, serviceName string) (*sdktrace.TracerProvider, error)
NewTracerProvider creates a TracerProvider with an OTLP/gRPC exporter. The endpoint is read from OTEL_EXPORTER_OTLP_ENDPOINT (default: localhost:4317). serviceName becomes the service.name resource attribute. The caller owns the returned provider and must call Shutdown when done.
type CallOption
CallOption configures per-call behavior for SDK operations.
type CallOption func(*callOptions)
func WithMaxAttempts
func WithMaxAttempts(n int) CallOption
WithMaxAttempts sets the maximum number of attempts for an operation. Values ≤0 are ignored and the default is used (1 for Run, 6 for file operations).
result, err := client.Run(ctx, "cat /etc/hostname", sandbox.WithMaxAttempts(6))
func WithTimeout
func WithTimeout(d time.Duration) CallOption
WithTimeout sets the total timeout for a single operation, overriding the default RequestTimeout for that call.
type Client
Client manages sandbox lifecycles and tracks active handles.
type Client struct {
// contains filtered or unexported fields
}
func NewClient
func NewClient(_ context.Context, opts Options) (*Client, error)
NewClient creates a Client with shared configuration.
func (*Client) CreateSandbox
func (c *Client) CreateSandbox(ctx context.Context, warmPoolName, namespace string) (*Sandbox, error)
CreateSandbox provisions a new sandbox and returns a managed handle. On failure, the orphaned claim is cleaned up.
func (*Client) DeleteAll
func (c *Client) DeleteAll(ctx context.Context)
DeleteAll closes and deletes all tracked sandboxes. Best-effort.
func (*Client) DeleteSandbox
func (c *Client) DeleteSandbox(ctx context.Context, claimName, namespace string) error
DeleteSandbox closes the handle (if tracked) and deletes the claim.
func (*Client) EnableAutoCleanup
func (c *Client) EnableAutoCleanup() (stop func())
EnableAutoCleanup calls DeleteAll on SIGINT/SIGTERM. Call the returned function to stop the signal handler.
func (*Client) GetSandbox
func (c *Client) GetSandbox(ctx context.Context, claimName, namespace string) (*Sandbox, error)
GetSandbox retrieves an existing sandbox by claim name. Returns the cached handle if connected, otherwise re-attaches.
func (*Client) ListActiveSandboxes
func (c *Client) ListActiveSandboxes() []Key
ListActiveSandboxes returns tracked sandboxes, pruning inactive handles.
func (*Client) ListAllSandboxes
func (c *Client) ListAllSandboxes(ctx context.Context, namespace string) ([]string, error)
ListAllSandboxes lists all SandboxClaim names in the given namespace.
type Commands
Commands provides command execution on a sandbox.
type Commands struct {
// contains filtered or unexported fields
}
func (*Commands) Run
func (c *Commands) Run(ctx context.Context, command string, opts ...CallOption) (*ExecutionResult, error)
Run executes a command in the sandbox and returns the result. The combined JSON response (stdout + stderr + metadata) is limited to 16 MB; commands producing more output will fail with ErrResponseTooLarge.
Because command execution is not idempotent, Run defaults to a single attempt (no retries). For idempotent commands that should retry on transient server errors (502, 503, etc.), use WithMaxAttempts:
result, err := client.Run(ctx, "cat /etc/hostname", sandbox.WithMaxAttempts(6))
type ConnectionStrategy
ConnectionStrategy defines how the SDK discovers the sandbox-router URL.
type ConnectionStrategy interface {
Connect(ctx context.Context) (baseURL string, err error)
Close() error
}
type DirectStrategy
DirectStrategy connects using a pre-configured URL, bypassing all discovery.
type DirectStrategy struct {
URL string
}
func (*DirectStrategy) Close
func (s *DirectStrategy) Close() error
func (*DirectStrategy) Connect
func (s *DirectStrategy) Connect(_ context.Context) (string, error)
type ExecutionResult
ExecutionResult holds the result of a command execution in the sandbox.
type ExecutionResult struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ExitCode int `json:"exit_code"`
}
type FileEntry
FileEntry represents a file or directory entry in the sandbox.
type FileEntry struct {
Name string `json:"name"`
Size int64 `json:"size"`
Type FileType `json:"type"`
ModTime float64 `json:"mod_time"`
}
type FileType
FileType represents the type of a file entry.
type FileType string
const (
FileTypeFile FileType = "file"
FileTypeDirectory FileType = "directory"
)
type Files
Files provides file operations on a sandbox.
type Files struct {
// contains filtered or unexported fields
}
func (*Files) Exists
func (f *Files) Exists(ctx context.Context, path string, opts ...CallOption) (bool, error)
Exists checks if a file or directory exists at the given path in the sandbox.
func (*Files) List
func (f *Files) List(ctx context.Context, path string, opts ...CallOption) ([]FileEntry, error)
List returns the contents of a directory in the sandbox.
func (*Files) Read
func (f *Files) Read(ctx context.Context, path string, opts ...CallOption) ([]byte, error)
Read downloads a file from the sandbox.
func (*Files) Write
func (f *Files) Write(ctx context.Context, path string, content []byte, opts ...CallOption) error
Write uploads content to the sandbox. The path must be a plain filename without directory separators (e.g., “script.py”, not “dir/script.py”).
The entire content is buffered in memory as a multipart form body to support retries on transient failures. Content exceeding MaxUploadSize (default 256 MB) is rejected before any network I/O.
type HTTPError
HTTPError represents a non-OK HTTP response from the sandbox.
type HTTPError struct {
StatusCode int
Body string
Operation string
}
func (*HTTPError) Error
func (e *HTTPError) Error() string
type Handle
Handle provides high-level interaction with a sandbox instance. Sandbox implements this interface; consumers should accept Handle in their APIs to enable testing with mocks. For sub-object access (Commands(), Files()), use the concrete *Sandbox type directly.
type Handle interface {
Open(ctx context.Context) error
Close(ctx context.Context) error
Disconnect(ctx context.Context) error
IsReady() bool
Run(ctx context.Context, command string, opts ...CallOption) (*ExecutionResult, error)
Write(ctx context.Context, path string, content []byte, opts ...CallOption) error
Read(ctx context.Context, path string, opts ...CallOption) ([]byte, error)
List(ctx context.Context, path string, opts ...CallOption) ([]FileEntry, error)
Exists(ctx context.Context, path string, opts ...CallOption) (bool, error)
}
type Info
Info provides read-only access to sandbox identity metadata.
type Info interface {
ClaimName() string
SandboxName() string
PodName() string
PodIP() string
Annotations() map[string]string
}
type K8sHelper
K8sHelper encapsulates all Kubernetes API interactions for sandbox lifecycle management. It can be shared across multiple Sandbox instances.
type K8sHelper struct {
AgentsClient agentsv1beta1.AgentsV1beta1Interface
ExtensionsClient extensionsv1beta1.ExtensionsV1beta1Interface
DynamicClient dynamic.Interface
CoreClient corev1client.CoreV1Interface
DiscoveryClient discoveryv1client.DiscoveryV1Interface
RestConfig *rest.Config
Log logr.Logger
}
func NewK8sHelper
func NewK8sHelper(restConfig *rest.Config, log logr.Logger) (*K8sHelper, error)
NewK8sHelper creates a K8sHelper by loading kubeconfig and constructing all required clientsets. If restConfig is non-nil it is used directly; otherwise in-cluster config is tried first, then ~/.kube/config.
type Key
Key identifies a tracked sandbox in the registry.
type Key struct {
Namespace string
ClaimName string
}
type Options
Options configures a Sandbox instance.
type Options struct {
// WarmPoolName is the name of the SandboxWarmPool to use. Required.
// Must be a valid Kubernetes DNS subdomain (lowercase, [a-z0-9.-]).
WarmPoolName string
// Namespace where the SandboxClaim will be created. Default: "default".
// Must be a valid Kubernetes DNS label (lowercase, [a-z0-9-]).
Namespace string
// GatewayName enables production mode. The client watches this Gateway resource
// for an external IP, then routes through the sandbox-router.
// Must be a valid Kubernetes DNS subdomain (lowercase, [a-z0-9.-]).
GatewayName string
// GatewayNamespace is where the Gateway lives. Default: "default".
// Must be a valid Kubernetes DNS label (lowercase, [a-z0-9-]).
GatewayNamespace string
// GatewayScheme is the URL scheme used when constructing the base URL
// from the Gateway's address. Default: "http".
GatewayScheme string
// APIURL enables advanced mode. The client connects directly to this URL,
// bypassing gateway discovery. Takes precedence over GatewayName.
APIURL string
// ServerPort is the port the sandbox runtime listens on. Default: 8888.
ServerPort int
// SandboxReadyTimeout is how long to wait for the sandbox to become ready. Default: 180s.
SandboxReadyTimeout time.Duration
// GatewayReadyTimeout is how long to wait for the gateway IP. Default: 180s.
GatewayReadyTimeout time.Duration
// PortForwardReadyTimeout is how long to wait for port-forward to be established. Default: 30s.
PortForwardReadyTimeout time.Duration
// CleanupTimeout is how long to wait for claim deletion during both Open
// rollback and Close. Uses a detached context so cleanup succeeds even if
// the caller's context is already cancelled. Default: 30s.
CleanupTimeout time.Duration
// RequestTimeout is the total timeout for a single SDK method call
// (e.g., Run, Read, Write), encompassing all retry attempts and backoff
// sleeps. Applied only when the caller's context has no deadline.
// Default: 180s.
RequestTimeout time.Duration
// PerAttemptTimeout bounds the time to receive response headers per
// HTTP attempt. Stopped on success so body reads use RequestTimeout.
// Default: 60s.
PerAttemptTimeout time.Duration
// MaxDownloadSize is the maximum response body size for Read().
// Run() uses a fixed 16 MB decode limit; List() and Exists() use a
// fixed 8 MB internal limit. Default: 256 MB.
MaxDownloadSize int64
// MaxUploadSize is the maximum content size for Write(). Content
// exceeding this limit is rejected before any network I/O. Default: 256 MB.
MaxUploadSize int64
// Logger for structured logging. Defaults to stderr at INFO level.
// Provide a custom logr.Logger for full control, or set Quiet to
// suppress output.
Logger logr.Logger
// Quiet suppresses the default stderr logger. Has no effect when a
// custom Logger is provided (non-zero-value).
Quiet bool
// K8sHelper provides pre-constructed Kubernetes clients. If nil, a new
// K8sHelper is created from RestConfig. Use this to share clients
// across multiple Sandbox instances.
K8sHelper *K8sHelper
// RestConfig overrides the Kubernetes REST config. If nil, the client first
// tries in-cluster config (for pods), then falls back to the default
// kubeconfig (~/.kube/config or KUBECONFIG env). Ignored when K8sHelper is set.
RestConfig *rest.Config
// HTTPTransport overrides the HTTP transport for sandbox operations.
// If nil, a default transport with sensible timeouts is created.
// Use this for custom TLS, proxies, or other transport-level settings.
HTTPTransport http.RoundTripper
// TraceServiceName is the OpenTelemetry service name used for the tracer's
// instrumentation scope and the resource's service.name attribute.
// Default: "sandbox-client".
TraceServiceName string
// TracerProvider sets the OpenTelemetry TracerProvider for span creation.
// If nil, falls back to otel.GetTracerProvider (noop by default).
TracerProvider trace.TracerProvider
}
type Sandbox
Sandbox manages the lifecycle of a single agent-sandbox instance. Operations are split across Commands and Files.
type Sandbox struct {
// contains filtered or unexported fields
}
func New
func New(_ context.Context, opts Options) (*Sandbox, error)
New creates a new Sandbox with the given options. Call Open() to create a sandbox and establish connectivity.
func (*Sandbox) Annotations
func (s *Sandbox) Annotations() map[string]string
func (*Sandbox) ClaimName
func (s *Sandbox) ClaimName() string
func (*Sandbox) Close
func (s *Sandbox) Close(ctx context.Context) error
Close deletes the SandboxClaim and cleans up resources.
func (*Sandbox) Commands
func (s *Sandbox) Commands() *Commands
Commands returns the command execution sub-object.
func (*Sandbox) Disconnect
func (s *Sandbox) Disconnect(ctx context.Context) error
Disconnect closes the transport connection without deleting the SandboxClaim. The sandbox stays alive on the server. Call Open() to reconnect. Disconnect is safe to call concurrently with Open; an in-progress Open is cancelled before the transport is torn down.
func (*Sandbox) Exists
func (s *Sandbox) Exists(ctx context.Context, path string, opts ...CallOption) (bool, error)
func (*Sandbox) Files
func (s *Sandbox) Files() *Files
Files returns the file operations sub-object.
func (*Sandbox) IsReady
func (s *Sandbox) IsReady() bool
IsReady returns true if the sandbox is ready for communication.
func (*Sandbox) List
func (s *Sandbox) List(ctx context.Context, path string, opts ...CallOption) ([]FileEntry, error)
func (*Sandbox) Open
func (s *Sandbox) Open(ctx context.Context) (retErr error)
Open creates a SandboxClaim and waits for the sandbox to become ready, then discovers the API URL based on the configured connection mode. On failure after claim creation, the claim is automatically deleted; if deletion also fails, call Close() to retry.
func (*Sandbox) PodIP
func (s *Sandbox) PodIP() string
func (*Sandbox) PodName
func (s *Sandbox) PodName() string
func (*Sandbox) Read
func (s *Sandbox) Read(ctx context.Context, path string, opts ...CallOption) ([]byte, error)
func (*Sandbox) Run
func (s *Sandbox) Run(ctx context.Context, command string, opts ...CallOption) (*ExecutionResult, error)
func (*Sandbox) SandboxName
func (s *Sandbox) SandboxName() string
func (*Sandbox) Write
func (s *Sandbox) Write(ctx context.Context, path string, content []byte, opts ...CallOption) error
Generated by gomarkdoc