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

Commit 8ef84c0

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 8ef84c0

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
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: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package context
22

33
import (
4+
"errors"
45
. "github.com/onsi/ginkgo"
56
. "github.com/onsi/ginkgo/extensions/table"
67
. "github.com/onsi/gomega"
78
"github.com/redhat-developer/service-binding-operator/api/v1alpha1"
9+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
10+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
811
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
912
"k8s.io/apimachinery/pkg/runtime"
1013
"k8s.io/apimachinery/pkg/runtime/schema"
11-
"k8s.io/client-go/dynamic"
14+
"k8s.io/apimachinery/pkg/types"
15+
"k8s.io/apimachinery/pkg/util/uuid"
1216
"k8s.io/client-go/dynamic/fake"
17+
"k8s.io/client-go/testing"
18+
"strings"
1319
)
1420

1521
var _ = Describe("Service", func() {
1622

1723
var (
18-
client dynamic.Interface
24+
client *fake.FakeDynamicClient
1925
)
2026

2127
DescribeTable("CRD exist", func(version string, gr schema.GroupResource) {
@@ -43,8 +49,121 @@ var _ = Describe("Service", func() {
4349
Expect(res).To(BeNil())
4450
})
4551

52+
Describe("OwnedResources", func() {
53+
It("should return owned resources", func() {
54+
id := uuid.NewUUID()
55+
id2 := uuid.NewUUID()
56+
ns := "ns1"
57+
u := &unstructured.Unstructured{}
58+
u.SetGroupVersionKind(schema.GroupVersionKind{Group: "foo.bar", Version: "v1", Kind: "Foo"})
59+
u.SetName("foo")
60+
u.SetNamespace(ns)
61+
u.SetUID(id)
62+
63+
var children []interface{}
64+
65+
client = fake.NewSimpleDynamicClient(runtime.NewScheme())
66+
67+
for i := range bindableResourceGVRs {
68+
gvr := bindableResourceGVRs[i]
69+
70+
if gvr.Resource == "configmaps" {
71+
client.PrependReactor("list", gvr.Resource, func(action testing.Action) (handled bool, ret runtime.Object, err error) {
72+
return true, nil, k8serrors.NewNotFound(gvr.GroupResource(), "foo")
73+
})
74+
continue
75+
}
76+
ul := &unstructured.UnstructuredList{}
77+
// compute kind
78+
kind := strings.Title(gvr.Resource)[:len(gvr.Resource)-1]
79+
80+
gvk := gvr.GroupVersion().WithKind(kind)
81+
ou := resource(gvk, "child1", ns, id)
82+
83+
ou2 := resource(gvk, "child2", ns, id2)
84+
85+
children = append(children, ou)
86+
ul.Items = append(ul.Items, *ou, *ou2)
87+
88+
client.PrependReactor("list", gvr.Resource, func(action testing.Action) (handled bool, ret runtime.Object, err error) {
89+
return true, ul, nil
90+
})
91+
}
92+
93+
impl := &service{client: client, resource: u, lookForOwnedResources: true, serviceRef: &v1alpha1.Service{ NamespacedRef: v1alpha1.NamespacedRef{Namespace: &ns}}}
94+
95+
ownedResources, err := impl.OwnedResources()
96+
Expect(err).NotTo(HaveOccurred())
97+
Expect(ownedResources).Should(HaveLen(len(bindableResourceGVRs)-1))
98+
Expect(ownedResources).Should(ConsistOf(children...))
99+
})
100+
101+
DescribeTable("return error if occurs at looking at owned resources", func(failingResourceName string) {
102+
id := uuid.NewUUID()
103+
id2 := uuid.NewUUID()
104+
ns := "ns1"
105+
u := &unstructured.Unstructured{}
106+
u.SetGroupVersionKind(schema.GroupVersionKind{Group: "foo.bar", Version: "v1", Kind: "Foo"})
107+
u.SetName("foo")
108+
u.SetNamespace(ns)
109+
u.SetUID(id)
110+
111+
var children []interface{}
112+
113+
client = fake.NewSimpleDynamicClient(runtime.NewScheme())
114+
expectedErr := errors.New("foo")
115+
116+
for _, gvr := range bindableResourceGVRs {
117+
ul := &unstructured.UnstructuredList{}
118+
// compute kind
119+
kind := strings.Title(gvr.Resource)[:len(gvr.Resource)-1]
120+
// fix for ConfigMap
121+
if kind == "Configmap" {
122+
kind = "ConfigMap"
123+
}
124+
gvk := gvr.GroupVersion().WithKind(kind)
125+
ou := resource(gvk, "child1", ns, id)
126+
127+
ou2 := resource(gvk, "child2", ns, id2)
128+
129+
children = append(children, ou)
130+
ul.Items = append(ul.Items, *ou, *ou2)
131+
132+
resourceName := gvr.Resource
133+
client.PrependReactor("list", resourceName, func(action testing.Action) (handled bool, ret runtime.Object, err error) {
134+
if failingResourceName == resourceName {
135+
return true, nil, expectedErr
136+
}
137+
return true, ul, nil
138+
})
139+
}
140+
141+
impl := &service{client: client, resource: u, lookForOwnedResources: true, serviceRef: &v1alpha1.Service{ NamespacedRef: v1alpha1.NamespacedRef{Namespace: &ns}}}
142+
143+
ownedResources, err := impl.OwnedResources()
144+
Expect(err).Should(Equal(expectedErr))
145+
Expect(ownedResources).Should(BeNil())
146+
},
147+
Entry("fail listing configmap", "configmaps"),
148+
Entry("fail listing services", "services"),
149+
)
150+
})
151+
46152
})
47153

154+
func resource(gvk schema.GroupVersionKind, name string, namespace string, owner types.UID) *unstructured.Unstructured {
155+
u := &unstructured.Unstructured{}
156+
u.SetGroupVersionKind(gvk)
157+
u.SetName(name)
158+
u.SetNamespace(namespace)
159+
u.SetOwnerReferences([]v1.OwnerReference {
160+
{
161+
UID: owner,
162+
},
163+
})
164+
return u
165+
}
166+
48167
func crd(version string, gr schema.GroupResource) *unstructured.Unstructured {
49168
u := &unstructured.Unstructured{}
50169
u.SetGroupVersionKind(schema.GroupVersionKind{Group: "apiextensions.k8s.io", Version: version, Kind: "CustomResourceDefinition"})

0 commit comments

Comments
 (0)