Documentation
¶
Overview ¶
Package cachekey derives a SourceDefinition's cache key from the context its template reads.
The key is generated at build time by `vela def` and validated at admission, so nothing infers at reconcile time: the controller evaluates the generated expression exactly as it would an authored one.
Policy - which fields count, in what order - lives in an embedded CUE file whose hash is the version identity. Mechanism lives here.
Index ¶
- Constants
- func CheckSurface(fields []string, surface string) error
- func InvalidCacheKeyChars(s string) string
- func KeyExpression(definitionName string, dims []Dimension, rules *Rules) (string, error)
- func KeyInputs(dims []Dimension) []string
- func MissingOn(fields []string, surface string) []string
- func RequiredContext(template string) ([]string, error)
- func SurfacesSupporting(fields, surfaces []string) []string
- func ValidateCacheKey(key string) error
- func Verify(definitionName, template, rulesHash string) error
- type Dimension
- type Rules
Constants ¶
const InternalField = "$internal"
InternalField is the block holding everything the tooling generates, kept apart from the authored storage: block so that what a user may edit needs no explaining: one block is theirs, the other is not.
The $ prefix follows the convention CueX already uses for $params and $returns. A leading underscore would be the CUE idiom for "internal", but hidden fields are dropped from exported output, and these have to stay visible - GitOps diffs them and admission re-derives them.
const (
// KeyField is the generated cache key, inside InternalField.
KeyField = "key"
)
const KeyInputsField = "keyInputs"
KeyInputsField is the generated sibling of the key, inside InternalField: the values the resolver folds into the identity hash. It is recorded rather than re-derived so that inference stays a build-time concern, and so the resolver hashes exactly what the template reads rather than, say, every label on the object.
const MaxCacheKeyLen = 253
MaxCacheKeyLen is the Kubernetes object-name limit. A resolved storage key becomes the name of the backing Config object, so it must fit.
const RulesAnnotation = "definition.oam.dev/cache-key-rules"
RulesAnnotation records which inference policy generated a definition's cache key. Validation loads the rules with this hash rather than the current ones, so changing inference never invalidates a definition already generated.
const RulesVersionAnnotation = "definition.oam.dev/cache-key-rules-version"
RulesVersionAnnotation records the readable name of the same policy. The hash above is what validation looks up; this is for whoever is reading the object and wants to know which policy applied without computing anything.
Variables ¶
This section is empty.
Functions ¶
func CheckSurface ¶
CheckSurface reports why a source reading these fields cannot resolve on a surface, or nil if it can.
func InvalidCacheKeyChars ¶
InvalidCacheKeyChars returns a comma-separated list of the distinct disallowed characters in s, or "" when every character is permitted.
func KeyExpression ¶
KeyExpression renders the CUE expression that becomes storage.key.
This is the *readable prefix* of the cache identity, not the whole of it. The resolver appends a hash covering every value the template reads, and that hash carries uniqueness on its own - so a segment here is cosmetic, and one that resolves to nothing can be dropped without two identities colliding.
Only fields the rules mark as segments are inlined: those that always render to a Kubernetes name. A struct, a label value that may contain a dot, or a field that is legitimately empty contributes to the hash instead, which is what removes the need for a sentinel or a default.
Length is not checked here: the expression is static text, and how long it resolves to is only knowable once the interpolation has values.
func KeyInputs ¶
KeyInputs names every value the identity hash must cover, in key order.
The resolver hashes exactly this set. Hashing more - every label on the object, say - would change the identity whenever GitOps stamped an unrelated annotation and leave the cache permanently cold; hashing less would let two bindings that differ in an unrecorded input share an entry.
func MissingOn ¶
MissingOn names the context fields a surface cannot supply, as an author wrote them - context.componentName, not componentName.
Exported alongside CheckSurface because a caller reporting per surface already knows which surface it is asking about, so the sentence CheckSurface builds ends in a clause it would only have to strip back off.
func RequiredContext ¶
RequiredContext returns the context fields a source template reads.
Derived from the template rather than read back from $internal.keyInputs, so it cannot disagree with what the resolver will actually ask for - the same reason admission re-derives the key instead of trusting the stamped one.
func SurfacesSupporting ¶
SurfacesSupporting returns the surfaces that can supply every field, out of those given.
func ValidateCacheKey ¶
ValidateCacheKey checks that a resolved SourceDefinition storage key can be used as the name of the backing Config object.
Keys are not sanitised automatically: an interpolated value that produces an illegal name is a definition-authoring error, and silently rewriting it would change the cache identity — and therefore which Applications share an entry.
The admission webhook applies this to statically-known keys; the resolver applies it again once interpolation has produced a concrete value.
func Verify ¶
Verify re-derives the cache key from a stored template and checks the key the template carries matches it.
This is the admission-side half of the contract. The CLI writes the key, but nothing stops a stored object being edited or written by hand, so the key is re-derived rather than trusted.
rulesHash names the policy that produced the key, taken from the object's annotation. It is used in preference to the current rules so that changing inference does not invalidate definitions already generated and committed - which matters because GitOps re-applies them continuously. An empty hash means the object was not produced by `vela def`, and the current rules apply: the key still has to be right, just by today's policy.
Types ¶
type Dimension ¶
type Dimension struct {
// Field is the context field, e.g. "cluster".
Field string
// Index is the literal key for an indexed read, e.g. the label name in
// context.appLabels["team"]. Empty for a plain field.
Index string
// contains filtered or unexported fields
}
Dimension is one context value a cache key is built from.
func Infer ¶
Infer returns the context dimensions a template's resolution depends on, in the order they contribute to the key.
The whole template is scanned, not just output:. A value reaches the output through aliases and helper fields, and deciding which reads actually influence it is not tractable - so a read anywhere counts. Over-inclusion costs a narrower cache; under-inclusion serves one consumer another's data.
type Rules ¶
type Rules struct {
// Hash identifies this policy. It is stamped onto generated definitions and
// used to load the same rules again when validating them.
Hash string
// Version is a readable name for the same policy, declared in the rules file
// so it cannot drift from the content it names. Stamped alongside the hash
// for whoever is reading an annotation rather than computing one.
Version string
// contains filtered or unexported fields
}
Rules is one version of the inference policy, identified by the hash of the file it was loaded from.
func LoadRulesByHash ¶
LoadRulesByHash returns the rules with the given hash, for validating a definition generated against an earlier policy. A missing hash is an error rather than a fallback: silently validating against different rules than the ones used to generate would defeat the point of recording it.
func Stamp ¶
Stamp writes the inferred cache key into a SourceDefinition template and returns it alongside the hash of the rules used.
A key already present is accepted when it matches what inference produces and rejected when it does not. Accepting a match is what lets a definition round-trip: `vela def get` emits the stored template, key included, so rejecting any key at all would break get, edit, apply. Rejecting a mismatch rather than overwriting it keeps the author's belief about how their source is cached from being silently corrected.
So there is one rule for both the authored CUE and the applied object: the key equals what inference produces, and is written for you when absent.
It is idempotent - the generated key is not itself read when inferring - so re-stamping an already stamped template yields the same bytes. That matters because a re-apply of an unchanged definition must not show a diff.