Runner fails to run when namespace does not allow privileged access
Summary
I installed Gitlab runner via FluxCD along with Gitlab AgentK and all seems to go well until I try to run any pipelines. Even with pod security enabled with values it keeps failing when the namespace does not allow privileged containers.
Steps to reproduce
Create a new namespace, helm repo and helm release in flux with the following config:
apiVersion: v1
kind: Namespace
metadata:
labels:
app.kubernetes.io/instance: gitlab
app.kubernetes.io/part-of: gitlab
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/warn-version: latest
name: gitlab
---
apiVersion: source.toolkit.fluxcd.io/v1
kind: HelmRepository
metadata:
labels:
app.kubernetes.io/component: runner
app.kubernetes.io/created-by: gitlab
app.kubernetes.io/name: runner
app.kubernetes.io/part-of: gitlab
name: gitlab-runner
namespace: gitlab
spec:
interval: 1h0m0s
url: https://charts.gitlab.io
---
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
name: gitlab-runner
namespace: gitlab
spec:
chart:
spec:
chart: gitlab-runner
sourceRef:
kind: HelmRepository
name: gitlab-runner
namespace: gitlab
interval: 1h0m0s
values:
securityContext: {"capabilities": { "drop": [ "ALL" ] }, "runAsNonRoot": true, "allowPrivilegeEscalation": false, "seccompProfile": { "type": "RuntimeDefault"}}
gitlabUrl: "https://gitlab.eajglobal.dev/"
runners:
secret: gitlab-runner-secret
rbac:
create: true
podSecurityPolicy:
enabled: true.gitlab-ci.yml
Add the job definition that is failing hereActual behavior
The runner error's out with the following error:
Running with gitlab-runner 17.6.0 (374d34fd)
on gitlab-runner-84c8594ddc-mm2qr t1_cgbEiY, system ID: r_PshZG1CGpxGb
Preparing the "kubernetes" executor 00:00
Using Kubernetes namespace: gitlab
Using Kubernetes executor with image node:lts ...
Using attach strategy to execute scripts...
Preparing environment 00:00
Using FF_USE_POD_ACTIVE_DEADLINE_SECONDS, the Pod activeDeadlineSeconds will be set to the job timeout: 1h0m0s...
ERROR: Error cleaning up pod: resource name may not be empty
ERROR: Job failed (system failure): prepare environment: setting up build pod: pods "runner-t1cgbeiy-project-3-concurrent-0-0ewh96kc" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (containers "init-permissions", "build", "helper" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (containers "init-permissions", "build", "helper" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or containers "init-permissions", "build", "helper" must set securityContext.runAsNonRoot=true), seccompProfile (pod or containers "init-permissions", "build", "helper" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost"). Check https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading for more informationExpected behavior
I expect the pipeline to run and complete or fail based on the gitlab-ci.yml script instead of fail for security reasons. Successful log when the name space is changed to allow privileged containers:
[0KRunning with gitlab-runner 17.6.0 (374d34fd)[0;m
[0K on gitlab-runner-84c8594ddc-mm2qr t1_cgbEiY, system ID: r_PshZG1CGpxGb[0;m
section_start:1733591020:prepare_executor
[0K[0K[36;1mPreparing the "kubernetes" executor[0;m[0;m
[0KUsing Kubernetes namespace: gitlab[0;m
[0KUsing Kubernetes executor with image node:lts ...[0;m
[0KUsing attach strategy to execute scripts...[0;m
section_end:1733591020:prepare_executor
[0Ksection_start:1733591020:prepare_script
[0K[0K[36;1mPreparing environment[0;m[0;m
[0KUsing FF_USE_POD_ACTIVE_DEADLINE_SECONDS, the Pod activeDeadlineSeconds will be set to the job timeout: 1h0m0s...[0;m
Waiting for pod gitlab/runner-t1cgbeiy-project-3-concurrent-0-5roxz71f to be running, status is Pending
Waiting for pod gitlab/runner-t1cgbeiy-project-3-concurrent-0-5roxz71f to be running, status is Pending
ContainersNotReady: "containers with unready status: [build helper]"
ContainersNotReady: "containers with unready status: [build helper]"
Running on runner-t1cgbeiy-project-3-concurrent-0-5roxz71f via gitlab-runner-84c8594ddc-mm2qr...
section_end:1733591026:prepare_script
[0Ksection_start:1733591026:get_sources
[0K[0K[36;1mGetting source from Git repository[0;m[0;m
[32;1mFetching changes with git depth set to 20...[0;m
Initialized empty Git repository in /builds/api-gateways/internal-gateway/.git/
[32;1mCreated fresh repository.[0;m
[32;1mChecking out a90a283f as detached HEAD (ref is main)...[0;m
[32;1mSkipping Git submodules setup[0;m
section_end:1733591027:get_sources
[0Ksection_start:1733591027:step_script
[0K[0K[36;1mExecuting "step_script" stage of the job script[0;m[0;m
[32;1m$ npx -y -p @semantic-release/commit-analyzer@9.0.2 -p @semantic-release/git@10.0.1 -p @semantic-release/gitlab@9.5.0 -p @semantic-release/release-notes-generator@10.0.3 -p @semantic-release/exec@6.0.3 -p @semantic-release/changelog@6.0.2 -p conventional-changelog-conventionalcommits@5.0.0 -p semantic-release@19.0.5 semantic-release[0;m
npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
npm warn deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported
npm warn deprecated q@1.5.1: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other.
npm warn deprecated
npm warn deprecated (For a CapTP with native promises, see @endo/eventual-send and @endo/captp)
[5:04:16 PM] [semantic-release] › ℹ Running semantic-release version 19.0.5
[5:04:16 PM] [semantic-release] › ✔ Loaded plugin "verifyConditions" from "@semantic-release/gitlab"
[5:04:16 PM] [semantic-release] › ✔ Loaded plugin "analyzeCommits" from "@semantic-release/commit-analyzer"
[5:04:16 PM] [semantic-release] › ✔ Loaded plugin "generateNotes" from "@semantic-release/release-notes-generator"
[5:04:16 PM] [semantic-release] › ✔ Loaded plugin "publish" from "@semantic-release/gitlab"
[5:04:16 PM] [semantic-release] › ✔ Loaded plugin "success" from "@semantic-release/gitlab"
[5:04:16 PM] [semantic-release] › ✔ Loaded plugin "fail" from "@semantic-release/gitlab"
[5:04:18 PM] [semantic-release] › ✔ Run automated release from branch main on repository https://gitlab-ci-token:[secure]@gitlab.eajglobal.dev/api-gateways/internal-gateway.git
[5:04:18 PM] [semantic-release] › ✔ Allowed to push to the Git repository
[5:04:18 PM] [semantic-release] › ℹ Start step "verifyConditions" of plugin "@semantic-release/gitlab"
[5:04:18 PM] [semantic-release] [@semantic-release/gitlab] › ℹ Verify GitLab authentication (https://gitlab.eajglobal.dev/api/v4)
[5:04:19 PM] [semantic-release] › ✔ Completed step "verifyConditions" of plugin "@semantic-release/gitlab"
[5:04:19 PM] [semantic-release] › ℹ No git tag version found on branch main
[5:04:19 PM] [semantic-release] › ℹ No previous release found, retrieving all commits
[5:04:19 PM] [semantic-release] › ℹ Found 1 commits since last release
[5:04:19 PM] [semantic-release] › ℹ Start step "analyzeCommits" of plugin "@semantic-release/commit-analyzer"
[5:04:19 PM] [semantic-release] [@semantic-release/commit-analyzer] › ℹ Analyzing commit: Initial Commit
[5:04:19 PM] [semantic-release] [@semantic-release/commit-analyzer] › ℹ The commit should not trigger a release
[5:04:19 PM] [semantic-release] [@semantic-release/commit-analyzer] › ℹ Analysis of 1 commits complete: no release
[5:04:19 PM] [semantic-release] › ✔ Completed step "analyzeCommits" of plugin "@semantic-release/commit-analyzer"
[5:04:19 PM] [semantic-release] › ℹ There are no relevant changes, so no new version is released.
section_end:1733591059:step_script
[0Ksection_start:1733591059:cleanup_file_variables
[0K[0K[36;1mCleaning up project directory and file based variables[0;m[0;m
section_end:1733591060:cleanup_file_variables
[0K[32;1mJob succeeded[0;mEnvironment description
Host Node: Fedora Server 41 running libvirtd and qemu all updates applied Gitlab VM: Debian 12 running omnibus package up to date Kubernetes Cluster: K3S Cluster v1.31.2+k3s1 with pod security enabled following the CIS hardening guide Flux installed following documentation [Gitlab Documentation] (https://docs.gitlab.com/ee/user/clusters/agent/gitops/flux_tutorial.html)
Used GitLab Runner version
Possible fixes
setting the namespace to privileged allows the runner to execute tasks properly. This is only a work around and should be able to configure the runner to operate compliant with CIS Guidelines.