forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_kubeconfig.go
199 lines (171 loc) · 5.92 KB
/
create_kubeconfig.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
package admin
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/spf13/cobra"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
)
const CreateKubeConfigCommandName = "create-kubeconfig"
type CreateKubeConfigOptions struct {
APIServerURL string
PublicAPIServerURL string
APIServerCAFile string
ServerNick string
CertFile string
KeyFile string
UserNick string
ContextNick string
ContextNamespace string
KubeConfigFile string
Output cmdutil.Output
}
func NewCommandCreateKubeConfig(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateKubeConfigOptions{Output: cmdutil.Output{out}}
cmd := &cobra.Command{
Use: commandName,
Short: "Create a basic .kubeconfig file from client certs",
Long: `
Create's a .kubeconfig file at <--kubeconfig> that looks like this:
clusters:
- cluster:
certificate-authority-data: <contents of --certificate-authority>
server: <--master>
name: <--cluster>
- cluster:
certificate-authority-data: <contents of --certificate-authority>
server: <--public-master>
name: public-<--cluster>
contexts:
- context:
cluster: <--cluster>
user: <--user>
namespace: <--namespace>
name: <--context>
- context:
cluster: public-<--cluster>
user: <--user>
namespace: <--namespace>
name: public-<--context>
current-context: <--context>
kind: Config
users:
- name: <--user>
user:
client-certificate-data: <contents of --client-certificate>
client-key-data: <contents of --client-key>
`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Validate(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if _, err := options.CreateKubeConfig(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.SetOutput(out)
flags := cmd.Flags()
flags.StringVar(&options.APIServerURL, "master", "https://localhost:8443", "The API server's URL.")
flags.StringVar(&options.PublicAPIServerURL, "public-master", "", "The API public facing server's URL (if applicable).")
flags.StringVar(&options.APIServerCAFile, "certificate-authority", "openshift.local.config/master/ca.crt", "Path to the API server's CA file.")
flags.StringVar(&options.ServerNick, "cluster", "master", "Nick name for this server in .kubeconfig.")
flags.StringVar(&options.CertFile, "client-certificate", "", "The client cert file.")
flags.StringVar(&options.KeyFile, "client-key", "", "The client key file.")
flags.StringVar(&options.UserNick, "user", "user", "Nick name for this user in .kubeconfig.")
flags.StringVar(&options.ContextNick, "context", "", "Nick name for this context in .kubeconfig.")
flags.StringVar(&options.ContextNamespace, "namespace", kapi.NamespaceDefault, "Namespace for this context in .kubeconfig.")
flags.StringVar(&options.KubeConfigFile, "kubeconfig", ".kubeconfig", "Path for the resulting .kubeconfig file.")
return cmd
}
func (o CreateKubeConfigOptions) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported")
}
if len(o.KubeConfigFile) == 0 {
return errors.New("kubeconfig must be provided")
}
if len(o.CertFile) == 0 {
return errors.New("client-certificate must be provided")
}
if len(o.KeyFile) == 0 {
return errors.New("client-key must be provided")
}
if len(o.APIServerCAFile) == 0 {
return errors.New("certificate-authority must be provided")
}
if len(o.ServerNick) == 0 {
return errors.New("cluster must be provided")
}
if len(o.UserNick) == 0 {
return errors.New("user-nick must be provided")
}
if len(o.ContextNamespace) == 0 {
return errors.New("namespace must be provided")
}
return nil
}
func (o CreateKubeConfigOptions) CreateKubeConfig() (*clientcmdapi.Config, error) {
glog.V(2).Infof("creating a .kubeconfig with: %#v", o)
// if you don't specify a context nick, assign it to the context namespace
if len(o.ContextNick) == 0 {
o.ContextNick = o.ContextNamespace
}
caData, err := ioutil.ReadFile(o.APIServerCAFile)
if err != nil {
return nil, err
}
certData, err := ioutil.ReadFile(o.CertFile)
if err != nil {
return nil, err
}
keyData, err := ioutil.ReadFile(o.KeyFile)
if err != nil {
return nil, err
}
credentials := make(map[string]clientcmdapi.AuthInfo)
credentials[o.UserNick] = clientcmdapi.AuthInfo{
ClientCertificateData: certData,
ClientKeyData: keyData,
}
clusters := make(map[string]clientcmdapi.Cluster)
clusters[o.ServerNick] = clientcmdapi.Cluster{
Server: o.APIServerURL,
CertificateAuthorityData: caData,
}
contexts := make(map[string]clientcmdapi.Context)
contexts[o.ContextNick] = clientcmdapi.Context{Cluster: o.ServerNick, AuthInfo: o.UserNick, Namespace: o.ContextNamespace}
createPublic := len(o.PublicAPIServerURL) > 0
if createPublic {
publicClusterNick := "public-" + o.ServerNick
publicContextNick := "public-" + o.ContextNick
clusters[publicClusterNick] = clientcmdapi.Cluster{
Server: o.PublicAPIServerURL,
CertificateAuthorityData: caData,
}
contexts[publicContextNick] = clientcmdapi.Context{Cluster: publicClusterNick, AuthInfo: o.UserNick, Namespace: o.ContextNamespace}
}
kubeConfig := &clientcmdapi.Config{
Clusters: clusters,
AuthInfos: credentials,
Contexts: contexts,
CurrentContext: o.ContextNick,
}
fmt.Fprintf(o.Output.Get(), "Generating '%s' API client config as %s\n", o.UserNick, o.KubeConfigFile)
// Ensure the parent dir exists
if err := os.MkdirAll(filepath.Dir(o.KubeConfigFile), os.FileMode(0755)); err != nil {
return nil, err
}
if err := clientcmd.WriteToFile(*kubeConfig, o.KubeConfigFile); err != nil {
return nil, err
}
return kubeConfig, nil
}