Skip to content
This repository was archived by the owner on Jun 26, 2024. It is now read-only.

Commit 1e55fd7

Browse files
committed
wip
Signed-off-by: Andy Sadler <[email protected]>
1 parent c2845e8 commit 1e55fd7

File tree

4 files changed

+106
-53
lines changed

4 files changed

+106
-53
lines changed

controllers/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,5 @@ func (r *BindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
120120
}
121121
return result, nil
122122
}
123-
// end::reconcile-func[]
123+
124+
// end::reconcile-func[]

pkg/reconcile/pipeline/handler/project/impl.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/redhat-developer/service-binding-operator/pkg/reconcile/pipeline"
1010
corev1 "k8s.io/api/core/v1"
1111
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12-
"k8s.io/apimachinery/pkg/runtime"
1312
)
1413

1514
func PreFlightCheck(mandatoryBindingKeys ...string) func(pipeline.Context) {
@@ -78,11 +77,11 @@ func BindingsAsEnv(ctx pipeline.Context) {
7877
}
7978

8079
secretName := ctx.BindingSecretName()
81-
var envVars []map[string]interface{}
80+
var envVars []corev1.EnvVar
8281
if len(envBindings) > 0 {
83-
envVars = make([]map[string]interface{}, 0, len(envBindings))
82+
envVars = make([]corev1.EnvVar, 0, len(envBindings))
8483
for _, e := range envBindings {
85-
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&corev1.EnvVar{
84+
u := corev1.EnvVar{
8685
Name: e.Var,
8786
ValueFrom: &corev1.EnvVarSource{
8887
SecretKeyRef: &corev1.SecretKeySelector{
@@ -92,10 +91,6 @@ func BindingsAsEnv(ctx pipeline.Context) {
9291
Key: e.Name,
9392
},
9493
},
95-
})
96-
if err != nil {
97-
stop(ctx, err)
98-
return
9994
}
10095
envVars = append(envVars, u)
10196
}
@@ -109,12 +104,6 @@ func BindingsAsEnv(ctx pipeline.Context) {
109104
},
110105
}
111106

112-
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&envFromSecret)
113-
if err != nil {
114-
stop(ctx, err)
115-
return
116-
}
117-
118107
for _, app := range applications {
119108
containerResources, err := app.BindableContainers()
120109
if err != nil {
@@ -124,12 +113,16 @@ func BindingsAsEnv(ctx pipeline.Context) {
124113
for _, container := range containerResources.Containers {
125114
if !ctx.BindAsFiles() {
126115
// Safety: EnvFrom is available when ctx.BindAsFiles() == false
127-
if err := container.AddEnvFromVars([]map[string]interface{}{u}); err != nil {
116+
fmt.Printf("Adding env var %v from secret %v\n", envFromSecret, secretName)
117+
if err := container.AddEnvFromVar(envFromSecret); err != nil {
118+
fmt.Printf("err: %v\n", err)
128119
stop(ctx, err)
129120
return
130121
}
131122
}
123+
fmt.Printf("Adding env vars %v\n", envVars)
132124
if err := container.AddEnvVars(envVars); err != nil {
125+
fmt.Printf("err: %v\n", err)
133126
stop(ctx, err)
134127
return
135128
}

pkg/reconcile/pipeline/mapping.go

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func FromWorkloadResourceMappingTemplate(mapping v1alpha3.ClusterWorkloadResourc
3939
if err := constructRestrictedPath(container.VolumeMounts, []string{"volumeMounts"}, &wc.VolumeMounts); err != nil {
4040
return nil, err
4141
}
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
4343
wc.EnvFrom = []string{"envFrom"}
4444

4545
path := jsonpath.New("")
@@ -145,42 +145,72 @@ func (container *MetaContainer) MountPath(bindingName string) (string, error) {
145145
return mp, nil
146146
}
147147

148+
type SBOMap map[string]interface{}
149+
148150
// We can't use unstructured.SetNestedField when we try to set a field to a value of type
149151
// []map[string]interface{}, since (apparently) map[string]interface{} doesn't implement interface{}.
150152
// Runtime panics occur when using SetNestedField, and SetNesetedSlice gives a compile-time error.
151153
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
184214
}
185215

186216
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]
197227
return setSlice(obj, nestedData, path)
198228
}
199229

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)
202240
}
203241

204242
func (container *MetaContainer) RemoveEnvVars(secretName string) error {
@@ -222,9 +260,13 @@ func (container *MetaContainer) RemoveEnvVars(secretName string) error {
222260
return nil
223261
}
224262

225-
func (container *MetaContainer) AddEnvFromVars(vars []map[string]interface{}) error {
263+
func (container *MetaContainer) AddEnvFromVar(envVar corev1.EnvFromSource) error {
226264
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)
228270
}
229271
return fmt.Errorf("No envFrom field")
230272
}

vendor/k8s.io/apimachinery/pkg/runtime/converter.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)