agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Feature]: KubernetesFilesystemSpec: add environment variable injection support (parity with DockerFilesystemSpec)

Open Buktal opened this issue 5 months ago • 0 comments

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:

  1. KubernetesSandboxClientOptions has no environment field — The options class does not declare an environment / envVars map, 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
  2. Fabric8KubernetesPodRuntime.createPod() never calls ContainerBuilder.withEnv() — The ContainerBuilder instance is constructed without any EnvVar entries. Even if KubernetesSandboxClientOptions were 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, ContainerBuilder block (approximately line 205–211)

To Reproduce

  1. Configure HarnessAgent with a KubernetesFilesystemSpec.
  2. 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.
  3. Observe that KubernetesFilesystemSpec exposes no environment(Map) method, unlike DockerFilesystemSpec.

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

  1. Add an environment field to KubernetesSandboxClientOptions:
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<>();
}
  1. Add a fluent setter on KubernetesFilesystemSpec (mirroring DockerFilesystemSpec):
public KubernetesFilesystemSpec environment(Map<String, String> environment) {
    options.setEnvironment(environment);
    return this;
}
  1. Include environment in KubernetesSandboxClient.merge() and copy():
// 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);
}
  1. 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)

Buktal avatar Jun 11 '26 11:06 Buktal