@@ -39,7 +39,7 @@ func FromWorkloadResourceMappingTemplate(mapping v1alpha3.ClusterWorkloadResourc
39
39
if err := constructRestrictedPath (container .VolumeMounts , []string {"volumeMounts" }, & wc .VolumeMounts ); err != nil {
40
40
return nil , err
41
41
}
42
- // not in the spec, but we need this for compatability with the downstream api
42
+ // not in the spec, but we need this for compatability with the coreos api
43
43
wc .EnvFrom = []string {"envFrom" }
44
44
45
45
path := jsonpath .New ("" )
@@ -145,42 +145,72 @@ func (container *MetaContainer) MountPath(bindingName string) (string, error) {
145
145
return mp , nil
146
146
}
147
147
148
+ type SBOMap map [string ]interface {}
149
+
148
150
// We can't use unstructured.SetNestedField when we try to set a field to a value of type
149
151
// []map[string]interface{}, since (apparently) map[string]interface{} doesn't implement interface{}.
150
152
// Runtime panics occur when using SetNestedField, and SetNesetedSlice gives a compile-time error.
151
153
func setSlice (obj map [string ]interface {}, resources []map [string ]interface {}, path []string ) error {
152
- dest := obj
153
- if len (path ) > 1 {
154
- slice , found , err := unstructured .NestedFieldNoCopy (obj , path [:len (path )- 1 ]... )
155
- if err != nil {
156
- return err
157
- } else if ! found {
158
- current := obj
159
- for i , p := range path [:len (path )- 1 ] {
160
- x , exists := current [p ]
161
- y , ok := x .(map [string ]interface {})
162
- if ! exists {
163
- y = make (map [string ]interface {})
164
- current [p ] = y
165
- } else if ! ok {
166
- return fmt .Errorf ("Value already exists at path %v" , path [:i ])
167
- }
168
- current = y
169
- }
170
-
171
- dest = current
172
- } else {
173
- x , ok := slice .(map [string ]interface {})
174
- if ! ok {
175
- return fmt .Errorf ("Value already exists at path %v" , path [:len (path )- 1 ])
176
- }
177
- dest = x
178
- }
179
- }
180
-
181
- key := path [len (path )- 1 ]
182
- dest [key ] = resources
183
- return nil
154
+ // for _, resource := range resources {
155
+ // err := insert(obj, resource, path)
156
+ // if err != nil {
157
+ // return err
158
+ // }
159
+ // }
160
+ // return nil
161
+ // var sbomap []*SBOMap
162
+ // for _, x := range resources {
163
+ // d := SBOMap(x)
164
+ // sbomap = append(sbomap, &d)
165
+ // }
166
+ return unstructured .SetNestedField (obj , resources , path ... )
167
+ // dest := obj
168
+ // if len(path) > 1 {
169
+ // slice, found, err := unstructured.NestedFieldNoCopy(obj, path[:len(path)-1]...)
170
+ // if err != nil {
171
+ // return err
172
+ // } else if !found {
173
+ // current := obj
174
+ // for i, p := range path[:len(path)-1] {
175
+ // x, exists := current[p]
176
+ // y, ok := x.(map[string]interface{})
177
+ // if !exists {
178
+ // y = make(map[string]interface{})
179
+ // current[p] = y
180
+ // } else if !ok {
181
+ // return fmt.Errorf("Value already exists at path %v", path[:i])
182
+ // }
183
+ // current = y
184
+ // }
185
+
186
+ // dest = current
187
+ // } else {
188
+ // x, ok := slice.(map[string]interface{})
189
+ // if !ok {
190
+ // return fmt.Errorf("Value already exists at path %v", path[:len(path)-1])
191
+ // }
192
+ // dest = x
193
+ // }
194
+ // }
195
+
196
+ // key := path[len(path)-1]
197
+ // slice, found := dest[key]
198
+ // fmt.Printf("Writing %v into %v\n", resources, key)
199
+ // if found {
200
+ // raw, ok := slice.([]interface{})
201
+ // if !ok {
202
+ // return fmt.Errorf("Value at path %v is not a slice", path)
203
+ // }
204
+ // rawData, ok := raw.([]map[string]interface{})
205
+ // if !ok {
206
+ // fmt.Printf("Got type %v", reflect.TypeOf)
207
+ // return fmt.Errorf("Value at path %v is not unstructured data", path)
208
+ // }
209
+ // dest[key] = append(raw, resources...)
210
+ // } else {
211
+ // dest[key] = resources
212
+ // }
213
+ // return nil
184
214
}
185
215
186
216
func addRaw (obj map [string ]interface {}, resource interface {}, data []map [string ]interface {}, path []string ) error {
@@ -197,8 +227,16 @@ func addRaw(obj map[string]interface{}, resource interface{}, data []map[string]
197
227
return setSlice (obj , nestedData , path )
198
228
}
199
229
200
- func (container * MetaContainer ) AddEnvVars (vars []map [string ]interface {}) error {
201
- return addRaw (container .Data , & corev1.EnvVar {}, vars , container .Env )
230
+ func (container * MetaContainer ) AddEnvVars (vars []corev1.EnvVar ) error {
231
+ var data []map [string ]interface {}
232
+ for _ , variable := range vars {
233
+ x , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& variable )
234
+ if err != nil {
235
+ return err
236
+ }
237
+ data = append (data , x )
238
+ }
239
+ return addRaw (container .Data , & corev1.EnvVar {}, data , container .Env )
202
240
}
203
241
204
242
func (container * MetaContainer ) RemoveEnvVars (secretName string ) error {
@@ -222,9 +260,13 @@ func (container *MetaContainer) RemoveEnvVars(secretName string) error {
222
260
return nil
223
261
}
224
262
225
- func (container * MetaContainer ) AddEnvFromVars ( vars [] map [ string ] interface {} ) error {
263
+ func (container * MetaContainer ) AddEnvFromVar ( envVar corev1. EnvFromSource ) error {
226
264
if len (container .EnvFrom ) != 0 {
227
- return addRaw (container .Data , & corev1.EnvFromSource {}, vars , container .EnvFrom )
265
+ data , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& envVar )
266
+ if err != nil {
267
+ return err
268
+ }
269
+ return addRaw (container .Data , & corev1.EnvFromSource {}, []map [string ]interface {}{data }, container .EnvFrom )
228
270
}
229
271
return fmt .Errorf ("No envFrom field" )
230
272
}
0 commit comments