Definition inheritance: spec.extends and spec.abstract on X-Definitions
Describe the problem
KubeVela's builtin definitions are a good starting point, but most organisations need them changed to meet their own requirements: a tenant label on every workload, a quota for every namespace, resource requests that are not optional.
Today there are two ways to do that and neither is good:
- Encode the requirement as a convention. Document it, and hope. Nothing enforces it, and an Application that ignores it renders happily.
- Fork
webservice. Hundreds of lines of CUE copied, the logic changed, and the copy owned forever. Upstream fixes a bug next release and the fork repeats the work by hand.
There is no way to say "this is webservice, plus our rules".
Proposed solution
Two new fields on X-Definitions.
spec.extends names a definition to build on. The extending definition says
what it hands its parent, adds resources of its own, and declares the parameters
it takes:
"tenant-webservice": {
type: "component"
extends: "webservice"
}
template: {
// What the parent is called with.
$super: properties: {
image: parameter.image
labels: "tenant.acme.io/name": parameter.tenant
}
// Merged onto the resource the parent made.
output: spec: template: spec: containers: [{
env: [{name: "TENANT", value: parameter.tenant}]
}]
// A resource of its own, beside the parent's.
outputs: config: {
apiVersion: "v1"
kind: "ConfigMap"
metadata: name: context.name + "-tenant"
data: tenant: parameter.tenant
}
parameter: {
tenant: string
image: string
}
}That renders webservice's complete Deployment with the tenant label on the pods, the env var in the container, and the ConfigMap beside it. No copy of webservice, and an upstream improvement to it arrives without further work.
spec.abstract marks a definition as a base: something to extend, never to
name directly from an Application. A platform team can then publish
acme-base for others to build on without anyone deploying it as-is.
"acme-base": {
type: "component"
abstract: true
}Chains should compose, so each layer can add its own rules and hide the ones below:
webservice -> acme-base -> acme-payments -> billing-api
upstream platform business unit the teamAcceptance criteria
-
spec.extendson ComponentDefinition and TraitDefinition, with a depth cap and cycles refused by name rather than left to recurse. -
spec.abstract, refused at Application admission and hidden fromvela def listunless asked for. - Health and status compose down the chain: a child that says nothing about health gets its parent's check whole.
- Ancestors recorded on the ApplicationRevision, so a render through a chain is reproducible after the definitions have moved on.
- The call a child makes to its parent validated at admission: a missing required parameter refused, an undeclared one warned about, and a parameter that reaches nothing reported.
- A definition that extends nothing must be unaffected, on the same code path and at the same cost.
- Behind a feature gate, default off.
Open questions
- Does a child inherit its parent's parameter schema, or declare its own? Inheriting is less to write; declaring is honest about what an Application is validated against, and means a definition cannot publish parameters that reach nothing.
- How deep should a chain be allowed to run? There is a cost per level, and a definition assembled from more places than a reader can hold is its own problem.
- What happens to a parent that reads its own
processing.outputfrom apatch?processingruns after the render, so the value is not there yet when the chain composes.
Scope
kubevela/kubevela only. kubevela/pkg, kubevela/workflow and
oam-dev/cluster-gateway are unaffected, though all three pin CUE and would be
involved if this needs anything from a newer one.
Addressed by #7386. Follow-ups still open after it:
- User documentation in kubevela/kubevela.io.
- A parent trait whose
patchreads its ownprocessing.outputrenders a non-concrete value once inherited. Onlydynamic-sain the registry has the shape today. Resolves itself on CUE 0.17, where values cross contexts. - VelaUX shows only an extending definition's own parameters;
HandleTemplatehas no client to resolve a chain with. - Trait composition into a definition: designed, not built.
- Revisit the depth cap. It was set to 8 when a level cost roughly 2.7x a plain render; a level now costs about 0.28x.
Source: kubevela/kubevela