-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
381 lines (285 loc) · 10.9 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goccy/go-yaml"
"github.com/lanfix/kube-config-merger/internal"
)
type ConfigNode struct {
Name string
Parameters interface{}
}
type ConfigGroup struct {
ClustersList []ConfigNode
ContextsList []ConfigNode
UsersList []ConfigNode
// Источник конфигурации для последующего слияния
Source string
// Дополнительные параметры
CurrentContext string
// Может ли контент из этой группы быть перезаписан или удален во время мерджа
CanBeDeleted bool
}
type ConfigNodePermanent struct {
ConfigNode ConfigNode
CanBeDeleted bool
}
func CollectConfigGroup(configFilePath string) (*ConfigGroup, error) {
configGroup := ConfigGroup{Source: configFilePath}
kubeConfigContent, err := os.ReadFile(configGroup.Source)
if err != nil {
panic(err)
}
if !strings.Contains(string(kubeConfigContent), "apiVersion") {
return nil, fmt.Errorf("skipping \"%s\", file does not contain apiVersion", configGroup.Source)
}
var kubeConfig interface{}
err = yaml.Unmarshal(kubeConfigContent, &kubeConfig)
if err != nil {
return nil, fmt.Errorf("skipping \"%s\", invalid yaml syntax", configGroup.Source)
}
kubeConfigMapOfInterfaces := internal.CastInterfaceToMap(kubeConfig)
if kubeConfigMapOfInterfaces["apiVersion"].(string) != "v1" || kubeConfigMapOfInterfaces["kind"].(string) != "Config" {
return nil, fmt.Errorf("skipping \"%s\", file is not a kubernetes access config", configGroup.Source)
}
clusterInterfacesList := internal.CastInterfaceToList(kubeConfigMapOfInterfaces["clusters"])
contextInterfacesList := internal.CastInterfaceToList(kubeConfigMapOfInterfaces["contexts"])
userInterfacesList := internal.CastInterfaceToList(kubeConfigMapOfInterfaces["users"])
// Если нет какого-то из параметров (contexts, clusters, users), то нет смысла мерджить такой конфиг
if len(clusterInterfacesList) == 0 || len(contextInterfacesList) == 0 || len(userInterfacesList) == 0 {
return nil, fmt.Errorf("skipping \"%s\", config has not important fields", configGroup.Source)
}
configGroup.CurrentContext = kubeConfigMapOfInterfaces["current-context"].(string)
for _, nodeInterface := range clusterInterfacesList {
node := internal.CastInterfaceToMap(nodeInterface)
configGroup.ClustersList = append(configGroup.ClustersList, ConfigNode{
Name: node["name"].(string),
Parameters: node["cluster"],
})
}
for _, nodeInterface := range contextInterfacesList {
node := internal.CastInterfaceToMap(nodeInterface)
configGroup.ContextsList = append(configGroup.ContextsList, ConfigNode{
Name: node["name"].(string),
Parameters: node["context"],
})
}
for _, nodeInterface := range userInterfacesList {
node := internal.CastInterfaceToMap(nodeInterface)
configGroup.UsersList = append(configGroup.UsersList, ConfigNode{
Name: node["name"].(string),
Parameters: node["user"],
})
}
return &configGroup, nil
}
func RecursiveFilesByDirectory(directoryPath string) []string {
var fileNames []string
filepath.Walk(directoryPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if !info.IsDir() {
fileNames = append(fileNames, path)
}
return nil
})
return fileNames
}
func RecursiveFilesByDirectories(directoryPaths []string) []string {
var fileNames []string
for _, directoryPath := range directoryPaths {
fileNames = append(fileNames, RecursiveFilesByDirectory(directoryPath)...)
}
return fileNames
}
func DebugConfigGroups(configGroups []ConfigGroup) {
fmt.Println("========== Contexts ==========")
for _, configGroup := range configGroups {
fmt.Println(configGroup.Source)
for _, context := range configGroup.ContextsList {
fmt.Println("---", context.Name)
}
}
fmt.Println("==============================")
}
func GetUniqueString(strings []string) []string {
uniqueStringsMap := make(map[string]bool)
var uniqueStrings []string
for _, item := range strings {
uniqueStringsMap[item] = true
}
for key, _ := range uniqueStringsMap {
uniqueStrings = append(uniqueStrings, key)
}
return uniqueStrings
}
func UnwrapConfigNodesFromMap(configNodes map[string]ConfigNodePermanent) []ConfigNode {
var output []ConfigNode
for _, configNodePermanent := range configNodes {
output = append(output, configNodePermanent.ConfigNode)
}
return output
}
func MergeConfigGroups(configGroups []ConfigGroup) ConfigGroup {
mergedClustersMap := make(map[string]ConfigNodePermanent)
mergedContextsMap := make(map[string]ConfigNodePermanent)
mergedUsersMap := make(map[string]ConfigNodePermanent)
// TODO: Отрефакторить
for _, configGroup := range configGroups {
for _, configNode := range configGroup.ClustersList {
if value, ok := mergedClustersMap[configNode.Name]; ok && !value.CanBeDeleted {
continue
}
mergedClustersMap[configNode.Name] = ConfigNodePermanent{
ConfigNode: configNode,
CanBeDeleted: configGroup.CanBeDeleted,
}
}
for _, configNode := range configGroup.ContextsList {
if value, ok := mergedContextsMap[configNode.Name]; ok && !value.CanBeDeleted {
continue
}
mergedContextsMap[configNode.Name] = ConfigNodePermanent{
ConfigNode: configNode,
CanBeDeleted: configGroup.CanBeDeleted,
}
}
for _, configNode := range configGroup.UsersList {
if value, ok := mergedUsersMap[configNode.Name]; ok && !value.CanBeDeleted {
continue
}
mergedUsersMap[configNode.Name] = ConfigNodePermanent{
ConfigNode: configNode,
CanBeDeleted: configGroup.CanBeDeleted,
}
}
}
return ConfigGroup{
ClustersList: UnwrapConfigNodesFromMap(mergedClustersMap),
ContextsList: UnwrapConfigNodesFromMap(mergedContextsMap),
UsersList: UnwrapConfigNodesFromMap(mergedUsersMap),
}
}
func (configGroup *ConfigGroup) toYaml() ([]byte, error) {
var clustersList []interface{}
var contextsList []interface{}
var usersList []interface{}
// TODO: Refactor
for _, node := range configGroup.ClustersList {
clustersList = append(clustersList, map[string]interface{}{
"name": node.Name,
"cluster": node.Parameters,
})
}
for _, node := range configGroup.ContextsList {
contextsList = append(contextsList, map[string]interface{}{
"name": node.Name,
"context": node.Parameters,
})
}
for _, node := range configGroup.UsersList {
usersList = append(usersList, map[string]interface{}{
"name": node.Name,
"user": node.Parameters,
})
}
configYamlStructure := map[string]interface{}{
"apiVersion": "v1",
"kind": "Config",
"clusters": clustersList,
"contexts": contextsList,
"users": usersList,
}
if configGroup.CurrentContext != "" {
configYamlStructure["current-context"] = configGroup.CurrentContext
}
return yaml.Marshal(&configYamlStructure)
}
func CreateDirectoriesForFilePath(filePath string, permissions os.FileMode) error {
dirPath := filepath.Dir(filePath)
if _, err := os.Stat(dirPath); err == nil {
return nil
}
return os.MkdirAll(dirPath, permissions)
}
func GetDefaultTarget() (string, error) {
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return fmt.Sprintf("%s/.kube/config", userHomeDir), nil
}
func main() {
var directories internal.ArrayFlags
var files internal.ArrayFlags
var target string
defaultTargetPath, err := GetDefaultTarget()
if err != nil {
fmt.Println(err.Error())
return
}
varbose := flag.Bool("v", false, "Show verbose output of command")
flag.Var(&directories, "directory", "Specify the directory where configs will be searched")
flag.Var(&files, "file", "Specify the path to concrete config file")
flag.StringVar(&target, "target", defaultTargetPath, "Specify the path to the file to merge all the contents there")
flag.Parse()
if len(directories) == 0 {
fmt.Println("You must specify flag --directory and set the name of the directory with configs")
return
}
var configGroups []ConfigGroup
validFilesToMerge := RecursiveFilesByDirectories(directories)
for _, filePath := range files {
if _, err := os.Stat(filePath); err != nil {
fmt.Printf("skipping \"%s\", file does not exists\n", filePath)
continue
}
validFilesToMerge = append(validFilesToMerge, filePath)
}
totalValidFilesEdge := 0
var targetFileMode os.FileMode = 0644
if info, err := os.Stat(target); err == nil {
totalValidFilesEdge++
targetFileMode = info.Mode()
validFilesToMerge = append(validFilesToMerge, target)
}
if len(validFilesToMerge) == totalValidFilesEdge {
fmt.Println("No valid files to merge")
return
}
// Отбираем только уникальные пути к файлам
validFilesToMerge = GetUniqueString(validFilesToMerge)
for _, configureFile := range validFilesToMerge {
configGroup, err := CollectConfigGroup(configureFile)
if err != nil {
fmt.Println(err.Error())
continue
}
configGroup.CanBeDeleted = true
if configureFile == target {
configGroup.CanBeDeleted = false
}
configGroups = append(configGroups, *configGroup)
}
if *varbose {
DebugConfigGroups(configGroups)
}
mergedConfig := MergeConfigGroups(configGroups)
mergedConfig.CurrentContext = configGroups[len(configGroups)-1].CurrentContext
yaml, err := mergedConfig.toYaml()
if err != nil {
fmt.Println("Can not convert merged structure to yaml")
return
}
if CreateDirectoriesForFilePath(target, 0755) != nil {
fmt.Println("Can not create directories for target file;", err.Error())
}
if os.WriteFile(target, yaml, targetFileMode) != nil {
fmt.Println("Can not write target file;", err.Error())
}
}