Skip to content

Commit

Permalink
add option to configure polling interval (#123)
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Sochacki <[email protected]>
  • Loading branch information
pascal-sochacki authored Mar 29, 2023
1 parent 3147b96 commit c6c358b
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 78 deletions.
13 changes: 10 additions & 3 deletions cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/logging"

"github.com/crossplane-contrib/provider-sql/apis"
Expand All @@ -38,8 +39,9 @@ func main() {
var (
app = kingpin.New(filepath.Base(os.Args[0]), "SQL support for Crossplane.").DefaultEnvars()
debug = app.Flag("debug", "Run with debug logging.").Short('d').Bool()
pollInterval = app.Flag("poll", "Poll interval controls how often an individual resource should be checked for drift.").Default("10m").Duration()
syncPeriod = app.Flag("sync", "Controller manager sync period such as 300ms, 1.5h, or 2h45m").Short('s').Default("1h").Duration()
leaderElection = app.Flag("leader-election", "Use leader election for the conroller manager.").Short('l').Default("false").OverrideDefaultFromEnvar("LEADER_ELECTION").Bool()
leaderElection = app.Flag("leader-election", "Use leader election for the conroller manager.").Short('l').Default("false").Envar("LEADER_ELECTION").Bool()
)
kingpin.MustParse(app.Parse(os.Args[1:]))

Expand All @@ -63,8 +65,13 @@ func main() {
SyncPeriod: syncPeriod,
})
kingpin.FatalIfError(err, "Cannot create controller manager")

kingpin.FatalIfError(apis.AddToScheme(mgr.GetScheme()), "Cannot add SQL APIs to scheme")
kingpin.FatalIfError(controller.Setup(mgr, log), "Cannot setup SQL controllers")

o := xpcontroller.Options{
Logger: log,
PollInterval: *pollInterval,
}

kingpin.FatalIfError(controller.Setup(mgr, o), "Cannot setup SQL controllers")
kingpin.FatalIfError(mgr.Start(ctrl.SetupSignalHandler()), "Cannot start controller manager")
}
6 changes: 3 additions & 3 deletions pkg/controller/mssql/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/providerconfig"
"github.com/crossplane/crossplane-runtime/pkg/resource"

Expand All @@ -30,7 +30,7 @@ import (

// Setup adds a controller that reconciles ProviderConfigs by accounting for
// their current usage.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o controller.Options) error {
name := providerconfig.ControllerName(v1alpha1.ProviderConfigGroupKind)

of := resource.ProviderConfigKinds{
Expand All @@ -43,6 +43,6 @@ func Setup(mgr ctrl.Manager, l logging.Logger) error {
For(&v1alpha1.ProviderConfig{}).
Watches(&source.Kind{Type: &v1alpha1.ProviderConfigUsage{}}, &resource.EnqueueRequestForProviderConfig{}).
Complete(providerconfig.NewReconciler(mgr, of,
providerconfig.WithLogger(l.WithValues("controller", name)),
providerconfig.WithLogger(o.Logger.WithValues("controller", name)),
providerconfig.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))))
}
9 changes: 4 additions & 5 deletions pkg/controller/mssql/database/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package database

import (
"context"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -28,8 +27,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
Expand All @@ -54,15 +53,15 @@ const (
)

// Setup adds a controller that reconciles Database managed resources.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o xpcontroller.Options) error {
name := managed.ControllerName(v1alpha1.DatabaseGroupKind)

t := resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{})
r := managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.DatabaseGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), usage: t, newClient: mssql.New}),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithPollInterval(10*time.Minute),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))

return ctrl.NewControllerManagedBy(mgr).
Expand Down
9 changes: 4 additions & 5 deletions pkg/controller/mssql/grant/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"sort"
"strings"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -32,8 +31,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"

Expand All @@ -57,16 +56,16 @@ const (
)

// Setup adds a controller that reconciles Grant managed resources.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o xpcontroller.Options) error {
name := managed.ControllerName(v1alpha1.GrantGroupKind)

t := resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{})
r := managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.GrantGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), usage: t, newClient: mssql.New}),
managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithPollInterval(10*time.Minute),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))

return ctrl.NewControllerManagedBy(mgr).
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ limitations under the License.
package mssql

import (
"github.com/crossplane-contrib/provider-sql/pkg/controller/mssql/grant"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/controller"

"github.com/crossplane-contrib/provider-sql/pkg/controller/mssql/config"
"github.com/crossplane-contrib/provider-sql/pkg/controller/mssql/database"
"github.com/crossplane-contrib/provider-sql/pkg/controller/mssql/grant"
"github.com/crossplane-contrib/provider-sql/pkg/controller/mssql/user"
)

