CodeBlanks Logo Icon CodeBlanks

CRD YAML

Learn the CRD YAML structure.

CRD YAML

A CRD's name must have the form <plural>.<group>. names gives the type's names, scope the namespace scope, and versions the served API versions and their validation definitions:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: yedekler.ornek.com
spec:
  group: ornek.com
  scope: Namespaced
  names:
    plural: yedekler
    singular: yedek
    kind: Yedek
    shortNames: [yd]
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                veritabani: {type: string}
                takvim: {type: string}
                saklamaGun: {type: integer}

storage: true shows which version is written to etcd; it must be on exactly one version.

Scope and names

scope: Namespaced binds the type to namespaces (most application resources); Cluster uses a single cluster-wide name space (e.g. a platform setting). names determines which names kubectl recognizes:

names:
  plural: yedekler      # kubectl get yedekler, URL path
  singular: yedek       # kubectl get yedek
  kind: Yedek           # kind in YAML
  shortNames: [yd]      # kubectl get yd
  categories: [all]     # appears in kubectl get all

Practice Write a CRD

Write a CRD YAML: apiextensions.k8s.io/v1; name yedekler.ornek.com; group: ornek.com, scope: Namespaced; names: plural: yedekler, singular: yedek, kind: Yedek; in versions, name: v1, served: true, storage: true. This is the first part of the file: the API server also requires a validation definition for each version; you'll add it in lesson 5 of this group.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: yedekler.ornek.com
spec:
  group: ornek.com
  scope: Namespaced
  names:
    plural: yedekler
    singular: yedek
    kind: Yedek
  versions:
    - name: v1
      served: true
      storage: true

Expected output

# crd.yaml (ilk kısım) hazır
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: yedekler.ornek.com

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What must metadata.name be for a CRD with group: ornek.com and plural: yedekler?

  • Yedek
  • ornek.com
  • yedek.v1
  • yedekler.ornek.com Correct answer

Explanation: If the name doesn't match, the API server rejects the CRD.

2 Make the type namespace-scoped.

Question
spec:
  group: ornek.com
  scope: ____
Answer
spec:
  group: ornek.com
  scope: Namespaced

Explanation: The other option is Cluster.

3 Match.

Term Meaning
plural URL and list name
kind YAML type
shortNames Abbreviation
storage: true etcd version

All lessons in Expert