Skip to content

Commit

Permalink
Merge pull request #267 from sergenyalcin/resource-pre-processor
Browse files Browse the repository at this point in the history
Adding ResourcePreProcessor interface
  • Loading branch information
sergenyalcin authored Sep 5, 2023
2 parents df18f12 + 60c2651 commit ac6f4d3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/migration/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/migration/plan_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions pkg/migration/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit ac6f4d3

Please sign in to comment.