forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_client.go
153 lines (120 loc) · 4.29 KB
/
create_client.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
package admin
import (
"errors"
"io"
"io/ioutil"
"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/util"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
)
const CreateClientCommandName = "create-api-client-config"
type CreateClientOptions struct {
GetSignerCertOptions *GetSignerCertOptions
ClientDir string
User string
Groups util.StringList
APIServerCAFile string
APIServerURL string
PublicAPIServerURL string
Output cmdutil.Output
}
const createClientLong = `
Create a client configuration for connecting to OpenShift
This command creates a folder containing a client certificate, a client key,
a server certificate authority, and a .kubeconfig file for connecting to the
master as the provided user.
`
func NewCommandCreateClient(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateClientOptions{GetSignerCertOptions: &GetSignerCertOptions{}, Output: cmdutil.Output{out}}
cmd := &cobra.Command{
Use: commandName,
Short: "Create a config file for connecting to the server as a user",
Long: createClientLong,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Validate(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.CreateClientFolder(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.SetOutput(out)
flags := cmd.Flags()
BindGetSignerCertOptions(options.GetSignerCertOptions, flags, "")
flags.StringVar(&options.ClientDir, "client-dir", "", "The client data directory.")
flags.StringVar(&options.User, "user", "", "The scope qualified username.")
flags.Var(&options.Groups, "groups", "The list of groups this user belongs to. Comma delimited list")
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.")
return cmd
}
func (o CreateClientOptions) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported")
}
if len(o.ClientDir) == 0 {
return errors.New("client-dir must be provided")
}
if len(o.User) == 0 {
return errors.New("user must be provided")
}
if len(o.APIServerURL) == 0 {
return errors.New("master must be provided")
}
if len(o.APIServerCAFile) == 0 {
return errors.New("certificate-authority must be provided")
}
if o.GetSignerCertOptions == nil {
return errors.New("signer options are required")
}
if err := o.GetSignerCertOptions.Validate(); err != nil {
return err
}
return nil
}
func (o CreateClientOptions) CreateClientFolder() error {
glog.V(2).Infof("creating a .kubeconfig with: %#v", o)
clientCertFile := DefaultCertFilename(o.ClientDir, o.User)
clientKeyFile := DefaultKeyFilename(o.ClientDir, o.User)
clientCopyOfCAFile := DefaultCAFilename(o.ClientDir, "ca")
kubeConfigFile := DefaultKubeConfigFilename(o.ClientDir, o.User)
createClientCertOptions := CreateClientCertOptions{
GetSignerCertOptions: o.GetSignerCertOptions,
CertFile: clientCertFile,
KeyFile: clientKeyFile,
User: o.User,
Groups: o.Groups,
Overwrite: true,
Output: o.Output,
}
if _, err := createClientCertOptions.CreateClientCert(); err != nil {
return err
}
// copy the CA file over
if caBytes, err := ioutil.ReadFile(o.APIServerCAFile); err != nil {
return err
} else if err := ioutil.WriteFile(clientCopyOfCAFile, caBytes, 0644); err != nil {
return nil
}
createKubeConfigOptions := CreateKubeConfigOptions{
APIServerURL: o.APIServerURL,
PublicAPIServerURL: o.PublicAPIServerURL,
APIServerCAFile: clientCopyOfCAFile,
ServerNick: "master",
CertFile: clientCertFile,
KeyFile: clientKeyFile,
UserNick: o.User,
ContextNamespace: kapi.NamespaceDefault,
KubeConfigFile: kubeConfigFile,
Output: o.Output,
}
if _, err := createKubeConfigOptions.CreateKubeConfig(); err != nil {
return err
}
return nil
}