Skip to main content

Component Definitions

Go Reference Go Reference

A ComponentDefinition declares a component type for one custom resource kind, with no Go code. It names the kind it emits and a CustomResourceDefinition file, pinned by SHA-256. kurel build loads the documents of <app dir>/definitions/ (see the kurel CLI reference); a library caller uses this package directly.

h, err := definition.Load("definitions/batchjob.yaml") // reads the CRD and checks its digest
if err != nil { /* refused: names the file and the reason */ }
err = definition.Register(transformer, h)              // the type is now authorable

LoadFS(fsys, name) is Load for a definition in a file system, with its CRD read from the same file system, relative to the definition’s directory. It runs every check Load runs. It is how the definitions embedded in the binary load (see shipped definitions).

A Handler does not change after the load, and PropertySchema returns a copy, so one handler may be registered on several transformers and used by them at once.

Load refuses a document with a field it does not know, a crd path that leaves the definition’s directory (by .., an absolute path or a symlink), a digest that differs, a CRD whose spec.scope is neither Namespaced nor Cluster, a CRD that does not define the emitted kind in a served version, and a schema that is not structural. Register refuses a type name the transformer already holds (the error names the declared type that holds it), and an identity that would take over a registered type, a handler or a lowering rule: its short name or its qualified name is the plain name of one, and no declared type claims that name. A name a declared type claims is not refused here: it resolves to that type, or is reported ambiguous where a document uses it. A declared type of the same qualified name is another version, which is accepted. It then declares the type’s identity, <kind>.<group>@<version>, so a model can name it. It uses Transformer.RegisterAuthoredComponent, so a document may author the type.

A component of the type takes the resource’s spec as its properties. The object is checked against the CRD’s schema, unknown fields included, and emitted by the passthrough handler, which applies its own per-object rules. The CRD’s spec.scope decides whether the object is namespaced. The environment policy reads the object only where the document says where: the pod specs in spec.podSpecs and the fields of the other classes in spec.markers, and a document states in spec.markerClasses which classes it was checked for (see “Field markers”: a document that states none is refused under NoopPolicy, which a build with no policy uses).

A CRD with no spec (a VolumeSnapshotClass keeps driver, deletionPolicy and parameters at the top level of the object, go-kure/launcher#1821) takes the fields of the object’s top level as its properties, and emits them there, never under a spec. The properties are the CRD’s declared top-level fields other than apiVersion, kind, metadata and status: launcher sets the first three (the name of the object is the component’s), and the cluster owns status. A property of one of those names, in any case, is refused as not authorable. Everything else is as for a spec: unknown fields, types, required fields and enums are checked by the CRD’s schema over the whole object, and the scope is the CRD’s. Load refuses a CRD whose top level is open (as it refuses an open spec), one that declares neither a spec nor a field of its own, and a document of such a kind that lists spec.podSpecs or spec.markers, since a path of either starts at spec: a kind with no spec has its pods and its marked fields unread, and states in spec.markerClasses the classes it was read for, as any definition does.

The scope reaches the object kind policy too. Handler.DeclaredKindScope (oam.KindScopeDeclarer) states the kind the definition emits and the scope its CRD gives it. The transform hands the object kind check that scope only for a handler marked with Transformer.MarkScopeTrusted (shipped.Load marks the definitions launcher ships) or for a kind the policy names in the optional oam.TrustedScopePolicy (TrustedScopeKinds(), matched like AllowedObjectKinds()). A definition an author supplies, registered with Register alone, is neither: its declaration is ignored, so it cannot relax a policy the author does not own (go-kure/launcher#1758). Under a policy that does not allow cluster-scoped objects, an object of a kind that counts passes when the CRD is namespaced and is refused when it is cluster-scoped, whatever component or trait emits it, an opted-in kind included (go-kure/launcher#1744). A kind no counted definition describes stays of unknown scope and is refused. This holds for a caller that passes an object kind policy and loads the definition, a library caller: kurel build passes no object kind policy and runs no object kind check.

What the check is

The API server’s structural schema check, run offline: types, required fields, enums, bounds, patterns, formats, array items, additionalProperties, int-or-string values and x-kubernetes-preserve-unknown-fields. Not done:

  • CEL rules (x-kubernetes-validations) are not evaluated.
  • Defaults are not applied to the object that is checked against the schema or emitted. The environment policy does read the object with them applied, as the API server will store it (see “Pod specs inside the resource”).
  • Only the version the document names is read, from a local file or, for LoadFS, from the file system it is given. Nothing is fetched.
  • An explicit null on a property is refused, so a CRD’s nullable is not reachable.
  • A CRD whose spec is open (additionalProperties or x-kubernetes-preserve-unknown-fields on spec itself) is refused: the published properties are the declared ones. Open properties further down are accepted.
  • A CRD with no spec is read at its top level (see above); one that declares neither a spec nor a field of its own, and a document that lists a pod spec or a marker path for such a CRD, are refused.
  • A CRD with a field marked x-kubernetes-embedded-resource is refused: the API server checks that field’s apiVersion, kind and metadata apart from the schema, and this loader does not.
  • A definition file and a pinned CRD file each hold one YAML document; a second is refused.

Pod specs inside the resource

The environment policy reads the pod of a workload kind it knows. A custom resource whose controller runs pods is a kind it does not know, so a definition says where the pods sit, and the build holds each one to what a Pod is held to (go-kure/launcher#1705). The paths go in spec.podSpecs:

apiVersion: launcher.gokure.dev/v1alpha1
kind: ComponentDefinition
metadata:
  name: broadcast-job
spec:
  emits:
    apiVersion: apps.kruise.io/v1beta1
    kind: BroadcastJob
  schema:
    crd: crds/broadcastjobs.yaml
    sha256: c74d121f38408fc028f2316af398587981502f6f43019baca5334c8976a773ce
  podSpecs:
    - spec.template.spec
  markerClasses: [secret, image, url, replicas, storage, hostPath, resources, claimSpec]   # see "Field markers"
  • A path runs from the object root, dotted, and starts with spec.. It names a node shaped as a Kubernetes PodSpec. For a PodTemplateSpec, name its spec: spec.template.spec.
  • [] after a key steps into every item of an array, and {} into every value of a map: spec.workers[].template.spec, spec.groups{}.template.spec. A key that holds a dot, a bracket or a brace cannot be written.
  • Load follows each path through the CRD’s schema. It refuses a path that does not start with spec., one listed twice, one that names a property the CRD does not declare, a [] on a property that is no array, a {} on one that is no map, a key, [] or {} below a node whose declared type cannot hold it, and a path that ends on a node of another type than object, an int-or-string node included (a property, an array item or a map value; go-kure/launcher#1807). A map whose values have a schema is followed through that schema, for a literal key as for {}, also where it preserves unknown fields. A node the CRD leaves open (x-kubernetes-preserve-unknown-fields or an open additionalProperties) says nothing about the keys it does not declare: for a key that has no declared property and no map-value schema, Load accepts the rest of the path, as long as the declared type of the node admits the step (an object that preserves unknown fields is still no array), and the build judges what the object holds there. A property the open node does declare is followed through its own schema.
  • A CRD default is no obstacle: the API server adds it after the build, so the policy reads the object with the CRD’s defaults applied, by the API server’s own algorithm, and never the object as authored (go-kure/launcher#1833). A pod that leaves out a defaulted field (hostNetwork: true, say) is checked with it, and the refusal names the node. A default that fills a whole node the author left out is read the same way. What is emitted stays as authored. This replaces the refusal of a default on or above a listed node that Load had (go-kure/launcher#1811): the error “the CRD declares a default at …” no longer exists, and a node is refused at build, with its path, only when the value the cluster will store is one the policy refuses. The defaults are the structural schema’s, pruned to their own schema first as the API server prunes them (a field a default holds that its closed node does not declare is not stored, so it is not read); what an operator fills in at run time is not (see “Not covered” below).
  • At build, each node found is read as the spec of a v1 Pod. It meets the refusals of an authored workload whatever the policy (a key a PodSpec does not declare, a container name used twice, the mount and lifecycle rules), then the environment policy: host namespaces, privileged containers, hostPath volumes, capabilities, the registry allowlist and image reference level, the cpu, memory and storage maxima, and ephemeral containers. The error names the node, with the index or key of every array item and map value on the way (spec.workers[1].template.spec, spec.groups["gpu"].template.spec).
  • A path the object does not reach (an optional field left out, an empty array) has nothing to check. A value of the wrong kind on the way, or a node that is no object, is refused as an unreadable-object.
  • The property at the top of the spec that holds a path publishes the classes those refusals can have (PropertySchema().BoundedBy): host-namespace, privileged, host-path, container-capability, registry, resource-maximum, storage-maximum and unreadable-object.

Pod specs the loader recognizes

A definition need not list a pod spec whose place in the CRD shows it (go-kure/launcher#1833). Load reads the CRD’s schema, and a node under spec is a pod spec when it holds the anchor properties of a Kubernetes PodSpec with the same JSON kinds (containers, a list of objects that hold a string name and a string image), holds no property a PodSpec does not have, declares none of its properties with a kind the PodSpec property cannot hold (a field the CRD leaves untyped is no mismatch), and declares at least half of the properties a PodSpec has. The key it sits under is never read: the pod of an Argo Rollouts Rollout is found at spec.template.spec, and that of a JobSet at spec.replicatedJobs[].template.spec.template.spec. Each path found joins podSpecs and is held to everything above, with the property that holds it publishing the pod classes. Listing a path that recognition also finds changes nothing.

  • A pod template (metadata and a recognized pod spec) is recognized for its spec only; the template has no check of its own.
  • A node that meets the anchor but declares fewer than half of a PodSpec’s properties is trimmed, and is not listed; the nodes below it are still read, so a whole pod spec in a trimmed one is found. The nodes below a listed pod spec are read too, since a CRD default can put a pod spec below one of its fields after the authored object was checked. A node that holds a foreign property, such as the flat spec of a Prometheus operator kind that mixes pod fields with its own, is not a pod spec at all, and neither is a pod spec that holds one property of the CRD’s own: the pod checks read a listed node as a native Pod, which refuses a key a PodSpec does not declare, and a pod spec in such a property would not be read. List such a node in podSpecs by hand; the same refusal then applies to its foreign key.
  • A node the CRD leaves open holds nothing recognition can see, so a pod spec below one still needs its path in podSpecs. A path the grammar cannot write (a key that holds a dot, a bracket or a brace, an array of arrays) is not listed.
  • Recognition only adds checks. A definition whose CRD has a pod spec that it did not list is now held to the pod policy for it; where that is wrong for a kind, the way out is to say so in an issue, since the definition has no switch to turn recognition off.

A definition whose CRD shows no pod spec and lists none does not have its pods checked. Its object passes the pod part of the environment policy whatever pods it holds, as a custom resource emitted by passthrough does. A resource whose pod settings do not sit in a PodSpec-shaped node (one that mixes pod and container fields in one securityContext, or names its image apart from a container list) cannot be read by a podSpecs path. A podSpecs path on such a node is refused at build, since it holds keys a PodSpec does not declare. What a field marker can read of such a kind is its image (image), its replica count, its storage size and its volumes, node by node; the rest of its pod settings (security context, host namespaces, capabilities, resources) are not read, and a kind that has to hold those to the policy needs a Go type.

The paths reach the passthrough config as PassthroughConfig.PodSpecPaths; an authored passthrough component has no property for them.

Field markers

Outside a pod spec the policy reads a custom resource’s secret, image, URL, replica count, storage size, volumes, resource requirements and claim specs only where the document says where (go-kure/launcher#1818, go-kure/launcher#1833). A definition lists the nodes in spec.markers, and the build holds each to the check launcher already has for its class:

spec:
  markerClasses: [secret, image, url, replicas, storage, hostPath, resources, claimSpec]
  markers:
    - path: spec.image
      class: image
    - path: spec.workers[].apiKey
      class: secret

A marker is a path, in the grammar of podSpecs (from spec, dotted, [] into array items, {} into map values; components.ParsePodSpecPath reads both), and a class:

ClassThe node holdsHeld to
secreta string, secret material when it is not emptyrefused as explicit-secret under a policy that forbids explicit secrets; the message quotes nothing
imagean image referencethe allowed registries (registry) and the image reference level
urla URL the build’s output fetches a source fromthe allowed registries, on the host (an oci:// URL has to name its registry, as a Flux source’s does)
replicasan integerthe replica maximum (replica-maximum)
storagea quantity, a string or an int-or-stringthe storage maximum (storage-maximum)
hostPathan array of volumesrefused as host-path unless the policy allows hostPath volumes
resourcesan object read as Kubernetes ResourceRequirementsthe cpu and memory maxima on its requests and limits (resource-maximum)
claimSpecan object read as a Kubernetes PersistentVolumeClaimSpecthe storage maximum on resources.requests.storage (storage-maximum)
  • Load follows each path through the CRD with the walk podSpecs uses, so the two kinds of path are read alike. It refuses a path the CRD cannot hold, a node whose declared type cannot hold the class (a string for secret, image and url; an integer for replicas; a string or an int-or-string for storage; an array of objects for hostPath; an object for resources and claimSpec), a path listed twice, an unknown class, and a marker whose class markerClasses does not state. A CRD default on the node, below it or above it is read as the API server will store it, as for podSpecs: a secret a default fills in is refused where the policy forbids secrets, and a replica count a default sets is held to the maximum (go-kure/launcher#1833). A node the CRD leaves open accepts the path, as for podSpecs, and the build judges the value found there: one of the wrong kind is refused as an unreadable-object.
  • A node the object does not reach (an optional field left out, an empty array, an empty string for a secret) has nothing to check. A class the policy leaves free is not read.
  • A hostPath marker reads each volume by the exact names of the fields of a Kubernetes Volume: a key that is none of them, a case variant of hostPath included, is refused as unreadable-object, since the decoder would file it under hostPath and let a null clear the hostPath the object still emits.
  • A resources or claimSpec marker reads its node by the exact names of the fields of the Kubernetes structure, at every level, as a hostPath marker reads a volume: a key that is none of them, a case variant or a Unicode fold of one included, is refused as unreadable-object, and so is a value the structure cannot hold (a quantity that is none).
  • The property at the top of the spec that holds a path publishes the classes of its check (PropertySchema().BoundedBy): explicit-secret, registry, replica-maximum, storage-maximum, resource-maximum, and host-path, resource-maximum and storage-maximum with unreadable-object. Where a property also holds a podSpecs path, the lists join.

markerClasses states what the author read the CRD for. A policy that bounds a class refuses a definition that does not list it, with the class marker-class-unstated, so a policy added later shows as a refusal and never as a silent pass. An empty list says that no class applies. A policy bounds secret when it forbids explicit secrets, image when it lists registries or holds an image reference level other than any, url when it lists registries, replicas and storage when it sets a maximum, hostPath unless it allows hostPath volumes, resources when it sets a cpu or a memory maximum, and claimSpec when it sets a storage maximum. NoopPolicy, which every build without a policy uses, holds the default level tagged and allows no hostPath volume, so it bounds image and hostPath: a definition lists at least those two, and one that states no markerClasses is refused under NoopPolicy, so by every build that passes no policy. A policy of a library caller that bounds none of the classes refuses no definition. List a class the CRD has no field of too: it says that none applies.

Output change, go-kure/launcher#1833: the classes resources and claimSpec. A definition written before them lists the six classes, and a policy with a cpu, a memory or a storage maximum bounds the two new ones, so it refuses that definition with marker-class-unstated: the definition of <Kind> states no marker class resources, which the environment policy bounds: list resources in markerClasses once its CRD is read for fields of that class. The one-line fix, after reading the CRD for such a node, is to add the class (and claimSpec) to spec.markerClasses. The three shipped VictoriaMetrics definitions state both, having been read: their CRDs hold no resource requirements and no claim spec, so the statement adds no check there.

Markers recognition finds

Recognition reads the CRD for the nodes a class reads, as it does for pod specs, so that a definition need not write them down. By the shape of the node and never by its key, a node outside a pod spec is read as a marker of a class when it is:

  • resource requirements: an object whose limits and requests are maps of quantities, and which holds no property ResourceRequirements does not (resources);
  • a claim spec: an object with accessModes, a list of strings, and resources with a map of quantities as its requests, and no property a PersistentVolumeClaimSpec does not have (claimSpec; the resources below it is the claim’s request for storage, and no marker of its own);
  • a container: an object with a string name and image and no property a Container does not have, whose image is read (image);
  • an array of volumes: items with a string name and a hostPath object and no property a Volume does not have (hostPath).

A definition adds the nodes of the classes it states, since recognition states no class and a class the document leaves out is refused where the policy bounds it. It adds none below a pod spec, which the pod policy already reads whole, none whose path the document lists, and none whose path the CRD shows no node of the class for. A node with a property of its own is not recognized, as for pod specs, and is listed by hand.

Hints

Load also gathers hints (go-kure/launcher#1833): warnings about a node of the CRD that the definition does not cover, where a podSpecs path or a marker may be wanted. Handler.Hints() returns them, each "<path>: <text>" with the path in the grammar of markers, sorted by path. kurel build writes each to stderr as hint: <type>: <path>: <text> when it loads the definition. A hint changes no check: a node it names stays unchecked until the author writes it down, and no policy outcome depends on one.

Structural hints name a node recognition found and could not use:

  • a pod spec or marker whose path cannot be listed (a key that holds a dot, a bracket or a brace, an array of arrays), with the reason;
  • a trimmed pod spec (the anchor, fewer than half the properties): list its path in podSpecs;
  • a node that is a pod spec but for a property of its own, or a field of another kind: list its path in podSpecs by hand, where the pod checks refuse the property;
  • a node of a marker class markerClasses does not state: state the class once the CRD is read for it, and the node is held to the class’s check.

Field hints are rule v2 of go-kure/launcher#1828 without its URL rule: a property under spec that no marker covers and that is not below a pod spec, whose name and type suggest a class.

ClassA property namedOf the type
secretending in password, passwd, secret, token, credential(s), apiKey or accessKeystring
imageimage, not rbd.imagestring
replicasreplicasinteger
storagesize, storage, storageSize or sizeLimitstring or int-or-string

There is no hostPath hint (0 right of 8 in the measurement) and no url hint. The measurement found 59% of the hints of rule v2 right, on the CRDs the rule was tuned on: the figure is its own, not a promise, and a hint is a place to look. Not covered: URL hints, CEL rules, and a node the CRD leaves open, which holds nothing a hint can see. A definition launcher ships draws none (TestShippedDefinitionsDrawNoHints).

Not covered, and each stays a Go type: a secret or a URL under any key of a node the CRD leaves open (a VMAlertmanagerConfig is one: a marker names one path, never every key an open node can hold); a value an operator fills in or multiplies; a relation between fields (the CRD’s CEL rules are not run).

The address of a backup or a database is not checked either, since launcher’s only host policy is the allowed registries, so there is no host class. That is no reason for a Go type. A url marker is for a source the build’s output fetches, not for a connection address: the shipped scrape definitions mark none of their proxy and token URLs.

PassthroughConfig.Markers carries the classes and the nodes to the passthrough config, always set by a definition; an authored passthrough component has no property for it.