Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoMatch = errors.New("field transformer: no match")
ErrNoMatch is returned by a FieldTransformer to signal that it does not apply to the given field path or value, so the processor should try the next registered transformer (if any) instead of treating it as an error.
Functions ¶
func NewPerVersionTranslators ¶
func NewPerVersionTranslators(scheme *runtime.Scheme, crd *apiextensionsv1.CustomResourceDefinition, crdVersion string, versions []string, opts ...TranslatorOption) (map[string]Translator, error)
NewPerVersionTranslators creates a set of translators indexed by SDK versions.
Given the following example resource:
apiVersion: atlas.generated.mongodb.com/v1
kind: SearchIndex
metadata:
name: search-index
spec:
v20250312:
...
v20250810:
In the above case crdVersion is "v1" and versions is ["v20250312", "v20250810"]. Optional opts ...TranslatorOption are applied to each translator created.
Types ¶
type FieldTransformer ¶
FieldTransformer transforms a field value found at fieldPath during translation (ToAPI or FromAPI). fieldPath is a dot-separated path from the root of the translated object map (e.g. "deleteAfterDate" or "foo.bar"), with array traversal represented by a trailing "[]" segment (e.g. "items[].deleteAfterDate").
Implementations must return ErrNoMatch (wrapped or not, checked via errors.Is) when they don't apply to this path/value, so that other registered transformers get a chance to run. Any other non-nil error aborts translation.
type Request
deprecated
type Request struct {
Translator Translator
Dependencies []client.Object
}
Request is deprecated do not use
Deprecated: request is no longer used in the ToAPI and FromAPI calls
type Translator ¶
type Translator interface {
// Scheme returns the Kubernetes scheme used to translate the CRD.
Scheme() *runtime.Scheme
// MajorVersion returns the pinned SDK major version
MajorVersion() string
// Mappings returns all the OpenAPi custom reference extensions, or an error
Mappings() ([]*refs.Mapping, error)
// ToAPI translates a source Kubernetes object into a target API structure.
// It uses the spec only to populate ethe API request, nothing from the status.
// The target is set to a API request struct to be filled.
// The source is set to the Kubernetes CR value. Only the spec data is used here.
// The request includes the translator and the dependencies associated with the
// source CR, usually Kubernetes secrets.
ToAPI(target any, source client.Object, objs ...client.Object) error
// FromAPI translates a source API structure into a Kubernetes object.
// The API source is used to populate the Kubernetes spec, including the
// spec.entry and status as well.
// The target is set to CR value to be filled. Both spec and status are filled.
// The source is set to API response.
// The request includes the translator and any dependencies associated with the
// source CR.
// Returns any extra objects extracted from the response as separate Kubernetes
// objects, such as Kubernetes secrets, for instance. This list does not include
// the mutated target, and will be empty if nothing else was extracted off the ç
// response.
FromAPI(target client.Object, source any, objs ...client.Object) ([]client.Object, error)
}
Translator allows to translate back and forth between a CRD schema and SDK API structures of a certain version. A translator is an immutable configuration object, it can be safely shared across goroutines
func NewTranslator ¶
func NewTranslator(scheme *runtime.Scheme, crd *apiextensionsv1.CustomResourceDefinition, crdVersion string, majorVersion string, opts ...TranslatorOption) (Translator, error)
NewTranslator creates a translator for a particular CRD version. It is also locked into a particular API majorVersion.
Given the following example resource:
apiVersion: atlas.generated.mongodb.com/v1 kind: SearchIndex metadata: name: search-index spec: v20250312:
In the above case crdVersion is "v1" and majorVersion is "v20250312".
type TranslatorOption ¶
type TranslatorOption func(*translator)
TranslatorOption configures optional behavior on a translator created via NewTranslator or NewPerVersionTranslators.
func WithFromAPIFieldTransformer ¶
func WithFromAPIFieldTransformer(fieldPathPattern string, fn FieldTransformer) TranslatorOption
WithFromAPIFieldTransformer registers fn to run during FromAPI for any field path matching fieldPathPattern. See WithToAPIFieldTransformer for matching and precedence semantics.
func WithToAPIFieldTransformer ¶
func WithToAPIFieldTransformer(fieldPathPattern string, fn FieldTransformer) TranslatorOption
WithToAPIFieldTransformer registers fn to run during ToAPI for any field path matching fieldPathPattern. fieldPathPattern is compiled as an anchored regular expression (^fieldPathPattern$). Multiple transformers can be registered; they run in registration order and the first one that does not return ErrNoMatch wins for that field.