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

add a prune package to handle cleanup of pods and job resources #75

Merged
merged 7 commits into from
Oct 28, 2021
Merged

Conversation

jmccormick2001
Copy link

Description of the change:

this package can be used by operator authors to implement a basic resource pruning capability
in their operators to cleanup Pods and Jobs that are considered prunable.

The use of this package is optional by authors. Author focused documentation would follow this PR
to show authors how to use this package.

This package would be used for pruning resources not owned by a CustomResource, but more temporary in nature. If left on cluster, resources can accumulate causing for example heavy etcd disk usage.

Motivation for the change:

operator authors inside Red Hat and the community at large outside of Red Hat typically
write their own custom logic to perform this form of pruning. This package implementation
covers Pod and Job cleanup which from experience are the most typical resources that need
to be cleaned up. This package would offer up a basic capability for authors to make use of.

@coveralls
Copy link

coveralls commented Oct 27, 2021

Pull Request Test Coverage Report for Build 1391762787

  • 146 of 188 (77.66%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-1.1%) to 80.914%

Changes Missing Coverage Covered Lines Changed/Added Lines %
prune/maxage.go 14 16 87.5%
prune/maxcount.go 14 16 87.5%
prune/resources.go 47 56 83.93%
prune/remove.go 14 24 58.33%
prune/prune.go 57 76 75.0%
Totals Coverage Status
Change from base Build 1342663332: -1.1%
Covered Lines: 602
Relevant Lines: 744

💛 - Coveralls

Copy link

@ryantking ryantking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, few questions and nits

Comment on lines 31 to 40
if !config.DryRun {
err := config.removeResource(resources[i])
if err != nil {
return err
}
}
removeCount--
if removeCount == 0 {
break
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this code to a separate function that is called outside the strategy since it's universal across all strategies? Then we can make the strategy function signature func(config Config, resources []ResourceInfo) (toPrune []ResourceInfo, err error) and remove the deletion responsibility from the strategy implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the config.DryRun check into the removeResource func to make it cleaner.

prune/prune.go Outdated
)

// ResourceKind describes the Kubernetes Kind we are wanting to prune
type ResourceKind string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we reuse a type from the Kubernetes library here like GroupKind or GroupResource? Could make it easier to integrate with other components later if we use a universal type. We would still be able to be opinionated on which resources we support removing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latest commit uses the apimachinery schema.GroupVersionKind to hold that value which should be better.

prune/prune.go Outdated

// StrategyImplementation function allows a means to specify
// custom prune strategies
type StrategyImplementation func(cfg Config, resources []ResourceInfo) error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since its a function type, not a struct, would StrategyFunc be a better type name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better name, latest commit uses that name.

prune/prune.go Outdated
// Config defines a pruning configuration and ultimately
// determines what will get pruned
type Config struct {
Ctx context.Context //context used by pruning

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its considered bad practice by the go standard library to use a context as a struct member.

Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I moved it outside the Config with this latest commit.

prune/prune.go Outdated
PreDeleteHook PreDelete //called before resource is deleteds
}

var log = logf.Log.WithName("prune")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to the config struct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the log into the Config with latest commit.

prune/prune.go Outdated
Comment on lines 109 to 127
switch config.Strategy.Mode {
case MaxAgeStrategy:
err = pruneByMaxAge(config, resourceList)
if err != nil {
return err
}
case MaxCountStrategy:
err = pruneByMaxCount(config, resourceList)
if err != nil {
return err
}
case CustomStrategy:
err = config.CustomStrategy(config, resourceList)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown strategy")
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you can just check err once at the end of the switch statement instead of in each branch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doh! thanks, new version corrects that.

if err != nil {
return err
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to return some sort of error if we attempt to delete an unsupported resource?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in the error return. thanks for catching that.

Copy link
Member

@jmrodri jmrodri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Oct 28, 2021
@jmrodri
Copy link
Member

jmrodri commented Oct 28, 2021

@ryantking GREAT review! Thanks for that.

@jmrodri
Copy link
Member

jmrodri commented Oct 28, 2021

/approve

@openshift-ci
Copy link

openshift-ci bot commented Oct 28, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jmrodri

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 28, 2021
@openshift-merge-robot openshift-merge-robot merged commit 68f5b7b into operator-framework:main Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants