[Feature]: KubernetesFilesystemSpec: add environment variable injection support (parity with DockerFilesystemSpec)
Describe the bug
KubernetesFilesystemSpec and KubernetesSandboxClientOptions provide no way to inject environment variables into the sandbox Pod. The DockerFilesystemSpec exposes an environment(Map<String, String>) method, but the Kubernetes backend has no equivalent. This means per-request secrets or configuration values (e.g. an API key that varies per user) cannot be passed into the container at startup time through the framework API.
Root cause summary:
-
KubernetesSandboxClientOptionshas noenvironmentfield — The options class does not declare anenvironment/envVarsmap, so there is no way to carry per-call environment variables from the spec to the Pod spec builder.Affected location:
agentscope-extensions-sandbox-kubernetes/src/main/java/io/agentscope/extensions/sandbox/kubernetes/KubernetesSandboxClientOptions.java
-
Fabric8KubernetesPodRuntime.createPod()never callsContainerBuilder.withEnv()— TheContainerBuilderinstance is constructed without anyEnvVarentries. Even ifKubernetesSandboxClientOptionswere extended to carry environment variables, the Pod creation code would not use them.Affected location:
agentscope-extensions-sandbox-kubernetes/src/main/java/io/agentscope/extensions/sandbox/kubernetes/Fabric8KubernetesPodRuntime.java—createPod()method,ContainerBuilderblock (approximately line 205–211)
To Reproduce
- Configure
HarnessAgentwith aKubernetesFilesystemSpec. - Attempt to pass a per-user environment variable (e.g.
KST_API_KEY) into the sandbox Pod so that a CLI tool installed in the container image can read it at runtime. - Observe that
KubernetesFilesystemSpecexposes noenvironment(Map)method, unlikeDockerFilesystemSpec.
Expected behavior
KubernetesFilesystemSpec should expose an environment(Map<String, String>) method (mirroring DockerFilesystemSpec) that causes the specified key-value pairs to appear as EnvVar entries on the container spec when the Pod is created.
The KubernetesSandboxClientOptions.merge() method already handles per-call option overrides; environment variables should participate in the same merge, with call-level values taking precedence over spec-level defaults.
Proposed fix
- Add an
environmentfield toKubernetesSandboxClientOptions:
private Map<String, String> environment = new HashMap<>();
public Map<String, String> getEnvironment() { return environment; }
public void setEnvironment(Map<String, String> environment) {
this.environment = environment != null ? new HashMap<>(environment) : new HashMap<>();
}
- Add a fluent setter on
KubernetesFilesystemSpec(mirroringDockerFilesystemSpec):
public KubernetesFilesystemSpec environment(Map<String, String> environment) {
options.setEnvironment(environment);
return this;
}
- Include
environmentinKubernetesSandboxClient.merge()andcopy():
// in copy()
o.setEnvironment(src.getEnvironment());
// in merge()
if (callOptions.getEnvironment() != null && !callOptions.getEnvironment().isEmpty()) {
Map<String, String> merged = new HashMap<>(base.getEnvironment());
merged.putAll(callOptions.getEnvironment());
o.setEnvironment(merged);
}
- Apply the environment map in
Fabric8KubernetesPodRuntime.createPod():
if (templateOptions.getEnvironment() != null && !templateOptions.getEnvironment().isEmpty()) {
List<io.fabric8.kubernetes.api.model.EnvVar> envVars = templateOptions.getEnvironment()
.entrySet().stream()
.map(e -> new io.fabric8.kubernetes.api.model.EnvVarBuilder()
.withName(e.getKey())
.withValue(e.getValue())
.build())
.toList();
cb.withEnv(envVars);
}
Workaround (until fixed)
Pass the value through the workspace/ projection as a file, then read it inside the container at runtime:
# Agent reads the key from the projected workspace file
kst-im-cli auth login -k "$(cat /workspace/.api-key)"
This is functional but exposes the secret in the workspace snapshot (persisted to JdbcSnapshotSpec / agent_sandbox_snapshots), which is undesirable for secrets.
Environment
- AgentScope-Java Version: 2.0.0-RC2
- Java Version: 21
- OS: Linux (Kubernetes)