From c13d8f47293ad0f7a3a206af0d1313bcb82bbc2d Mon Sep 17 00:00:00 2001 From: Dasha Komsa Date: Tue, 27 Feb 2024 14:26:15 +0000 Subject: [PATCH] refactor with a struct to hold ansiblerun controller setup options --- cmd/provider/main.go | 9 ++++++++- internal/controller/ansible.go | 18 ++++++++---------- internal/controller/ansibleRun/ansibleRun.go | 18 +++++++++++++----- internal/controller/config/config.go | 4 +--- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cmd/provider/main.go b/cmd/provider/main.go index 1d55634..785e1d4 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -22,6 +22,7 @@ import ( "github.com/crossplane-contrib/provider-ansible/apis" ansible "github.com/crossplane-contrib/provider-ansible/internal/controller" + ansiblerun "github.com/crossplane-contrib/provider-ansible/internal/controller/ansibleRun" "github.com/crossplane/crossplane-runtime/pkg/controller" "github.com/crossplane/crossplane-runtime/pkg/feature" "github.com/crossplane/crossplane-runtime/pkg/logging" @@ -77,6 +78,12 @@ func main() { Features: &feature.Flags{}, } - kingpin.FatalIfError(ansible.Setup(mgr, o, *ansibleCollectionsPath, *ansibleRolesPath, *timeout, *artifactsHistoryLimit), "Cannot setup Ansible controllers") + ansibleOpts := ansiblerun.SetupOptions{ + AnsibleCollectionsPath: *ansibleCollectionsPath, + AnsibleRolesPath: *ansibleRolesPath, + Timeout: *timeout, + ArtifactsHistoryLimit: *artifactsHistoryLimit, + } + kingpin.FatalIfError(ansible.Setup(mgr, o, ansibleOpts), "Cannot setup Ansible controllers") kingpin.FatalIfError(mgr.Start(ctrl.SetupSignalHandler()), "Cannot start controller manager") } diff --git a/internal/controller/ansible.go b/internal/controller/ansible.go index 2f7965e..cb92120 100644 --- a/internal/controller/ansible.go +++ b/internal/controller/ansible.go @@ -17,8 +17,6 @@ limitations under the License. package controller import ( - "time" - ansiblerun "github.com/crossplane-contrib/provider-ansible/internal/controller/ansibleRun" "github.com/crossplane/crossplane-runtime/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" @@ -28,14 +26,14 @@ import ( // Setup creates all Template controllers with the supplied logger and adds them to // the supplied manager. -func Setup(mgr ctrl.Manager, o controller.Options, ansibleCollectionsPath, ansibleRolesPath string, timeout time.Duration, artifactsHistoryLimit int) error { - for _, setup := range []func(ctrl.Manager, controller.Options, string, string, time.Duration, int) error{ - config.Setup, - ansiblerun.Setup, - } { - if err := setup(mgr, o, ansibleCollectionsPath, ansibleRolesPath, timeout, artifactsHistoryLimit); err != nil { - return err - } +func Setup(mgr ctrl.Manager, o controller.Options, s ansiblerun.SetupOptions) error { + if err := config.Setup(mgr, o); err != nil { + return err + } + + if err := ansiblerun.Setup(mgr, o, s); err != nil { + return err } + return nil } diff --git a/internal/controller/ansibleRun/ansibleRun.go b/internal/controller/ansibleRun/ansibleRun.go index 5fd293f..7ae8f9b 100644 --- a/internal/controller/ansibleRun/ansibleRun.go +++ b/internal/controller/ansibleRun/ansibleRun.go @@ -90,8 +90,16 @@ type ansibleRunner interface { Run() (*exec.Cmd, io.Reader, error) } +// SetupOptions constains settings specific to the ansible run controller. +type SetupOptions struct { + AnsibleCollectionsPath string + AnsibleRolesPath string + Timeout time.Duration + ArtifactsHistoryLimit int +} + // Setup adds a controller that reconciles AnsibleRun managed resources. -func Setup(mgr ctrl.Manager, o controller.Options, ansibleCollectionsPath, ansibleRolesPath string, timeout time.Duration, artifactsHistoryLimit int) error { +func Setup(mgr ctrl.Manager, o controller.Options, s SetupOptions) error { name := managed.ControllerName(v1alpha1.AnsibleRunGroupKind) fs := afero.Afero{Fs: afero.NewOsFs()} @@ -114,9 +122,9 @@ func Setup(mgr ctrl.Manager, o controller.Options, ansibleCollectionsPath, ansib WorkingDirPath: dir, GalaxyBinary: galaxyBinary, RunnerBinary: runnerBinary, - CollectionsPath: ansibleCollectionsPath, - RolesPath: ansibleRolesPath, - ArtifactsHistoryLimit: artifactsHistoryLimit, + CollectionsPath: s.AnsibleCollectionsPath, + RolesPath: s.AnsibleRolesPath, + ArtifactsHistoryLimit: s.ArtifactsHistoryLimit, } }, } @@ -125,7 +133,7 @@ func Setup(mgr ctrl.Manager, o controller.Options, ansibleCollectionsPath, ansib resource.ManagedKind(v1alpha1.AnsibleRunGroupVersionKind), managed.WithExternalConnecter(c), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithTimeout(timeout), + managed.WithTimeout(s.Timeout), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))) return ctrl.NewControllerManagedBy(mgr). diff --git a/internal/controller/config/config.go b/internal/controller/config/config.go index 4f354a2..f44c6bc 100644 --- a/internal/controller/config/config.go +++ b/internal/controller/config/config.go @@ -17,8 +17,6 @@ limitations under the License. package config import ( - "time" - "github.com/crossplane/crossplane-runtime/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/source" @@ -33,7 +31,7 @@ import ( // Setup adds a controller that reconciles ProviderConfigs by accounting for // their current usage. -func Setup(mgr ctrl.Manager, o controller.Options, _, _ string, _ time.Duration, _ int) error { +func Setup(mgr ctrl.Manager, o controller.Options) error { name := providerconfig.ControllerName(v1alpha1.ProviderConfigGroupKind) of := resource.ProviderConfigKinds{