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

Commit 615d956

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 615d956

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-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: 118 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,118 @@ 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+
client = fake.NewSimpleDynamicClient(runtime.NewScheme())
112+
expectedErr := errors.New("foo")
113+
114+
for _, gvr := range bindableResourceGVRs {
115+
ul := &unstructured.UnstructuredList{}
116+
// compute kind
117+
kind := strings.Title(gvr.Resource)[:len(gvr.Resource)-1]
118+
// fix for ConfigMap
119+
if kind == "Configmap" {
120+
kind = "ConfigMap"
121+
}
122+
gvk := gvr.GroupVersion().WithKind(kind)
123+
ou := resource(gvk, "child1", ns, id)
124+
125+
ou2 := resource(gvk, "child2", ns, id2)
126+
127+
ul.Items = append(ul.Items, *ou, *ou2)
128+
129+
resourceName := gvr.Resource
130+
client.PrependReactor("list", resourceName, func(action testing.Action) (handled bool, ret runtime.Object, err error) {
131+
if failingResourceName == resourceName {
132+
return true, nil, expectedErr
133+
}
134+
return true, ul, nil
135+
})
136+
}
137+
138+
impl := &service{client: client, resource: u, lookForOwnedResources: true, serviceRef: &v1alpha1.Service{ NamespacedRef: v1alpha1.NamespacedRef{Namespace: &ns}}}
139+
140+
ownedResources, err := impl.OwnedResources()
141+
Expect(err).Should(Equal(expectedErr))
142+
Expect(ownedResources).Should(BeNil())
143+
},
144+
Entry("fail listing configmap", "configmaps"),
145+
Entry("fail listing services", "services"),
146+
)
147+
})
148+
46149
})
47150

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

0 commit comments

Comments
 (0)