github.com/crossplane/upjet@v1.3.0/pkg/migration/interfaces.go (about) 1 // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package migration 6 7 import ( 8 "github.com/crossplane/crossplane-runtime/pkg/resource" 9 xpv1 "github.com/crossplane/crossplane/apis/apiextensions/v1" 10 xpmetav1 "github.com/crossplane/crossplane/apis/pkg/meta/v1" 11 xpmetav1alpha1 "github.com/crossplane/crossplane/apis/pkg/meta/v1alpha1" 12 xppkgv1 "github.com/crossplane/crossplane/apis/pkg/v1" 13 xppkgv1beta1 "github.com/crossplane/crossplane/apis/pkg/v1beta1" 14 ) 15 16 // ResourceConverter converts a managed resource from 17 // the migration source provider's schema to the migration target 18 // provider's schema. 19 type ResourceConverter interface { 20 // Resource takes a managed resource and returns zero or more managed 21 // resources to be created. 22 Resource(mg resource.Managed) ([]resource.Managed, error) 23 } 24 25 // ComposedTemplateConverter converts a Composition's ComposedTemplate 26 // from the migration source provider's schema to the migration target 27 // provider's schema. Conversion of the `Base` must be handled by 28 // a ResourceConverter. 29 type ComposedTemplateConverter interface { 30 // ComposedTemplate receives a migration source v1.ComposedTemplate 31 // that has been converted, by a resource converter, to the 32 // v1.ComposedTemplates with the new shapes specified in the 33 // `convertedTemplates` argument. 34 // Conversion of the v1.ComposedTemplate.Bases is handled 35 // via ResourceConverter.Resource and ComposedTemplate must only 36 // convert the other fields (`Patches`, `ConnectionDetails`, 37 // `PatchSet`s, etc.) 38 // Returns any errors encountered. 39 ComposedTemplate(sourceTemplate xpv1.ComposedTemplate, convertedTemplates ...*xpv1.ComposedTemplate) error 40 } 41 42 // CompositionConverter converts a managed resource and a Composition's 43 // ComposedTemplate that composes a managed resource of the same kind 44 // from the migration source provider's schema to the migration target 45 // provider's schema. 46 type CompositionConverter interface { 47 ResourceConverter 48 ComposedTemplateConverter 49 } 50 51 // PatchSetConverter converts patch sets of Compositions. 52 // Any registered PatchSetConverters 53 // will be called before any resource or ComposedTemplate conversion is done. 54 // The rationale is to convert the Composition-wide patch sets before 55 // any resource-specific conversions so that migration targets can 56 // automatically inherit converted patch sets if their schemas match them. 57 // Registered PatchSetConverters will be called in the order 58 // they are registered. 59 type PatchSetConverter interface { 60 // PatchSets converts the `spec.patchSets` of a Composition 61 // from the migration source provider's schema to the migration target 62 // provider's schema. 63 PatchSets(psMap map[string]*xpv1.PatchSet) error 64 } 65 66 // ConfigurationMetadataConverter converts a Crossplane Configuration's metadata. 67 type ConfigurationMetadataConverter interface { 68 // ConfigurationMetadataV1 takes a Crossplane Configuration v1 metadata, 69 // converts it, and stores the converted metadata in its argument. 70 // Returns any errors encountered during the conversion. 71 ConfigurationMetadataV1(configuration *xpmetav1.Configuration) error 72 // ConfigurationMetadataV1Alpha1 takes a Crossplane Configuration v1alpha1 73 // metadata, converts it, and stores the converted metadata in its 74 // argument. Returns any errors encountered during the conversion. 75 ConfigurationMetadataV1Alpha1(configuration *xpmetav1alpha1.Configuration) error 76 } 77 78 // ConfigurationPackageConverter converts a Crossplane configuration package. 79 type ConfigurationPackageConverter interface { 80 // ConfigurationPackageV1 takes a Crossplane Configuration v1 package, 81 // converts it possibly to multiple packages and returns 82 // the converted configuration package. 83 // Returns any errors encountered during the conversion. 84 ConfigurationPackageV1(pkg *xppkgv1.Configuration) error 85 } 86 87 // ProviderPackageConverter converts a Crossplane provider package. 88 type ProviderPackageConverter interface { 89 // ProviderPackageV1 takes a Crossplane Provider v1 package, 90 // converts it possibly to multiple packages and returns the 91 // converted provider packages. 92 // Returns any errors encountered during the conversion. 93 ProviderPackageV1(pkg xppkgv1.Provider) ([]xppkgv1.Provider, error) 94 } 95 96 // PackageLockConverter converts a Crossplane package lock. 97 type PackageLockConverter interface { 98 // PackageLockV1Beta1 takes a Crossplane v1beta1 package lock, 99 // converts it, and stores the converted lock in its argument. 100 // Returns any errors encountered during the conversion. 101 PackageLockV1Beta1(lock *xppkgv1beta1.Lock) error 102 } 103 104 // Source is a source for reading resource manifests 105 type Source interface { 106 // HasNext returns `true` if the Source implementation has a next manifest 107 // available to return with a call to Next. Any errors encountered while 108 // determining whether a next manifest exists will also be reported. 109 HasNext() (bool, error) 110 // Next returns the next resource manifest available or 111 // any errors encountered while reading the next resource manifest. 112 Next() (UnstructuredWithMetadata, error) 113 // Reset resets the Source so that it can read the manifests 114 // from the beginning. There is no guarantee that the Source 115 // will return the same set of manifests or it will return 116 // them in the same order after a reset. 117 Reset() error 118 } 119 120 // Target is a target where resource manifests can be manipulated 121 // (e.g., added, deleted, patched, etc.) 122 type Target interface { 123 // Put writes a resource manifest to this Target 124 Put(o UnstructuredWithMetadata) error 125 // Delete deletes a resource manifest from this Target 126 Delete(o UnstructuredWithMetadata) error 127 } 128 129 // Executor is a migration plan executor. 130 type Executor interface { 131 // Init initializes an executor using the supplied executor specific 132 // configuration data. 133 Init(config map[string]any) error 134 // Step asks the executor to execute the next step passing any available 135 // context from the previous step, and returns any new context to be passed 136 // to the next step if there exists one. 137 Step(s Step, ctx map[string]any) error 138 // Destroy is called when all the steps have been executed, 139 // or a step has returned an error, and we would like to stop 140 // executing the plan. 141 Destroy() error 142 } 143 144 // UnstructuredPreProcessor allows manifests read by the Source 145 // to be pre-processed before the converters are run 146 // It's not possible to do any conversions via the pre-processors, 147 // and they only allow migrators to extract information from 148 // the manifests read by the Source before any converters are run. 149 type UnstructuredPreProcessor interface { 150 // PreProcess is called for a manifest read by the Source 151 // before any converters are run. 152 PreProcess(u UnstructuredWithMetadata) error 153 } 154 155 // ManagedPreProcessor allows manifests read by the Source 156 // to be pre-processed before the converters are run. 157 // These pre-processors will work for GVKs that have ResourceConverter 158 // registered. 159 type ManagedPreProcessor interface { 160 // ResourcePreProcessor is called for a manifest read by the Source 161 // before any converters are run. 162 ResourcePreProcessor(mg resource.Managed) error 163 } 164 165 // CategoricalConverter is a converter that converts resources of a given 166 // Category. Because it receives an unstructured argument, it should be 167 // used for implementing generic conversion functions acting on a specific 168 // category, such as setting a deletion policy on all the managed resources 169 // observed by the migration Source. 170 type CategoricalConverter interface { 171 Convert(u *UnstructuredWithMetadata) error 172 }