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

Commit 2a673a5

Browse files
committed
Fix panic when modifying slices
Signed-off-by: Andy Sadler <[email protected]>
1 parent ed9caa2 commit 2a673a5

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

pkg/reconcile/pipeline/builder/pipeline_integration_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/redhat-developer/service-binding-operator/pkg/reconcile/pipeline/context"
1818
appsv1 "k8s.io/api/apps/v1"
1919
corev1 "k8s.io/api/core/v1"
20+
"k8s.io/apimachinery/pkg/api/errors"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2223
"k8s.io/apimachinery/pkg/runtime"
@@ -158,10 +159,10 @@ var _ = Describe("Default Pipeline", func() {
158159
u, err = client.Resource(appGVR).Namespace(sb.Namespace).Get(c.Background(), appName, metav1.GetOptions{})
159160
Expect(err).NotTo(HaveOccurred())
160161

161-
u, err = client.
162+
_, err = client.
162163
Resource(v1alpha3.WorkloadResourceMappingGroupVersionResource).
163164
Get(c.Background(), serviceGVK.GroupKind().String(), metav1.GetOptions{})
164-
Expect(err).NotTo(HaveOccurred())
165+
Expect(err).To(Equal(errors.NewNotFound(v1alpha3.WorkloadResourceMappingGroupVersionResource.GroupResource(), serviceGVK.GroupKind().String())), "Binding should occur without a workload resource mapping")
165166

166167
updatedApp := &appsv1.Deployment{}
167168
err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, updatedApp)

pkg/reconcile/pipeline/context/impl.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2828
"k8s.io/apimachinery/pkg/labels"
29+
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/apimachinery/pkg/runtime/schema"
3031
"k8s.io/client-go/dynamic"
3132
)
@@ -437,10 +438,25 @@ func (i *impl) Close() error {
437438
}
438439
for _, app := range i.applications {
439440
if app.IsUpdated() {
441+
// We explicitly want to marshal through json here; for some reason,
442+
// runtime.DeepCopyJSON() causes panics, but marshalling then calling DeepCopyJSON (in
443+
// Update()) works fine for some reason.
444+
// TODO: Figure out why this is.
445+
bytes, err := json.Marshal(app.Resource())
446+
if err != nil {
447+
return err
448+
}
449+
450+
var clone map[string]interface{}
451+
err = json.Unmarshal(bytes, &clone)
452+
if err != nil {
453+
return err
454+
}
455+
resource := &unstructured.Unstructured{Object: clone}
440456
_, err = i.client.
441457
Resource(app.GroupVersionResource()).
442458
Namespace(i.bindingMeta.Namespace).
443-
Update(context.Background(), app.Resource(), metav1.UpdateOptions{})
459+
Update(context.Background(), resource, metav1.UpdateOptions{})
444460
if err != nil {
445461
i.SetCondition(apis.Conditions().
446462
NotBindingReady().

pkg/reconcile/pipeline/mapping.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (container *MetaContainer) MountPath(bindingName string) (string, error) {
147147
// We can't use unstructured.SetNestedField when we try to set a field to a value of type
148148
// []map[string]interface{}, since (apparently) map[string]interface{} doesn't implement interface{}.
149149
// Runtime panics occur when using SetNestedField, and SetNesetedSlice gives a compile-time error.
150-
func setSlice(obj map[string]interface{}, resource []map[string]interface{}, path []string) error {
150+
func setSlice(obj map[string]interface{}, resources []map[string]interface{}, path []string) error {
151151
dest := obj
152152
if len(path) > 1 {
153153
slice, found, err := unstructured.NestedFieldNoCopy(obj, path[:len(path)-1]...)
@@ -177,13 +177,8 @@ func setSlice(obj map[string]interface{}, resource []map[string]interface{}, pat
177177
}
178178
}
179179

180-
dest[path[len(path)-1]] = make([]map[string]interface{}, len(resource))
181-
x := dest[path[len(path)-1]].([]map[string]interface{})
182-
for i, r := range resource {
183-
//FIXME: This fails for some reason. Help‽
184-
x[i] = runtime.DeepCopyJSON(r)
185-
}
186-
fmt.Println(x)
180+
key := path[len(path)-1]
181+
dest[key] = resources
187182
return nil
188183
}
189184

0 commit comments

Comments
 (0)