forked from ankilosaurus/kube_remediator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
oldpoddeleter.go
41 lines (34 loc) · 839 Bytes
/
oldpoddeleter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package remediator
import (
"context"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sync"
"time"
)
type OldPodDeleter struct {
Base
}
func (p *OldPodDeleter) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
p.reconcileEvery(ctx, p.deleteOldPods, 1*time.Hour)
}
func (p *OldPodDeleter) deleteOldPods() {
p.logger.Info("Running")
// get all pods that opted in to deletion
pods, err := p.client.GetPods("", metav1.ListOptions{
LabelSelector: "kube-remediator/OldPodDeleter=true",
})
if err != nil {
p.logger.Error("Error getting pod list", zap.Error(err))
return
}
// deleteOldPods those that are too old
cutoff := time.Now().Add(-24 * time.Hour)
for _, pod := range pods.Items {
if pod.ObjectMeta.CreationTimestamp.Time.After(cutoff) {
continue
}
p.deletePod(pod)
}
}