forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
174 lines (159 loc) · 4.07 KB
/
environment.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
package cmd
import (
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
"k8s.io/client-go/kubernetes"
)
type EnvironmentOptions struct {
CommonOptions
}
const ()
var (
environment_long = templates.LongDesc(`
Displays or changes the current environment.`)
environment_example = templates.Examples(`
# view the current environment
jx env -b
# pick which environment to switch to
jx env
# Change the current environment to 'staging'
jx env staging
`)
)
func NewCmdEnvironment(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &EnvironmentOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "environment",
Aliases: []string{"env"},
Short: "View or change the current environment in the current kubernetes cluster",
Long: environment_long,
Example: environment_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
options.addCommonFlags(cmd)
return cmd
}
func (o *EnvironmentOptions) Run() error {
f := o.Factory
kubeClient, currentNs, err := f.CreateClient()
if err != nil {
return err
}
jxClient, _, err := f.CreateJXClient()
if err != nil {
return err
}
apisClient, err := f.CreateApiExtensionsClient()
if err != nil {
return err
}
kube.RegisterEnvironmentCRD(apisClient)
devNs, currentEnv, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return err
}
envNames, err := kube.GetEnvironmentNames(jxClient, devNs)
if err != nil {
return err
}
config, po, err := kube.LoadConfig()
if err != nil {
return err
}
env := ""
args := o.Args
if len(args) > 0 {
env = args[0]
}
if env == "" && !o.BatchMode {
pick, err := kube.PickEnvironment(envNames, currentEnv)
if err != nil {
return err
}
env = pick
}
info := util.ColorInfo
if env != "" && env != currentEnv {
envResource, err := jxClient.JenkinsV1().Environments(devNs).Get(env, meta_v1.GetOptions{})
if err != nil {
return util.InvalidArg(env, envNames)
}
ns := envResource.Spec.Namespace
if ns == "" {
return fmt.Errorf("Cannot change to environment %s as it has no namespace!", env)
}
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 environment '%s' in tean '%s' on server '%s'.\n", info(env), info(devNs), info(kube.Server(config, ctx)))
} else {
ns := kube.CurrentNamespace(config)
server := kube.CurrentServer(config)
if env == "" {
env = currentEnv
}
if env == "" {
fmt.Fprintf(o.Out, "Using namespace '%s' from context named '%s' on server '%s'.\n", info(ns), info(config.CurrentContext), info(server))
} else {
fmt.Fprintf(o.Out, "Using environment '%s' in team '%s' on server '%s'.\n", info(env), info(devNs), info(server))
}
}
return nil
}
func (o *EnvironmentOptions) PickNamespace(client *kubernetes.Clientset, defaultNamespace string) (string, error) {
list, err := client.CoreV1().Namespaces().List(meta_v1.ListOptions{})
if err != nil {
return "", fmt.Errorf("Failed to load current namespaces %s", err)
}
names := []string{}
for _, n := range list.Items {
names = append(names, n.Name)
}
var qs = []*survey.Question{
{
Name: "namespace",
Prompt: &survey.Select{
Message: "Change namespace: ",
Options: names,
Default: defaultNamespace,
},
},
}
answers := struct {
Namespace string
}{}
err = survey.Ask(qs, &answers)
if err != nil {
return "", err
}
return answers.Namespace, nil
}