Skip to content

Commit

Permalink
refactor with a struct to hold ansiblerun controller setup options
Browse files Browse the repository at this point in the history
  • Loading branch information
d-honeybadger committed Feb 27, 2024
1 parent 2807ad0 commit c13d8f4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
9 changes: 8 additions & 1 deletion cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
18 changes: 8 additions & 10 deletions internal/controller/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
18 changes: 13 additions & 5 deletions internal/controller/ansibleRun/ansibleRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand All @@ -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,
}
},
}
Expand All @@ -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).
Expand Down
4 changes: 1 addition & 3 deletions internal/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down

0 comments on commit c13d8f4

Please sign in to comment.