This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
69 lines (60 loc) · 2.07 KB
/
run.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cmd
import (
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/bitnami-labs/kube-custodian/pkg/cleaner"
)
const (
flagSkipLabels = "skip-labels"
flagSkipNamespaceRe = "skip-namespace-re"
flagTagTTL = "tag-ttl"
flagTagForDeletion = "tag-for-deletion"
flagDeleteTagged = "delete-tagged"
flagCleanUpTag = "cleanup-tag"
)
func init() {
rootCmd.AddCommand(runCmd)
runCmd.PersistentFlags().StringSlice(flagSkipLabels, cleaner.CommonDefaults.SkipLabels, "Labels required for resources to be skipped from scanning")
runCmd.PersistentFlags().String(flagSkipNamespaceRe, cleaner.CommonDefaults.SkipNamespaceRE, "Regex of namespaces to skip, typically 'system' ones and alike")
runCmd.PersistentFlags().String(flagTagTTL, cleaner.CommonDefaults.TagTTL, "Time to live after marked, before deletion")
runCmd.PersistentFlags().Bool(flagTagForDeletion, true, "Tag resources for later deletion")
runCmd.PersistentFlags().Bool(flagDeleteTagged, true, "Delete tagged resources, after their Tag TTL has passed")
runCmd.PersistentFlags().Bool(flagCleanUpTag, false, "Untag resources from later deletion")
}
var runCmd = &cobra.Command{
Use: "run",
Short: "Scan Kubernetes objects, mark for deletion (via annotation), delete those already \"expired\"",
Run: func(cmd *cobra.Command, args []string) {
var err error
flags := cmd.Flags()
c := cleaner.Common{}
c.SkipLabels, err = flags.GetStringSlice(flagSkipLabels)
if err != nil {
log.Fatal(err)
}
c.Namespace, err = flags.GetString("namespace")
if err != nil {
log.Fatal(err)
}
c.TagTTL, err = flags.GetString(flagTagTTL)
if err != nil {
log.Fatal(err)
}
c.DryRun, err = flags.GetBool(flagDryRun)
if err != nil {
log.Fatal(err)
}
if len(c.SkipLabels) < 1 {
log.Fatal("At least one skip-labels is needed")
}
log.Debugf("Skipping workloads with labels: %v ...", c.SkipLabels)
c.SkipNamespaceRE, err = flags.GetString(flagSkipNamespaceRe)
if err != nil {
log.Fatal(err)
}
c.Init(NewKubeClient())
_, _, errCnt := c.Run()
os.Exit(errCnt)
},
}