conversion

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2026 License: Apache-2.0 Imports: 12 Imported by: 12

Documentation

Index

Constants

View Source
const (
	// AllVersions denotes that a Conversion is applicable for all versions
	// of an API with which the Conversion is registered. It can be used for
	// both the conversion source or target API versions.
	AllVersions = "*"

	// AnnotationKey is the annotation key used for storing field
	// conversion values as a JSON map during API version conversions.
	AnnotationKey = "internal.upjet.crossplane.io/field-conversions"
)

Variables

This section is empty.

Functions

func Convert

func Convert(params map[string]any, p []string, mode ListConversionMode, opts *ConvertOptions) (map[string]any, error)

Convert performs conversion between singleton lists and embedded objects while passing the CRD parameters to the Terraform layer and while reading state from the Terraform layer at runtime. The paths where the conversion will be performed are specified using paths and the conversion mode (whether an embedded object will be converted into a singleton list or a singleton list will be converted into an embedded object) is determined by the mode parameter.

func DefaultPathPrefixes

func DefaultPathPrefixes() []string

DefaultPathPrefixes returns the list of the default path prefixes for excluding paths in the identity conversion. The returned value is ["spec.forProvider", "spec.initProvider", "status.atProvider"].

func ExpandParameters

func ExpandParameters(prefixes []string, excludePaths ...string) []string

ExpandParameters sorts and expands the given list of field path suffixes with the given prefixes.

Types

type Conversion

type Conversion interface {
	// Applicable should return true if this Conversion is applicable while
	// converting the API of the `src` object to the API of the `dst` object.
	Applicable(src, dst runtime.Object) bool
}

Conversion is the interface for the CRD API version converters. Conversion implementations registered for a source, target pair are called in chain so Conversion implementations can be modular, e.g., a Conversion implementation registered for a specific source and target versions does not have to contain all the needed API conversions between these two versions. All PavedConversions are run in their registration order before the ManagedConversions. Conversions are run in three stages:

  1. PrioritizedManagedConversions are run.
  2. The source and destination objects are paved and the PavedConversions are run in chain without unpaving the unstructured representation between conversions.
  3. The destination paved object is converted back to a managed resource and ManagedConversions are run in the order they are registered.

func NewCustomConverter

func NewCustomConverter(sourceVersion, targetVersion string, converter func(src, target resource.Managed) error) Conversion

NewCustomConverter returns a new Conversion from the specified `sourceVersion` of an API to the specified `targetVersion` and invokes the specified converter function to perform the conversion on the managed resources.

func NewFieldRenameConversion

func NewFieldRenameConversion(sourceVersion, sourceField, targetVersion, targetField string) Conversion

NewFieldRenameConversion returns a new Conversion that implements a field renaming conversion from the specified `sourceVersion` to the specified `targetVersion` of an API. The field's name in the `sourceVersion` is given with the `sourceField` parameter and its name in the `targetVersion` is given with `targetField` parameter.

func NewFieldTypeConversion added in v2.3.0

func NewFieldTypeConversion(sourceVersion, targetVersion, fieldPath string, mode TypeConversionMode) Conversion

NewFieldTypeConversion returns a new Conversion that handles type changes of fields between API versions. It converts primitive types according to the given conversion mode. Type assumptions for upjet: - Integers are represented as int64 (though JSON unmarshaling may produce float64) - Floating-point numbers are represented as float64 - Booleans are represented as bool - Strings are represented as string

Supported conversions: IntToString/StringToInt: int64 ↔ string FloatToString/StringToFloat: float64 ↔ string BoolToString/StringToBool: bool ↔ string

func NewIdentityConversionExpandPaths

func NewIdentityConversionExpandPaths(sourceVersion, targetVersion string, pathPrefixes []string, excludePaths ...string) Conversion

NewIdentityConversionExpandPaths returns a new Conversion from the specified sourceVersion of an API to the specified targetVersion, which copies the identical paths from the source to the target. excludePaths can be used to ignore certain field paths while copying. Exclude paths must be specified in standard crossplane-runtime fieldpath library syntax, i.e., with proper indices for traversing map and slice types (e.g., a.b[*].c). The field paths in excludePaths are sorted in lexical order and are prefixed with each of the path prefixes specified with pathPrefixes. So if an exclude path "x" is specified with the prefix slice ["a", "b"], then paths a.x and b.x will both be skipped while copying fields from a source to a target.

func NewNewlyIntroducedFieldConversion added in v2.3.0

func NewNewlyIntroducedFieldConversion(sourceVersion, targetVersion, fieldPath string, mode NewlyIntroducedFieldConversionMode) Conversion

NewNewlyIntroducedFieldConversion returns a new Conversion that handles newly introduced fields that exist in some API versions but not others. When converting from a version that has the field to one that doesn't (ToAnnotation mode), the field value is stored in an annotation. When converting from a version without the field to one that has it (FromAnnotation mode), the field value is restored from the annotation.