// Setup creates all MSSQL controllers with the supplied logger and adds
// them to the supplied manager.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
for _, setup := range []func(ctrl.Manager, logging.Logger) error{
func Setup(mgr ctrl.Manager, o controller.Options) error {
for _, setup := range []func(ctrl.Manager, controller.Options) error{
config.Setup,
database.Setup,
user.Setup,
grant.Setup,
} {
if err := setup(mgr, l); err != nil {
if err := setup(mgr, o); err != nil {
return err
}
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/controller/mssql/user/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package user
import (
"context"
"fmt"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -30,8 +29,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/password"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
Expand Down Expand Up @@ -59,15 +58,15 @@ const (
)

// Setup adds a controller that reconciles User managed resources.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o xpcontroller.Options) error {
name := managed.ControllerName(v1alpha1.UserGroupKind)

t := resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{})
r := managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.UserGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), usage: t, newClient: mssql.New}),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithPollInterval(10*time.Minute),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))

return ctrl.NewControllerManagedBy(mgr).
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/mysql/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/providerconfig"
"github.com/crossplane/crossplane-runtime/pkg/resource"

Expand All @@ -30,7 +30,7 @@ import (

// Setup adds a controller that reconciles ProviderConfigs by accounting for
// their current usage.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o controller.Options) error {
name := providerconfig.ControllerName(v1alpha1.ProviderConfigGroupKind)

of := resource.ProviderConfigKinds{
Expand All @@ -43,6 +43,6 @@ func Setup(mgr ctrl.Manager, l logging.Logger) error {
For(&v1alpha1.ProviderConfig{}).
Watches(&source.Kind{Type: &v1alpha1.ProviderConfigUsage{}}, &resource.EnqueueRequestForProviderConfig{}).
Complete(providerconfig.NewReconciler(mgr, of,
providerconfig.WithLogger(l.WithValues("controller", name)),
providerconfig.WithLogger(o.Logger.WithValues("controller", name)),
providerconfig.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))))
}
9 changes: 4 additions & 5 deletions pkg/controller/mysql/database/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package database

import (
"context"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -28,8 +27,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
Expand All @@ -54,15 +53,15 @@ const (
)

// Setup adds a controller that reconciles Database managed resources.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o xpcontroller.Options) error {
name := managed.ControllerName(v1alpha1.DatabaseGroupKind)

t := resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{})
r := managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.DatabaseGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), usage: t, newDB: mysql.New}),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithPollInterval(10*time.Minute),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))

return ctrl.NewControllerManagedBy(mgr).
Expand Down
9 changes: 4 additions & 5 deletions pkg/controller/mysql/grant/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"regexp"
"sort"
"strings"
"time"

mysqldriver "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
Expand All @@ -33,8 +32,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"

Expand Down Expand Up @@ -65,16 +64,16 @@ var (
)

// Setup adds a controller that reconciles Grant managed resources.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o xpcontroller.Options) error {
name := managed.ControllerName(v1alpha1.GrantGroupKind)

t := resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{})
r := managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.GrantGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), usage: t, newDB: mysql.New}),
managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithPollInterval(10*time.Minute),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))

return ctrl.NewControllerManagedBy(mgr).
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package mysql
import (
ctrl "sigs.k8s.io/controller-runtime"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/controller"

"github.com/crossplane-contrib/provider-sql/pkg/controller/mysql/config"
"github.com/crossplane-contrib/provider-sql/pkg/controller/mysql/database"
Expand All @@ -29,14 +29,14 @@ import (

// Setup creates all MySQL controllers with the supplied logger and adds
// them to the supplied manager.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
for _, setup := range []func(ctrl.Manager, logging.Logger) error{
func Setup(mgr ctrl.Manager, o controller.Options) error {
for _, setup := range []func(ctrl.Manager, controller.Options) error{
config.Setup,
database.Setup,
user.Setup,
grant.Setup,
} {
if err := setup(mgr, l); err != nil {
if err := setup(mgr, o); err != nil {
return err
}
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/controller/mysql/user/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -30,8 +29,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/password"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
Expand Down Expand Up @@ -61,15 +60,15 @@ const (
)

// Setup adds a controller that reconciles User managed resources.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o xpcontroller.Options) error {
name := managed.ControllerName(v1alpha1.UserGroupKind)

t := resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{})
r := managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.UserGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), usage: t, newDB: mysql.New}),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithPollInterval(10*time.Minute),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))

return ctrl.NewControllerManagedBy(mgr).
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/postgresql/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/providerconfig"
"github.com/crossplane/crossplane-runtime/pkg/resource"

Expand All @@ -30,7 +30,7 @@ import (

// Setup adds a controller that reconciles ProviderConfigs by accounting for
// their current usage.
func Setup(mgr ctrl.Manager, l logging.Logger) error {
func Setup(mgr ctrl.Manager, o controller.Options) error {
name := providerconfig.ControllerName(v1alpha1.ProviderConfigGroupKind)

of := resource.ProviderConfigKinds{
Expand All @@ -43,6 +43,6 @@ func Setup(mgr ctrl.Manager, l logging.Logger) error {
For(&v1alpha1.ProviderConfig{}).
Watches(&source.Kind{Type: &v1alpha1.ProviderConfigUsage{}}, &resource.EnqueueRequestForProviderConfig{}).
Complete(providerconfig.NewReconciler(mgr, of,
providerconfig.WithLogger(l.WithValues("controller", name)),
providerconfig.WithLogger(o.Logger.WithValues("controller", name)),
providerconfig.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))))
}
Loading

0 comments on commit c6c358b

Please sign in to comment.