@@ -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,13 @@ 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
+ return unstructured .SetNestedField (obj , resources , path ... )
184
155
}
185
156
186
157
func addRaw (obj map [string ]interface {}, resource interface {}, data []map [string ]interface {}, path []string ) error {
@@ -197,8 +168,16 @@ func addRaw(obj map[string]interface{}, resource interface{}, data []map[string]
197
168
return setSlice (obj , nestedData , path )
198
169
}
199
170
200
- func (container * MetaContainer ) AddEnvVars (vars []map [string ]interface {}) error {
201
- return addRaw (container .Data , & corev1.EnvVar {}, vars , container .Env )
171
+ func (container * MetaContainer ) AddEnvVars (vars []corev1.EnvVar ) error {
172
+ var data []map [string ]interface {}
173
+ for _ , variable := range vars {
174
+ x , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& variable )
175
+ if err != nil {
176
+ return err
177
+ }
178
+ data = append (data , x )
179
+ }
180
+ return addRaw (container .Data , & corev1.EnvVar {}, data , container .Env )
202
181
}
203
182
204
183
func (container * MetaContainer ) RemoveEnvVars (secretName string ) error {
@@ -222,9 +201,13 @@ func (container *MetaContainer) RemoveEnvVars(secretName string) error {
222
201
return nil
223
202
}
224
203
225
- func (container * MetaContainer ) AddEnvFromVars ( vars [] map [ string ] interface {} ) error {
204
+ func (container * MetaContainer ) AddEnvFromVar ( envVar corev1. EnvFromSource ) error {
226
205
if len (container .EnvFrom ) != 0 {
227
- return addRaw (container .Data , & corev1.EnvFromSource {}, vars , container .EnvFrom )
206
+ data , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& envVar )
207
+ if err != nil {
208
+ return err
209
+ }
210
+ return addRaw (container .Data , & corev1.EnvFromSource {}, []map [string ]interface {}{data }, container .EnvFrom )
228
211
}
229
212
return fmt .Errorf ("No envFrom field" )
230
213
}
0 commit comments