The plugin enables a GoCD Kubernetes Elastic Agent to execute workloads inside a guest container within the same Pod. It relies on nsenter and leverages namespace sharing between containers, allowing the agent setup to remain agnostic and enabling the use of any base image without specific setup requirements.
The plugin operates as a bridge between GoCD agents and containerized workloads:
- GoCD task configuration defines script content and shell type
- Script is written to a shared filesystem accessible by both host and container
nsentercommand is used to enter container namespaces and execute the script- Output streams are captured and forwarded to GoCD console logger
- Temporary script files are cleaned up post-execution
- Shell Support: bash, zsh, sh
- Namespace Entry: Uses
nsenterwith--target,--all,--wdnsflags - Environment Propagation: All GoCD environment variables are exported to the container context
- Working Directory: Maintains correct working directory through
--wdnsparameter
The nsenter utility must be available in the agent container’s filesystem and accessible in the $PATH.
Before executing tasks, the agent must ensure that the target (guest) container is in a ready state and that all required environment variables are correctly configured.
The following environment variables must be set by the GoCD Kubernetes Elastic Agent to correctly interact with the guest container:
| Variable | Description |
|---|---|
GO_K8S_CONTAINER_SHARED_ROOT |
Shared filesystem path accessible by both host and container |
GO_K8S_CONTAINER_PID |
Process ID of the target container's main process |
GO_K8S_CONTAINER_ROOT |
Root directory path within the container |
The use of nsenter requires additional capabilities. The Pod must enable shareProcessNamespace: true, and the agent container must run with the SYS_ADMIN capability to allow namespace access.
apiVersion: v1
kind: Pod
metadata:
name: gocd-build-pod
spec:
shareProcessNamespace: true
volumes:
- name: shared-workspace
emptyDir: {}
containers:
- name: gocd-agent
image: custom/gocd-k8s-elastic-agent
securityContext:
allowPrivilegeEscalation: false
capabilities:
add:
- SYS_ADMIN
volumeMounts:
- mountPath: /go
name: shared-workspace
- name: build-environment
image: node:16.14.0 # Any base image from Docker Hub
volumeMounts:
- mountPath: /go
name: shared-workspace
command: ["/go/pause"]-
Build the plugin:
./gradlew clean build
-
Locate the generated JAR file in
build/libs/ -
Deploy to GoCD server:
- Copy JAR to
<gocd-server>/plugins/external/ - Restart GoCD server
- Copy JAR to
In GoCD pipeline configuration, add a task of type "k8s Executor":
- Shell: Select shell interpreter (bash, zsh, or sh)
- Script: Multi-line script content to execute
tasks:
- plugin:
configuration:
id: k8s-executor
version: 1.0.0
options:
shtype: sh
script: |
set -e
echo "[INFO] Starting Node.js build pipeline..."
# Install dependencies
echo "[STEP] Installing dependencies..."
npm ci
# Run build
echo "[STEP] Building project..."
npm run build
# Run tests
echo "[STEP] Running tests..."
npm test
echo "[SUCCESS] Build and tests completed successfully."nsenter \
--target <GO_K8S_CONTAINER_PID> \
--all \
--wdns=<working_directory> \
-e \
-- \
<shell_type> \
-c \
"<environment_variables> <shell_type> <script_path>"- Exit code 0: Task success
- Non-zero exit code: Task failure with exit code logged
- Exception during execution: Task failure with error message
- Script cleanup: Always attempted, failures silently ignored
- Scripts are created with user-executable permissions only (
chmod u+x) - Temporary script files use UUID-based naming to prevent collisions
- Environment variables are properly escaped during export
- No Windows support reduces attack surface for that platform
- Plugin ID:
k8s-executor - Version: 1.0.0
- Minimum GoCD Version: 20.1.0
- Java Version: 11
- Dependencies:
- GoCD Plugin API: 24.1.0
- Gson: 2.11.0
- Linux-only operation
- Requires
nsenteravailability - Depends on specific environment variables set by Kubernetes infrastructure
- No native Windows container support
- Shared filesystem requirement between host and container
./gradlew testexport PRERELEASE=No
./gradlew clean buildScript not found: Verify GO_K8S_CONTAINER_SHARED_ROOT is correctly mounted in both host and container
Permission denied: Ensure GoCD agent has permissions to execute nsenter (typically requires elevated privileges)
nsenter command not found: Install util-linux package containing nsenter
Environment variables not propagated: Check that variable names do not contain special characters that break shell escaping