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

Commit 2c48563

Browse files
committed
Fix ServiceBinding removal when service not exist
Signed-off-by: qibobo <[email protected]>
1 parent ecbab0f commit 2c48563

File tree

2 files changed

+93
-33
lines changed

2 files changed

+93
-33
lines changed

controllers/reconciler.go

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"context"
55
"errors"
6+
67
"k8s.io/apimachinery/pkg/api/meta"
78

89
k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -113,41 +114,43 @@ func (r *ServiceBindingReconciler) doReconcile(request reconcile.Request) (recon
113114
// being worked in https://github.com/redhat-developer/service-binding-operator/pull/442.
114115
return requeueError(errEmptyServices)
115116
}
116-
117-
serviceCtxs, err := buildServiceContexts(
118-
logger.WithName("buildServiceContexts"),
119-
r.dynClient,
120-
sbr.GetNamespace(),
121-
sbr.Spec.Services,
122-
sbr.Spec.DetectBindingResources,
123-
r.restMapper,
124-
)
125-
if err != nil {
126-
//handle service not found error
127-
if k8serrors.IsNotFound(err) {
128-
err = updateSBRConditions(r.dynClient, sbr,
129-
metav1.Condition{
130-
Type: v1alpha1.CollectionReady,
131-
Status: metav1.ConditionFalse,
132-
Reason: v1alpha1.ServiceNotFoundReason,
133-
Message: err.Error(),
134-
},
135-
metav1.Condition{
136-
Type: v1alpha1.InjectionReady,
137-
Reason: v1alpha1.ServiceNotFoundReason,
138-
Status: metav1.ConditionFalse,
139-
},
140-
metav1.Condition{
141-
Type: v1alpha1.BindingReady,
142-
Reason: v1alpha1.ServiceNotFoundReason,
143-
Status: metav1.ConditionFalse,
144-
},
145-
)
146-
if err != nil {
147-
logger.Error(err, "Failed to update SBR conditions", "sbr", sbr)
117+
serviceCtxs := serviceContextList{}
118+
// we only need to build the service context if SBR is not deleted
119+
if sbr.GetDeletionTimestamp() == nil {
120+
serviceCtxs, err = buildServiceContexts(
121+
logger.WithName("buildServiceContexts"),
122+
r.dynClient,
123+
sbr.GetNamespace(),
124+
sbr.Spec.Services,
125+
sbr.Spec.DetectBindingResources,
126+
r.restMapper,
127+
)
128+
if err != nil {
129+
//handle service not found error
130+
if k8serrors.IsNotFound(err) {
131+
err = updateSBRConditions(r.dynClient, sbr,
132+
metav1.Condition{
133+
Type: v1alpha1.CollectionReady,
134+
Status: metav1.ConditionFalse,
135+
Reason: v1alpha1.ServiceNotFoundReason,
136+
Message: err.Error(),
137+
},
138+
metav1.Condition{
139+
Type: v1alpha1.InjectionReady,
140+
Status: metav1.ConditionFalse,
141+
},
142+
metav1.Condition{
143+
Type: v1alpha1.BindingReady,
144+
Status: metav1.ConditionFalse,
145+
},
146+
)
147+
if err != nil {
148+
logger.Error(err, "Failed to update SBR conditions", "sbr", sbr)
149+
}
148150
}
151+
return requeueError(err)
152+
149153
}
150-
return requeueError(err)
151154
}
152155
binding, err := buildBinding(
153156
r.dynClient,

test/acceptance/features/unbindAppToService.feature

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,60 @@ Feature: Unbind an application from a service
4949

5050
Then The env var "BACKEND_HOST" is not available to the application
5151
And The env var "BACKEND_USERNAME" is not available to the application
52+
53+
54+
Scenario: Unbind a generic test application from the backing service when the backing service has been deleted
55+
Given OLM Operator "backend" is running
56+
* The Custom Resource is present
57+
"""
58+
apiVersion: "stable.example.com/v1"
59+
kind: Backend
60+
metadata:
61+
name: example-backend
62+
annotations:
63+
service.binding/host: path={.spec.host}
64+
service.binding/username: path={.spec.username}
65+
spec:
66+
host: example.com
67+
username: foo
68+
"""
69+
* Generic test application "generic-app-a-d-u" is running
70+
* Service Binding is applied
71+
"""
72+
apiVersion: operators.coreos.com/v1alpha1
73+
kind: ServiceBinding
74+
metadata:
75+
name: binding-request-a-d-u
76+
spec:
77+
application:
78+
name: generic-app-a-d-u
79+
group: apps
80+
version: v1
81+
resource: deployments
82+
services:
83+
- group: stable.example.com
84+
version: v1
85+
kind: Backend
86+
name: example-backend
87+
id: backend
88+
"""
89+
* The application env var "BACKEND_HOST" has value "example.com"
90+
* The application env var "BACKEND_USERNAME" has value "foo"
91+
92+
When BackingService is deleted
93+
"""
94+
apiVersion: "stable.example.com/v1"
95+
kind: Backend
96+
metadata:
97+
name: example-backend
98+
annotations:
99+
service.binding/host: path={.spec.host}
100+
service.binding/username: path={.spec.username}
101+
spec:
102+
host: example.com
103+
username: foo
104+
"""
105+
When Service binding "binding-request-a-d-u" is deleted
106+
107+
Then The env var "BACKEND_HOST" is not available to the application
108+
And The env var "BACKEND_USERNAME" is not available to the application

0 commit comments

Comments
 (0)