-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (78 loc) · 2.35 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
87
88
89
90
91
92
93
94
95
package main
import (
"bufio"
"flag"
"log"
"os"
"path/filepath"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
//
// Uncomment to load all auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth
//
// Or uncomment to load specific auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// _ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
deploymentsClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)
// List Deployments
log.Printf("Searching deployments in namespace %q:\n", apiv1.NamespaceDefault)
list, err := deploymentsClient.List(metav1.ListOptions{})
if err != nil {
log.Fatalf("Could not get list deployments: %s", err)
}
// Get length of deployments returned
i := len(list.Items)
// Make slice of strings with length i
repos := make([]string, i)
// Iterate over deployments and get annotations
for i, d := range list.Items {
log.Printf(" * %s with annotations: (%v)\n", d.Name, d.Annotations)
a := d.Annotations
// If annotations have a key of "repo"...
if _, ok := a["repo"]; ok {
// Get value of key, "repo"
repo := a["repo"]
// Add value at index i to slice
repos[i] = repo
}
}
log.Printf("Full slice of values: %v", repos)
// Write file
writeFile(repos)
}
func writeFile(toWrite []string) {
file, err := os.OpenFile("output", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Failed to create file: %s", err)
}
datawriter := bufio.NewWriter(file)
for i, data := range toWrite {
log.Printf("Writing file, index %v with data %v", i, data)
_, _ = datawriter.WriteString(data + "\n")
}
datawriter.Flush()
file.Close()
}