Repository: redhat-developer/service-binding-operator (go)

Issue:
ServiceBindingRequest CR requires both resourceName and resourceKind despite CRD spec
# Steps to reproduce

Check the current version of the application...

```sh
$ oc get dc service-binding-demo -o jsonpath='{.status.latestVersion}'
9
```
... and check for the `envFrom` presence...
```sh
$ oc get dc service-binding-demo -o jsonpath='{.spec.template.spec.containers[0].envFrom}'
```
...nothing, which is expected.

So the current version is `9`. Then, after applying the `ServiceBindingRequest` I'd expect that to be `10` (which would indicate that the DC was updated by the operator).

Now, apply `ServiceBindingRequest` according to the [CRD](https://github.com/redhat-developer/service-binding-operator/blob/master/deploy/crds/apps_v1alpha1_servicebindingrequest_crd.yaml#L67) (and [code](https://github.com/redhat-developer/service-binding-operator/blob/master/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go#L53)) - i.e. `resourcenName`, `resourceVersion` and `resourceRef` (*Variant N*)...
```sh
$ cat tmp/sbr-n.yaml
```
```yaml
apiVersion: apps.openshift.io/v1alpha1
kind: ServiceBindingRequest
metadata:
  name: binding-request
  namespace: service-binding-demo
spec:
  applicationSelector:
    matchLabels:
      connects-to: postgres
      environment: demo
    resourceKind: DeploymentConfig
  backingServiceSelector:
    resourceName: postgresql.baiju.dev
    resourceVersion: v1alpha1
    resourceRef: db-demo
```
```sh
$ oc apply -f tmp/sbr-n.yaml
The ServiceBindingRequest "binding-request" is invalid: []: Invalid value: map[string]interface {}{"apiVersion":"apps.openshift.io/v1alpha1", "kind":"ServiceBindingRequest", "metadata":map[string]interface {}{"annotations":map[string]interface {}{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"apps.openshift.io/v1alpha1\",\"kind\":\"ServiceBindingRequest\",\"metadata\":{\"annotations\":{},\"name\":\"binding-request\",\"namespace\":\"service-binding-demo\"},\"spec\":{\"applicationSelector\":{\"matchLabels\":{\"connects-to\":\"postgres\",\"environment\":\"demo\"},\"resourceKind\":\"DeploymentConfig\"},\"backingServiceSelector\":{\"resourceName\":\"postgresql.baiju.dev\",\"resourceRef\":\"db-demo\",\"resourceVersion\":\"v1alpha1\"}}}\n"}, "creationTimestamp":"2019-07-12T09:52:13Z", "generation":1, "name":"binding-request", "namespace":"service-binding-demo", "uid":"b98233f4-a48a-11e9-934e-024164363ef2"}, "spec":map[string]interface {}{"applicationSelector":map[string]interface {}{"matchLabels":map[string]interface {}{"connects-to":"postgres", "environment":"demo"}, "resourceKind":"DeploymentConfig"}, "backingServiceSelector":map[string]interface {}{"resourceName":"postgresql.baiju.dev", "resourceRef":"db-demo", "resourceVersion":"v1alpha1"}}}: validation failure list:
spec.backingServiceSelector.resourceKind in body is required
```
... and that caused an error complaining that `resourceKind` is required. So try to use the `resourceKind` instead (*Variant K*):

```sh
$ cat tmp/sbr-k.yaml 
```
```yaml
apiVersion: apps.openshift.io/v1alpha1
kind: ServiceBindingRequest
metadata:
  name: binding-request
  namespace: service-binding-demo
spec:
  applicationSelector:
    matchLabels:
      connects-to: postgres
      environment: demo
    resourceKind: DeploymentConfig
  backingServiceSelector:
    resourceKind: Database
    resourceVersion: v1alpha1
    resourceRef: db-demo
```
```sh
$ oc apply -f tmp/sbr-k.yaml
servicebindingrequest.apps.openshift.io/binding-request created
```
After a while check for the status of the DC...
```sh
$ oc get dc service-binding-demo -o jsonpath='{.status.latestVersion}'
9
$ oc get dc service-binding-demo -o jsonpath='{.spec.template.spec.containers[0].envFrom}'
```

... the version is still `9` and no `envFrom` was injeceted - nothing happeed so it does not work.

So try to use both `resourceName` and `resourceKind` at the same time (*Variant KN*):

```sh
$ oc delete servicebindingrequest binding-request
servicebindingrequest.apps.openshift.io "binding-request" deleted
$ cat tmp/sbr-kn.yaml 
```
```yaml
apiVersion: apps.openshift.io/v1alpha1
kind: ServiceBindingRequest
metadata:
  name: binding-request
  namespace: service-binding-demo
spec:
  applicationSelector:
    matchLabels:
      connects-to: postgres
      environment: demo
    resourceKind: DeploymentConfig
  backingServiceSelector:
    resourceName: postgresql.baiju.dev
    resourceKind: Database
    resourceVersion: v1alpha1
    resourceRef: db-demo
```

This works!

```sh
$ oc get dc service-binding-demo -o jsonpath='{.status.latestVersion}'
10

$ oc get dc service-binding-demo -o jsonpath='{.spec.template.spec.containers[0].envFrom}'
[map[secretRef:map[name:binding-request]]]
```

Pull request description:
Use ResourceKind instead of ResourceName
Fixes #63

Patch:
diff --git a/deploy/crds/apps_v1alpha1_servicebindingrequest_crd.yaml b/deploy/crds/apps_v1alpha1_servicebindingrequest_crd.yaml
index 055151e6..e88d4af9 100644
--- a/deploy/crds/apps_v1alpha1_servicebindingrequest_crd.yaml
+++ b/deploy/crds/apps_v1alpha1_servicebindingrequest_crd.yaml
@@ -33,8 +33,8 @@ spec:
               description: "ApplicationSelector is used to identify the application
                 connecting to the backing service operator. Example 1: \tapplicationSelector:
                 \t\tmatchLabels: \t\t\tconnects-to: postgres \t\t\tenvironment: stage
-                \t\tresourceKind: Deployment Example 2: \tapplicationSelector: \t\tresourceKind:
-                Deployment \t\tresourceName: my-app"
+                \t\tresourceKind: Deployment Example 2: \tapplicationSelector: \t\tmatchLabels:
+                \t\t\tconnects-to: postgres \t\t\tenvironment: stage"
               properties:
                 matchLabels:
                   additionalProperties:
@@ -54,18 +54,18 @@ spec:
                 MySQL or CouchDB), messaging/queueing systems (such as RabbitMQ or
                 Beanstalkd), SMTP services for outbound email (such as Postfix), and
                 caching systems (such as Memcached).  Example 1: \tbackingServiceSelector:
