From edccc89182c3f53dfb8a960e07c4518302744f34 Mon Sep 17 00:00:00 2001
From: Chris Laprun <metacosm@gmail.com>
Date: Thu, 23 Jan 2020 14:32:54 +0100
Subject: [PATCH] feat: log to the operator's log

Relates to halkyonio/operator-framework#3
---
 cmd/kubedb-capability/plugin.go   | 10 ----------
 go.mod                            |  5 +++--
 go.sum                            |  4 ++++
 pkg/plugin/mongodb/resource.go    | 23 ++++++++++++++---------
 pkg/plugin/mysql/resource.go      | 23 ++++++++++++++---------
 pkg/plugin/postgresql/resource.go | 24 ++++++++++++------------
 6 files changed, 47 insertions(+), 42 deletions(-)

diff --git a/cmd/kubedb-capability/plugin.go b/cmd/kubedb-capability/plugin.go
index d850b8f..cd8da97 100644
--- a/cmd/kubedb-capability/plugin.go
+++ b/cmd/kubedb-capability/plugin.go
@@ -5,18 +5,8 @@ import (
 	"halkyon.io/kubedb-capability/pkg/plugin/mysql"
 	"halkyon.io/kubedb-capability/pkg/plugin/postgresql"
 	plugins "halkyon.io/operator-framework/plugins/capability"
-	"log"
-	"os"
-	"path/filepath"
 )
 
 func main() {
-	pluginName := filepath.Base(os.Args[0])
-	f, err := os.OpenFile(pluginName+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
-	if err != nil {
-		log.Fatalf("error opening file: %v", err)
-	}
-	log.SetOutput(f)
-
 	plugins.StartPluginServerFor(postgresql.NewPluginResource(), mysql.NewPluginResource(), mongodb.NewPluginResource())
 }
diff --git a/go.mod b/go.mod
index 66eb15b..57d4206 100644
--- a/go.mod
+++ b/go.mod
@@ -3,9 +3,10 @@ module halkyon.io/kubedb-capability
 go 1.13
 
 require (
-	github.com/appscode/go v0.0.0-20191025021232-311ac347b3ef
+	github.com/appscode/go v0.0.0-20191119085241-0887d8ec2ecc
+	github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd
 	halkyon.io/api v1.0.0-rc.3
-	halkyon.io/operator-framework v1.0.0-beta.3
+	halkyon.io/operator-framework v1.0.0-beta.3.0.20200123132453-20459cfad8a3
 	k8s.io/api v0.0.0-20190918195907-bd6ac527cfd2
 	k8s.io/apimachinery v0.17.0
 	kmodules.xyz/offshoot-api v0.0.0-20190901210649-de049192326c
diff --git a/go.sum b/go.sum
index d9a63fa..f2254ca 100644
--- a/go.sum
+++ b/go.sum
@@ -69,6 +69,8 @@ github.com/appscode/go v0.0.0-20191006073906-e3d193d493fc/go.mod h1:hUW7Fq0KY2/n
 github.com/appscode/go v0.0.0-20191016085057-e186b6c94a3b/go.mod h1:hUW7Fq0KY2/ntGnYAzemyUpIhLL6bXrTljN6SRY/+Lc=
 github.com/appscode/go v0.0.0-20191025021232-311ac347b3ef h1:nTbtvdVAVwFgKANCR4KU36Jo1V9Xs/Z2GMEAlpOVG1Y=
 github.com/appscode/go v0.0.0-20191025021232-311ac347b3ef/go.mod h1:hUW7Fq0KY2/ntGnYAzemyUpIhLL6bXrTljN6SRY/+Lc=
+github.com/appscode/go v0.0.0-20191119085241-0887d8ec2ecc h1:Q5jwbRq/a1Tien+Is8Zn6HMBRgE13+UOQ9mTvrIuvuE=
+github.com/appscode/go v0.0.0-20191119085241-0887d8ec2ecc/go.mod h1:OawnOmAL4ZX3YaPdN+8HTNwBveT1jMsqP74moa9XUbE=
 github.com/appscode/osm v0.12.0/go.mod h1:M2dkG7yApVG4hWvRli6FgDxYItfkkN7jZditR2AAvdA=
 github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
 github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
@@ -811,6 +813,8 @@ halkyon.io/api v1.0.0-rc.3 h1:evcR0y3QEXwLdYY9z1PKBZIXgsuxu//M81fEVGwLjGU=
 halkyon.io/api v1.0.0-rc.3/go.mod h1:bCOZh54+rJ4SQad0L0OMO0B+Jf7J3OtKp7SPmdLowGE=
 halkyon.io/operator-framework v1.0.0-beta.3 h1:sVZKZcHYs6iUYfDqzyCzzHjTMUFIoj8/a7XiLEAS4v4=
 halkyon.io/operator-framework v1.0.0-beta.3/go.mod h1:sOn1iLYLTQ9JmoVdZ7WNvhlCDVPLYRBKnaxYBMJQ3OI=
+halkyon.io/operator-framework v1.0.0-beta.3.0.20200123132453-20459cfad8a3 h1:kAHFnbRBGvnDinRKMBYUEKjqiaSv56jLjerc0si9EiI=
+halkyon.io/operator-framework v1.0.0-beta.3.0.20200123132453-20459cfad8a3/go.mod h1:sOn1iLYLTQ9JmoVdZ7WNvhlCDVPLYRBKnaxYBMJQ3OI=
 honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20180920025451-e3ad64cb4ed3/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
diff --git a/pkg/plugin/mongodb/resource.go b/pkg/plugin/mongodb/resource.go
index c997e41..dcf1b13 100644
--- a/pkg/plugin/mongodb/resource.go
+++ b/pkg/plugin/mongodb/resource.go
@@ -2,6 +2,7 @@ package mongodb
 
 import (
 	"github.com/appscode/go/strings"
+	"github.com/hashicorp/go-hclog"
 	"halkyon.io/api/capability/v1beta1"
 	beta1 "halkyon.io/api/v1beta1"
 	"halkyon.io/kubedb-capability/pkg/plugin"
@@ -14,7 +15,7 @@ import (
 var _ capability.PluginResource = &MongoDBPluginResource{}
 
 type MongoDBPluginResource struct {
-	capability.SimplePluginResourceStem
+	capability.QueryingSimplePluginResourceStem
 }
 
 func (m MongoDBPluginResource) GetDependentResourcesWith(owner beta1.HalkyonResource) []framework.DependentResource {
@@ -26,19 +27,23 @@ func (m MongoDBPluginResource) GetDependentResourcesWith(owner beta1.HalkyonReso
 }
 
 func NewPluginResource() capability.PluginResource {
+	return &MongoDBPluginResource{capability.NewQueryingSimplePluginResourceStem(v1beta1.DatabaseCategory, resolver)}
+}
+
+func resolver(logger hclog.Logger) capability.TypeInfo {
 	list, err := plugin.Client.MongoDBVersions().List(v1.ListOptions{})
-	versions := []string{}
-	if err == nil {
-		versions = make([]string, 0, len(list.Items))
-		for _, version := range list.Items {
-			if !version.Spec.Deprecated && !strings.Contains(versions, version.Spec.Version) {
-				versions = append(versions, version.Spec.Version)
-			}
+	if err != nil {
+		logger.Error("error retrieving versions: %v", err)
+	}
+	versions := make([]string, 0, len(list.Items))
+	for _, version := range list.Items {
+		if !version.Spec.Deprecated && !strings.Contains(versions, version.Spec.Version) {
+			versions = append(versions, version.Spec.Version)
 		}
 	}
 	info := capability.TypeInfo{
 		Type:     kubedbv1.ResourceKindMongoDB,
 		Versions: versions,
 	}
-	return &MongoDBPluginResource{capability.NewSimplePluginResourceStem(v1beta1.DatabaseCategory, info)}
+	return info
 }
diff --git a/pkg/plugin/mysql/resource.go b/pkg/plugin/mysql/resource.go
index 77c5eab..0b940a7 100644
--- a/pkg/plugin/mysql/resource.go
+++ b/pkg/plugin/mysql/resource.go
@@ -2,6 +2,7 @@ package mysql
 
 import (
 	"github.com/appscode/go/strings"
+	"github.com/hashicorp/go-hclog"
 	"halkyon.io/api/capability/v1beta1"
 	beta1 "halkyon.io/api/v1beta1"
 	"halkyon.io/kubedb-capability/pkg/plugin"
@@ -14,7 +15,7 @@ import (
 var _ capability.PluginResource = &MySQLPluginResource{}
 
 type MySQLPluginResource struct {
-	capability.SimplePluginResourceStem
+	capability.QueryingSimplePluginResourceStem
 }
 
 func (m MySQLPluginResource) GetDependentResourcesWith(owner beta1.HalkyonResource) []framework.DependentResource {
@@ -26,19 +27,23 @@ func (m MySQLPluginResource) GetDependentResourcesWith(owner beta1.HalkyonResour
 }
 
 func NewPluginResource() capability.PluginResource {
+	return &MySQLPluginResource{capability.NewQueryingSimplePluginResourceStem(v1beta1.DatabaseCategory, resolver)}
+}
+
+func resolver(logger hclog.Logger) capability.TypeInfo {
 	list, err := plugin.Client.MySQLVersions().List(v1.ListOptions{})
-	versions := []string{}
-	if err == nil {
-		versions = make([]string, 0, len(list.Items))
-		for _, version := range list.Items {
-			if !version.Spec.Deprecated && !strings.Contains(versions, version.Spec.Version) {
-				versions = append(versions, version.Spec.Version)
-			}
+	if err != nil {
+		logger.Error("error retrieving versions: %v", err)
+	}
+	versions := make([]string, 0, len(list.Items))
+	for _, version := range list.Items {
+		if !version.Spec.Deprecated && !strings.Contains(versions, version.Spec.Version) {
+			versions = append(versions, version.Spec.Version)
 		}
 	}
 	info := capability.TypeInfo{
 		Type:     kubedbv1.ResourceKindMySQL,
 		Versions: versions,
 	}
-	return &MySQLPluginResource{capability.NewSimplePluginResourceStem(v1beta1.DatabaseCategory, info)}
+	return info
 }
diff --git a/pkg/plugin/postgresql/resource.go b/pkg/plugin/postgresql/resource.go
index 8af2f05..fbd53f5 100644
--- a/pkg/plugin/postgresql/resource.go
+++ b/pkg/plugin/postgresql/resource.go
@@ -2,6 +2,7 @@ package postgresql
 
 import (
 	"github.com/appscode/go/strings"
+	"github.com/hashicorp/go-hclog"
 	"halkyon.io/api/capability/v1beta1"
 	v1beta12 "halkyon.io/api/v1beta1"
 	"halkyon.io/kubedb-capability/pkg/plugin"
@@ -9,35 +10,34 @@ import (
 	"halkyon.io/operator-framework/plugins/capability"
 	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	kubedbv1 "kubedb.dev/apimachinery/apis/kubedb/v1alpha1"
-	"log"
 )
 
 var _ capability.PluginResource = &PostgresPluginResource{}
 
 func NewPluginResource() capability.PluginResource {
-	log.Println("Starting Postgres support")
+	return &PostgresPluginResource{capability.NewQueryingSimplePluginResourceStem(v1beta1.DatabaseCategory, resolver)}
+}
+
+func resolver(logger hclog.Logger) capability.TypeInfo {
 	list, err := plugin.Client.PostgresVersions().List(v1.ListOptions{})
 	if err != nil {
-		log.Printf("error retrieving versions: %v", err)
+		logger.Error("error retrieving versions: %v", err)
 	}
-	versions := []string{}
-	if err == nil {
-		versions = make([]string, 0, len(list.Items))
-		for _, version := range list.Items {
-			if !version.Spec.Deprecated && !strings.Contains(versions, version.Spec.Version) {
-				versions = append(versions, version.Spec.Version)
-			}
+	versions := make([]string, 0, len(list.Items))
+	for _, version := range list.Items {
+		if !version.Spec.Deprecated && !strings.Contains(versions, version.Spec.Version) {
+			versions = append(versions, version.Spec.Version)
 		}
 	}
 	info := capability.TypeInfo{
 		Type:     kubedbv1.ResourceKindPostgres,
 		Versions: versions,
 	}
-	return &PostgresPluginResource{capability.NewSimplePluginResourceStem(v1beta1.DatabaseCategory, info)}
+	return info
 }
 
 type PostgresPluginResource struct {
-	capability.SimplePluginResourceStem
+	capability.QueryingSimplePluginResourceStem
 }
 
 func (p *PostgresPluginResource) GetDependentResourcesWith(owner v1beta12.HalkyonResource) []framework.DependentResource {