Skip to content

Commit

Permalink
Check if unready binding can be removed (#959)
Browse files Browse the repository at this point in the history
* added missing acceptance tests
* in the reconcile loop, add finalizer only if deletion timestamp is unset
* unbind handler: stop processing and do not retry in case of any error when reading the application resource

Signed-off-by: Predrag Knezevic <pknezevi@redhat.com>
  • Loading branch information
pedjak committed May 3, 2021
1 parent c256ac6 commit 4efb48c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
8 changes: 6 additions & 2 deletions controllers/servicebinding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ func (r *ServiceBindingReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
log.Error(err, "Failed to get ServiceBinding", "name", req.NamespacedName, "err", err)
return ctrl.Result{}, err
}
if serviceBinding.MaybeAddFinalizer() {
if serviceBinding.DeletionTimestamp.IsZero() && serviceBinding.MaybeAddFinalizer() {
if err = r.Update(ctx, serviceBinding); err != nil {
return ctrl.Result{}, err
}
}
log.Info("Reconciling", "sb", serviceBinding)
if serviceBinding.DeletionTimestamp.IsZero() {
log.Info("Reconciling")
} else {
log.Info("Deleted, unbind the application")
}
retry, err := r.pipeline.Process(serviceBinding)
if !retry && err == nil {
if !serviceBinding.DeletionTimestamp.IsZero() {
Expand Down
11 changes: 1 addition & 10 deletions pkg/reconcile/pipeline/handler/project/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/redhat-developer/service-binding-operator/api/v1alpha1"
"github.com/redhat-developer/service-binding-operator/pkg/reconcile/pipeline"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"path"
Expand Down Expand Up @@ -224,15 +223,7 @@ func Unbind(ctx pipeline.Context) {
return
}
applications, err := ctx.Applications()
if err != nil {
if k8serrors.IsNotFound(err) {
ctx.StopProcessing()
return
}
ctx.RetryProcessing(err)
return
}
if len(applications) == 0 {
if err != nil || len(applications) == 0 {
ctx.StopProcessing()
return
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/reconcile/pipeline/handler/project/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,20 @@ var _ = Describe("Unbind handler", func() {
ctx = mocks.NewMockContext(mockCtrl)
ctx.EXPECT().UnbindRequested().Return(true)
ctx.EXPECT().StopProcessing()

})

AfterEach(func() {
mockCtrl.Finish()
})

It("should stop processing when there is error getting applications", func() {
ctx.EXPECT().Applications().Return(nil, errors.New("foo"))
project.Unbind(ctx)
})
It("should stop processing when there are no applications", func() {
ctx.EXPECT().Applications().Return([]pipeline.Application{}, nil)
project.Unbind(ctx)
})
Context("successful processing", func() {
var (
deploymentsUnstructured []*unstructured.Unstructured
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/features/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ def validate_error(context, err_msg=None):
@then(u'Service Binding "{sb_name}" is not persistent in the cluster')
def validate_absent_sb(context, sb_name):
openshift = Openshift()
output = openshift.search_resource_in_namespace("servicebindings", sb_name, context.namespace.name)
assert output is None, f"Service Binding {sb_name} is present in namespace '{context.namespace.name}'"
polling2.poll(lambda: openshift.search_resource_in_namespace("servicebindings", sb_name, context.namespace.name),
step=5, timeout=400, check_success=lambda v: v is None)


@then(u'Service Binding "{sb_name}" is not updated')
Expand Down
39 changes: 38 additions & 1 deletion test/acceptance/features/unbindAppToService.feature
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,41 @@ Feature: Unbind an application from a service
Then The application got redeployed 2 times so far
* File "/bindings/remove-bindings-as-files-app-sb/host" is unavailable in application pod
* File "/bindings/remove-bindings-as-files-app-sb/port" is unavailable in application pod
* Service Binding secret is not present
* Service Binding secret is not present

Scenario: Remove not ready binding
Given The Custom Resource is present
"""
apiVersion: "stable.example.com/v1"
kind: Backend
metadata:
name: example-backend
annotations:
service.binding/host: path={.spec.host}
service.binding/username: path={.spec.username}
spec:
host: example.com
username: foo
"""
* Service Binding is applied
"""
apiVersion: binding.operators.coreos.com/v1alpha1
kind: ServiceBinding
metadata:
name: unready-binding
spec:
bindAsFiles: false
application:
name: not-found-app
group: apps
version: v1
resource: deployments
services:
- group: stable.example.com
version: v1
kind: Backend
name: example-backend
"""
* jq ".status.conditions[] | select(.type=="Ready").status" of Service Binding "unready-binding" should be changed to "False"
When Service binding "unready-binding" is deleted
Then Service Binding "unready-binding" is not persistent in the cluster

0 comments on commit 4efb48c

Please sign in to comment.