-                \t\tresourceName: database.example.org      resourceRef: mysql-database
-                Example 2: \tbackingServiceSelector: \t\tresourceName: database.example.org
+                \t\tresourceKind: database.example.org      resourceRef: mysql-database
+                Example 2: \tbackingServiceSelector: \t\tresourceKind: database.example.org
                 \t\tresourceVersion: v1alpha1      resourceRef: mysql-database"
               properties:
-                resourceName:
+                resourceKind:
                   type: string
                 resourceRef:
                   type: string
                 resourceVersion:
                   type: string
               required:
-              - resourceName
+              - resourceKind
               - resourceVersion
               - resourceRef
               type: object
diff --git a/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go b/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go
index 66ba584a..4ce283e9 100644
--- a/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go
+++ b/pkg/apis/apps/v1alpha1/servicebindingrequest_types.go
@@ -24,11 +24,11 @@ type ServiceBindingRequestSpec struct {
 	//
 	// Example 1:
 	//	backingServiceSelector:
-	//		resourceName: database.example.org
+	//		resourceKind: database.example.org
 	//      resourceRef: mysql-database
 	// Example 2:
 	//	backingServiceSelector:
-	//		resourceName: database.example.org
+	//		resourceKind: database.example.org
 	//		resourceVersion: v1alpha1
 	//      resourceRef: mysql-database
 	BackingServiceSelector BackingServiceSelector `json:"backingServiceSelector"`
@@ -43,15 +43,16 @@ type ServiceBindingRequestSpec struct {
 	//		resourceKind: Deployment
 	// Example 2:
 	//	applicationSelector:
-	//		resourceKind: Deployment
-	//		resourceName: my-app
+	//		matchLabels:
+	//			connects-to: postgres
+	//			environment: stage
 	ApplicationSelector ApplicationSelector `json:"applicationSelector"`
 }
 
 // BackingServiceSelector defines the selector based on resource name, version, and resource kind
 // +k8s:openapi-gen=true
 type BackingServiceSelector struct {
-	ResourceName    string `json:"resourceName"`
+	ResourceKind    string `json:"resourceKind"`
 	ResourceVersion string `json:"resourceVersion"`
 	ResourceRef     string `json:"resourceRef"`
 }
diff --git a/pkg/apis/apps/v1alpha1/zz_generated.openapi.go b/pkg/apis/apps/v1alpha1/zz_generated.openapi.go
index 4f43ef39..ea0fa1c8 100644
--- a/pkg/apis/apps/v1alpha1/zz_generated.openapi.go
+++ b/pkg/apis/apps/v1alpha1/zz_generated.openapi.go
@@ -60,7 +60,7 @@ func schema_pkg_apis_apps_v1alpha1_BackingServiceSelector(ref common.ReferenceCa
 			SchemaProps: spec.SchemaProps{
 				Description: "BackingServiceSelector defines the selector based on resource name, version, and resource kind",
 				Properties: map[string]spec.Schema{
-					"resourceName": {
+					"resourceKind": {
 						SchemaProps: spec.SchemaProps{
 							Type:   []string{"string"},
 							Format: "",
@@ -79,7 +79,7 @@ func schema_pkg_apis_apps_v1alpha1_BackingServiceSelector(ref common.ReferenceCa
 						},
 					},
 				},
-				Required: []string{"resourceName", "resourceVersion", "resourceRef"},
+				Required: []string{"resourceKind", "resourceVersion", "resourceRef"},
 			},
 		},
 		Dependencies: []string{},
@@ -137,13 +137,13 @@ func schema_pkg_apis_apps_v1alpha1_ServiceBindingRequestSpec(ref common.Referenc
 				Properties: map[string]spec.Schema{
 					"backingServiceSelector": {
 						SchemaProps: spec.SchemaProps{
-							Description: "BackingServiceSelector is used to identify the backing service operator.\n\nRefer: https://12factor.net/backing-services A backing service is any service the app consumes over the network as part of its normal operation. Examples include datastores (such as MySQL or CouchDB), messaging/queueing systems (such as RabbitMQ or Beanstalkd), SMTP services for outbound email (such as Postfix), and caching systems (such as Memcached).\n\nExample 1:\n\tbackingServiceSelector:\n\t\tresourceName: database.example.org\n     resourceRef: mysql-database\nExample 2:\n\tbackingServiceSelector:\n\t\tresourceName: database.example.org\n\t\tresourceVersion: v1alpha1\n     resourceRef: mysql-database",
+							Description: "BackingServiceSelector is used to identify the backing service operator.\n\nRefer: https://12factor.net/backing-services A backing service is any service the app consumes over the network as part of its normal operation. Examples include datastores (such as MySQL or CouchDB), messaging/queueing systems (such as RabbitMQ or Beanstalkd), SMTP services for outbound email (such as Postfix), and caching systems (such as Memcached).\n\nExample 1:\n\tbackingServiceSelector:\n\t\tresourceKind: database.example.org\n     resourceRef: mysql-database\nExample 2:\n\tbackingServiceSelector:\n\t\tresourceKind: database.example.org\n\t\tresourceVersion: v1alpha1\n     resourceRef: mysql-database",
 							Ref:         ref("github.com/redhat-developer/service-binding-operator/pkg/apis/apps/v1alpha1.BackingServiceSelector"),
 						},
 					},
 					"applicationSelector": {
 						SchemaProps: spec.SchemaProps{
-							Description: "ApplicationSelector is used to identify the application connecting to the backing service operator. Example 1:\n\tapplicationSelector:\n\t\tmatchLabels:\n\t\t\tconnects-to: postgres\n\t\t\tenvironment: stage\n\t\tresourceKind: Deployment\nExample 2:\n\tapplicationSelector:\n\t\tresourceKind: Deployment\n\t\tresourceName: my-app",
+							Description: "ApplicationSelector is used to identify the application connecting to the backing service operator. Example 1:\n\tapplicationSelector:\n\t\tmatchLabels:\n\t\t\tconnects-to: postgres\n\t\t\tenvironment: stage\n\t\tresourceKind: Deployment\nExample 2:\n\tapplicationSelector:\n\t\tmatchLabels:\n\t\t\tconnects-to: postgres\n\t\t\tenvironment: stage",
 							Ref:         ref("github.com/redhat-developer/service-binding-operator/pkg/apis/apps/v1alpha1.ApplicationSelector"),
 						},
 					},
diff --git a/pkg/controller/servicebindingrequest/planner.go b/pkg/controller/servicebindingrequest/planner.go
index b3a59790..9d7a9983 100644
--- a/pkg/controller/servicebindingrequest/planner.go
+++ b/pkg/controller/servicebindingrequest/planner.go
@@ -38,13 +38,13 @@ type Plan struct {
 // searchCRDDescription based on BackingServiceSelector instance, find a CustomResourceDefinitionDescription
 // to return, otherwise creating a not-found error.
 func (p *Planner) searchCRDDescription() (*olmv1alpha1.CRDDescription, error) {
-	var resourceName = strings.ToLower(
-		fmt.Sprintf(".%s", p.sbr.Spec.BackingServiceSelector.ResourceName))
+	var resourceKind = strings.ToLower(
+		fmt.Sprintf(".%s", p.sbr.Spec.BackingServiceSelector.ResourceKind))
 	var resourceVersion = strings.ToLower(p.sbr.Spec.BackingServiceSelector.ResourceVersion)
 	var err error
 
 	logger := p.logger.WithValues(
-		"BackingServiceSelector.ResourceName", resourceName,
+		"BackingServiceSelector.ResourceKind", resourceKind,
 		"BackingServiceSelector.ResourceVersion", resourceVersion)
 	logger.Info("Looking for a CSV based on backing-selector")
 	csvList := &olmv1alpha1.ClusterServiceVersionList{}
@@ -67,7 +67,7 @@ func (p *Planner) searchCRDDescription() (*olmv1alpha1.CRDDescription, error) {
 			logger.Info("Inspecting CustomResourceDefinitionDescription object...")
 
 			// checking for suffix since is expected to have object type as prefix
-			if !strings.HasSuffix(strings.ToLower(crd.Name), resourceName) {
+			if !strings.HasSuffix(strings.ToLower(crd.Name), resourceKind) {
 				continue
 			}
 			if crd.Version != "" && resourceVersion != strings.ToLower(crd.Version) {
@@ -87,7 +87,7 @@ func (p *Planner) searchCRDDescription() (*olmv1alpha1.CRDDescription, error) {
 func (p *Planner) searchCR(kind string) (*ustrv1.Unstructured, error) {
 	var resourceRef = p.sbr.Spec.BackingServiceSelector.ResourceRef
 	var apiVersion = fmt.Sprintf("%s/%s",
-		p.sbr.Spec.BackingServiceSelector.ResourceName,
+		p.sbr.Spec.BackingServiceSelector.ResourceKind,
 		p.sbr.Spec.BackingServiceSelector.ResourceVersion)
 	var err error
 


Test patch:
diff --git a/test/mocks/mocks.go b/test/mocks/mocks.go
index 2a7ad624..f7c8a025 100644
--- a/test/mocks/mocks.go
+++ b/test/mocks/mocks.go
@@ -143,7 +143,7 @@ func ServiceBindingRequestMock(
 		},
 		Spec: v1alpha1.ServiceBindingRequestSpec{
 			BackingServiceSelector: v1alpha1.BackingServiceSelector{
-				ResourceName:    CRDName,
+				ResourceKind:    CRDName,
 				ResourceVersion: CRDVersion,
 				ResourceRef:     resourceRef,
 			},