-
Notifications
You must be signed in to change notification settings - Fork 2
/
configmaps.go
105 lines (91 loc) · 3.47 KB
/
configmaps.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cmd
import (
"context"
"fmt"
"strings"
"github.com/appuio/seiso/cfg"
"github.com/appuio/seiso/pkg/configmap"
"github.com/appuio/seiso/pkg/kubernetes"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
configMapCommandLongDescription = `Sometimes ConfigMaps are left unused in the Kubernetes cluster.
This command deletes ConfigMaps that are not being used anymore.`
)
var (
configMapCmd = &cobra.Command{
Use: "configmaps",
Short: "Cleans up your unused ConfigMaps in the Kubernetes cluster",
Long: configMapCommandLongDescription,
Aliases: []string{"configmap", "cm"},
SilenceUsage: true,
PreRunE: validateConfigMapCommandInput,
RunE: executeConfigMapCleanupCommand,
}
)
func init() {
rootCmd.AddCommand(configMapCmd)
defaults := cfg.NewDefaultConfig()
configMapCmd.PersistentFlags().BoolP("delete", "d", defaults.Delete, "Effectively delete ConfigMaps found")
configMapCmd.PersistentFlags().StringSliceP("label", "l", defaults.Resource.Labels,
"Identify the ConfigMap by these \"key=value\" labels")
configMapCmd.PersistentFlags().IntP("keep", "k", defaults.History.Keep,
"Keep most current <k> ConfigMaps; does not include currently used ConfigMaps (if detected)")
configMapCmd.PersistentFlags().String("older-than", defaults.Resource.OlderThan,
"Delete ConfigMaps that are older than the duration, e.g. [1y2mo3w4d5h6m7s]")
}
func validateConfigMapCommandInput(cmd *cobra.Command, args []string) (returnErr error) {
defer showUsageOnError(cmd, returnErr)
if len(config.Resource.Labels) == 0 {
return missingLabelSelectorError(config.Namespace, "configmaps")
}
for _, label := range config.Resource.Labels {
if !strings.Contains(label, "=") {
return fmt.Errorf("incorrect label format does not match expected \"key=value\" format: %s", label)
}
}
if _, err := parseCutOffDateTime(config.Resource.OlderThan); err != nil {
return fmt.Errorf("could not parse older-than flag: %w", err)
}
return nil
}
func executeConfigMapCleanupCommand(cmd *cobra.Command, args []string) error {
coreClient, err := kubernetes.NewCoreV1Client()
if err != nil {
return fmt.Errorf("cannot initiate kubernetes client: %w", err)
}
ctx := context.Background()
c := config.Resource
namespace := config.Namespace
service := configmap.NewConfigMapsService(
coreClient.ConfigMaps(namespace),
kubernetes.New(),
configmap.ServiceConfiguration{Batch: config.Log.Batch})
log.WithField("namespace", namespace).Debug("Getting ConfigMaps")
foundConfigMaps, err := service.List(ctx, toListOptions(c.Labels))
if err != nil {
return fmt.Errorf("could not retrieve ConfigMaps with labels '%s' for '%s': %w", c.Labels, namespace, err)
}
unusedConfigMaps, err := service.GetUnused(ctx, namespace, foundConfigMaps)
if err != nil {
return fmt.Errorf("could not retrieve unused config maps for '%s': %w", namespace, err)
}
cutOffDateTime, _ := parseCutOffDateTime(c.OlderThan)
filteredConfigMaps := service.FilterByTime(unusedConfigMaps, cutOffDateTime)
filteredConfigMaps = service.FilterByMaxCount(filteredConfigMaps, config.History.Keep)
if config.Delete {
err := service.Delete(ctx, filteredConfigMaps)
if err != nil {
return fmt.Errorf("could not delete ConfigMaps for '%s': %s", namespace, err)
}
} else {
log.WithFields(log.Fields{
"namespace": namespace,
"keep": config.History.Keep,
"older_than": c.OlderThan,
}).Info("Showing results")
service.Print(filteredConfigMaps)
}
return nil
}