-
Notifications
You must be signed in to change notification settings - Fork 244
/
namespace.go
144 lines (113 loc) · 4.03 KB
/
namespace.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
package namespace
import (
"fmt"
"os"
"strings"
"context"
dfutil "github.com/devfile/library/pkg/util"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
scontext "github.com/redhat-developer/odo/pkg/segment/context"
)
const RecommendedCommandName = "namespace"
var (
createExample = ktemplates.Examples(`
# Create a new namespace and set it as the current active namespace
%[1]s my-namespace
`)
createLongDesc = ktemplates.LongDesc(`Create a new namespace.
This command directly performs actions on the cluster and doesn't require a push.
Any new namespace created with this command will also be set as the current active namespace.
`)
createShortDesc = `Create a new namespace`
)
// NamespaceCreateOptions encapsulates the options for the odo namespace create command
type NamespaceCreateOptions struct {
// Context
*genericclioptions.Context
// Clients
clientset *clientset.Clientset
// Parameters
namespaceName string
// Flags
waitFlag bool
// value can be either 'project' or 'namespace', depending on what command is called
commandName string
}
var _ genericclioptions.Runnable = (*NamespaceCreateOptions)(nil)
// NewNamespaceCreateOptions creates a NamespaceCreateOptions instance
func NewNamespaceCreateOptions() *NamespaceCreateOptions {
return &NamespaceCreateOptions{}
}
func (o *NamespaceCreateOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}
// Complete completes NamespaceCreateOptions after they've been created
func (nco *NamespaceCreateOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
nco.namespaceName = args[0]
nco.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline))
if err != nil {
return err
}
if scontext.GetTelemetryStatus(cmdline.Context()) {
scontext.SetClusterType(cmdline.Context(), nco.KClient)
}
return nil
}
// Validate validates the parameters of the NamespaceCreateOptions
func (nco *NamespaceCreateOptions) Validate() error {
return dfutil.ValidateK8sResourceName("namespace name", nco.namespaceName)
}
// Run runs the namespace create command
func (nco *NamespaceCreateOptions) Run(ctx context.Context) (err error) {
// Create the "spinner"
s := &log.Status{}
// If the --wait parameter has been passed, we add a spinner..
if nco.waitFlag {
s = log.Spinnerf("Waiting for %s to come up", nco.commandName)
defer s.End(false)
}
// Create the namespace & end the spinner (if there is any..)
err = nco.clientset.ProjectClient.Create(nco.namespaceName, nco.waitFlag)
if err != nil {
return err
}
s.End(true)
successMessage := fmt.Sprintf(`%s %q is ready for use`, strings.Title(nco.commandName), nco.namespaceName)
log.Successf(successMessage)
// Set the current namespace when created
err = nco.clientset.ProjectClient.SetCurrent(nco.namespaceName)
if err != nil {
return err
}
log.Successf("New %[1]s created and now using %[1]s: %v", nco.commandName, nco.namespaceName)
return nil
}
// NewCmdNamespaceCreate creates the namespace create command
func NewCmdNamespaceCreate(name, fullName string) *cobra.Command {
o := NewNamespaceCreateOptions()
// To help the UI messages deal better with namespace vs project
o.commandName = name
if len(os.Args) > 2 {
o.commandName = os.Args[2]
}
namespaceCreateCmd := &cobra.Command{
Use: name,
Short: createShortDesc,
Long: createLongDesc,
Example: fmt.Sprintf(createExample, fullName),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
Annotations: map[string]string{"command": "main"},
Aliases: []string{"project"},
}
namespaceCreateCmd.Flags().BoolVarP(&o.waitFlag, "wait", "w", false, "Wait until the namespace is ready")
clientset.Add(namespaceCreateCmd, clientset.PROJECT)
return namespaceCreateCmd
}