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

Commit 1f72711

Browse files
authored
Make sure to return nil on error (#1079)
When CrdReader fail and returns error other than NotFound service.CustomResourceDefinition should return 'nil' instead of CRD that might be invalid. Signed-off-by: Tomas Kral <[email protected]>
1 parent 9ee6062 commit 1f72711

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

pkg/reconcile/pipeline/context/service/service.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package service
33
import (
44
"context"
55
e "errors"
6+
67
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
78
"github.com/redhat-developer/service-binding-operator/pkg/binding"
89
"github.com/redhat-developer/service-binding-operator/pkg/binding/registry"
910
"github.com/redhat-developer/service-binding-operator/pkg/client/kubernetes"
1011
"github.com/redhat-developer/service-binding-operator/pkg/reconcile/pipeline"
1112

13+
"reflect"
14+
1215
"github.com/redhat-developer/service-binding-operator/pkg/util"
1316
"k8s.io/apimachinery/pkg/api/errors"
1417
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1518
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1619
"k8s.io/apimachinery/pkg/runtime"
1720
"k8s.io/apimachinery/pkg/runtime/schema"
1821
"k8s.io/client-go/dynamic"
19-
"reflect"
2022
)
2123

2224
var _ pipeline.Service = &service{}
@@ -184,6 +186,9 @@ func (s *service) CustomResourceDefinition() (pipeline.CRD, error) {
184186
s.crdLookup = true
185187
return nil, nil
186188
}
189+
if err != nil {
190+
return nil, err
191+
}
187192
s.crd = &customResourceDefinition{resource: u, client: s.client, ns: s.namespace, serviceGVR: s.groupVersionResource}
188193
return s.crd, err
189194
}

pkg/reconcile/pipeline/context/service/service_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package service
22

33
import (
44
"errors"
5+
"strings"
6+
57
"github.com/golang/mock/gomock"
68
. "github.com/onsi/ginkgo"
79
. "github.com/onsi/ginkgo/extensions/table"
@@ -17,7 +19,6 @@ import (
1719
"k8s.io/apimachinery/pkg/util/uuid"
1820
"k8s.io/client-go/dynamic/fake"
1921
"k8s.io/client-go/testing"
20-
"strings"
2122
)
2223

2324
var _ = Describe("Service", func() {
@@ -90,6 +91,23 @@ var _ = Describe("Service", func() {
9091
Expect(res).To(BeNil())
9192
})
9293

94+
It("should return nil when CrdReader returned error", func() {
95+
client = fake.NewSimpleDynamicClient(runtime.NewScheme())
96+
typeLookup.EXPECT().ResourceForKind(gomock.Any()).Return(&schema.GroupVersionResource{Group: "app", Resource: "deployments", Version: "v1"}, nil)
97+
crdResource := &unstructured.Unstructured{}
98+
crdResource.SetName("crdfoo1")
99+
returnErorr := errors.New("some error")
100+
builer := NewBuilder(typeLookup).WithClient(client).WithCrdReader(func(gvk *schema.GroupVersionResource) (*unstructured.Unstructured, error) {
101+
return crdResource, returnErorr
102+
})
103+
service, err := builer.Build(&unstructured.Unstructured{})
104+
Expect(err).NotTo(HaveOccurred())
105+
106+
res, err := service.CustomResourceDefinition()
107+
Expect(err).Should(MatchError(returnErorr))
108+
Expect(res).To(BeNil())
109+
})
110+
93111
Describe("OwnedResources", func() {
94112
It("should return owned resources", func() {
95113
id := uuid.NewUUID()

0 commit comments

Comments
 (0)