forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_devpod.go
168 lines (146 loc) · 3.91 KB
/
create_devpod.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
package cmd
import (
"fmt"
"io"
"os/user"
"strconv"
"time"
"github.com/ghodss/yaml"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
optionLabel = "label"
)
// CreateDevPodOptions the options for the create spring command
type CreateDevPodOptions struct {
CreateOptions
Label string
Suffix string
}
// NewCmdCreateDevPod creates a command object for the "create" command
func NewCmdCreateDevPod(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &CreateDevPodOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "devpod",
Short: "Creates a Developer Pod for running builds and tests inside the cluster",
Aliases: []string{"dpod", "buildpod"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Label, optionLabel, "l", "", "The label of the pod template to use")
cmd.Flags().StringVarP(&options.Suffix, "suffix", "s", "", "The suffix to append the pod name")
options.addCommonFlags(cmd)
return cmd
}
// Run implements this command
func (o *CreateDevPodOptions) Run() error {
client, curNs, err := o.KubeClient()
if err != nil {
return err
}
ns, _, err := kube.GetDevNamespace(client, curNs)
if err != nil {
return err
}
cm, err := client.CoreV1().ConfigMaps(ns).Get(kube.ConfigMapJenkinsPodTemplates, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Failed to find ConfigMap %s in namespace %s: %s", kube.ConfigMapJenkinsPodTemplates, ns, err)
}
podTemplates := cm.Data
labels := util.SortedMapKeys(podTemplates)
label := o.Label
yml := ""
if label != "" {
yml = podTemplates[label]
if yml == "" {
return util.InvalidOption(optionLabel, label, labels)
}
}
if label == "" {
label, err = util.PickName(labels, "Pick which kind of dev pod you wish to create: ")
if err != nil {
return err
}
yml = podTemplates[label]
if yml == "" {
return fmt.Errorf("Could not find YAML for pod template label %s", label)
}
}
o.Printf("Creating a dev pod of label: %s\n", label)
pod := &corev1.Pod{}
err = yaml.Unmarshal([]byte(yml), &pod)
if err != nil {
return fmt.Errorf("Failed to parse Pod Template YAML: %s\n%s", err, yml)
}
u, err := user.Current()
if err != nil {
return err
}
userName := u.Username
name := kube.ToValidName(userName + "-" + label)
if o.Suffix != "" {
name += "-" + o.Suffix
}
names, err := kube.GetPodNames(client, ns, "")
if err != nil {
return err
}
name = uniquePodName(names, name)
pod.Name = name
pod.Labels[kube.LabelPodTemplate] = label
pod.Labels[kube.LabelDevPodName] = name
pod.Labels[kube.LabelDevPodUsername] = userName
_, err = client.CoreV1().Pods(ns).Create(pod)
if err != nil {
if o.Verbose {
return fmt.Errorf("Failed to create pod %s\nYAML: %s", err, yml)
} else {
return fmt.Errorf("Failed to create pod %s", err)
}
}
o.Printf("Created pod %s - waiting for it to be ready...\n", util.ColorInfo(name))
err = kube.WaitForPodNameToBeReady(client, ns, name, time.Hour)
if err != nil {
return err
}
o.Printf("Pod %s is now ready!\n", util.ColorInfo(name))
o.Printf("You can open other shells into this DevPod via %s\n", util.ColorInfo("jx rsh -d"))
options := &RshOptions{
CommonOptions: o.CommonOptions,
Namespace: ns,
Executable: "bash",
Pod: name,
}
options.Args = []string{}
return options.Run()
}
func uniquePodName(names []string, prefix string) string {
count := 1
for {
name := prefix
if count > 1 {
name += strconv.Itoa(count)
}
if util.StringArrayIndex(names, name) < 0 {
return name
}
count++
}
}