Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up both tests and main.go in one place + add tests to check missing deps #208

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions controllers/manager_setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package controllers

import (
"net/http"

"github.com/ibm/cloud-operators/internal/ibmcloud"
"github.com/ibm/cloud-operators/internal/ibmcloud/auth"
"github.com/ibm/cloud-operators/internal/ibmcloud/cfservice"
"github.com/ibm/cloud-operators/internal/ibmcloud/iam"
"github.com/ibm/cloud-operators/internal/ibmcloud/resource"
"github.com/pkg/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// Controllers passes back references to set up controllers for test mocking purposes
type Controllers struct {
*BindingReconciler
*ServiceReconciler
*TokenReconciler
}

func SetUpControllers(mgr ctrl.Manager) (*Controllers, error) {
c := setUpControllerDependencies(mgr)
if err := c.BindingReconciler.SetupWithManager(mgr); err != nil {
return nil, errors.Wrap(err, "Unable to setup binding controller")
}
if err := c.ServiceReconciler.SetupWithManager(mgr); err != nil {
return nil, errors.Wrap(err, "Unable to setup service controller")
}
if err := c.TokenReconciler.SetupWithManager(mgr); err != nil {
return nil, errors.Wrap(err, "Unable to setup token controller")
}

// +kubebuilder:scaffold:builder
return c, nil
}

func setUpControllerDependencies(mgr ctrl.Manager) *Controllers {
return &Controllers{
BindingReconciler: &BindingReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Binding"),
Scheme: mgr.GetScheme(),

CreateCFServiceKey: cfservice.CreateKey,
CreateResourceServiceKey: resource.CreateKey,
DeleteCFServiceKey: cfservice.DeleteKey,
DeleteResourceServiceKey: resource.DeleteKey,
GetCFServiceKeyCredentials: cfservice.GetKey,
GetIBMCloudInfo: ibmcloud.GetInfo,
GetResourceServiceKey: resource.GetKey,
GetServiceInstanceCRN: resource.GetServiceInstanceCRN,
GetServiceName: resource.GetServiceName,
GetServiceRoleCRN: iam.GetServiceRoleCRN,
SetControllerReference: controllerutil.SetControllerReference,
},
ServiceReconciler: &ServiceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Service"),
Scheme: mgr.GetScheme(),

CreateCFServiceInstance: cfservice.CreateInstance,
CreateResourceServiceInstance: resource.CreateServiceInstance,
DeleteCFServiceInstance: cfservice.DeleteInstance,
DeleteResourceServiceInstance: resource.DeleteServiceInstance,
GetCFServiceInstance: cfservice.GetInstance,
GetIBMCloudInfo: ibmcloud.GetInfo,
GetResourceServiceAliasInstance: resource.GetServiceAliasInstance,
GetResourceServiceInstanceState: resource.GetServiceInstanceState,
UpdateResourceServiceInstance: resource.UpdateServiceInstance,
},
TokenReconciler: &TokenReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Token"),
Scheme: mgr.GetScheme(),
Authenticate: auth.New(http.DefaultClient),
},
}
}
51 changes: 51 additions & 0 deletions controllers/manager_setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package controllers

import (
"reflect"
"testing"
"unicode"
)

func TestSetUpControllers(t *testing.T) {
if testing.Short() {
t.SkipNow()
}

c := setUpControllerDependencies(k8sManager)

assertNoNilFields(t, c)
}

func assertNoNilFields(t *testing.T, v interface{}) {
assertNoNilFieldsReflect(t, reflect.ValueOf(v), "")
if t.Failed() {
t.Log("See the above failures for fields that must be filled in, inside controllers/manager_setup.go")
}
}

func assertNoNilFieldsReflect(t *testing.T, v reflect.Value, name string) {
t.Helper()
switch v.Kind() {
case reflect.Ptr:
assertNoNilFieldsReflect(t, v.Elem(), name)
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
typeField := v.Type().Field(i)
if len(typeField.Name) > 0 && unicode.IsUpper([]rune(typeField.Name)[0]) {
assertNoNilFieldsReflect(t, field, joinFields(name, typeField.Name))
}
}
default:
if v.IsZero() {
t.Errorf("Field %q is not set up properly for controllers", name)
}
}
}

func joinFields(a, b string) string {
if a == "" {
return b
}
return a + "." + b
}
57 changes: 6 additions & 51 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ import (
"testing"
"time"

"github.com/ibm/cloud-operators/internal/ibmcloud"
"github.com/ibm/cloud-operators/internal/ibmcloud/auth"
"github.com/ibm/cloud-operators/internal/ibmcloud/cfservice"
"github.com/ibm/cloud-operators/internal/ibmcloud/iam"
"github.com/ibm/cloud-operators/internal/ibmcloud/resource"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
v1 "k8s.io/api/core/v1"
Expand All @@ -42,7 +37,6 @@ import (
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/envtest"
runtimeZap "sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand Down Expand Up @@ -140,56 +134,17 @@ func mainSetup(ctx context.Context) error {
return err
}

if err = (&BindingReconciler{
Client: k8sManager.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Binding"),
Scheme: k8sManager.GetScheme(),

CreateCFServiceKey: cfservice.CreateKey,
CreateResourceServiceKey: resource.CreateKey,
DeleteCFServiceKey: cfservice.DeleteKey,
DeleteResourceServiceKey: resource.DeleteKey,
GetCFServiceKeyCredentials: cfservice.GetKey,
GetIBMCloudInfo: ibmcloud.GetInfo,
GetResourceServiceKey: resource.GetKey,
GetServiceInstanceCRN: resource.GetServiceInstanceCRN,
GetServiceName: resource.GetServiceName,
GetServiceRoleCRN: iam.GetServiceRoleCRN,
SetControllerReference: controllerutil.SetControllerReference,
}).SetupWithManager(k8sManager); err != nil {
return errors.Wrap(err, "Failed to set up binding controller")
}
if err = (&ServiceReconciler{
Client: k8sManager.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Service"),
Scheme: k8sManager.GetScheme(),

CreateCFServiceInstance: cfservice.CreateInstance,
CreateResourceServiceInstance: resource.CreateServiceInstance,
DeleteResourceServiceInstance: resource.DeleteServiceInstance,
GetCFServiceInstance: cfservice.GetInstance,
GetIBMCloudInfo: ibmcloud.GetInfo,
GetResourceServiceAliasInstance: resource.GetServiceAliasInstance,
GetResourceServiceInstanceState: resource.GetServiceInstanceState,
UpdateResourceServiceInstance: resource.UpdateServiceInstance,
}).SetupWithManager(k8sManager); err != nil {
return errors.Wrap(err, "Failed to set up service controller")
}
tokenReconciler := &TokenReconciler{
Client: k8sManager.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Token"),
Scheme: k8sManager.GetScheme(),
Authenticate: auth.New(http.DefaultClient),
c, err := SetUpControllers(k8sManager)
if err != nil {
return err
}

setTokenHTTPClient = func(tb testing.TB, authenticator auth.Authenticator) {
tokenReconciler.Authenticate = authenticator
c.TokenReconciler.Authenticate = authenticator
tb.Cleanup(func() {
tokenReconciler.Authenticate = auth.New(http.DefaultClient)
c.TokenReconciler.Authenticate = auth.New(http.DefaultClient)
})
}
if err = tokenReconciler.SetupWithManager(k8sManager); err != nil {
return errors.Wrap(err, "Failed to set up token controller")
}

go func() {
err = k8sManager.Start(ctx.Done())
Expand Down
53 changes: 2 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ package main

import (
"flag"
"net/http"
"os"

"github.com/ibm/cloud-operators/controllers"
"github.com/ibm/cloud-operators/internal/ibmcloud"
"github.com/ibm/cloud-operators/internal/ibmcloud/auth"
"github.com/ibm/cloud-operators/internal/ibmcloud/cfservice"
"github.com/ibm/cloud-operators/internal/ibmcloud/iam"
"github.com/ibm/cloud-operators/internal/ibmcloud/resource"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

ibmcloudv1 "github.com/ibm/cloud-operators/api/v1"
Expand Down Expand Up @@ -77,52 +70,10 @@ func main() {
os.Exit(1)
}

if err = (&controllers.BindingReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Binding"),
Scheme: mgr.GetScheme(),

CreateCFServiceKey: cfservice.CreateKey,
CreateResourceServiceKey: resource.CreateKey,
DeleteCFServiceKey: cfservice.DeleteKey,
DeleteResourceServiceKey: resource.DeleteKey,
GetCFServiceKeyCredentials: cfservice.GetKey,
GetIBMCloudInfo: ibmcloud.GetInfo,
GetResourceServiceKey: resource.GetKey,
GetServiceName: resource.GetServiceName,
GetServiceRoleCRN: iam.GetServiceRoleCRN,
SetControllerReference: controllerutil.SetControllerReference,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Binding")
os.Exit(1)
}
if err = (&controllers.ServiceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Service"),
Scheme: mgr.GetScheme(),

CreateCFServiceInstance: cfservice.CreateInstance,
CreateResourceServiceInstance: resource.CreateServiceInstance,
DeleteResourceServiceInstance: resource.DeleteServiceInstance,
GetCFServiceInstance: cfservice.GetInstance,
GetIBMCloudInfo: ibmcloud.GetInfo,
GetResourceServiceAliasInstance: resource.GetServiceAliasInstance,
GetResourceServiceInstanceState: resource.GetServiceInstanceState,
UpdateResourceServiceInstance: resource.UpdateServiceInstance,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Service")
os.Exit(1)
}
if err = (&controllers.TokenReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Token"),
Scheme: mgr.GetScheme(),
Authenticate: auth.New(http.DefaultClient),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Token")
if _, err := controllers.SetUpControllers(mgr); err != nil {
setupLog.Error(err, "Unable to set up controllers")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
Expand Down