Skip to main content

☑️ Ensure resource has a valid configured name

metadata.name OR metadata.generateName is a mandatory property in every K8s resource type. Validation that this property exists occurs is on the k8s server-side only, and is not enforced as part of the official schema. Therefore, a YAML without this property will pass k8s schema validation, but will fail when pushed into a cluster (i.e. when running kubectl apply/create).

Most resources require a name that complies with DNS subdomain names, as defined here. This means that the name must:

  • contain at most 63 characters
  • contain only lowercase alphanumeric characters or '-'
  • start with an alphanumeric character
  • end with an alphanumeric character

Targeted objects by this rule (types of kind): All

Complexity: medium (What does this mean?)

Policy as code identifier: RESOURCE_MISSING_NAME


This rule will fail

If a resource does not contain one of the propreties name or generateName, or if the name is not valid:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
namespace: "myGreatNamespace"
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: "thisIsAn!nvalidName"
namespace: "myGreatNamespace"

Rule output in the CLI

$ datree test *.yaml

>> File: failExample.yaml
❌ Ensure resource has a valid configured name [1 occurrence]
💡 Invalid/missing key `name` or `generateName` - one of them must be set with a valid value to apply a resource to a cluster

How to fix this failure

Set either name or generateName and give it a valid value:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: "myGreatIngress"
namespace: "myGreatNamespace"

Read more