Repository navigation
feat(cli): a git-ignored .labels file labels the namespaces Kates creates - #445
Merged
Merged
Conversation
…ates Shared clusters often admit a namespace only with the labels their policies require. A .labels file (one key=value per line, git-ignored, with a commented .labels.example) now feeds the same label set as KATES_NAMESPACE_LABELS and --namespace-label, merged per key in that order. Every source is held to the Kubernetes label syntax, and keys under kubernetes.io, k8s.io, helm.sh, strimzi.io and kates.io are refused, except pod-security.kubernetes.io. kates deploy checks the labels before its pre-flight, and --dry-run lists each label with its source. A namespace Kates creates gets the labels, managed-by and a kates.io/namespace-labels annotation in one create. An existing namespace is patched only when a label differs, managed-by is never added to it, and a key the annotation lists is removed once it leaves the labels. When RBAC refuses the create or the patch, the error keeps kubectl's message and adds the manifest or the kubectl label command for a cluster admin. An admission denial keeps only its own message. --verbose no longer hides that message. The labels also reach cert-manager and kyverno when deploy installs them, kates auto, kates kyverno apply, kates detect, migrate lab namespaces, the detect probe namespaces (without Pod Security labels; --no-write-probes skips those probes) and scripts/common.sh ensure_namespace. --labels-file reads another file on each of these commands.
… arithmetic CodeQL's go/allocation-size-overflow flagged make([]string, 0, len(args)+1) once .labels file data reached kubectl's arguments. The +1 cannot overflow, but appending to args[:end:end] does the same copy without it. The test now gives the input spare capacity, so a write into the caller's array shows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Phase 1 of the "Kates on any cluster" plan. Many shared clusters admit a namespace only when it carries the labels their policies require, such as an owning team or a segment. Today labels come only from
--namespace-labelorKATES_NAMESPACE_LABELS. Neither is checked, and several namespaces Kates creates never get them: cert-manager, kyverno, the detect probes,kates autoand the migrate labs. On top of that, an existing namespace is relabelled on every run, and an account that may not patch it stops the deploy.What changes
.labelsfile: onekey=valueper line,#comments, read from the current directory, or from--labels-file..gitignorelists it, and.labels.exampledocuments it. Merge order per key:.labels<KATES_NAMESPACE_LABELS<--namespace-label.kubernetes.io,k8s.io,helm.sh,strimzi.ioandkates.ioare refused, exceptpod-security.kubernetes.io..labels:3.kates deploychecks the labels before its pre-flight, and--dry-runlists each label with its source.ensureDeployNamespace):kates.io/namespace-labelsannotation listing the keys Kates set.kubectl labelcommand, for a cluster admin. An admission denial (Gatekeeper, VAP) keeps only its own message, because an admin would be refused the same way.--verboseno longer drops kubectl's stderr from the error.kates auto,kates kyverno apply,kates detect;migrate up,migrate runandmigrate source deploy, where the lab's own labels win;kates-detect-*probe namespace, created in one call from a manifest and without Pod Security labels, because the probes' pods aren't restricted-ready. The new--no-write-probesskips those probes;ensure_namespaceinscripts/common.sh, which applies./.labelsand does nothing when the file is absent, so CI is unaffected.Evidence
go vet,go test ./..., and-race -p 1oncmdandpkg/detectall pass.golangci-lint --new-from-rev=origin/mainreports 0 issues. shellcheck passes with CI's flags.check-book-style.shpasses. A scratch Quarto render passes the fragment check, with 8008 links landing.--verbose;kates detectand the bench probe namespaces not wired;.labelsignored by the script;cdwithout a subshell indeploy-kafka-generic.sh;kind-panda:kates detectwith a.labelsfile createdkates-detect-latency-<id>carryingteam=payments, and the Secret audit passed through the manifest path.kates deploy --dry-runwith all three sources printedcost-center=9999 KATES_NAMESPACE_LABELS,owner=sre --namespace-labelandteam=payments .labels:1..labelswithapp.kubernetes.io/part-ofstopped the deploy with.labels:2: … prefix "app.kubernetes.io" is reservedbefore any cluster call.Behaviour changes for existing users
KATES_NAMESPACE_LABELSand--namespace-labelnow accept empty values and refuse reserved prefixes, such asapp.kubernetes.io/part-of.app.kubernetes.io/managed-by=katesis no longer stamped on namespaces Kates did not create.Not done
kates deployagainst a cluster whose policy requires labels was not run; the namespace paths are covered by unit tests and the live detect run.scripts/deploy-kafka-generic.shstill creates the Kafka namespace withkubectl create namespace … | kubectl applyand does not label it, anddeploy-monitoring.shrelies onhelm --create-namespace. The book's table names only whatensure_namespacedoes.podLabelscontract carrying.labelsto pods) are Phase 3.