-
Notifications
You must be signed in to change notification settings - Fork 786
/
namespace.go
161 lines (145 loc) · 3.77 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/kube"
"io"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"sort"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"k8s.io/client-go/kubernetes"
)
type NamespaceOptions struct {
CommonOptions
}
const (
noContextDefinedError = "There is no context defined in your Kubernetes configuration"
)
var (
namespace_long = templates.LongDesc(`
Displays or changes the current namespace.`)
namespace_example = templates.Examples(`
# view the current namespace
jx ns -b
# to select the namespace to switch to
jx ns
# Change the current namespace to 'cheese'
jx ns cheese`)
)
func NewCmdNamespace(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &NamespaceOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "namespace",
Aliases: []string{"ns"},
Short: "View or change the current namespace context in the current Kubernetes cluster",
Long: namespace_long,
Example: namespace_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
return cmd
}
func (o *NamespaceOptions) Run() error {
config, po, err := o.Kube().LoadConfig()
if err != nil {
return err
}
ns := ""
args := o.Args
if len(args) > 0 {
ns = args[0]
}
client, _, err := o.KubeClient()
if err != nil {
return err
}
names, err := GetNamespaceNames(client)
if err != nil {
return err
}
currentNS := kube.CurrentNamespace(config)
if ns == "" && !o.BatchMode {
defaultNamespace := ""
ctx := kube.CurrentContext(config)
if ctx != nil {
defaultNamespace = currentNS
}
pick, err := o.PickNamespace(names, defaultNamespace)
if err != nil {
return err
}
ns = pick
}
info := util.ColorInfo
if ns != "" && ns != currentNS {
_, err = client.CoreV1().Namespaces().Get(ns, meta_v1.GetOptions{})
if err != nil {
return util.InvalidArg(ns, names)
}
newConfig := *config
ctx := kube.CurrentContext(config)
if ctx == nil {
return fmt.Errorf(noContextDefinedError)
}
if ctx.Namespace == ns {
return nil
}
ctx.Namespace = ns
err = clientcmd.ModifyConfig(po, newConfig, false)
if err != nil {
return fmt.Errorf("Failed to update the kube config %s", err)
}
fmt.Fprintf(o.Out, "Now using namespace '%s' on server '%s'.\n", info(ctx.Namespace), info(kube.Server(config, ctx)))
} else {
ns := kube.CurrentNamespace(config)
server := kube.CurrentServer(config)
fmt.Fprintf(o.Out, "Using namespace '%s' from context named '%s' on server '%s'.\n", info(ns), info(config.CurrentContext), info(server))
}
return nil
}
// GetNamespaceNames returns the sorted list of environment names
func GetNamespaceNames(client kubernetes.Interface) ([]string, error) {
names := []string{}
list, err := client.CoreV1().Namespaces().List(meta_v1.ListOptions{})
if err != nil {
return names, fmt.Errorf("Failed to load Namespaces %s", err)
}
for _, n := range list.Items {
names = append(names, n.Name)
}
sort.Strings(names)
return names, nil
}
func (o *NamespaceOptions) PickNamespace(names []string, defaultNamespace string) (string, error) {
if len(names) == 0 {
return "", nil
}
if len(names) == 1 {
return names[0], nil
}
name := ""
prompt := &survey.Select{
Message: "Change namespace:",
Options: names,
Default: defaultNamespace,
}
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
err := survey.AskOne(prompt, &name, nil, surveyOpts)
return name, err
}