-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
361 lines (332 loc) · 12.2 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"fmt"
"kompare/cli"
"kompare/compare"
"kompare/connect"
"kompare/query"
"kompare/tools"
"path/filepath"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
func main() {
// Parse CLI arguments
args := cli.PaserReader()
if args.Err != nil {
err := fmt.Errorf("error parsing arguments: %v", args.Err)
panic(err)
}
if args.FileOutput != "" {
defer tools.LogOutput(args.FileOutput)()
}
// Connect to source cluster
clientsetToSource, err := connect.ConnectToSource(args.SourceClusterContext, &args.KubeconfigFile)
if err != nil {
err = fmt.Errorf("error connecting to source cluster: %v", err)
panic(err)
}
// Connect to target cluster
clientsetToTarget, err := connect.ContextSwitch(args.TargetClusterContext, &args.KubeconfigFile)
if err != nil {
err = fmt.Errorf("error switching context: %v", err)
panic(err)
}
// Determine namespace argument type
var sourceNameSpacesList *v1.NamespaceList
var sourceNameSpace *v1.Namespace
namespaceArgType := DetectNamespacePattern(args.NamespaceName)
switch namespaceArgType {
case "specific":
fmt.Println("Using", args.NamespaceName, "namespace")
sourceNameSpace, err = query.GetNamespace(clientsetToSource, args.NamespaceName)
if err != nil {
err = fmt.Errorf("error listing namespaces: %v", err)
panic(err)
}
sourceNameSpacesList = &v1.NamespaceList{Items: []v1.Namespace{*sourceNameSpace}}
case "wildcard":
sourceNameSpacesList, err = query.ListNameSpaces(clientsetToSource)
if err != nil {
err = fmt.Errorf("error listing namespaces: %v", err)
panic(err)
}
sourceNameSpacesList = filterNamespaces(sourceNameSpacesList, args.NamespaceName)
case "empty":
iterateGoglabObjects(clientsetToSource, clientsetToTarget, args)
sourceNameSpacesList, err = query.ListNameSpaces(clientsetToSource)
if err != nil {
err = fmt.Errorf("error listing namespaces: %v", err)
panic(err)
}
}
// Iterate over namespaces
iterateNamespaces(sourceNameSpacesList, clientsetToSource, clientsetToTarget, args)
fmt.Println("Finished all comparison works!")
}
func iterateGoglabObjects(clientsetToSource, clientsetToTarget *kubernetes.Clientset, args cli.ArgumentsReceivedValidated) bool {
// Flag to track if any comparison was performed
comparisonPerformed := false
// Compare objects based on include list
if args.Include != nil {
includeObjects := []string{"namespace", "crd", "clusterrole", "clusterrolebinding"}
for _, objectType := range includeObjects {
if tools.IsInList(objectType, args.Include) {
switch objectType {
case "namespace":
_, err := compare.CompareNameSpaces(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("error comparing Namespaces: %v", err)
panic(err)
}
case "crd":
_, err := compare.CompareCRDs(args.TargetClusterContext, args.KubeconfigFile, args)
if err != nil {
err = fmt.Errorf("error comparing CRDs: %v", err)
panic(err)
}
case "clusterrole":
_, err := compare.CompareClusterRoles(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("error comparing Cluster Role: %v", err)
panic(err)
}
case "clusterrolebinding":
_, err := compare.CompareClusterRoleBindings(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("error comparing Cluster Role: %v", err)
panic(err)
}
}
comparisonPerformed = true
}
}
}
// Compare objects based on exclude list
if args.Exclude != nil {
excludeObjects := []string{"namespace", "crd", "clusterrole", "clusterrolebinding"}
for _, objectType := range excludeObjects {
if !tools.IsInList(objectType, args.Exclude) {
switch objectType {
case "namespace":
_, err := compare.CompareNameSpaces(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Namspace: %v", err)
panic(err)
}
case "crd":
_, err := compare.CompareCRDs(args.TargetClusterContext, args.KubeconfigFile, args)
if err != nil {
err = fmt.Errorf("Error comparing CRDs: %v", err)
panic(err)
}
case "clusterrole":
_, err := compare.CompareClusterRoles(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Cluster Role: %v", err)
panic(err)
}
case "clusterrolebinding":
_, err := compare.CompareClusterRoleBindings(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Cluster Role Binding: %v", err)
panic(err)
}
}
comparisonPerformed = true
}
}
}
// If no include or exclude lists are provided, perform default comparisons
if args.Include == nil && args.Exclude == nil {
_, err := compare.CompareNameSpaces(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("error comparing Namespaces: %v", err)
panic(err)
}
_, err = compare.CompareCRDs(args.TargetClusterContext, args.KubeconfigFile, args)
if err != nil {
err = fmt.Errorf("error comparing CRDs: %v", err)
panic(err)
}
_, err = compare.CompareClusterRoles(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("error comparing Cluster Roles: %v", err)
panic(err)
}
_, err = compare.CompareClusterRoleBindings(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("error comparing Cluster Role Bindings: %v", err)
panic(err)
}
comparisonPerformed = true
}
// Print completion message if any comparison was performed
if comparisonPerformed {
fmt.Println("Done comparing Kubernetes global objects.")
}
return comparisonPerformed
}
func compareAllResourcesInNamespace(clientsetToSource, clientsetToTarget *kubernetes.Clientset, namespace string, TheArgs cli.ArgumentsReceivedValidated) {
fmt.Printf("Looping on Namespace: %s\n", namespace)
// Compare all resources for the namespace
resources := []string{"deployment", "ingress", "service", "serviceaccount", "configmap", "secret", "role", "rolebinding", "hpa", "cronjob", "networkpolicy"}
// Create a title case converter for English
titleCase := cases.Title(language.English)
for _, resource := range resources {
titleResource := titleCase.String(resource)
fmt.Printf("%s\n", titleResource)
compareResource(clientsetToSource, clientsetToTarget, namespace, resource, TheArgs)
fmt.Printf("Finished %s for namespace: %s\n", titleResource, namespace)
}
fmt.Printf("... Done with all resources in ns: %s.\n", namespace)
}
func compareResourcesByLists(clientsetToSource, clientsetToTarget *kubernetes.Clientset, namespace string, TheArgs cli.ArgumentsReceivedValidated) {
fmt.Printf("Looping namespace: %s\n", namespace)
includeResources := TheArgs.Include
excludeResources := TheArgs.Exclude
// Create a title case converter for English
titleCase := cases.Title(language.English)
// Define all resources
resources := []string{"deployment", "ingress", "service", "serviceaccout", "configmap", "secret", "role", "rolebinding", "networkpolicy", "hpa", "cronjob"}
// Compare resources based on include list
for _, resource := range includeResources {
titleResource := titleCase.String(resource)
fmt.Printf("%s\n", titleResource)
compareResource(clientsetToSource, clientsetToTarget, namespace, resource, TheArgs)
fmt.Printf("Finished %s for namespace: %s\n", titleResource, namespace)
}
// Compare resources based on exclude list
if excludeResources != nil {
for _, resource := range resources {
// Check if resource is not in the exclude list
if !tools.IsInList(resource, excludeResources) {
titleResource := titleCase.String(resource)
fmt.Printf("%s\n", titleResource)
compareResource(clientsetToSource, clientsetToTarget, namespace, resource, TheArgs)
fmt.Printf("Finished %s for namespace: %s\n", titleResource, namespace)
}
}
}
}
func compareResource(clientsetToSource, clientsetToTarget *kubernetes.Clientset, namespace, resource string, TheArgs cli.ArgumentsReceivedValidated) {
switch resource {
case "deployment":
_, err := compare.CompareDeployments(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Deployments: %v", err)
panic(err)
}
case "ingress":
_, err := compare.CompareIngresses(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Ingresses: %v", err)
panic(err)
}
case "service":
_, err := compare.CompareServices(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Services: %v", err)
panic(err)
}
case "serviceaccount":
_, err := compare.CompareServiceAccounts(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Service Accounts: %v", err)
panic(err)
}
case "configmap":
_, err := compare.CompareConfigMaps(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Config Maps: %v", err)
panic(err)
}
case "secret":
_, err := compare.CompareSecrets(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Secrets: %v", err)
panic(err)
}
case "role":
_, err := compare.CompareRoles(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Roles: %v", err)
panic(err)
}
case "rolebinding":
_, err := compare.CompareRoleBindings(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Role Bindings: %v", err)
panic(err)
}
case "hpa":
_, err := compare.CompareHPAs(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Horizontal Pod Autoscalers: %v", err)
panic(err)
}
case "cronjob":
_, err := compare.CompareCronJobs(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Cron Jobs: %v", err)
panic(err)
}
case "networkpolicy":
_, err := compare.CompareNetworkPolicies(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("error comparing Network Policies: %v", err)
panic(err)
}
}
}
func iterateNamespaces(sourceNameSpacesList *v1.NamespaceList, clientsetToSource, clientsetToTarget *kubernetes.Clientset, TheArgs cli.ArgumentsReceivedValidated) {
// Check if include or exclude lists are provided, or if no specific lists are provided
if TheArgs.Include == nil && TheArgs.Exclude == nil {
// If no include or exclude lists are provided, compare all resources for each namespace
for _, ns := range sourceNameSpacesList.Items {
compareAllResourcesInNamespace(clientsetToSource, clientsetToTarget, ns.Name, TheArgs)
}
} else {
// Compare resources based on include or exclude lists
resources := []string{"deployment", "ingress", "service", "serviceaccount", "configmap", "secret", "role", "rolebinding", "hpa", "cronjob", "networkpolicy"}
if tools.AreAnyInLists(TheArgs.Include, resources) || tools.AreAnyInLists(TheArgs.Exclude, resources) {
for _, ns := range sourceNameSpacesList.Items {
compareResourcesByLists(clientsetToSource, clientsetToTarget, ns.Name, TheArgs)
}
} else {
fmt.Println("No namespaced resources to compare")
}
}
}
// filterNamespaces filters namespaces based on the wildcard pattern
func filterNamespaces(namespaces *v1.NamespaceList, pattern string) *v1.NamespaceList {
matchingNamespaces := v1.NamespaceList{
Items: []v1.Namespace{},
}
for _, ns := range namespaces.Items {
if matchWildcard(ns.Name, pattern) {
matchingNamespaces.Items = append(matchingNamespaces.Items, ns)
}
}
return &matchingNamespaces
}
// matchWildcard checks if a string matches the wildcard pattern
func matchWildcard(s, pattern string) bool {
match, err := filepath.Match(pattern, s)
if err != nil {
return false
}
return match
}
func DetectNamespacePattern(pattern string) string {
if pattern == "" {
return "empty"
} else if strings.Contains(pattern, "*") {
return "wildcard"
} else {
return "specific"
}
}