@@ -38,7 +38,9 @@ type Translator interface {
3838 Mappings () ([]* refs.Mapping , error )
3939}
4040
41- // Request holds common parameters for all translation request
41+ // Request is deprecated do not use
42+ //
43+ // Deprecated: request is no longer used in the ToAPI and FromAPI calls
4244type Request struct {
4345 Translator Translator
4446 Dependencies []client.Object
@@ -47,13 +49,13 @@ type Request struct {
4749// APIImporter can translate itself into Kubernetes Objects.
4850// Use to customize or accelerate translations ad-hoc
4951type APIImporter [T any , P refs.PtrClientObj [T ]] interface {
50- FromAPI (tr * Request , target P ) ([]client.Object , error )
52+ FromAPI (tr Translator , target P , objs ... client. Object ) ([]client.Object , error )
5153}
5254
5355// APIExporter can translate itself to an API Object.
5456// Use to customize or accelerate translations ad-hoc
5557type APIExporter [T any ] interface {
56- ToAPI (tr * Request , target * T ) error
58+ ToAPI (tr Translator , target * T , objs ... client. Object ) error
5759}
5860
5961// ToAPI translates a source Kubernetes spec into a target API structure.
@@ -62,18 +64,18 @@ type APIExporter[T any] interface {
6264// The source is set to the Kubernetes CR value. Only the spec data is used here.
6365// The request includes the translator and the dependencies associated with the
6466// source CR, usually Kubernetes secrets.
65- func ToAPI [T any ](r * Request , target * T , source client.Object ) error {
67+ func ToAPI [T any ](tr Translator , target * T , source client. Object , objs ... client.Object ) error {
6668 exporter , ok := (source ).(APIExporter [T ])
6769 if ok {
68- return exporter .ToAPI (r , target )
70+ return exporter .ToAPI (tr , target )
6971 }
7072 unstructuredSrc , err := unstructured .ToUnstructured (source )
7173 if err != nil {
7274 return fmt .Errorf ("failed to convert k8s source value to unstructured: %w" , err )
7375 }
7476 targetUnstructured := map [string ]any {}
7577
76- if err := collapseReferences (r , unstructuredSrc , source ); err != nil {
78+ if err := collapseReferences (tr , unstructuredSrc , source , objs ); err != nil {
7779 return fmt .Errorf ("failed to process API mappings: %w" , err )
7880 }
7981
@@ -82,7 +84,7 @@ func ToAPI[T any](r *Request, target *T, source client.Object) error {
8284 return fmt .Errorf ("target must be a struct but got %v" , targetType .Kind ())
8385 }
8486
85- value , err := unstructured .GetField [map [string ]any ](unstructuredSrc , "spec" , r . Translator .MajorVersion ())
87+ value , err := unstructured .GetField [map [string ]any ](unstructuredSrc , "spec" , tr .MajorVersion ())
8688 if err != nil {
8789 return fmt .Errorf ("failed to access source spec value: %w" , err )
8890 }
@@ -108,10 +110,10 @@ func ToAPI[T any](r *Request, target *T, source client.Object) error {
108110// objects, such as Kubernetes secrets, for instance. This list does not include
109111// the mutated target, and will be empty if nothing else was extracted off the ç
110112// response.
111- func FromAPI [S any , T any , P refs.PtrClientObj [T ]](r * Request , target P , source * S ) ([]client.Object , error ) {
113+ func FromAPI [S any , T any , P refs.PtrClientObj [T ]](tr Translator , target P , source * S , objs ... client. Object ) ([]client.Object , error ) {
112114 importer , ok := any (source ).(APIImporter [T , P ])
113115 if ok {
114- return importer .FromAPI (r , target )
116+ return importer .FromAPI (tr , target , objs ... )
115117 }
116118 sourceUnstructured , err := unstructured .ToUnstructured (source )
117119 if err != nil {
@@ -124,12 +126,12 @@ func FromAPI[S any, T any, P refs.PtrClientObj[T]](r *Request, target P, source
124126 }
125127
126128 versionedSpec , err := unstructured .GetOrCreateField (
127- targetUnstructured , map [string ]any {}, "spec" , r . Translator .MajorVersion ())
129+ targetUnstructured , map [string ]any {}, "spec" , tr .MajorVersion ())
128130 if err != nil {
129131 return nil , fmt .Errorf ("failed to ensure versioned spec object in unstructured target: %w" , err )
130132 }
131133 versionedStatus , err := unstructured .GetOrCreateField (
132- targetUnstructured , map [string ]any {}, "status" , r . Translator .MajorVersion ())
134+ targetUnstructured , map [string ]any {}, "status" , tr .MajorVersion ())
133135 if err != nil {
134136 return nil , fmt .Errorf ("failed to create versioned status object in unstructured target: %w" , err )
135137 }
@@ -140,7 +142,7 @@ func FromAPI[S any, T any, P refs.PtrClientObj[T]](r *Request, target P, source
140142 versionedSpec ["entry" ] = versionedSpecEntry
141143 unstructured .CopyFields (versionedStatus , sourceUnstructured )
142144
143- extraObjects , err := expandReferences (r , targetUnstructured , target )
145+ extraObjects , err := expandReferences (tr , targetUnstructured , target , objs )
144146 if err != nil {
145147 return nil , fmt .Errorf ("failed to process API mappings: %w" , err )
146148 }
@@ -153,23 +155,23 @@ func FromAPI[S any, T any, P refs.PtrClientObj[T]](r *Request, target P, source
153155// collapseReferences finds all Kubernetes references, solves them and collapses
154156// them by replacing their values from the reference (e.g Kubernetes secret or
155157// group), into the corresponding API request value
156- func collapseReferences (r * Request , req map [string ]any , main client.Object ) error {
157- mappings , err := r . Translator .Mappings ()
158+ func collapseReferences (tr Translator , req map [string ]any , main client. Object , objs [] client.Object ) error {
159+ mappings , err := tr .Mappings ()
158160 if err != nil {
159161 return fmt .Errorf ("failed to extract mappings to collapse: %w" , err )
160162 }
161- return refs .CollapseAll (mappings , main , r . Dependencies , req )
163+ return refs .CollapseAll (mappings , main , objs , req )
162164}
163165
164166// expandReferences finds all API fields that must be referenced, and expand
165167// such reference, moving the value (e.g. sensitive field or group id) to a
166168// referenced Kubernetes object (e.g. Kubernetes secret or Atlas Group)
167- func expandReferences (r * Request , cr map [string ]any , main client.Object ) ([]client.Object , error ) {
168- mappings , err := r . Translator .Mappings ()
169+ func expandReferences (tr Translator , cr map [string ]any , main client. Object , objs [] client.Object ) ([]client.Object , error ) {
170+ mappings , err := tr .Mappings ()
169171 if err != nil {
170172 return nil , fmt .Errorf ("failed to extract mappings to expand: %w" , err )
171173 }
172- return refs .ExpandAll (mappings , main , r . Dependencies , cr )
174+ return refs .ExpandAll (mappings , main , objs , cr )
173175}
174176
175177func propertyValueOrNil (schema * openapi3.Schema , propertyName string ) * openapi3.Schema {
0 commit comments