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

Commit 7e6bf7e

Browse files
committed
Fix Service.OwnedResources() to return correct result
We were bitten by https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable Fixed the loop to use the new variable. Signed-off-by: Predrag Knezevic <[email protected]>
1 parent d4fd4b7 commit 7e6bf7e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

pkg/reconcile/pipeline/context/service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ func (s *service) OwnedResources() ([]*unstructured.Unstructured, error) {
6363
}
6464
return nil, err
6565
}
66-
for _, item := range list.Items {
66+
for i, _ := range list.Items {
67+
item := list.Items[i]
6768
for _, ownerRef := range item.GetOwnerReferences() {
6869
if reflect.DeepEqual(ownerRef.UID, uid) {
6970
result = append(result, &item)

pkg/reconcile/pipeline/context/service_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import (
55
. "github.com/onsi/ginkgo/extensions/table"
66
. "github.com/onsi/gomega"
77
"github.com/redhat-developer/service-binding-operator/api/v1alpha1"
8+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
910
"k8s.io/apimachinery/pkg/runtime"
1011
"k8s.io/apimachinery/pkg/runtime/schema"
12+
"k8s.io/apimachinery/pkg/types"
13+
"k8s.io/apimachinery/pkg/util/uuid"
1114
"k8s.io/client-go/dynamic"
1215
"k8s.io/client-go/dynamic/fake"
16+
"strings"
1317
)
1418

1519
var _ = Describe("Service", func() {
@@ -43,8 +47,55 @@ var _ = Describe("Service", func() {
4347
Expect(res).To(BeNil())
4448
})
4549

50+
It("should return owned resources", func() {
51+
var objs []runtime.Object
52+
id := uuid.NewUUID()
53+
id2 := uuid.NewUUID()
54+
ns := "ns1"
55+
u := &unstructured.Unstructured{}
56+
u.SetGroupVersionKind(schema.GroupVersionKind{Group: "foo.bar", Version: "v1", Kind: "Foo"})
57+
u.SetName("foo")
58+
u.SetNamespace(ns)
59+
u.SetUID(id)
60+
61+
objs = append(objs, u)
62+
63+
var children []interface{}
64+
65+
for _, gvr := range bindableResourceGVRs {
66+
gvk := gvr.GroupVersion().WithKind(strings.Title(gvr.Resource)[:len(gvr.Resource)-1])
67+
ou := resource(gvk, "child1", ns, id)
68+
69+
ou2 := resource(gvk, "child2", ns, id2)
70+
71+
children = append(children, ou)
72+
objs = append(objs, ou, ou2)
73+
}
74+
75+
client = fake.NewSimpleDynamicClient(runtime.NewScheme(), objs...)
76+
77+
impl := &service{client: client, resource: u, lookForOwnedResources: true, serviceRef: &v1alpha1.Service{ NamespacedRef: v1alpha1.NamespacedRef{Namespace: &ns}}}
78+
79+
ownedResources, err := impl.OwnedResources()
80+
Expect(err).NotTo(HaveOccurred())
81+
Expect(ownedResources).Should(HaveLen(len(bindableResourceGVRs)))
82+
Expect(ownedResources).Should(ContainElements(children...))
83+
})
4684
})
4785

86+
func resource(gvk schema.GroupVersionKind, name string, namespace string, owner types.UID) *unstructured.Unstructured {
87+
u := &unstructured.Unstructured{}
88+
u.SetGroupVersionKind(gvk)
89+
u.SetName(name)
90+
u.SetNamespace(namespace)
91+
u.SetOwnerReferences([]v1.OwnerReference {
92+
{
93+
UID: owner,
94+
},
95+
})
96+
return u
97+
}
98+
4899
func crd(version string, gr schema.GroupResource) *unstructured.Unstructured {
49100
u := &unstructured.Unstructured{}
50101
u.SetGroupVersionKind(schema.GroupVersionKind{Group: "apiextensions.k8s.io", Version: version, Kind: "CustomResourceDefinition"})

0 commit comments

Comments
 (0)