← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Has No Labels Set

pod-template-labels

severityLow resourcePods productKubernetes bundles1

A pod with no labels cannot be selected by label, so NetworkPolicies and other selector-driven controls written for it never apply, and the workload runs unisolated and unattributable to any owner.

Rejects unless

has(object.metadata.labels) && size(object.metadata.labels) > 0

Pods must set metadata.labels (non-empty) for policy targeting.

Remediation

Add at least one identifying label, such as app.kubernetes.io/name, to the pod. Field: metadata.labels.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: pod-template-labels
  annotations:
    kubeapt.io/uuid: "7dc91776-2f31-44d1-bb85-f9a18ed08618"
    security.kubeapt.io/displayName: "Has No Labels Set"
    security.kubeapt.io/description: "A pod with no labels cannot be selected by label, so NetworkPolicies and other selector-driven controls written for it never apply, and the workload runs unisolated and unattributable to any owner."
    security.kubeapt.io/resource: "Pods"
    security.kubeapt.io/severity: "Low"
    security.kubeapt.io/remediation: "Add at least one identifying label, such as app.kubernetes.io/name, to the pod. Field: metadata.labels."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - pods
  validations:
  - expression: |
      has(object.metadata.labels) && size(object.metadata.labels) > 0
    message: |
      Pods must set metadata.labels (non-empty) for policy targeting.

Save it as pod-template-labels.yaml — the commands below assume that name.

Validate with kubeapt

Check a workload against this one policy, before anything is installed on a cluster.

$ kubeapt validate -p ./pod-template-labels.yaml -r ./workload.yaml

Or without the file, from the published policy set — -r takes a directory too:

$ kubeapt policies download && kubeapt validate --policy-name pod-template-labels -r ./manifests

Or against every namespace in the cluster your kubeconfig points at:

$ kubeapt validate --policy-name pod-template-labels -A