generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 98
/
main.go
160 lines (133 loc) · 4.26 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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
flag "github.com/spf13/pflag"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api/v1"
"sigs.k8s.io/yaml"
"sigs.k8s.io/prow/pkg/kube"
)
const (
// defaultInput is the default input source.
defaultInput = "/dev/stdin"
// defaultOutput is the default output source.
defaultOutput = "/dev/stdout"
)
// Cluster represents the information necessary to talk to a Kubernetes master endpoint.
type Cluster struct {
// The IP address of the cluster's master endpoint.
Endpoint string `json:"endpoint"`
// Base64-encoded public cert used by clients to authenticate to the cluster endpoint.
ClientCertificate []byte `json:"clientCertificate"`
// Base64-encoded private key used by clients..
ClientKey []byte `json:"clientKey"`
// Base64-encoded public certificate that is the root of trust for the cluster.
ClusterCACertificate []byte `json:"clusterCaCertificate"`
}
// options are the available command-line flags.
type options struct {
input string
output string
}
// parseFlags parses the command-line flags.
func (o *options) parseFlags() {
flag.StringVarP(&o.input, "input", "i", defaultInput, "Input cluster map file.")
flag.StringVarP(&o.output, "output", "o", defaultOutput, "Output kubeconfig file.")
flag.Parse()
}
// printErrAndExit prints an error message to stderr and exits with a status code.
func printErrAndExit(err error, code int) {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(code)
}
// unmarshalClusterMap reads a map[string]Cluster in yaml bytes.
func unmarshalClusterMap(data []byte) (map[string]Cluster, error) {
var raw map[string]Cluster
if err := yaml.Unmarshal(data, &raw); err != nil {
// If we failed to unmarshal the multicluster format try the single Cluster format.
var singleConfig Cluster
if err := yaml.Unmarshal(data, &singleConfig); err != nil {
return nil, err
}
raw = map[string]Cluster{kube.DefaultClusterAlias: singleConfig}
}
return raw, nil
}
// createKubeConfigFromClusterMap creates a standard kube config from a cluster map.
func createKubeConfigFromClusterMap(cm map[string]Cluster) ([]byte, error) {
config := clientcmdapi.Config{
APIVersion: "v1",
Kind: "Config",
Clusters: []clientcmdapi.NamedCluster{},
AuthInfos: []clientcmdapi.NamedAuthInfo{},
Contexts: []clientcmdapi.NamedContext{},
CurrentContext: kube.DefaultClusterAlias,
}
names := make([]string, 0, len(cm))
for k := range cm {
names = append(names, k)
}
sort.Strings(names)
for _, name := range names {
config.Clusters = append(config.Clusters, clientcmdapi.NamedCluster{
Name: name,
Cluster: clientcmdapi.Cluster{
Server: cm[name].Endpoint,
CertificateAuthorityData: cm[name].ClusterCACertificate,
},
})
config.AuthInfos = append(config.AuthInfos, clientcmdapi.NamedAuthInfo{
Name: name,
AuthInfo: clientcmdapi.AuthInfo{
ClientCertificateData: cm[name].ClientCertificate,
ClientKeyData: cm[name].ClientKey,
},
})
config.Contexts = append(config.Contexts, clientcmdapi.NamedContext{
Name: name,
Context: clientcmdapi.Context{
Cluster: name,
AuthInfo: name,
},
})
}
return yaml.Marshal(config)
}
// main entry point.
func main() {
var o options
o.parseFlags()
in, err := os.ReadFile(o.input)
if err != nil {
printErrAndExit(err, 1)
}
cm, err := unmarshalClusterMap(in)
if err != nil {
printErrAndExit(err, 1)
}
kc, err := createKubeConfigFromClusterMap(cm)
if err != nil {
printErrAndExit(err, 1)
}
dir := filepath.Dir(o.output)
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
printErrAndExit(err, 1)
}
if err = os.WriteFile(o.output, kc, 0644); err != nil {
printErrAndExit(err, 1)
}
}