This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
231 lines (206 loc) · 5.43 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
v1 "k8s.io/client-go/tools/clientcmd/api/v1"
"sigs.k8s.io/yaml"
)
var (
force bool
setCurrentContext bool
name string
stdout bool
help bool
flags = new(flag.FlagSet)
Usage = func() {
fmt.Fprintf(os.Stderr, "`k8s-ctx-import` is an utility to merge kubernetes contexts to a single kubeconfig.\n")
fmt.Fprintf(os.Stderr, "It imports the context either to `~/.kube/config` or to the file defined by the `KUBECONFIG` environment variable.\n")
fmt.Fprintf(os.Stderr, "Usage of k8s-ctx-import:\n")
flags.PrintDefaults()
fmt.Fprintf(os.Stderr, "Example:\n")
fmt.Fprintf(os.Stderr, " cat /some/kubeconfig | k8s-ctx-import\n")
}
)
func init() {
flag.Usage = Usage
flags.BoolVar(&help, "h", false, "")
flags.BoolVar(&help, "help", false, "display this help and exit")
flags.BoolVar(&force, "force", false, "force import of context")
flags.BoolVar(&setCurrentContext, "set-current-context", true, "set current context to imported context")
flags.StringVar(&name, "name", "", "renames the context for the import")
flags.BoolVar(&stdout, "stdout", false, "print result to stdout instead of writing to file")
}
// readFile reads from stdin (if path is empty) or from a file and returns its string
func readFile(path string) ([]byte, error) {
var b []byte
var err error
if path == "" {
b, err = ioutil.ReadAll(os.Stdin)
} else {
b, err = ioutil.ReadFile(path)
}
if err != nil {
return nil, err
}
return b, err
}
func readKubeconfig(path string) (*v1.Config, error) {
d := &v1.Config{}
c, err := readFile(path)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(c, &d)
if err != nil {
return nil, err
}
return d, nil
}
func main() {
flags.Parse(os.Args[1:])
if help {
Usage()
os.Exit(0)
}
// determine output file
destinationPath := os.Getenv("KUBECONFIG")
if destinationPath == "" {
destinationPath = path.Join(os.Getenv("HOME"), ".kube", "config")
}
// set output to stderr
log.SetOutput(os.Stderr)
// "" means we read from stdin
newcfg, err := mergeKubeconfig("", destinationPath)
if err != nil {
log.Fatalf("%v\n", err)
}
b, err := yaml.Marshal(newcfg)
if err != nil {
log.Fatalf("error Marshaling new kubeconfig\n")
}
if destinationPath != "" && !stdout {
err = ioutil.WriteFile(destinationPath, b, 0644)
if err != nil {
log.Fatalf("unable to write file %s\n", destinationPath)
}
} else {
fmt.Println(string(b))
}
}
// mergeKubeconfig tries to merge the kubeconfig at sourcePath (stdin if empty string)
// to the kubeconfig at destinationPath and returns the result
func mergeKubeconfig(sourcePath, destinationPath string) (*v1.Config, error) {
source, err := readKubeconfig(sourcePath)
if err != nil {
return nil, err
}
if source == nil {
return nil, fmt.Errorf("source kubeconfig is empty")
}
destination, err := readKubeconfig(destinationPath)
if err != nil {
return nil, err
}
if destination == nil {
destination = &v1.Config{
APIVersion: "v1",
Kind: "Config",
}
}
// extract context, auth and cluster information from source
var ctx *v1.NamedContext
for _, c := range source.Contexts {
if c.Name == source.CurrentContext {
ctx = &c
break
}
}
if ctx == nil {
return nil, fmt.Errorf("ERROR: context %s not found", source.CurrentContext)
}
var authInfo *v1.NamedAuthInfo
for _, a := range source.AuthInfos {
if a.Name == ctx.Context.AuthInfo {
authInfo = &a
break
}
}
if authInfo == nil {
return nil, fmt.Errorf("authInfo %s not found", ctx.Context.AuthInfo)
}
var cluster *v1.NamedCluster
for _, c := range source.Clusters {
if c.Name == ctx.Context.Cluster {
cluster = &c
break
}
}
if cluster == nil {
return nil, fmt.Errorf("cluster %s not found", ctx.Context.Cluster)
}
// set new context name if flag is set
if name != "" {
ctx.Name = name
ctx.Context.Cluster = name + "-" + cluster.Name
ctx.Context.AuthInfo = name + "-" + authInfo.Name
cluster.Name = name + "-" + cluster.Name
authInfo.Name = name + "-" + authInfo.Name
}
var exists bool
// check if context having the same name already exists
exists = false
for i, c := range destination.Contexts {
if c.Name == ctx.Name {
if force {
destination.Contexts[i] = *ctx
} else {
log.Printf("WARN: context having the same name (%s) already exists\n", c.Name)
}
exists = true
break
}
}
if !exists {
destination.Contexts = append(destination.Contexts, *ctx)
}
// check if cluster having the same name already exists
exists = false
for i, c := range destination.Clusters {
if c.Name == cluster.Name {
if force {
destination.Clusters[i] = *cluster
} else {
log.Printf("WARN: cluster information having the same name (%s) already exists\n", c.Name)
}
exists = true
break
}
}
if !exists {
destination.Clusters = append(destination.Clusters, *cluster)
}
// check if authInfo having the same name already exists
exists = false
for i, a := range destination.AuthInfos {
if a.Name == authInfo.Name {
if force {
destination.AuthInfos[i] = *authInfo
} else {
log.Printf("WARN: authentication information having the same name (%s) already exists\n", a.Name)
}
exists = true
break
}
}
if !exists {
destination.AuthInfos = append(destination.AuthInfos, *authInfo)
}
if setCurrentContext {
destination.CurrentContext = ctx.Name
}
return destination, nil
}