Skip to content

Commit 1c1f5cd

Browse files
committed
feat(harness): mount tool images read-only into the runtime
Harness.spec.workload.toolImages lists digest-pinned OCI images. The controller adds one Substrate image volume per entry to the ActorTemplate, mounted read-only at /opt/kagent/tools/<name> for every runtime, and sets KAGENT_TOOL_PATHS to the colon-joined bin directories. Each managed runtime appends them to its own PATH after the image's binaries; the variable is reserved in Harness.spec.env. The Harness edit form keeps the field it does not author. Closes #3190 Signed-off-by: QuentinBisson <quentin@giantswarm.io>
1 parent 12aa4d4 commit 1c1f5cd

27 files changed

Lines changed: 703 additions & 19 deletions

File tree

‎docs/architecture/configuration-and-compilation.md‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
`Harness` describes how to run a class of agents. It selects exactly one runtime
66
variant—kagent, Codex, Claude, or BYO—and contains workload image/command/args,
7-
literal environment values, WorkerPool configuration, and snapshot location.
8-
Harness has no status subresource; compilation and runtime readiness are reported
9-
on each Agent that selects it.
7+
tool images, literal environment values, WorkerPool configuration, and snapshot
8+
location. Harness has no status subresource; compilation and runtime readiness
9+
are reported on each Agent that selects it.
1010

1111
`AgentTemplate` describes what the agent does. It contains model configuration,
1212
description and prompt, MCP tool bindings, skills, plugins, and Shared
@@ -130,6 +130,58 @@ spec:
130130
Command and argument changes participate in revision identity. They affect newly
131131
prepared revisions, not existing Sessions pinned to an older revision.
132132

133+
### Tool images
134+
135+
`spec.workload.toolImages` lists digest-pinned OCI images whose contents the
136+
controller mounts read-only next to the runtime image, for every runtime
137+
variant. Each entry is one Substrate image volume mounted at
138+
`/opt/kagent/tools/<name>`:
139+
140+
```yaml
141+
spec:
142+
claude: {}
143+
workload:
144+
image: registry.example.com/claude-harness@sha256:...
145+
toolImages:
146+
- name: go
147+
oci: registry.example.com/tools/go@sha256:...
148+
- name: kubectl
149+
oci: registry.example.com/tools/kubectl@sha256:...
150+
```
151+
152+
The controller sets `KAGENT_TOOL_PATHS` to the colon-joined
153+
`/opt/kagent/tools/<name>/bin` directories in list order. It never sets `PATH`,
154+
because a template `PATH` would replace the image's. Each managed runtime
155+
appends the listed directories to its own `PATH` at start, so the image's
156+
binaries always win: the Claude and Codex harnesses extend the environment of
157+
the CLI they spawn, the Go ADK extends its process environment before the
158+
`bash` tool and stdio MCP servers inherit it, and the Python ADK extends
159+
`os.environ` when `kagent-adk` starts (its `bash` tool still puts the venv
160+
first). `KAGENT_TOOL_PATHS` is reserved; a Harness `spec.env` entry of that
161+
name is rejected.
162+
163+
An image may ship any number of binaries in its `bin/`. `PATH` resolves
164+
conflicts: a binary the runtime image already ships is never shadowed by a
165+
tool image, and when two tool images ship the same binary the earlier entry
166+
wins. Nothing reports a shadowed binary.
167+
168+
Tool images follow the same rules as every other image in the API: the
169+
reference carries a sha256 digest and the controller resolves no tags. Keeping
170+
a digest current is the job of whatever writes the Harness. A change prepares a
171+
new revision; running Sessions keep theirs. Each entry costs one of Substrate's
172+
32 volume and 32 mount slots, and a non-empty list costs one of its 32
173+
environment entries. Substrate pulls tool images with the node's image
174+
credential provider, the same path as the runtime image.
175+
176+
A tool image is a plain filesystem: it ships its files under one prefix with
177+
the executables in `bin/`, its entrypoint is never run, and the mount is
178+
read-only. Binaries must be static or self-contained for the runtime image
179+
they are mounted into: the built-in Claude, Codex, and Go ADK images are
180+
Alpine (musl), the Python ADK image is Debian (glibc).
181+
182+
Tool images reach the runtime container only. A sandbox guest runs in its own
183+
tools image and does not see them.
184+
133185
## Prepared revision pipeline
134186

135187
The controller compiles each Agent through one pipeline:
@@ -330,7 +382,9 @@ load these skills; the Go ADK BYO fixture does so.
330382
provide this integration. Other images must implement the same TaskStore
331383
contract; an upstream A2A server with only an in-memory store is insufficient.
332384
Optional model, prompt, tool, skill, and plugin configuration is
333-
supplied in the ADK-shaped format when requested.
385+
supplied in the ADK-shaped format when requested. Tool images are mounted
386+
under `/opt/kagent/tools` and listed in `KAGENT_TOOL_PATHS`; a BYO image
387+
extends its own `PATH` from that variable.
334388

335389
The kagent and BYO compilers serialize the same `ADKConfig` protobuf
336390
contract from `adk.proto` using ProtoJSON. Go and Python use generated messages;

‎docs/architecture/process-configuration.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ an explicit generated `RuntimeConfig`. Python ADK loads the full document with
250250
`parse_config()` and constructs the SDK agent with `create_agent()`. Managed processes still materialize the private
251251
`KAGENT_CONFIG_JSON` and `KAGENT_AGENT_CARD_JSON` payloads supplied by Substrate.
252252
Actor/session identity, credential projection, and private bootstrap paths keep
253-
their existing contracts. Substrate's limits remain 32 environment entries and
253+
their existing contracts. `KAGENT_TOOL_PATHS` is present only when the Harness
254+
lists tool images. Substrate's limits remain 32 environment entries and
254255
32,768 characters per value.
255256

256257
## OpenTelemetry

‎docs/architecture/sandboxes.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ separate and retains its own snapshot and execution-claim rules.
210210
The guest uses `agent-substrate/env/guest` for process and filesystem services.
211211
Kagent owns its main function, flags, logging, listener, readiness, and shutdown.
212212
The tools image supplies installed programs; an image volume supplies the managed
213-
guest binary and replaces the tools image's entrypoint.
213+
guest binary and replaces the tools image's entrypoint. Harness tool images
214+
(`spec.workload.toolImages`) use the same image-volume mechanism.
214215

215216
The Actor mounts a durable directory at `/data`. The guest creates
216217
`/data/workspace` before readiness and stores process output in `/data/guest-logs`.

‎go/adk/cmd/main.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/kagent-dev/kagent/go/adk/pkg/telemetry"
2424
processconfig "github.com/kagent-dev/kagent/go/core/pkg/config"
2525
"github.com/kagent-dev/kagent/go/pkg/logging"
26+
"github.com/kagent-dev/kagent/go/pkg/toolpath"
2627
"github.com/kagent-dev/kagent/go/pkg/tracing"
2728
)
2829

@@ -45,6 +46,11 @@ func main() {
4546
}
4647

4748
func run(logLevel, host, port, configDir string) error {
49+
// The bash tool and stdio MCP servers inherit the process environment, so
50+
// tool image binaries are exposed here, after the image's own PATH.
51+
if err := toolpath.ExtendProcess(); err != nil {
52+
return fmt.Errorf("expose tool image binaries on PATH: %w", err)
53+
}
4854
if err := config.MaterializeFromEnv(configDir); err != nil {
4955
return fmt.Errorf("materialize agent config in %s: %w", configDir, err)
5056
}

‎go/api/v1alpha3/configuration_crd_cel_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,43 @@ func TestConfigurationCRDValidation(t *testing.T) {
260260
}),
261261
wantReject: "spec.workload.image",
262262
},
263+
{
264+
name: "Harness rejects tag-only tool image",
265+
object: validHarness(namespace, "harness-tagged-tool-image", HarnessSpec{
266+
Kagent: &KagentHarness{},
267+
Workload: HarnessWorkload{ToolImages: []HarnessToolImage{{Name: "kubectl", OCI: "registry.example.com/kubectl:latest"}}},
268+
}),
269+
wantReject: "spec.workload.toolImages[0].oci",
270+
},
271+
{
272+
name: "Harness rejects tool image name that is not a DNS label",
273+
object: validHarness(namespace, "harness-tool-image-name", HarnessSpec{
274+
Kagent: &KagentHarness{},
275+
Workload: HarnessWorkload{ToolImages: []HarnessToolImage{{Name: "Kube_ctl", OCI: "registry.example.com/kubectl@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}}},
276+
}),
277+
wantReject: "spec.workload.toolImages[0].name",
278+
},
279+
{
280+
name: "Harness rejects duplicate tool image names",
281+
object: validHarness(namespace, "harness-duplicate-tool-image", HarnessSpec{
282+
Kagent: &KagentHarness{},
283+
Workload: HarnessWorkload{ToolImages: []HarnessToolImage{
284+
{Name: "kubectl", OCI: "registry.example.com/kubectl@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"},
285+
{Name: "kubectl", OCI: "registry.example.com/kubectl@sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"},
286+
}},
287+
}),
288+
wantReject: "Duplicate value",
289+
},
290+
{
291+
name: "Harness accepts digest-pinned tool images",
292+
object: validHarness(namespace, "harness-tool-images", HarnessSpec{
293+
Kagent: &KagentHarness{},
294+
Workload: HarnessWorkload{ToolImages: []HarnessToolImage{
295+
{Name: "kubectl", OCI: "registry.example.com/kubectl@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"},
296+
{Name: "helm", OCI: "registry.example.com/helm@sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"},
297+
}},
298+
}),
299+
},
263300
{
264301
name: "Harness memory requires a model reference",
265302
object: validHarness(namespace, "harness-empty-memory-model", HarnessSpec{

‎go/api/v1alpha3/harness_types.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@ type HarnessWorkload struct {
126126
// +kubebuilder:validation:MaxItems=64
127127
// +optional
128128
Args []string `json:"args,omitempty"`
129+
130+
// ToolImages lists digest-pinned OCI images the controller mounts read-only
131+
// at /opt/kagent/tools/<name> in every runtime. KAGENT_TOOL_PATHS carries
132+
// the colon-joined /opt/kagent/tools/<name>/bin directories in list order;
133+
// each runtime appends them to PATH after the image's own binaries.
134+
// +kubebuilder:validation:MaxItems=16
135+
// +listType=map
136+
// +listMapKey=name
137+
// +optional
138+
ToolImages []HarnessToolImage `json:"toolImages,omitempty"`
139+
}
140+
141+
// HarnessToolImage is one read-only image volume added to the runtime container.
142+
type HarnessToolImage struct {
143+
// Name selects the mount directory /opt/kagent/tools/<name>. It must be a
144+
// DNS label of at most 58 characters: the controller prefixes it with
145+
// "tool-" to form the volume name, which is itself limited to 63.
146+
// +kubebuilder:validation:MinLength=1
147+
// +kubebuilder:validation:MaxLength=58
148+
// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`
149+
// +required
150+
Name string `json:"name"`
151+
152+
// OCI is the image reference pinned by sha256 digest. Tags are not resolved.
153+
// +kubebuilder:validation:MaxLength=512
154+
// +kubebuilder:validation:Pattern=`^[^[:space:]@]+@sha256:[a-f0-9]{64}$`
155+
// +required
156+
OCI string `json:"oci"`
129157
}
130158

131159
// HarnessSpec defines a reusable runtime and its infrastructure policy.

‎go/api/v1alpha3/zz_generated.deepcopy.go‎

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go/core/internal/substrate/actor_template.go‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ import (
1616
corev1 "k8s.io/api/core/v1"
1717
"k8s.io/apimachinery/pkg/api/resource"
1818
"k8s.io/apimachinery/pkg/types"
19+
"k8s.io/apimachinery/pkg/util/validation"
1920

2021
apia2a "github.com/kagent-dev/kagent/go/api/a2a"
22+
"github.com/kagent-dev/kagent/go/core/internal/image"
2123
"github.com/kagent-dev/kagent/go/core/internal/translator"
2224
"github.com/kagent-dev/kagent/go/core/pkg/env"
25+
"github.com/kagent-dev/kagent/go/pkg/toolpath"
2326
"github.com/kagent-dev/kagent/go/pkg/tracing"
2427
)
2528

@@ -39,6 +42,10 @@ const egressTrustMount = "/run/kagent/egress"
3942
const actorIdentityVolume = "actor-identity"
4043
const actorIdentityMount = "/run/kagent/identity"
4144

45+
// Tool image volumes carry a prefix so a tool named like a built-in volume
46+
// cannot collide with it.
47+
const toolImageVolumePrefix = "tool-"
48+
4249
var egressTrustEnvironment = map[string]struct{}{
4350
"SSL_CERT_FILE": {}, "SSL_CERT_DIR": {}, "REQUESTS_CA_BUNDLE": {}, "AWS_CA_BUNDLE": {},
4451
"NODE_EXTRA_CA_CERTS": {}, "CURL_CA_BUNDLE": {}, "GIT_SSL_CAINFO": {},
@@ -71,11 +78,21 @@ func ActorTemplateForRevision(spec *translator.Revision, revisionID translator.R
7178
if _, owned := egressTrustEnvironment[variable.Name]; owned {
7279
return nil, fmt.Errorf("runtime environment %q conflicts with gateway trust", variable.Name)
7380
}
81+
if variable.Name == toolpath.EnvName {
82+
return nil, fmt.Errorf("runtime environment %q is reserved for tool images", variable.Name)
83+
}
7484
}
7585
for _, name := range []string{"SSL_CERT_FILE", "REQUESTS_CA_BUNDLE", "AWS_CA_BUNDLE", "NODE_EXTRA_CA_CERTS", "CURL_CA_BUNDLE", "GIT_SSL_CAINFO"} {
7686
environment = append(environment, corev1.EnvVar{Name: name, Value: egressTrustMount + "/trust-bundle.pem"})
7787
}
7888
environment = append(environment, corev1.EnvVar{Name: "SSL_CERT_DIR", Value: egressTrustMount})
89+
toolVolumes, toolMounts, toolNames, err := toolImageVolumes(spec.ToolImages)
90+
if err != nil {
91+
return nil, err
92+
}
93+
if len(toolNames) > 0 {
94+
environment = append(environment, corev1.EnvVar{Name: toolpath.EnvName, Value: toolpath.JoinBinDirs(toolNames)})
95+
}
7996
environment = append(environment,
8097
corev1.EnvVar{Name: "KAGENT_CONFIG_JSON", Value: string(spec.ConfigJSON)},
8198
corev1.EnvVar{Name: "KAGENT_AGENT_CARD_JSON", Value: string(cardJSON)},
@@ -105,11 +122,11 @@ func ActorTemplateForRevision(spec *translator.Revision, revisionID translator.R
105122
Path: "/readyz",
106123
Port: 8081,
107124
}, TimeoutSeconds: 30},
108-
VolumeMounts: []*ateapipb.VolumeMount{
125+
VolumeMounts: append([]*ateapipb.VolumeMount{
109126
{Name: durableDataVolume, MountPath: durableDataMount},
110127
{Name: egressTrustVolume, MountPath: egressTrustMount},
111128
{Name: actorIdentityVolume, MountPath: actorIdentityMount},
112-
},
129+
}, toolMounts...),
113130
}},
114131
WorkerSelector: workerSelectorForPool(workerKey),
115132
SnapshotConfig: &ateapipb.SnapshotConfig{
@@ -118,7 +135,7 @@ func ActorTemplateForRevision(spec *translator.Revision, revisionID translator.R
118135
// Pin the server default so immutable comparison survives creation.
119136
GoldenSnapshotConfig: &ateapipb.GoldenSnapshotConfig{Mode: ateapipb.GoldenSnapshotMode_GOLDEN_SNAPSHOT_MODE_ENABLED},
120137
},
121-
Volumes: []*ateapipb.Volume{
138+
Volumes: append([]*ateapipb.Volume{
122139
{Name: durableDataVolume, DurableDir: &ateapipb.DurableDirVolumeSource{}},
123140
// Substrate regenerates this projection on Run and Restore. A fork
124141
// therefore routes storage calls as its own actor, never its source.
@@ -130,7 +147,7 @@ func ActorTemplateForRevision(spec *translator.Revision, revisionID translator.R
130147
{Name: egressTrustVolume, SystemInfo: &ateapipb.SystemInfoVolumeSource{DataSources: []*ateapipb.SystemInfoDataSource{
131148
{TrustBundle: &ateapipb.TrustBundleDataSource{Names: []string{"egress-mitm.ate.dev"}, Path: "trust-bundle.pem"}},
132149
}}},
133-
},
150+
}, toolVolumes...),
134151
}
135152
for i, image := range spec.SkillImages {
136153
name := fmt.Sprintf("skill-%d", i)
@@ -147,6 +164,33 @@ func ActorTemplateForRevision(spec *translator.Revision, revisionID translator.R
147164
return template, nil
148165
}
149166

167+
// toolImageVolumes projects each tool image as a read-only image volume mounted
168+
// under toolpath.MountRoot. The CRD validates the entries; the check here keeps
169+
// a hand-built revision on the same contract Substrate enforces.
170+
func toolImageVolumes(images []translator.ToolImage) ([]*ateapipb.Volume, []*ateapipb.VolumeMount, []string, error) {
171+
var volumes []*ateapipb.Volume
172+
var mounts []*ateapipb.VolumeMount
173+
var names []string
174+
seen := make(map[string]struct{}, len(images))
175+
for _, tool := range images {
176+
if errs := validation.IsDNS1123Label(tool.Name); len(errs) > 0 {
177+
return nil, nil, nil, fmt.Errorf("tool image name %q: %s", tool.Name, strings.Join(errs, "; "))
178+
}
179+
if _, duplicate := seen[tool.Name]; duplicate {
180+
return nil, nil, nil, fmt.Errorf("tool image name %q is listed twice", tool.Name)
181+
}
182+
seen[tool.Name] = struct{}{}
183+
if !image.IsDigestPinned(tool.Reference) {
184+
return nil, nil, nil, fmt.Errorf("tool image %q must be pinned by sha256 digest", tool.Name)
185+
}
186+
volumeName := toolImageVolumePrefix + tool.Name
187+
volumes = append(volumes, &ateapipb.Volume{Name: volumeName, Image: &ateapipb.ImageVolumeSource{Reference: tool.Reference}})
188+
mounts = append(mounts, &ateapipb.VolumeMount{Name: volumeName, MountPath: toolpath.MountPath(tool.Name)})
189+
names = append(names, tool.Name)
190+
}
191+
return volumes, mounts, names, nil
192+
}
193+
150194
// ActorTemplateSpecEqual compares the client-owned immutable fields of two
151195
// templates, excluding server-owned metadata and golden-snapshot status.
152196
func ActorTemplateSpecEqual(left, right *ateapipb.ActorTemplate) bool {

0 commit comments

Comments
 (0)