Repository: redhat-developer/service-binding-operator
Pull request #540 (2020-07-03 15:15:21)

added link to Binding an Imported App to a Route/Ingress
Added a missing link in README.md

openshift-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **avni-sharma**
You can assign the PR to them by writing `/assign @avni-sharma` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=redhat-developer%2Fservice-binding-operator).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/redhat-developer/service-binding-operator/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["avni-sharma"]} -->
openshift-ci-robot: @Avni-Sharma: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
ci/prow/4.5-e2e | 99fa7631dfbd7c36b6bc99e686f7eff8af1d6b7f | [link](https://prow.svc.ci.openshift.org/view/gcs/origin-ci-test/pr-logs/pull/redhat-developer_service-binding-operator/540/pull-ci-redhat-developer-service-binding-operator-master-4.5-e2e/1279071243684810752) | `/test 4.5-e2e`

[Full PR test history](https://prow.svc.ci.openshift.org/pr-history?org=redhat-developer&repo=service-binding-operator&pr=540). [Your PR dashboard](https://prow.svc.ci.openshift.org/pr?query=is:pr+state:open+author:Avni-Sharma). Please help us cut down on flakes by [linking to](https://github.com/kubernetes/community/blob/master/contributors/devel/flaky-tests.md#filing-issues-for-flaky-tests) an [open issue](https://github.com/redhat-developer/service-binding-operator/issues?q=is:issue+is:open) when you hit one in your PR.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->

Patch:
diff --git a/README.md b/README.md
index 00649cb6..47333fb3 100644
--- a/README.md
+++ b/README.md
@@ -96,6 +96,8 @@ The following example scenarios are available:
 
 [Binding an Imported app in one namespace with an In-cluster Managed PostgreSQL Database in another namespace](examples/nodejs_postgresql_namespaces/README.md)
 
+[Binding an Imported app to a Route/Ingress][examples/route_k8s_resource/README.md]
+
 ## Community, discussion, contribution, and support
 
 The Service Binding community meets weekly on Thursdays at 11:15 AM UTC via [Google Meet](https://meet.google.com/jti-pujt-zqs).
diff --git a/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go b/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go
index 321ae486..472c4c7b 100644
--- a/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go
+++ b/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go
@@ -112,3 +112,14 @@ type ServiceBindingRequestList struct {
 func init() {
 	SchemeBuilder.Register(&ServiceBindingRequest{}, &ServiceBindingRequestList{})
 }
+
+func (sbr ServiceBindingRequest) AsOwnerReference() metav1.OwnerReference {
+	var ownerRefController bool = true
+	return metav1.OwnerReference{
+		Name:       sbr.Name,
+		UID:        sbr.UID,
+		Kind:       sbr.Kind,
+		APIVersion: sbr.APIVersion,
+		Controller: &ownerRefController,
+	}
+}
diff --git a/pkg/controller/servicebindingrequest/secret.go b/pkg/controller/servicebindingrequest/secret.go
index 22eba8ac..f05af5c7 100644
--- a/pkg/controller/servicebindingrequest/secret.go
+++ b/pkg/controller/servicebindingrequest/secret.go
@@ -27,12 +27,13 @@ func (s *secret) buildResourceClient() dynamic.ResourceInterface {
 
 // createOrUpdate will take informed payload and either create a new secret or update an existing
 // one. It can return error when Kubernetes client does.
-func (s *secret) createOrUpdate(payload map[string][]byte) (*unstructured.Unstructured, error) {
+func (s *secret) createOrUpdate(payload map[string][]byte, ownerReference metav1.OwnerReference) (*unstructured.Unstructured, error) {
 	logger := s.logger.WithValues("Namespace", s.ns, "Name", s.name)
 	secretObj := &corev1.Secret{
 		ObjectMeta: metav1.ObjectMeta{
-			Namespace: s.ns,
-			Name:      s.name,
+			Namespace:       s.ns,
+			Name:            s.name,
+			OwnerReferences: []metav1.OwnerReference{ownerReference},
 		},
 		Data: payload,
 	}
@@ -59,12 +60,6 @@ func (s *secret) createOrUpdate(payload map[string][]byte) (*unstructured.Unstru
 	return u, nil
 }
 
-// commit will store informed data as a secret, commit it against the API server. It can forward
-// errors from custom environment parser component, or from the API server itself.
-func (s *secret) commit(payload map[string][]byte) (*unstructured.Unstructured, error) {
-	return s.createOrUpdate(payload)
-}
-
 // get an unstructured object from the secret handled by this component. It can return errors in case
 // the API server does.
 func (s *secret) get() (*unstructured.Unstructured, bool, error) {
@@ -76,16 +71,6 @@ func (s *secret) get() (*unstructured.Unstructured, bool, error) {
 	return u, u != nil, nil
 }
 
-// delete the secret represented by this component. It can return error when the API server does.
-func (s *secret) delete() error {
-	resourceClient := s.buildResourceClient()
-	err := resourceClient.Delete(s.name, &metav1.DeleteOptions{})
-	if err != nil && !errors.IsNotFound(err) {
-		return err
-	}
-	return nil
-}
-
 // newSecret instantiate a new Secret.
 func newSecret(
 	client dynamic.Interface,
diff --git a/pkg/controller/servicebindingrequest/servicebinder.go b/pkg/controller/servicebindingrequest/servicebinder.go
index f3e767f1..03e3aa32 100644
--- a/pkg/controller/servicebindingrequest/servicebinder.go
+++ b/pkg/controller/servicebindingrequest/servicebinder.go
@@ -152,12 +152,6 @@ func (b *serviceBinder) unbind() (reconcile.Result, error) {
 		return requeueError(err)
 	}
 
-	logger.Info("Deleting intermediary secret")
-	if err := b.secret.delete(); err != nil {
-		logger.Error(err, "On deleting intermediary secret.")
-		return requeueError(err)
-	}
-
 	logger.Debug("Removing resource finalizers...")
 	b.sbr.SetFinalizers(removeStringSlice(b.sbr.GetFinalizers(), finalizer))
 	if _, err := b.updateServiceBindingRequest(b.sbr); err != nil {
@@ -247,7 +241,8 @@ func (b *serviceBinder) bind() (reconcile.Result, error) {
 	sbrStatus := b.sbr.Status.DeepCopy()
 
 	b.logger.Info("Saving data on intermediary secret...")
-	secretObj, err := b.secret.commit(b.envVars)
+
+	secretObj, err := b.secret.createOrUpdate(b.envVars, b.sbr.AsOwnerReference())
 	if err != nil {
 		b.logger.Error(err, "On saving secret data..")
 		return b.onError(err, b.sbr, sbrStatus, nil)


Test patch:
diff --git a/pkg/controller/servicebindingrequest/secret_test.go b/pkg/controller/servicebindingrequest/secret_test.go
index 8f579eb0..ba7c4934 100644
--- a/pkg/controller/servicebindingrequest/secret_test.go
+++ b/pkg/controller/servicebindingrequest/secret_test.go
@@ -4,14 +4,26 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 
 	"github.com/redhat-developer/service-binding-operator/test/mocks"
 )
 
+var ownerRefController bool = true
+var secretOwnerReference = v1.OwnerReference{
+	Name:       "binding-request",
+	UID:        "c77ca1ae-72d0-4fdd-809f-58fdd37facf3",
+	Kind:       "ServiceBindingRequest",
+	APIVersion: "apps.openshift.io/v1alpha1",
+	Controller: &ownerRefController,
+}
+
 func assertSecretNamespacedName(t *testing.T, u *unstructured.Unstructured, ns, name string) {
 	assert.Equal(t, ns, u.GetNamespace())
 	assert.Equal(t, name, u.GetName())
+	ownerReference := u.GetOwnerReferences()
+	assert.Equal(t, secretOwnerReference, ownerReference[0])
 }
 
 func TestSecretNew(t *testing.T) {
@@ -29,18 +41,7 @@ func TestSecretNew(t *testing.T) {
 	)
 
 	t.Run("createOrUpdate", func(t *testing.T) {
-		u, err := s.createOrUpdate(data)
-		assert.NoError(t, err)
-		assertSecretNamespacedName(t, u, ns, name)
-	})
-
-	t.Run("Delete", func(t *testing.T) {
-		err := s.delete()
-		assert.NoError(t, err)
-	})
-
-	t.Run("Commit", func(t *testing.T) {
-		u, err := s.commit(data)
+		u, err := s.createOrUpdate(data, secretOwnerReference)
 		assert.NoError(t, err)
 		assertSecretNamespacedName(t, u, ns, name)
 	})