-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (77 loc) · 1.83 KB
/
main.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
package main
import (
"context"
"flag"
"log"
"os"
"time"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"github.com/jessequinn/k8s-resource-finder/internal"
)
// Flags
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var namespaces arrayFlags
func main() {
// Flags
flag.Var(&namespaces, "namespaces", "list of namespaces. (Required)")
flag.Parse()
// Test flags
if len(namespaces) < 1 {
flag.PrintDefaults()
os.Exit(1)
}
config := ctrl.GetConfigOrDie()
clientSet := kubernetes.NewForConfigOrDie(config)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*200)
defer cancel()
var ns []string
for _, v := range namespaces {
ns = append(ns, v)
}
k := internal.New(ctx, ns, clientSet.CoreV1())
err := k.GetUsedResources()
if err != nil {
log.Fatalln(err)
}
println("Unused Secrets")
unusedSecrets, err := k.GetUnusedSecrets()
if err != nil {
log.Fatalln(err)
}
for _, v := range unusedSecrets {
println("\t", v.Name, v.Namespaces)
}
println("Unused ConfigMaps")
unusedConfigMaps, err := k.GetUnusedConfigMaps()
if err != nil {
log.Fatalln(err)
}
for _, v := range unusedConfigMaps {
println("\t", v.Name, v.Namespaces)
}
println("Unused Service Accounts")
unusedServiceAccounts, err := k.GetUnusedServiceAccounts()
if err != nil {
log.Fatalln(err)
}
for _, v := range unusedServiceAccounts {
println("\t", v.Name, v.Namespaces)
}
println("Unused Persistent Volume Claims")
unusedPersistentVolumeClaims, err := k.GetUnusedPersistentVolumeClaims()
if err != nil {
log.Fatalln(err)
}
for _, v := range unusedPersistentVolumeClaims {
println("\t", v.Name, v.Namespaces)
}
}