-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (127 loc) · 3.06 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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/reynn/merge-kubeconfig/types"
"gopkg.in/yaml.v2"
)
var (
output string
outPath string
namespace string
)
func init() {
flag.StringVar(&output, "out", "yaml", "How to display the result, should be text or YAML.")
flag.StringVar(&outPath, "outPath", "config", "Where to write the file if out is set to YAML.")
flag.StringVar(&namespace, "namespace", "", "The namespace to use for all contexts.")
flag.Parse()
}
func loadConfigFile(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
reader := bufio.NewReader(f)
if bytes, err := ioutil.ReadAll(reader); err == nil {
return bytes, nil
} else {
return nil, err
}
}
func unmarshalYAML(contents []byte) (*types.Config, error) {
a := &types.Config{}
err := yaml.Unmarshal(contents, a)
if err == nil {
return a, nil
}
return nil, err
}
func writeOutYaml(c *types.Config) error {
newConfig, err := yaml.Marshal(c)
if err != nil {
panic("Failed to generate the output YAML.")
}
switch {
case output == "text":
fmt.Printf("%s\n", newConfig)
return nil
default:
fmt.Printf("Writing file %s...\n", outPath)
return ioutil.WriteFile(outPath, newConfig, 0777)
}
}
func handleMerge(configs []*types.Config) *types.Config {
outConfig := &types.Config{
Kind: "Config",
ApiVersion: "v1",
}
var users []types.User
var contexts []types.Context
var clusters []types.Cluster
for _, config := range configs {
cluster := config.Clusters[0]
clusters = append(clusters, cluster)
user := config.Users[0]
user.Name = fmt.Sprintf("%s-user", cluster.Name)
context := &types.Context{
Name: fmt.Sprintf("%s-context", cluster.Name),
Context: types.SubContext{
Cluster: cluster.Name,
User: user.Name,
Namespace: namespace,
},
}
users = append(users, user)
contexts = append(contexts, *context)
}
outConfig.Users = users
outConfig.Contexts = contexts
outConfig.Clusters = clusters
outConfig.CurrentContext = contexts[0].Name
return outConfig
}
func main() {
files := flag.Args()
if len(files) == 0 {
localFiles, err := ioutil.ReadDir(".")
if err != nil {
log.Fatalf("no localFiles provided, unable to read from the current directory")
}
for _, f := range localFiles {
ext := path.Ext(f.Name())
if ext == ".yaml" {
files = append(files, f.Name())
}
}
if len(files) == 0 {
log.Fatalf("No files available to merge")
}
}
var configs []*types.Config
for _, file := range files {
if bytes, err := loadConfigFile(file); err == nil {
if len(bytes) == 0 {
fmt.Printf("Successfully loaded %s but it was empty.\n", file)
continue
}
config, err := unmarshalYAML(bytes)
if err != nil {
fmt.Printf("Failed to unmarshal file %s due to %v\n", file, err)
} else {
configs = append(configs, config)
}
} else {
fmt.Printf("Failed to load file: %s\n", err)
}
}
outConfig := handleMerge(configs)
if e := writeOutYaml(outConfig); e != nil {
log.Fatalf("Failed to write to %s [%v]")
}
}