forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.go
382 lines (318 loc) · 12.7 KB
/
project.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package cmd
import (
"bytes"
"errors"
"fmt"
"io"
"net/url"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
restclient "k8s.io/client-go/rest"
kclientcmd "k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
clientcfg "github.com/openshift/origin/pkg/client/config"
cliconfig "github.com/openshift/origin/pkg/oc/cli/config"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
projectapihelpers "github.com/openshift/origin/pkg/project/apis/project/helpers"
projectclientinternal "github.com/openshift/origin/pkg/project/generated/internalclientset"
projectclient "github.com/openshift/origin/pkg/project/generated/internalclientset/typed/project/internalversion"
projectutil "github.com/openshift/origin/pkg/project/util"
"github.com/spf13/cobra"
)
type ProjectOptions struct {
Config clientcmdapi.Config
ClientConfig *restclient.Config
ClientFn func() (projectclient.ProjectInterface, kclientset.Interface, error)
Out io.Writer
PathOptions *kclientcmd.PathOptions
ProjectName string
ProjectOnly bool
DisplayShort bool
// SkipAccessValidation means that if a specific name is requested, don't bother checking for access to the project
SkipAccessValidation bool
}
var (
projectLong = templates.LongDesc(`
Switch to another project and make it the default in your configuration
If no project was specified on the command line, display information about the current active
project. Since you can use this command to connect to projects on different servers, you will
occasionally encounter projects of the same name on different servers. When switching to that
project, a new local context will be created that will have a unique name - for instance,
'myapp-2'. If you have previously created a context with a different name than the project
name, this command will accept that context name instead.
For advanced configuration, or to manage the contents of your config file, use the 'config'
command.`)
projectExample = templates.Examples(`
# Switch to 'myapp' project
%[1]s myapp
# Display the project currently in use
%[1]s`)
)
// NewCmdProject implements the OpenShift cli rollback command
func NewCmdProject(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &ProjectOptions{}
cmd := &cobra.Command{
Use: "project [NAME]",
Short: "Switch to another project",
Long: projectLong,
Example: fmt.Sprintf(projectExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
options.PathOptions = cliconfig.NewPathOptions(cmd)
if err := options.Complete(f, args, out); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(cmd, err.Error()))
}
if err := options.RunProject(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().BoolVarP(&options.DisplayShort, "short", "q", false, "If true, display only the project name")
return cmd
}
func (o *ProjectOptions) Complete(f *clientcmd.Factory, args []string, out io.Writer) error {
var err error
argsLength := len(args)
switch {
case argsLength > 1:
return errors.New("Only one argument is supported (project name or context name).")
case argsLength == 1:
o.ProjectName = args[0]
}
o.Config, err = f.RawConfig()
if err != nil {
return err
}
o.ClientConfig, err = f.ClientConfig()
if err != nil {
contextNameExists := false
if _, exists := o.GetContextFromName(o.ProjectName); exists {
contextNameExists = exists
}
if _, isURLError := err.(*url.Error); !(isURLError || kapierrors.IsInternalError(err)) || !contextNameExists {
return err
}
// if the argument for o.ProjectName passed by the user is a context name,
// prevent local context-switching from failing due to an unreachable
// server or an unfetchable ClientConfig.
o.Config.CurrentContext = o.ProjectName
if err := kclientcmd.ModifyConfig(o.PathOptions, o.Config, true); err != nil {
return err
}
// since we failed to retrieve ClientConfig for the current server,
// fetch local OpenShift client config
o.ClientConfig, err = f.ClientConfig()
if err != nil {
return err
}
}
o.ClientFn = func() (projectclient.ProjectInterface, kclientset.Interface, error) {
kc, err := f.ClientSet()
if err != nil {
return nil, nil, err
}
projectClient, err := projectclientinternal.NewForConfig(o.ClientConfig)
if err != nil {
return nil, nil, err
}
return projectClient.Project(), kc, nil
}
o.Out = out
return nil
}
func (o ProjectOptions) Validate() error {
return nil
}
// RunProject contains all the necessary functionality for the OpenShift cli project command
func (o ProjectOptions) RunProject() error {
config := o.Config
clientCfg := o.ClientConfig
out := o.Out
var currentProject string
currentContext := config.Contexts[config.CurrentContext]
if currentContext != nil {
currentProject = currentContext.Namespace
}
// No argument provided, we will just print info
if len(o.ProjectName) == 0 {
if len(currentProject) > 0 {
if o.DisplayShort {
fmt.Fprintln(out, currentProject)
return nil
}
client, kubeclient, err := o.ClientFn()
if err != nil {
return err
}
switch err := confirmProjectAccess(currentProject, client, kubeclient); {
case kapierrors.IsForbidden(err):
return fmt.Errorf("you do not have rights to view project %q.", currentProject)
case kapierrors.IsNotFound(err):
return fmt.Errorf("the project %q specified in your config does not exist.", currentProject)
case err != nil:
return err
}
defaultContextName := clientcfg.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
// a context name they didn't choose
if config.CurrentContext == defaultContextName {
fmt.Fprintf(out, "Using project %q on server %q.\n", currentProject, clientCfg.Host)
} else {
fmt.Fprintf(out, "Using project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
}
} else {
if o.DisplayShort {
return fmt.Errorf("no project has been set")
}
fmt.Fprintf(out, "No project has been set. Pass a project name to make that the default.\n")
}
return nil
}
// We have an argument that can be either a context or project
argument := o.ProjectName
contextInUse := ""
namespaceInUse := ""
// Check if argument is an existing context, if so just set it as the context in use.
// If not a context then we will try to handle it as a project.
if context, contextExists := o.GetContextFromName(argument); contextExists {
contextInUse = argument
namespaceInUse = context.Namespace
config.CurrentContext = argument
} else {
if !o.SkipAccessValidation {
client, kubeclient, err := o.ClientFn()
if err != nil {
return err
}
if err := confirmProjectAccess(argument, client, kubeclient); err != nil {
if isNotFound, isForbidden := kapierrors.IsNotFound(err), kapierrors.IsForbidden(err); isNotFound || isForbidden {
var msg string
if isForbidden {
msg = fmt.Sprintf("You are not a member of project %q.", argument)
} else {
msg = fmt.Sprintf("A project named %q does not exist on %q.", argument, clientCfg.Host)
}
projects, err := getProjects(client, kubeclient)
if err == nil {
switch len(projects) {
case 0:
msg += "\nYou are not a member of any projects. You can request a project to be created with the 'new-project' command."
case 1:
msg += fmt.Sprintf("\nYou have one project on this server: %s", projectapihelpers.DisplayNameAndNameForProject(&projects[0]))
default:
msg += "\nYour projects are:"
for _, project := range projects {
msg += fmt.Sprintf("\n* %s", projectapihelpers.DisplayNameAndNameForProject(&project))
}
}
}
if hasMultipleServers(config) {
msg += "\nTo see projects on another server, pass '--server=<server>'."
}
return errors.New(msg)
}
return err
}
}
projectName := argument
kubeconfig, err := cliconfig.CreateConfig(projectName, o.ClientConfig)
if err != nil {
return err
}
merged, err := cliconfig.MergeConfig(config, *kubeconfig)
if err != nil {
return err
}
config = *merged
namespaceInUse = projectName
contextInUse = merged.CurrentContext
}
if err := kclientcmd.ModifyConfig(o.PathOptions, config, true); err != nil {
return err
}
if o.DisplayShort {
fmt.Fprintln(out, namespaceInUse)
return nil
}
// calculate what name we'd generate for the context. If the context has the same name, don't drop it into the output, because the user won't
// recognize the name since they didn't choose it.
defaultContextName := clientcfg.GetContextNickname(namespaceInUse, config.Contexts[contextInUse].Cluster, config.Contexts[contextInUse].AuthInfo)
switch {
// if there is no namespace, then the only information we can provide is the context and server
case (len(namespaceInUse) == 0):
fmt.Fprintf(out, "Now using context named %q on server %q.\n", contextInUse, clientCfg.Host)
// inform them that they are already in the project they are trying to switch to
case currentProject == namespaceInUse:
fmt.Fprintf(out, "Already on project %q on server %q.\n", currentProject, clientCfg.Host)
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
// a context name they didn't choose
case (argument == namespaceInUse) && (contextInUse == defaultContextName):
fmt.Fprintf(out, "Now using project %q on server %q.\n", namespaceInUse, clientCfg.Host)
// in all other cases, display all information
default:
fmt.Fprintf(out, "Now using project %q from context named %q on server %q.\n", namespaceInUse, contextInUse, clientCfg.Host)
}
return nil
}
// returns a context by the given contextName and a boolean true if the context exists
func (o *ProjectOptions) GetContextFromName(contextName string) (*clientcmdapi.Context, bool) {
if context, contextExists := o.Config.Contexts[contextName]; !o.ProjectOnly && contextExists {
return context, true
}
return nil, false
}
func confirmProjectAccess(currentProject string, projectClient projectclient.ProjectInterface, kClient kclientset.Interface) error {
_, projectErr := projectClient.Projects().Get(currentProject, metav1.GetOptions{})
if !kapierrors.IsNotFound(projectErr) && !kapierrors.IsForbidden(projectErr) {
return projectErr
}
// at this point we know the error is a not found or forbidden, but we'll test namespaces just in case we're running on kube
if _, err := kClient.Core().Namespaces().Get(currentProject, metav1.GetOptions{}); err == nil {
return nil
}
// otherwise return the openshift error default
return projectErr
}
func getProjects(projectClient projectclient.ProjectInterface, kClient kclientset.Interface) ([]projectapi.Project, error) {
projects, err := projectClient.Projects().List(metav1.ListOptions{})
if err == nil {
return projects.Items, nil
}
// if this is kube with authorization enabled, this endpoint will be forbidden. OpenShift allows this for everyone.
if err != nil && !(kapierrors.IsNotFound(err) || kapierrors.IsForbidden(err)) {
return nil, err
}
namespaces, err := kClient.Core().Namespaces().List(metav1.ListOptions{})
if err != nil {
return nil, err
}
projects = projectutil.ConvertNamespaceList(namespaces)
return projects.Items, nil
}
func clusterAndAuthEquality(clientCfg *restclient.Config, cluster clientcmdapi.Cluster, authInfo clientcmdapi.AuthInfo) bool {
return cluster.Server == clientCfg.Host &&
cluster.InsecureSkipTLSVerify == clientCfg.Insecure &&
cluster.CertificateAuthority == clientCfg.CAFile &&
bytes.Equal(cluster.CertificateAuthorityData, clientCfg.CAData) &&
authInfo.Token == clientCfg.BearerToken &&
authInfo.ClientCertificate == clientCfg.TLSClientConfig.CertFile &&
bytes.Equal(authInfo.ClientCertificateData, clientCfg.TLSClientConfig.CertData) &&
authInfo.ClientKey == clientCfg.TLSClientConfig.KeyFile &&
bytes.Equal(authInfo.ClientKeyData, clientCfg.TLSClientConfig.KeyData)
}
// TODO these kind of funcs could be moved to some kind of clientcmd util
func hasMultipleServers(config clientcmdapi.Config) bool {
server := ""
for _, cluster := range config.Clusters {
if len(server) == 0 {
server = cluster.Server
}
if server != cluster.Server {
return true
}
}
return false
}