Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/config/conversion/list_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ type ConvertOptions struct {
// 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 Convert(params map[string]any, paths []string, mode ListConversionMode, opts *ConvertOptions) (map[string]any, error) { //nolint:gocyclo // easier to follow as a unit
func Convert(params map[string]any, p []string, mode ListConversionMode, opts *ConvertOptions) (map[string]any, error) { //nolint:gocyclo // easier to follow as a unit
// make a copy of p to prevent races on the configured conversion paths.
paths := append([]string(nil), p...)
switch mode {
case ToSingletonList:
slices.Sort(paths)
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/conversion/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (r *registry) RoundTrip(dst, src resource.Terraformed) error { //nolint:goc
}

// first PrioritizedManagedConversions are run in their registration order
r.logger.Debug("Running the registered PrioritizedManagedConversions.")
for _, c := range r.GetConversions(dst) {
if pc, ok := c.(conversion.PrioritizedManagedConversion); ok {
if _, err := pc.ConvertManaged(src, dst); err != nil {
Expand All @@ -65,6 +66,7 @@ func (r *registry) RoundTrip(dst, src resource.Terraformed) error { //nolint:goc
srcPaved := fieldpath.Pave(srcMap)
dstPaved := fieldpath.Pave(dstMap)
// then run the PavedConversions
r.logger.Debug("Running the registered PavedConversions.")
for _, c := range r.GetConversions(dst) {
if pc, ok := c.(conversion.PavedConversion); ok {
if _, err := pc.ConvertPaved(srcPaved, dstPaved); err != nil {
Expand All @@ -79,6 +81,7 @@ func (r *registry) RoundTrip(dst, src resource.Terraformed) error { //nolint:goc
}

// finally at the third stage, run the ManagedConverters
r.logger.Debug("Running the registered ManagedConverters.")
for _, c := range r.GetConversions(dst) {
if tc, ok := c.(conversion.ManagedConversion); ok {
if _, ok := tc.(conversion.PrioritizedManagedConversion); ok {
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/conversion/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
xpresource "github.com/crossplane/crossplane-runtime/v2/pkg/resource"
"github.com/crossplane/crossplane-runtime/v2/pkg/test"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -169,6 +170,7 @@ func TestRoundTrip(t *testing.T) {
}
r := &registry{
scheme: s,
logger: logging.NewNopLogger(),
}
if err := r.RegisterConversions(p, nil); err != nil {
t.Fatalf("\n%s\nRegisterConversions(p): Failed to register the conversions with the registry.\n", tc.reason)
Expand Down
18 changes: 17 additions & 1 deletion pkg/controller/conversion/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package conversion

import (
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"

Expand All @@ -24,6 +25,17 @@ type registry struct {
providerCluster *config.Provider
providerNamespaced *config.Provider
scheme *runtime.Scheme
logger logging.Logger
}

// RegistryOption sets an option for the conversion registry.
type RegistryOption func(*registry)

// WithLogger configures the logger to be used with the conversion registry.
func WithLogger(logger logging.Logger) RegistryOption {
return func(r *registry) {
r.logger = logger
}
}

// RegisterConversions registers the API version conversions from the specified
Expand Down Expand Up @@ -65,12 +77,16 @@ func GetConversions(tr resource.Terraformed) []conversion.Conversion {
// for the types whose versions are to be converted. If a registration for a
// Go schema is not found in the specified registry, RoundTrip does not error
// but only wildcard conversions must be used with the registry.
func RegisterConversions(providerCluster, providerNamespaced *config.Provider, scheme *runtime.Scheme) error {
func RegisterConversions(providerCluster, providerNamespaced *config.Provider, scheme *runtime.Scheme, opts ...RegistryOption) error {
if instance != nil {
return errors.New(errAlreadyRegistered)
}
instance = &registry{
scheme: scheme,
logger: logging.NewNopLogger(),
}
for _, o := range opts {
o(instance)
}
return instance.RegisterConversions(providerCluster, providerNamespaced)
}
Loading