func NewSingletonListConversion

func NewSingletonListConversion(sourceVersion, targetVersion string, pathPrefixes []string, crdPaths []string, mode ListConversionMode, opts ...SingletonListConversionOption) Conversion

NewSingletonListConversion returns a new Conversion from the specified sourceVersion of an API to the specified targetVersion and uses the CRD field paths given in crdPaths to convert between the singleton lists and embedded objects in the given conversion mode.

type ConvertOptions

type ConvertOptions struct {
	// ListInjectKeys is used to inject a key with a default value into the
	// singleton list for a given path.
	ListInjectKeys map[string]SingletonListInjectKey
}

type ListConversionMode

type ListConversionMode int

ListConversionMode denotes the mode of the list-object API conversion, e.g., conversion of embedded objects into singleton lists.

const (
	// ToEmbeddedObject represents a runtime conversion from a singleton list
	// to an embedded object, i.e., the runtime conversions needed while
	// reading from the Terraform state and updating the CRD
	// (for status, late-initialization, etc.)
	ToEmbeddedObject ListConversionMode = iota
	// ToSingletonList represents a runtime conversion from an embedded object
	// to a singleton list, i.e., the runtime conversions needed while passing
	// the configuration data to the underlying Terraform layer.
	ToSingletonList
)

func (ListConversionMode) String

func (m ListConversionMode) String() string

String returns a string representation of the conversion mode.

type ManagedConversion

type ManagedConversion interface {
	Conversion
	// ConvertManaged converts from the `src` managed resource to the `dst`
	// managed resource and returns `true` if the conversion has been done,
	// `false` otherwise, together with any errors encountered.
	ConvertManaged(src, target resource.Managed) (bool, error)
}

ManagedConversion defines a Conversion from a specific source resource.Managed type to a target one. Generic Conversion implementations may prefer to implement the PavedConversion interface. Implementations of ManagedConversion can do type assertions to specific source and target types, and so, they are expected to be strongly typed.

type NewlyIntroducedFieldConversionMode added in v2.3.0

type NewlyIntroducedFieldConversionMode int

NewlyIntroducedFieldConversionMode denotes the mode of new field conversion.

const (
	// ToAnnotation converts a field value to an annotation when the field
	// does not exist in the target API version (newer → older version).
	ToAnnotation NewlyIntroducedFieldConversionMode = iota
	// FromAnnotation converts an annotation value back to a field when
	// the field exists in the target API version (older → newer version).
	FromAnnotation
)

func (NewlyIntroducedFieldConversionMode) String added in v2.3.0

String returns a string representation of the new field conversion mode.

type PavedConversion

type PavedConversion interface {
	Conversion
	// ConvertPaved converts from the `src` paved object to the `dst`
	// paved object and returns `true` if the conversion has been done,
	// `false` otherwise, together with any errors encountered.
	ConvertPaved(src, target *fieldpath.Paved) (bool, error)
}

PavedConversion is an optimized Conversion between two fieldpath.Paved objects. PavedConversion implementations for a specific source and target version pair are chained together and the source and the destination objects are paved once at the beginning of the chained PavedConversion.ConvertPaved calls. The target fieldpath.Paved object is then converted into the original resource.Terraformed object at the end of the chained calls. This prevents the intermediate conversions between fieldpath.Paved and the resource.Terraformed representations of the same object, and the fieldpath.Paved representation is convenient for writing generic Conversion implementations not bound to a specific type.

type PrioritizedManagedConversion

type PrioritizedManagedConversion interface {
	ManagedConversion
	Prioritized()
}

PrioritizedManagedConversion is a ManagedConversion that take precedence over all the other converters. PrioritizedManagedConversions are run, in their registration order, before the PavedConversions.

type SingletonListConversionOption

type SingletonListConversionOption func(*singletonListConverter)

func WithConvertOptions

func WithConvertOptions(opts *ConvertOptions) SingletonListConversionOption

WithConvertOptions sets the ConvertOptions for the singleton list conversion.

type SingletonListInjectKey

type SingletonListInjectKey struct {
	Key   string
	Value string
}

type TypeConversionMode added in v2.3.0

type TypeConversionMode int

TypeConversionMode denotes the mode of type conversion between different primitive types in API versions.

const (
	// IntToString converts integer values to string representation
	IntToString TypeConversionMode = iota
	// StringToInt converts string values to integer representation
	StringToInt
	// BoolToString converts boolean values to string representation ("true"/"false")
	BoolToString
	// StringToBool converts string values to boolean representation
	StringToBool
	// FloatToString converts float values to string representation
	FloatToString
	// StringToFloat converts string values to float representation
	StringToFloat
)

func (TypeConversionMode) String added in v2.3.0

func (m TypeConversionMode) String() string

String returns a string representation of the type conversion mode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL