Skip to content

metadata.name validation #1424

Description

@jpbetz

What do you want to happen?

@thockin had mentioned to me earlier that using CEL is problematic because of cost limits on name field:

// +kubebuilder:validation:XValidation:rule="!format.dns1123Label().validate(self.metadata.name).hasValue()",message="must be a valid DNS label"

Which let me to wonder a few things:

  1. Why not just set format: k8s-short-name in the CRD declaration via Kubebuilder?
    • Reason 1.1: I don't know how to put JSON Schema validation on CRDs using +kubebuilder tags (it's possible in YAML, see below)
  2. Why not just put a maxLength: 63 on metadata.name and then use the CEL format.dns1123Label() validation?
    • Reason 2.1: Same as reason 1.1
  3. Why isn't CEL smart enough to know that the name field has a bounded length?

Is anyone aware of solutions that work well today? Is there appetite for PRs to add kubebuilder support for JSON Schema metadata.name validation in Kubebuilder or is CEL the answer? If there is appetite, any recommendations for the +kubebuilder tag representation?

CRD support for metdata.name (and generateName) validation:

CRD schemas allow the name and generateName field to be validated directly using JSON Schema:

For example:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: ...
spec:
  group: ...
  versions:
    - name: v1
      # ...
      schema:
        openAPIV3Schema:
          type: object
          properties:
            metadata:
              type: object
              properties:
                name:
                  type: string
                  pattern: "^required-prefix.*"  # metadata.name validation
                  maxLength: 40                     # metadata.name validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions