-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
245 lines (189 loc) · 6.05 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/schollz/progressbar/v3"
)
var active_resources []string
var namespace string
var destination string
func error_handler(msg string, err error) {
fmt.Fprintf(os.Stderr, "%s: %v", msg, err)
os.Exit(1)
}
func checkResource(resource string) bool {
cmd := exec.Command("kubectl", "get", resource, "--no-headers")
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = nil
if err := cmd.Run(); err != nil {
return false
}
return len(stdout.String()) > 0
}
func getAPIResources() []string {
cmd := exec.Command("kubectl", "api-resources", "--verbs=list", "--namespaced=true", "-n", namespace, "-o", "name")
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
error_handler("Error while getting API resources", err)
}
return strings.Fields(stdout.String())
}
func create_directory(path string) {
// Ensure proper path formatting for Windows
cleanPath := filepath.FromSlash(path)
// Check if directory already exists
if _, err := os.Stat(cleanPath); os.IsNotExist(err) {
err := os.MkdirAll(cleanPath, 0755)
if err != nil {
error_handler(fmt.Sprintf("Failed to create directory %s: ", cleanPath), err)
}
} else {
error_handler(fmt.Sprintf("Directory already exists %s: ", cleanPath), err)
}
}
func get_resource(path, resource string) {
filename := "manifest_" + resource + ".yaml"
cmd := exec.Command("kubectl", "get", resource, "-o", "yaml")
cmd.Dir = path
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
error_handler(fmt.Sprintf("Error while getting the resource %s: ", resource), err)
}
// Write the output to the file
cleanpath := filepath.Join(path, filename)
if err := os.WriteFile(cleanpath, stdout.Bytes(), 0644); err != nil {
error_handler(fmt.Sprintf("Error writing to file %s: ", filename), err)
}
}
func removeDuplicatesAndEmpty(slice []string, ns_path string) {
// using map to record duplicates
encountered := map[string]bool{}
count := 0
// create a file
filePath := filepath.Join(ns_path, "docker_images.txt")
active_file, err := os.Create(filePath)
if err != nil {
error_handler("Error while creating a file", err)
}
for _, str := range slice {
if str == "" {
continue
}
if !encountered[str] {
count++
encountered[str] = true
_, err := active_file.WriteString(str + "\n")
if err != nil {
error_handler("Error while writing to a file", err)
}
}
}
fmt.Println("Total Docker Images in use: ", count)
}
func grab_images(ns_path string) {
var allImages []string
// you can add or remove the places where you want to check
res_types := []string{
"deployments",
"statefulsets",
"daemonsets",
"cronjobs",
"jobs",
"pods",
"replicasets",
}
// Build jsonpath query for both containers and initContainers
jsonPath := "{.items[*].spec.template.spec.containers[*].image} {.items[*].spec.template.spec.initContainers[*].image}"
for _, resource := range res_types {
// Construct kubectl command
cmd := exec.Command("kubectl", "get", resource, "-n", namespace, "-o", fmt.Sprintf("jsonpath=%s", jsonPath))
// Capture command output
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
// Skip if resource type doesn't exist in the namespace
if strings.Contains(stderr.String(), "not found") {
continue
}
// not using error_handler because we still want to continue
fmt.Printf("\nError getting images for %s: %v - %s \nContinuing the execution ...\n", resource, err, stderr.String())
}
if stdout.Len() > 0 {
images := strings.Fields(stdout.String())
allImages = append(allImages, images...)
}
}
removeDuplicatesAndEmpty(allImages, ns_path)
}
func main() {
dest := flag.String("destination", "", "Destination directory/folder to save.")
ns := flag.String("namespace", "", "Provide namespace or it can take from current context's default namespace set.")
flag.Parse()
destination = *dest
namespace = *ns
if destination == "" {
flag.Usage()
os.Exit(0)
}
_, err := os.Stat(destination)
if err != nil {
error_handler("Give directory does not exist!", err)
}
if namespace == "" {
cmd := exec.Command("kubectl", "config", "view", "--minify", "-o", "jsonpath={..namespace}")
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
error_handler("Error getting the current context's namespace", err)
}
namespace = stdout.String()
if stdout.String() == "" {
error_handler("No default namespace found in your current context", nil)
} else {
fmt.Println("Taking current context's namespace:", namespace)
}
}
// create initial namespace directory
ns_path := filepath.Join(destination, namespace)
create_directory(ns_path)
grab_images(ns_path)
// create active_resources.txt file
filePath := filepath.Join(ns_path, "active_resources.txt")
active_res_file, err := os.Create(filePath)
if err != nil {
error_handler("Error while creating a file", err)
}
resources := getAPIResources()
total := len(resources)
fmt.Printf("Total resources %s namespace supports: %v\n\n", namespace, total)
activeCount := 0
bar := progressbar.Default(int64(total))
for _, resource := range resources {
bar.Add(1)
if checkResource(resource) {
activeCount++
active_resources = append(active_resources, resource)
_, err := active_res_file.WriteString(resource + "\n")
if err != nil {
error_handler("Error while writing to a file", err)
}
// create resource directory
res_path := filepath.Join(ns_path, resource)
create_directory(res_path)
get_resource(res_path, resource)
}
}
fmt.Printf("\nTotal Active Resources: %d/%d", activeCount, total)
fmt.Printf("\nAll the results are stored at %s\n", ns_path)
defer active_res_file.Close()
}