Review Kubernetes metadata label syntax

Check valid label syntax and actual selector matches together.

Description

A label value can be empty or up to 63 characters long. A nonempty value must start and end with an ASCII letter or digit. It can also contain -, _ or . between those characters.

Potential impact

The API rejects an invalid label. A valid label with the wrong value can leave a Pod outside its Service or NetworkPolicy selector.

Remediation

Check the name and optional DNS prefix of the label key. When changing a value, update the affected selectors and verify which Pods they select.

Examples

The first app value contains invalid * and + characters and ends in .. goproxy is valid. Use a maintained image for deployment.

Before

yaml
apiVersion: v1
kind: Pod
metadata:
  name: goproxy
  labels:
    app: g**dy.l+bel.
spec:
  containers:
    - name: goproxy
      image: k8s.gcr.io/goproxy:0.1

After

yaml
apiVersion: v1
kind: Pod
metadata:
  name: goproxy
  labels:
    app: goproxy
spec:
  containers:
    - name: goproxy
      image: k8s.gcr.io/goproxy:0.1

References