diff --git a/pkg/migration/converter.go b/pkg/migration/converter.go index 6d5cf7e7..914dfd14 100644 --- a/pkg/migration/converter.go +++ b/pkg/migration/converter.go @@ -15,9 +15,6 @@ package migration import ( - "fmt" - "strings" - "github.com/crossplane/crossplane-runtime/pkg/fieldpath" "github.com/crossplane/crossplane-runtime/pkg/resource" xpv1 "github.com/crossplane/crossplane/apis/apiextensions/v1" @@ -263,33 +260,7 @@ func toPackageLock(u unstructured.Unstructured) (*xppkgv1beta1.Lock, error) { return lock, nil } -func ConvertComposedTemplateTags(sourceTemplate xpv1.ComposedTemplate, value string, key string) ([]xpv1.Patch, error) { - var patchesToAdd []xpv1.Patch - for _, p := range sourceTemplate.Patches { - if p.ToFieldPath != nil { - if strings.HasPrefix(*p.ToFieldPath, "spec.forProvider.tags") { - u, err := FromRawExtension(sourceTemplate.Base) - if err != nil { - return nil, errors.Wrap(err, "failed to convert ComposedTemplate") - } - paved := fieldpath.Pave(u.Object) - key, err := paved.GetString(strings.ReplaceAll(*p.ToFieldPath, value, key)) - if err != nil { - return nil, errors.Wrap(err, "failed to get value from paved") - } - s := fmt.Sprintf(`spec.forProvider.tags["%s"]`, key) - patchesToAdd = append(patchesToAdd, xpv1.Patch{ - FromFieldPath: p.FromFieldPath, - ToFieldPath: &s, - Transforms: p.Transforms, - Policy: p.Policy, - }) - } - } - } - return patchesToAdd, nil -} - +// ConvertComposedTemplatePatchesMap converts the composed templates with given conversion map func ConvertComposedTemplatePatchesMap(sourceTemplate xpv1.ComposedTemplate, conversionMap map[string]string) []xpv1.Patch { var patchesToAdd []xpv1.Patch for _, p := range sourceTemplate.Patches { diff --git a/pkg/migration/registry.go b/pkg/migration/registry.go index b716cd99..c9994227 100644 --- a/pkg/migration/registry.go +++ b/pkg/migration/registry.go @@ -15,9 +15,7 @@ package migration import ( - "fmt" "regexp" - "strings" "github.com/crossplane/crossplane-runtime/pkg/resource" xpv1 "github.com/crossplane/crossplane/apis/apiextensions/v1" @@ -454,35 +452,6 @@ func (d *delegatingConverter) PatchSets(psMap map[string]*xpv1.PatchSet) error { return d.psFn(psMap) } -func DefaultPatchSetsConverter(sourcePatchSets map[string]*xpv1.PatchSet) error { - tagsPatchSetName := "" - for _, patchSet := range sourcePatchSets { - for _, patch := range patchSet.Patches { - if patch.ToFieldPath != nil { - if strings.HasPrefix(*patch.ToFieldPath, "spec.forProvider.tags") { - tagsPatchSetName = patchSet.Name - break - } - } - } - if tagsPatchSetName != "" { - break - } - } - - tPs := sourcePatchSets[tagsPatchSetName] - if tPs == nil { - return nil - } - for i, p := range tPs.Patches { - r := strings.NewReplacer("metadata.labels[", "", "]", "") - key := r.Replace(*p.FromFieldPath) - *tPs.Patches[i].ToFieldPath = fmt.Sprintf(`spec.forProvider.tags[%s]`, key) - } - // convert patch sets in the source - return nil -} - // Resource takes a managed resource and returns zero or more managed // resources to be created by calling the configured ResourceConversionFn. func (d *delegatingConverter) Resource(mg resource.Managed) ([]resource.Managed, error) { @@ -502,15 +471,19 @@ func (d *delegatingConverter) ComposedTemplate(sourceTemplate xpv1.ComposedTempl return d.cmpFn(sourceTemplate, convertedTemplates...) } -func DefaultCompositionConverter(convertTags bool, conversionMap map[string]string) ComposedTemplateConversionFn { +// DefaultCompositionConverter is a generic composition converter +// conversionMap: is fieldpath map for conversion +// Example: "spec.forProvider.assumeRolePolicyDocument": "spec.forProvider.assumeRolePolicy", +// fns are functions that manipulate the patchsets +func DefaultCompositionConverter(conversionMap map[string]string, fns ...func(sourceTemplate xpv1.ComposedTemplate) ([]xpv1.Patch, error)) ComposedTemplateConversionFn { return func(sourceTemplate xpv1.ComposedTemplate, convertedTemplates ...*xpv1.ComposedTemplate) error { var patchesToAdd []xpv1.Patch - var err error - if convertTags { - patchesToAdd, err = ConvertComposedTemplateTags(sourceTemplate, ".value", ".key") + for _, fn := range fns { + patches, err := fn(sourceTemplate) if err != nil { - return errors.Wrap(err, "failed to convert tags") + return errors.Wrap(err, "cannot run the patch sets converter function") } + patchesToAdd = append(patchesToAdd, patches...) } patchesToAdd = append(patchesToAdd, ConvertComposedTemplatePatchesMap(sourceTemplate, conversionMap)...) for i := range convertedTemplates {