Skip to content

Commit

Permalink
Add Label Selector to available parameters (#71)
Browse files Browse the repository at this point in the history
* Add label-selector flag

* Add labelSelector to job and pod filter

* Update README.md
  • Loading branch information
RocketRaccoon authored Mar 28, 2021
1 parent 1e14822 commit e709a88
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ Usage of ./bin/kube-cleanup-operator:
Limit scope to a single namespace
-run-outside-cluster
Set this flag when running outside of the cluster.
-label-selector
Delete only jobs and pods that meet label selector requirements. #See https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
```

### Optional parameters
Expand Down
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func main() {
legacyMode := flag.Bool("legacy-mode", true, "Legacy mode: `true` - use old `keep-*` flags, `false` - enable new `delete-*-after` flags")

dryRun := flag.Bool("dry-run", false, "Print only, do not delete anything.")

labelSelector := flag.String("label-selector", "", "Delete only jobs and pods that meet label selector requirements")

flag.Parse()
setupLogging()

Expand All @@ -77,6 +80,8 @@ func main() {
optsInfo.WriteString(fmt.Sprintf("\tkeep-successful: %d\n", *legacyKeepSuccessHours))
optsInfo.WriteString(fmt.Sprintf("\tkeep-failures: %d\n", *legacyKeepFailedHours))
optsInfo.WriteString(fmt.Sprintf("\tkeep-pending: %d\n", *legacyKeepPendingHours))

optsInfo.WriteString(fmt.Sprintf("\tlabel-selector: %s\n", *labelSelector))
log.Println(optsInfo.String())

if *legacyMode {
Expand Down Expand Up @@ -129,6 +134,7 @@ func main() {
*deleteOrphanedAfter,
*deleteEvictedAfter,
*ignoreOwnedByCronjob,
*labelSelector,
stopCh,
).Run()
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Kleaner struct {
deleteEvictedAfter time.Duration

ignoreOwnedByCronjob bool

labelSelector string

dryRun bool
ctx context.Context
Expand All @@ -60,13 +62,16 @@ type Kleaner struct {
// NewKleaner creates a new NewKleaner
func NewKleaner(ctx context.Context, kclient *kubernetes.Clientset, namespace string, dryRun bool, deleteSuccessfulAfter,
deleteFailedAfter, deletePendingAfter, deleteOrphanedAfter, deleteEvictedAfter time.Duration, ignoreOwnedByCronjob bool,
labelSelector string,
stopCh <-chan struct{}) *Kleaner {
jobInformer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.LabelSelector = labelSelector
return kclient.BatchV1().Jobs(namespace).List(ctx, options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.LabelSelector = labelSelector
return kclient.BatchV1().Jobs(namespace).Watch(ctx, options)
},
},
Expand All @@ -78,9 +83,11 @@ func NewKleaner(ctx context.Context, kclient *kubernetes.Clientset, namespace st
podInformer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.LabelSelector = labelSelector
return kclient.CoreV1().Pods(namespace).List(ctx, options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.LabelSelector = labelSelector
return kclient.CoreV1().Pods(namespace).Watch(ctx, options)
},
},
Expand All @@ -99,6 +106,7 @@ func NewKleaner(ctx context.Context, kclient *kubernetes.Clientset, namespace st
deleteOrphanedAfter: deleteOrphanedAfter,
deleteEvictedAfter: deleteEvictedAfter,
ignoreOwnedByCronjob: ignoreOwnedByCronjob,
labelSelector: labelSelector,
}
jobInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(old, new interface{}) {
Expand Down

0 comments on commit e709a88

Please sign in to comment.