gitlab-runner-operator
by alekc
A Kubernetes operator for GitLab CI runners. Run GitLab runners as typed CRDs instead of config.toml, with registration, config and cleanup on GitLab handled for you.
About this chart
gitlab-runner-operator
Run GitLab CI runners on Kubernetes as objects you apply, not TOML you template.
An independent project. Not affiliated with, nor endorsed by, GitLab. GitLab maintains its own GitLab Runner Operator for OpenShift and OLM. That is a different operator with a different CRD API; manifests are not interchangeable between the two.
This chart is maintained by the same author as the operator it packages, not a third-party wrapper around someone else's project.
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner01
spec:
gitlab_instance_url: https://gitlab.com/
authentication:
token:
secret_key_ref:
name: gitlab-runner-token
$ kubectl get runners
NAME READY AGE
runner01 true 12s
That is the whole thing. The operator registers the runner, renders its config.toml, runs the manager pod, and keeps GitLab in sync with what is in the cluster.
Why not the official gitlab-runner chart
The official chart works, and if you have one runner that never changes, it is the simpler choice. It starts to hurt at the second runner.
| gitlab-runner chart | this operator | |
|---|---|---|
| Runner config | config.toml as a templated string inside values.yaml | typed fields on a CRD |
| Mistakes caught | at pod start, in the manager log | at kubectl apply, by the API server |
| Adding a runner | another Helm release to manage | another object in the same file |
| Changing tags or project | re-register, often by hand in the UI | edit the spec, operator recreates it |
| Runner deleted in the GitLab UI | chart does not notice | reconciled back (managed mode) |
| Removing a runner | helm uninstall, then tidy up GitLab | kubectl delete, operator deregisters it |
| Several runners, one pod | not possible | MultiRunner |
The practical difference is where errors show up. Writing TOML inside YAML inside Go templating means a wrong executor field is a quoting bug you find in a crashed pod. Here the field is on a CRD, so the API server rejects it while you still have the terminal open:
$ kubectl apply -f runner.yaml
The Runner "runner01" is invalid: spec.authentication: Invalid value: "object":
exactly one of token or create_options must be set
The other difference is ownership. The operator holds the runner's identity on
GitLab: it creates the runner, stores the token in a Secret, recreates it when
create_options change, and deregisters it when you delete the object. Nothing
drifts into a state where the cluster and GitLab disagree.
When the official chart is the better choice
You want a runner and nothing else, you are not on GitOps, or you need a runner feature this operator does not expose yet. It is upstream-maintained and tracks new gitlab-runner releases the day they land.
Install
helm repo add alekc https://charts.alekc.dev
helm install gitlab-runner-operator alekc/gitlab-runner-operator \
--namespace gitlab-runner-operator --create-namespace
Needs Kubernetes 1.25 or newer. Validation is expressed in CEL, which older API servers ignore, so the chart refuses to install below that rather than accepting Runner objects nothing will check.
Upgrading
helm upgrade gitlab-runner-operator alekc/gitlab-runner-operator \
--namespace gitlab-runner-operator
CRDs
Helm installs the files under crds/ on first install and never touches them
again, which is Helm's own design: a CRD is cluster-scoped and deleting one
takes every custom resource with it. The cost is that an upgrade would otherwise
leave the old schema in place, and the apiserver then prunes spec fields the
installed CRD has not heard of, silently and with no error.
So crds.upgradeJob is enabled by default. It is a pre-install,pre-upgrade
hook that applies this chart's own crds/ and waits for them to be established
before any Runner is applied, which is what makes a newly added spec field
usable in the same upgrade that introduces it. The hook can only create and
patch CRDs; it has no delete verb, deliberately.
Turn it off with --set crds.upgradeJob.enabled=false if something else owns
the CRDs, and apply them yourself when they change:
kubectl apply --server-side -f https://raw.githubusercontent.com/alekc/gitlab-runner-operator/v2.1.4/config/crd/bases/gitlab.k8s.alekc.dev_runners.yaml
kubectl apply --server-side -f https://raw.githubusercontent.com/alekc/gitlab-runner-operator/v2.1.4/config/crd/bases/gitlab.k8s.alekc.dev_multirunners.yaml
helm uninstall never removes the CRDs, so your runners survive it. Removing
them is a deliberate, separate kubectl delete crd.
The runner-editor-role and runner-viewer-role ClusterRoles are now prefixed with the
release name. Rebind anything that referenced the old names.
Upgrading from a v1 chart is not in-place: v1beta1 was removed and there is no conversion, so export any Runner objects, upgrade, then reapply them as v1beta2.
Examples
Bring your own token
You made the token in GitLab under Settings, CI/CD, Runners. The operator just
uses it. The Secret key defaults to token.
apiVersion: v1
kind: Secret
metadata:
name: gitlab-runner-token
stringData:
token: glrt-REPLACE_ME
---
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner01
spec:
gitlab_instance_url: https://gitlab.com/
authentication:
token:
secret_key_ref:
name: gitlab-runner-token
Let the operator create the runner
Give it an access token with the create_runner scope and describe the runner
you want. The operator creates it on GitLab, keeps the returned authentication
token in a Secret, and recreates the runner if you later change create_options.
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner01
spec:
gitlab_instance_url: https://gitlab.com/
concurrent: 4
authentication:
access_token:
secret_key_ref:
name: gitlab-pat
key: token
create_options:
runner_type: project_type
project_id: 1234567
tag_list:
- k8s
- linux
Several runners, one manager pod
A MultiRunner renders every entry into one config.toml served by a single manager. Use it when the runners share a GitLab instance and differ only by tags or project; it is a lot cheaper than a manager pod each.
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: MultiRunner
metadata:
name: shared
spec:
gitlab_instance_url: https://gitlab.com/
concurrent: 10
entries:
- name: amd64
authentication:
access_token:
secret_key_ref: {name: gitlab-pat, key: token}
create_options:
runner_type: project_type
project_id: 1234567
tag_list: [amd64]
- name: arm64
authentication:
access_token:
secret_key_ref: {name: gitlab-pat, key: token}
create_options:
runner_type: project_type
project_id: 1234567
tag_list: [arm64]
executor_config:
node_selector:
kubernetes.io/arch: arm64
Executor configuration
Everything the Kubernetes executor takes is a field, including volumes:
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner01
spec:
gitlab_instance_url: https://gitlab.com/
log_level: debug
executor_config:
image: debian:slim
memory_limit: 150Mi
memory_request: 150Mi
volumes:
config_map:
- name: test-config
mount_path: /cm/
secret:
- name: test-secret
mount_path: /secrets/
empty_dir:
- name: docker-certs
mount_path: /certs/client
medium: Memory
authentication:
token:
secret_key_ref:
name: gitlab-runner-token
Self-signed GitLab
Point at a CA and both the operator's API client and the runner will trust it.
Takes an inline value, a secretKeyRef, or a configMapKeyRef.
spec:
caCertificate:
secretKeyRef:
name: gitlab-ca
key: ca.crt
Creating runners from the chart
Handy for a single-file GitOps setup. runners and multiRunners take the same
specs as above:
runners:
- name: runner01
spec:
gitlab_instance_url: https://gitlab.com/
authentication:
token:
secret_key_ref:
name: gitlab-runner-token
Restricting where builds run
By default a runner may only provision executor RBAC in its own namespace. Widen it deliberately:
allowedBuildNamespaces:
- ci-builds
- ci-builds-priv
The reconciler enforces this, and prunes the RoleBinding if a namespace stops
being allowed. ["*"] allows any namespace.
Troubleshooting
A runner that stays READY false puts the reason on the object:
kubectl get runners -o custom-columns=NAME:.metadata.name,READY:.status.ready,ERROR:.status.error
kubectl describe runner runner01
Most common causes: the access token lacks the create_runner scope, the
project id is wrong, or the GitLab URL is unreachable from the cluster.
Homepage: https://gitlab-runner-operator.alekc.dev/
Maintainers
| Name | Url | |
|---|---|---|
| Alexander Chernov | [email protected] |
Source Code
- https://github.com/alekc/gitlab-runner-operator
- https://gitlab.com/alexander-chernov/helm/gitlab-runner-operator
Requirements
Kubernetes: >=1.25.0-0
Values
| Key | Type | Default | Description |
|---|---|---|---|
| affinity | object | {} | Affinity rules for the operator pod. |
| allowedBuildNamespaces | list | [] | Namespaces (besides a runner's own) where the operator may provision executor RBAC. Empty restricts each runner to its own namespace; use ["*"] to allow any. Enforced by the reconciler, which also prunes the RoleBinding when a namespace stops being allowed. |
| crds.upgradeJob.enabled | bool | true | Keep the CRDs current. Helm installs files under crds/ once and never touches them again, so without this an upgrade leaves the old schema in place and the apiserver silently prunes spec fields it does not know. The hook applies the chart's own CRDs and waits for them to be established before any Runner is applied. |
| crds.upgradeJob.image | string | "registry.k8s.io/kubectl:v1.37.3" | Image for the hook. Needs kubectl on PATH. |
| crds.upgradeJob.resources | object | {"limits":{"cpu":"200m","memory":"128Mi"},"requests":{"cpu":"50m","memory":"64Mi"}} | Deliberately set rather than empty: a ResourceQuota whose hard limits include requests or limits.cpu would otherwise reject the hook pod and fail the upgrade. Both are set for that reason. |
| crds.upgradeJob.waitTimeoutSeconds | int | 90 | How long to wait for the CRDs to become established, in seconds. Helm's own hook budget is 300s, so leave headroom for the image pull and apply. |
| fullnameOverride | string | "gitlab-runner-operator" | full name override. It's set to gitlab-runner-operator since there is no sense in setting multiple instances of the operator |
| image.pullPolicy | string | "IfNotPresent" | pull policy to use for the operator image |
| image.repository | string | "ghcr.io/alekc/gitlab-runner-operator" | source repository |
| image.tag | string | "" | Overrides the image tag whose default is the chart appVersion. |
| imagePullSecrets | list | [] | Image pull secrets |
| kubeTargetVersionOverride | string | "" | Override detected kube version. Useful for rendering |
| livenessProbe | object | {"httpGet":{"path":"/healthz","port":"health"},"initialDelaySeconds":15,"periodSeconds":20} | Liveness probe for the operator container. |
| metrics.enabled | bool | true | Serve controller-runtime metrics. Bound to all interfaces so a Service can reach them; the endpoint is unauthenticated, so restrict it with a NetworkPolicy or turn this off if that is not acceptable. |
| metrics.port | int | 8080 | Port the metrics endpoint listens on. |
| metrics.service.annotations | object | {} | Annotations for the metrics Service, for example scrape hints. |
| metrics.service.enabled | bool | true | Create a Service in front of the metrics port. |
| multiRunners | list | [] | MultiRunner objects to create with the chart. One manager pod serves every entry, so use this instead of several runners when they share a GitLab instance and differ only by tags or project. |
| nameOverride | string | "" | override default name |
| nodeSelector | object | {} | Node selector for the operator pod. |
| podAnnotations | object | {} | Annotations added to the operator pod. |
| podSecurityContext | object | {"runAsNonRoot":true,"seccompProfile":{"type":"RuntimeDefault"}} | Pod-level security context. The default satisfies the restricted Pod Security Standard. |
| readinessProbe | object | {"httpGet":{"path":"/readyz","port":"health"},"initialDelaySeconds":5,"periodSeconds":10} | Readiness probe for the operator container. |
| replicaCount | int | 1 | Operator replicas. Leader election is on, so extra replicas are standby only; one reconciles at a time. |
| resources | object | {"limits":{"cpu":"200m","memory":"128Mi"},"requests":{"cpu":"100m","memory":"64Mi"}} | Resource requests and limits for the operator container. |
| revisionHistoryLimit | int | 3 | Deployments to keep for rollback. |
| runners | list | [] | Runner objects to creat |
Deploy gitlab-runner-operator with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has gitlab-runner-operator on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartgitlab-runner-operator is pinned at v2.5.7 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply