Skip to content

Commit

Permalink
Move the provider specific functions to extensions-migration
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Aug 10, 2023
1 parent 2309a95 commit 3d06353
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 66 deletions.
31 changes: 1 addition & 30 deletions pkg/migration/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
45 changes: 9 additions & 36 deletions pkg/migration/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 3d06353

Please sign in to comment.