1313// limitations under the License.
1414//
1515
16- package translate
16+ package refs
1717
1818import (
1919 "errors"
2020 "fmt"
2121
22- "github.com/stretchr/testify/assert/yaml"
2322 "sigs.k8s.io/controller-runtime/pkg/client"
2423
2524 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/autogen/translate/unstructured"
@@ -31,102 +30,19 @@ const (
3130 SecretProperySelector = "$.data.#"
3231)
3332
34- type mapContext struct {
35- main client.Object
36- m map [client.ObjectKey ]client.Object
37- added []client.Object
38- }
39-
40- func newMapContext (main client.Object , deps []client.Object ) * mapContext {
41- m := map [client.ObjectKey ]client.Object {}
42- for _ , obj := range deps {
43- m [client .ObjectKeyFromObject (obj )] = obj
44- }
45- return & mapContext {main : main , m : m }
46- }
47-
48- func (mc * mapContext ) find (name string ) client.Object {
49- key := client.ObjectKey {Name : name , Namespace : mc .main .GetNamespace ()}
50- return mc .m [key ]
51- }
52-
53- func (mc * mapContext ) has (name string ) bool {
54- return mc .find (name ) != nil
55- }
56-
57- func (mc * mapContext ) add (obj client.Object ) {
58- mc .m [client .ObjectKeyFromObject (obj )] = obj
59- mc .added = append (mc .added , obj )
60- }
61-
62- type mapper struct {
63- * mapContext
33+ // Handler hodls the context needed to expand or collapse the references on an
34+ // Kubernetes object translaion to and from API data
35+ type Handler struct {
36+ * context
6437 expand bool
6538}
6639
67- func newExpanderMapper (main client.Object , deps []client.Object ) * mapper {
68- return newMapper (true , main , deps )
69- }
70-
71- func newCollarserMapper (main client.Object , deps []client.Object ) * mapper {
72- return newMapper (false , main , deps )
40+ func NewHandler (main client.Object , deps []client.Object ) * Handler {
41+ return & Handler {context : newMapContext (main , deps )}
7342}
7443
75- func newMapper (expand bool , main client.Object , deps []client.Object ) * mapper {
76- return & mapper {
77- mapContext : newMapContext (main , deps ),
78- expand : expand ,
79- }
80- }
81-
82- func ExpandMappings (r * Request , obj map [string ]any , main client.Object ) ([]client.Object , error ) {
83- em := newExpanderMapper (main , r .Dependencies )
84- mappingsYML := r .Translator .annotations [APIMAppingsAnnotation ]
85- if mappingsYML == "" {
86- return []client.Object {}, nil
87- }
88- mappings := map [string ]any {}
89- if err := yaml .Unmarshal ([]byte (mappingsYML ), mappings ); err != nil {
90- return nil , fmt .Errorf ("failed to unmarshal mappings YAML: %w" , err )
91- }
92-
93- for _ , entry := range []struct {
94- title string
95- path []string
96- }{
97- {title : "spec" , path : []string {"spec" , r .Translator .majorVersion }},
98- {title : "spec entry" , path : []string {"spec" , r .Translator .majorVersion , "entry" }},
99- {title : "status" , path : []string {"status" , r .Translator .majorVersion }},
100- } {
101- if err := em .expandMappingsAt (obj , mappings , entry .path ... ); err != nil {
102- return nil , fmt .Errorf ("failed to map properties of %q from API to Kubernetes: %w" , entry .title , err )
103- }
104- }
105- return em .added , nil
106- }
107-
108- func CollapseMappings (r * Request , spec map [string ]any , main client.Object ) error {
109- cm := newCollarserMapper (main , r .Dependencies )
110- mappingsYML := r .Translator .annotations [APIMAppingsAnnotation ]
111- if mappingsYML == "" {
112- return nil
113- }
114- mappings := map [string ]any {}
115- if err := yaml .Unmarshal ([]byte (mappingsYML ), mappings ); err != nil {
116- return fmt .Errorf ("failed to unmarshal mappings YAML: %w" , err )
117- }
118- props , err := unstructured .AccessField [map [string ]any ](mappings ,
119- "properties" , "spec" , "properties" , r .Translator .majorVersion , "properties" )
120- if errors .Is (err , unstructured .ErrNotFound ) {
121- return nil
122- }
123- if err != nil {
124- return fmt .Errorf ("failed to access the API mapping properties for the spec: %w" , err )
125- }
126- return cm .mapProperties ([]string {}, props , spec )
127- }
128-
129- func (m * mapper ) expandMappingsAt (obj , mappings map [string ]any , fields ... string ) error {
44+ func (h * Handler ) ExpandMappings (obj , mappings map [string ]any , fields ... string ) error {
45+ h .expand = true
13046 expandedPath := []string {"properties" }
13147 for _ , field := range fields {
13248 expandedPath = append (expandedPath , field , "properties" )
@@ -142,13 +58,22 @@ func (m *mapper) expandMappingsAt(obj, mappings map[string]any, fields ...string
14258 if err != nil {
14359 return fmt .Errorf ("failed to access object's %v: %w" , fields , err )
14460 }
145- if err := m .mapProperties ([]string {}, props , field ); err != nil {
61+ if err := h .mapProperties ([]string {}, props , field ); err != nil {
14662 return fmt .Errorf ("failed to process properties from API into %v: %w" , fields , err )
14763 }
14864 return nil
14965}
15066
151- func (m * mapper ) mapProperties (path []string , props , obj map [string ]any ) error {
67+ func (h * Handler ) Added () []client.Object {
68+ return h .added
69+ }
70+
71+ func (h * Handler ) CollapseReferences (path []string , props , obj map [string ]any ) error {
72+ h .expand = false
73+ return h .mapProperties (path , props , obj )
74+ }
75+
76+ func (m * Handler ) mapProperties (path []string , props , obj map [string ]any ) error {
15277 for key , prop := range props {
15378 mapping , ok := (prop ).(map [string ]any )
15479 if ! ok {
@@ -185,7 +110,7 @@ func (m *mapper) mapProperties(path []string, props, obj map[string]any) error {
185110 return nil
186111}
187112
188- func (m * mapper ) mapArray (path []string , mapping map [string ]any , list []any ) error {
113+ func (m * Handler ) mapArray (path []string , mapping map [string ]any , list []any ) error {
189114 mapItems , err := unstructured .AccessField [map [string ]any ](mapping , "items" , "properties" )
190115 if err != nil {
191116 return fmt .Errorf ("failed to access %q: %w" , unstructured .Base (path ), err )
@@ -210,7 +135,7 @@ func (m *mapper) mapArray(path []string, mapping map[string]any, list []any) err
210135 return nil
211136}
212137
213- func (m * mapper ) mapObject (path []string , mapName string , mapping , obj map [string ]any ) error {
138+ func (m * Handler ) mapObject (path []string , mapName string , mapping , obj map [string ]any ) error {
214139 if mapping ["properties" ] != nil {
215140 props , err := unstructured .AccessField [map [string ]any ](mapping , "properties" )
216141 if err != nil {
@@ -224,16 +149,16 @@ func (m *mapper) mapObject(path []string, mapName string, mapping, obj map[strin
224149 return fmt .Errorf ("unsupported extension at %v with fields %v" , path , unstructured .FieldsOf (mapping ))
225150}
226151
227- func (m * mapper ) mapReference (path []string , mappingName string , mapping , obj map [string ]any ) error {
152+ func (m * Handler ) mapReference (path []string , mappingName string , mapping , obj map [string ]any ) error {
228153 rm := refMapping {}
229154 if err := unstructured .FromUnstructured (& rm , mapping ); err != nil {
230155 return fmt .Errorf ("failed to parse a reference mapping: %w" , err )
231156 }
232157 ref := newRef (mappingName , & rm )
233158 if m .expand {
234- return ref .Expand (m .mapContext , path , obj )
159+ return ref .Expand (m .context , path , obj )
235160 }
236- return ref .Collapse (m .mapContext , path , obj )
161+ return ref .Collapse (m .context , path , obj )
237162}
238163
239164func entryMatchingMapping (mapName string , mapping map [string ]any , list []any , expand bool ) (string , map [string ]any , error ) {
0 commit comments