From 60c2651e15d2f065d295056dcd6713432d67f742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 31 Aug 2023 23:57:02 +0200 Subject: [PATCH] Add ResourcePreProcessor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- pkg/migration/interfaces.go | 10 ++++++++++ pkg/migration/plan_generator.go | 7 +++++++ pkg/migration/registry.go | 14 ++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/pkg/migration/interfaces.go b/pkg/migration/interfaces.go index ee5d1d30..a9aff16d 100644 --- a/pkg/migration/interfaces.go +++ b/pkg/migration/interfaces.go @@ -162,6 +162,16 @@ type UnstructuredPreProcessor interface { PreProcess(u UnstructuredWithMetadata) error } +// ManagedPreProcessor allows manifests read by the Source +// to be pre-processed before the converters are run. +// These pre-processors will work for GVKs that have ResourceConverter +// registered. +type ManagedPreProcessor interface { + // ResourcePreProcessor is called for a manifest read by the Source + // before any converters are run. + ResourcePreProcessor(mg resource.Managed) error +} + // CategoricalConverter is a converter that converts resources of a given // Category. Because it receives an unstructured argument, it should be // used for implementing generic conversion functions acting on a specific diff --git a/pkg/migration/plan_generator.go b/pkg/migration/plan_generator.go index c03c7097..aa629656 100644 --- a/pkg/migration/plan_generator.go +++ b/pkg/migration/plan_generator.go @@ -407,6 +407,13 @@ func (pg *PlanGenerator) convertResource(o UnstructuredWithMetadata, composition if err != nil { return nil, false, errors.Wrap(err, errResourceMigrate) } + if pg.registry.resourcePreProcessors != nil { + for _, pp := range pg.registry.resourcePreProcessors { + if err = pp.ResourcePreProcessor(mg); err != nil { + return nil, false, errors.Wrap(err, errResourceMigrate) + } + } + } resources, err := conv.Resource(mg) if err != nil { return nil, false, errors.Wrap(err, errResourceMigrate) diff --git a/pkg/migration/registry.go b/pkg/migration/registry.go index b88a7b27..51c582df 100644 --- a/pkg/migration/registry.go +++ b/pkg/migration/registry.go @@ -97,6 +97,7 @@ type packageLockConverter struct { // runtime.Scheme with which the corresponding types are registered. type Registry struct { unstructuredPreProcessors map[Category][]UnstructuredPreProcessor + resourcePreProcessors []ManagedPreProcessor resourceConverters map[schema.GroupVersionKind]ResourceConverter templateConverters map[schema.GroupVersionKind]ComposedTemplateConverter patchSetConverters []patchSetConverter @@ -536,3 +537,16 @@ func (pp PreProcessor) PreProcess(u UnstructuredWithMetadata) error { } return pp(u) } + +func (r *Registry) RegisterResourcePreProcessor(pp ManagedPreProcessor) { + r.resourcePreProcessors = append(r.resourcePreProcessors, pp) +} + +type ResourcePreProcessor func(mg resource.Managed) error + +func (pp ResourcePreProcessor) ResourcePreProcessor(mg resource.Managed) error { + if pp == nil { + return nil + } + return pp(mg) +}