Skip to content

Commit

Permalink
limit the number of artifacts directories retained on disk
Browse files Browse the repository at this point in the history
Signed-off-by: Dasha Komsa <komsa.darya@gmail.com>
  • Loading branch information
d-honeybadger committed Feb 16, 2024
1 parent fbd8487 commit 2807ad0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func main() {
timeout = app.Flag("timeout", "Controls how long Ansible processes may run before they are killed.").Default("20m").Duration()
leaderElection = app.Flag("leader-election", "Use leader election for the controller manager.").Short('l').Default("false").OverrideDefaultFromEnvar("LEADER_ELECTION").Bool()
maxReconcileRate = app.Flag("max-reconcile-rate", "The maximum number of concurrent reconciliation operations.").Default("1").Int()
artifactsHistoryLimit = app.Flag("artifacts-history-limit", "Each attempt to run the playbook/role generates a set of artifacts on disk. This settings limits how many of these to keep.").Default("50").Int()
)
kingpin.MustParse(app.Parse(os.Args[1:]))

Expand Down Expand Up @@ -76,6 +77,6 @@ func main() {
Features: &feature.Flags{},
}

kingpin.FatalIfError(ansible.Setup(mgr, o, *ansibleCollectionsPath, *ansibleRolesPath, *timeout), "Cannot setup Ansible controllers")
kingpin.FatalIfError(ansible.Setup(mgr, o, *ansibleCollectionsPath, *ansibleRolesPath, *timeout, *artifactsHistoryLimit), "Cannot setup Ansible controllers")
kingpin.FatalIfError(mgr.Start(ctrl.SetupSignalHandler()), "Cannot start controller manager")
}
26 changes: 20 additions & 6 deletions internal/ansible/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"strconv"
"time"

"github.com/apenella/go-ansible/pkg/stdoutcallback/results"
Expand Down Expand Up @@ -69,6 +70,8 @@ type Parameters struct {
CollectionsPath string
// The source of this filed is either controller flag `--ansible-roles-path` or the env vars : `ANSIBLE_ROLES_PATH` , DEFAULT_ROLES_PATH`
RolesPath string
// the limit on the number of artifact directories to keep for each run
ArtifactsHistoryLimit int
}

// RunPolicy represents the run policies of Ansible.
Expand Down Expand Up @@ -144,6 +147,14 @@ func withAnsibleRunPolicy(p *RunPolicy) runnerOption {
}
}

// withArtifactsHistoryLimit sets the limit on the number of artifacts
// directories to keep; each invocation of ansible-runner produces an artifacts directory.
func withArtifactsHistoryLimit(limit int) runnerOption {
return func(r *Runner) {
r.artifactsHistoryLimit = limit
}
}

type cmdFuncType func(behaviorVars map[string]string, checkMode bool) *exec.Cmd

// playbookCmdFunc mimics https://github.com/operator-framework/operator-sdk/blob/707240f006ecfc0bc86e5c21f6874d302992d598/internal/ansible/runner/runner.go#L75-L90
Expand Down Expand Up @@ -307,17 +318,19 @@ func (p Parameters) Init(ctx context.Context, cr *v1alpha1.AnsibleRun, behaviorV
withAnsibleRunPolicy(rPolicy),
// TODO should be moved to connect() func
withAnsibleEnvDir(ansibleEnvDir),
withArtifactsHistoryLimit(p.ArtifactsHistoryLimit),
), nil
}

// Runner struct holds the configuration to run the cmdFunc
type Runner struct {
Path string // absolute path on disk to a playbook or role depending on what cmdFunc expects
behaviorVars map[string]string
cmdFunc cmdFuncType // returns a Cmd that runs ansible-runner
AnsibleEnvDir string
checkMode bool
AnsibleRunPolicy *RunPolicy
Path string // absolute path on disk to a playbook or role depending on what cmdFunc expects
behaviorVars map[string]string
cmdFunc cmdFuncType // returns a Cmd that runs ansible-runner
AnsibleEnvDir string
checkMode bool
AnsibleRunPolicy *RunPolicy
artifactsHistoryLimit int
}

// new returns a runner that will be used as ansible-runner client
Expand Down Expand Up @@ -345,6 +358,7 @@ func (r *Runner) Run() (*exec.Cmd, io.Reader, error) {
)

dc := r.cmdFunc(r.behaviorVars, r.checkMode)
dc.Args = append(dc.Args, "--rotate-artifacts", strconv.Itoa(r.artifactsHistoryLimit))
if !r.checkMode {
// for disabled checkMode dc.Stdout and dc.Stderr are respectfully
// written to os.Stdout and os.Stdout for debugging purpose
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ 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) error {
for _, setup := range []func(ctrl.Manager, controller.Options, string, string, time.Duration) error{
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); err != nil {
if err := setup(mgr, o, ansibleCollectionsPath, ansibleRolesPath, timeout, artifactsHistoryLimit); err != nil {
return err
}
}
Expand Down
13 changes: 7 additions & 6 deletions internal/controller/ansibleRun/ansibleRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type ansibleRunner interface {
}

// Setup adds a controller that reconciles AnsibleRun managed resources.
func Setup(mgr ctrl.Manager, o controller.Options, ansibleCollectionsPath, ansibleRolesPath string, timeout time.Duration) error {
func Setup(mgr ctrl.Manager, o controller.Options, ansibleCollectionsPath, ansibleRolesPath string, timeout time.Duration, artifactsHistoryLimit int) error {
name := managed.ControllerName(v1alpha1.AnsibleRunGroupKind)

fs := afero.Afero{Fs: afero.NewOsFs()}
Expand All @@ -111,11 +111,12 @@ func Setup(mgr ctrl.Manager, o controller.Options, ansibleCollectionsPath, ansib
fs: fs,
ansible: func(dir string) params {
return ansible.Parameters{
WorkingDirPath: dir,
GalaxyBinary: galaxyBinary,
RunnerBinary: runnerBinary,
CollectionsPath: ansibleCollectionsPath,
RolesPath: ansibleRolesPath,
WorkingDirPath: dir,
GalaxyBinary: galaxyBinary,
RunnerBinary: runnerBinary,
CollectionsPath: ansibleCollectionsPath,
RolesPath: ansibleRolesPath,
ArtifactsHistoryLimit: artifactsHistoryLimit,
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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) error {
func Setup(mgr ctrl.Manager, o controller.Options, _, _ string, _ time.Duration, _ int) error {
name := providerconfig.ControllerName(v1alpha1.ProviderConfigGroupKind)

of := resource.ProviderConfigKinds{
Expand Down

0 comments on commit 2807ad0

Please sign in to comment.