Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some common utility functions to migration framework #251

Merged
merged 3 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 40 additions & 0 deletions pkg/migration/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,43 @@ func toPackageLock(u unstructured.Unstructured) (*xppkgv1beta1.Lock, error) {
}
return lock, 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 {
switch p.Type { // nolint:exhaustive
case xpv1.PatchTypeFromCompositeFieldPath, xpv1.PatchTypeCombineFromComposite, "":
{
if p.ToFieldPath != nil {
if to, ok := conversionMap[*p.ToFieldPath]; ok {
patchesToAdd = append(patchesToAdd, xpv1.Patch{
Type: p.Type,
FromFieldPath: p.FromFieldPath,
ToFieldPath: &to,
Transforms: p.Transforms,
Policy: p.Policy,
Combine: p.Combine,
})
}
}
}
case xpv1.PatchTypeToCompositeFieldPath, xpv1.PatchTypeCombineToComposite:
{
if p.FromFieldPath != nil {
if to, ok := conversionMap[*p.FromFieldPath]; ok {
patchesToAdd = append(patchesToAdd, xpv1.Patch{
Type: p.Type,
FromFieldPath: &to,
ToFieldPath: p.ToFieldPath,
Transforms: p.Transforms,
Policy: p.Policy,
Combine: p.Combine,
})
}
}
}
}
}
return patchesToAdd
}
22 changes: 22 additions & 0 deletions pkg/migration/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,28 @@ func (d *delegatingConverter) ComposedTemplate(sourceTemplate xpv1.ComposedTempl
return d.cmpFn(sourceTemplate, convertedTemplates...)
}

// DefaultCompositionConverter is a generic composition converter
// conversionMap: is fieldpath map for conversion
sergenyalcin marked this conversation as resolved.
Show resolved Hide resolved
// 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
for _, fn := range fns {
patches, err := fn(sourceTemplate)
if err != nil {
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 {
convertedTemplates[i].Patches = append(convertedTemplates[i].Patches, patchesToAdd...)
}
return nil
}
}

// RegisterAPIConversionFunctions registers the supplied ResourceConversionFn and
// ComposedTemplateConversionFn for the specified GVK, and the supplied
// PatchSetsConversionFn for all the discovered Compositions.
Expand Down
Loading