forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.go
385 lines (324 loc) · 12.5 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
383
384
385
package project
import (
"errors"
"fmt"
"net/url"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
kclientcmd "k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
projectv1 "github.com/openshift/api/project/v1"
projectv1client "github.com/openshift/client-go/project/clientset/versioned/typed/project/v1"
oapi "github.com/openshift/origin/pkg/api"
clientcfg "github.com/openshift/origin/pkg/client/config"
cliconfig "github.com/openshift/origin/pkg/oc/lib/kubeconfig"
)
type ProjectOptions struct {
Config clientcmdapi.Config
RESTConfig *rest.Config
ClientFn func() (projectv1client.ProjectV1Interface, corev1client.CoreV1Interface, error)
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
genericclioptions.IOStreams
}
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 project myapp
# Display the project currently in use
%[1]s project`)
)
func NewProjectOptions(streams genericclioptions.IOStreams) *ProjectOptions {
return &ProjectOptions{
IOStreams: streams,
}
}
// NewCmdProject implements the OpenShift cli rollback command
func NewCmdProject(fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := NewProjectOptions(streams)
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) {
o.PathOptions = cliconfig.NewPathOptions(cmd)
kcmdutil.CheckErr(o.Complete(f, args))
kcmdutil.CheckErr(o.Run())
},
}
cmd.Flags().BoolVarP(&o.DisplayShort, "short", "q", o.DisplayShort, "If true, display only the project name")
return cmd
}
func (o *ProjectOptions) Complete(f genericclioptions.RESTClientGetter, args []string) error {
switch argsLength := len(args); {
case argsLength > 1:
return errors.New("Only one argument is supported (project name or context name).")
case argsLength == 1:
o.ProjectName = args[0]
}
var err error
o.Config, err = f.ToRawKubeConfigLoader().RawConfig()
if err != nil {
return err
}
o.RESTConfig, err = f.ToRESTConfig()
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 KubeControlPlaneRESTConfig.
o.Config.CurrentContext = o.ProjectName
if err := kclientcmd.ModifyConfig(o.PathOptions, o.Config, true); err != nil {
return err
}
// since we failed to retrieve KubeControlPlaneRESTConfig for the current server,
// fetch local OpenShift client config
o.RESTConfig, err = f.ToRESTConfig()
if err != nil {
return err
}
}
o.ClientFn = func() (projectv1client.ProjectV1Interface, corev1client.CoreV1Interface, error) {
kc, err := corev1client.NewForConfig(o.RESTConfig)
if err != nil {
return nil, nil, err
}
projectClient, err := projectv1client.NewForConfig(o.RESTConfig)
if err != nil {
return nil, nil, err
}
return projectClient, kc, nil
}
return nil
}
// RunProject contains all the necessary functionality for the OpenShift cli project command
func (o ProjectOptions) Run() error {
config := o.Config
clientCfg := o.RESTConfig
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 {
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
}
if o.DisplayShort {
fmt.Fprintln(o.Out, currentProject)
return nil
}
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(o.Out, "Using project %q on server %q.\n", currentProject, clientCfg.Host)
} else {
fmt.Fprintf(o.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(o.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", DisplayNameForProject(&projects[0]))
default:
msg += "\nYour projects are:"
for _, project := range projects {
msg += fmt.Sprintf("\n* %s", DisplayNameForProject(&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.RESTConfig)
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(o.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(o.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(o.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(o.Out, "Now using project %q on server %q.\n", namespaceInUse, clientCfg.Host)
// in all other cases, display all information
default:
fmt.Fprintf(o.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 projectv1client.ProjectV1Interface, kClient corev1client.CoreV1Interface) 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.Namespaces().Get(currentProject, metav1.GetOptions{}); err == nil {
return nil
}
// otherwise return the openshift error default
return projectErr
}
func GetProjects(projectClient projectv1client.ProjectV1Interface, kClient corev1client.CoreV1Interface) ([]projectv1.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.Namespaces().List(metav1.ListOptions{})
if err != nil {
return nil, err
}
projects = convertNamespaceList(namespaces)
return projects.Items, nil
}
// 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
}
func convertNamespaceList(namespaceList *corev1.NamespaceList) *projectv1.ProjectList {
projects := &projectv1.ProjectList{}
for _, ns := range namespaceList.Items {
projects.Items = append(projects.Items, projectv1.Project{
ObjectMeta: ns.ObjectMeta,
Spec: projectv1.ProjectSpec{
Finalizers: ns.Spec.Finalizers,
},
Status: projectv1.ProjectStatus{
Phase: ns.Status.Phase,
},
})
}
return projects
}
func DisplayNameForProject(project *projectv1.Project) string {
displayName := project.Annotations[oapi.OpenShiftDisplayName]
if len(displayName) == 0 {
displayName = project.Annotations["displayName"]
}
if len(displayName) > 0 && displayName != project.Name {
return fmt.Sprintf("%s (%s)", displayName, project.Name)
}
return project.Name
}