Skip to content
LatchkeyLatchkey home

kubectl label --overwrite: Usage, Options & Common CI Errors

Change a label that already exists without the re-run failure.

kubectl label --overwrite updates the value of a label that is already set, which the bare label command refuses to do. It is the flag that makes labelling idempotent in a re-runnable CI pipeline.

What it does

kubectl label RESOURCE NAME key=value --overwrite replaces an existing label's value (without it, re-labelling an existing key errors). It composes with -l to bulk-relabel by selector and --all to cover a namespace; key- still removes a label regardless of --overwrite.

Common usage

Terminal
kubectl label pod my-pod env=prod --overwrite
kubectl label pods -l app=web tier=frontend --overwrite
kubectl label deploy/web version=${GIT_SHA} --overwrite
kubectl label node ip-10-0-1-5 disktype=ssd --overwrite

Common errors in CI

"\"env\" already has a value (staging), and --overwrite is false" is the canonical re-run trap: the first run set the label, the retry fails because --overwrite was omitted - add it for idempotent scripts. "metadata.labels: Invalid value" means the value breaks the rules: ≤63 characters, alphanumeric plus -, _, ., starting and ending alphanumeric. Git branch names and image tags routinely violate this (slashes, leading non-alphanumeric), so sanitize before labelling. Relabelling a pod that a Service selects on can move it out of (or into) the Service's endpoints, so mind selector-driven side effects.

Using this in CI

A runner has no kubeconfig, no cached context, and no interactive auth. Every kubectl invocation in CI needs the context supplied explicitly, and most confusing CI failures here are the command running against the wrong cluster or no cluster at all.

Terminal
# never rely on the ambient context on a runner
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods

# confirm what you are actually connected to before mutating anything
kubectl config current-context
kubectl cluster-info

# fail fast instead of hanging on an unreachable API server
kubectl --request-timeout=30s get nodes

Frequently asked questions

kubectl label --overwrite: Usage, Options & Common CI Errors?
kubectl label --overwrite updates the value of a label that is already set, which the bare label command refuses to do. It is the flag that makes labelling idempotent in a re-runnable CI pipeline.
What it does?
kubectl label RESOURCE NAME key=value --overwrite replaces an existing label's value (without it, re-labelling an existing key errors). It composes with -l to bulk-relabel by selector and --all to cover a namespace; key- still removes a label regardless of --overwrite.
Common errors in CI?
"\"env\" already has a value (staging), and --overwrite is false" is the canonical re-run trap: the first run set the label, the retry fails because --overwrite was omitted - add it for idempotent scripts.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card