-
Notifications
You must be signed in to change notification settings - Fork 785
/
create_devpod.go
1272 lines (1150 loc) · 39.2 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package create
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/auth"
"github.com/jenkins-x/jx/v2/pkg/tekton"
"github.com/jenkins-x/jx/v2/pkg/cmd/step/git/credentials"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/rsh"
"github.com/jenkins-x/jx/v2/pkg/cmd/sync"
"github.com/jenkins-x/jx/v2/pkg/kube/naming"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/gits"
v12 "k8s.io/client-go/kubernetes/typed/core/v1"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/kube/serviceaccount"
"github.com/jenkins-x/jx/v2/pkg/kube/services"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
)
const (
optionRequestCPU = "request-cpu"
optionRequestMemory = "request-memory"
devPodGoPath = "/workspace"
devPodContainerName = "devpod"
)
var (
createDevPodLong = templates.LongDesc(`
Creates a new DevPod
For more documentation see: [https://jenkins-x.io/developing/devpods/](https://jenkins-x.io/developing/devpods/)
`)
createDevPodExample = templates.Examples(`
# creates a new DevPod asking the user for the label to use
jx create devpod
# creates a new Maven DevPod
jx create devpod -l maven
`)
)
// CreateDevPodResults the results of running the command
type CreateDevPodResults struct {
TheaServiceURL string
ExposePortURLs []string
PodName string
}
// CreateDevPodOptions the options for the create spring command
type CreateDevPodOptions struct {
options.CreateOptions
opts.CommonDevPodOptions
Label string
Suffix string
WorkingDir string
RequestCpu string
RequestMemory string
Dir string
Reuse bool
Sync bool
Ports []int
AutoExpose bool
Persist bool
ImportURL string
Import bool
TempDir bool
Theia bool
ShellCmd string
DockerRegistry string
TillerNamespace string
ServiceAccount string
PullSecrets string
GitCredentials credentials.StepGitCredentialsOptions
Results CreateDevPodResults
}
// devPodLabels split gitInfo labels
type devPodLabels struct {
gitSchemeLabelKey string
gitSchemeLabelValue string
gitHostLabelKey string
gitHostLabelValue string
gitOrgLabelKey string
gitOrgLabelValue string
gitRepoLabelKey string
gitRepoLabelValue string
gitLabels map[string]string
}
// populateFromGitInfo populate this struct with the git repository elements (scheme, host, org and repo)
func (d *devPodLabels) populateFromGitInfo(gitInfo *gits.GitRepository) error {
d.gitSchemeLabelKey = fmt.Sprintf("%s-scheme", kube.LabelDevPodGitPrefix)
d.gitSchemeLabelValue = naming.ToValidNameWithDotsTruncated(gitInfo.Scheme, 63)
d.gitHostLabelKey = fmt.Sprintf("%s-host", kube.LabelDevPodGitPrefix)
d.gitHostLabelValue = naming.ToValidNameWithDotsTruncated(gitInfo.Host, 63)
d.gitOrgLabelKey = fmt.Sprintf("%s-organisation", kube.LabelDevPodGitPrefix)
d.gitOrgLabelValue = naming.ToValidNameWithDots(gitInfo.Organisation)
d.gitRepoLabelKey = fmt.Sprintf("%s-name", kube.LabelDevPodGitPrefix)
d.gitRepoLabelValue = naming.ToValidNameWithDots(gitInfo.Name)
if len(d.gitOrgLabelValue) > 63 {
return errors.New("git organization label or value exceed length")
}
if len(d.gitRepoLabelValue) > 63 {
return errors.New("git repo label or value exceed length")
}
d.gitLabels = make(map[string]string)
if len(d.gitSchemeLabelValue) > 0 {
d.gitLabels[d.gitSchemeLabelKey] = d.gitSchemeLabelValue
}
if len(d.gitHostLabelValue) > 0 {
d.gitLabels[d.gitHostLabelKey] = d.gitHostLabelValue
}
if len(d.gitOrgLabelValue) > 0 {
d.gitLabels[d.gitOrgLabelKey] = d.gitOrgLabelValue
}
if len(d.gitRepoLabelValue) > 0 {
d.gitLabels[d.gitRepoLabelKey] = d.gitRepoLabelValue
}
return nil
}
// getLabels return a map with the labels
func (d *devPodLabels) getLabels() map[string]string {
return d.gitLabels
}
// mergeLabels return a map with the labels
func (d *devPodLabels) mergeLabels(labels map[string]string) map[string]string {
l := d.getLabels()
for k, v := range labels {
l[k] = v
}
return l
}
// NewCmdCreateDevPod creates a command object for the "create" command
func NewCmdCreateDevPod(commonOpts *opts.CommonOptions) *cobra.Command {
options := &CreateDevPodOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
GitCredentials: credentials.StepGitCredentialsOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
}
cmd := &cobra.Command{
Use: "devpod",
Short: "Creates a DevPod for running builds and tests inside the cluster",
Aliases: []string{"dpod", "buildpod"},
Long: createDevPodLong,
Example: createDevPodExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Label, opts.OptionLabel, "l", "", "The label of the pod template to use")
cmd.Flags().StringVarP(&options.Suffix, "suffix", "s", "", "The suffix to append the pod name")
cmd.Flags().StringVarP(&options.WorkingDir, "working-dir", "w", "", "The working directory of the DevPod")
cmd.Flags().StringVarP(&options.RequestCpu, optionRequestCPU, "c", "1", "The request CPU of the DevPod")
cmd.Flags().StringVarP(&options.RequestMemory, optionRequestMemory, "m", "512Mi", "The request Memory of the DevPod")
cmd.Flags().BoolVarP(&options.Reuse, "reuse", "", true, "Reuse an existing DevPod if a suitable one exists. The DevPod will be selected based on the label (or current working directory)")
cmd.Flags().BoolVarP(&options.Sync, "sync", "", false, "Also synchronise the local file system into the DevPod")
cmd.Flags().IntSliceVarP(&options.Ports, "ports", "p", []int{}, "Container ports exposed by the DevPod")
cmd.Flags().BoolVarP(&options.AutoExpose, "auto-expose", "", false, "Automatically expose useful ports via ingresses such as the ide port, debug port, as well as any ports specified using --ports")
cmd.Flags().BoolVarP(&options.Persist, "persist", "", false, "Persist changes made to the DevPod. Cannot be used with --sync")
cmd.Flags().StringVarP(&options.ImportURL, "import-url", "u", "", "Clone a Git repository into the DevPod. Cannot be used with --sync")
cmd.Flags().BoolVarP(&options.Import, "import", "", true, "Detect if there is a Git repository in the current directory and attempt to clone it into the DevPod. Ignored if used with --sync")
cmd.Flags().BoolVarP(&options.TempDir, "temp-dir", "", false, "If enabled and --import-url is supplied then create a temporary directory to clone the source to detect what kind of DevPod to create")
cmd.Flags().BoolVarP(&options.Theia, "theia", "", false, "If enabled use Eclipse Theia as the web based IDE")
cmd.Flags().StringVarP(&options.ShellCmd, "shell", "", "", "The name of the shell to invoke in the DevPod. If nothing is specified it will use 'bash'")
cmd.Flags().StringVarP(&options.DockerRegistry, "docker-registry", "", "", "The Docker registry to use within the DevPod. If not specified, default to the built-in registry or $DOCKER_REGISTRY")
cmd.Flags().StringVarP(&options.TillerNamespace, "tiller-namespace", "", "", "The optional tiller namespace to use within the DevPod.")
cmd.Flags().StringVarP(&options.ServiceAccount, "service-account", "", "", "The ServiceAccount name used for the DevPod")
cmd.Flags().StringVarP(&options.PullSecrets, optionPullSecrets, "", "", "A list of Kubernetes secret names that will be attached to the service account (e.g. foo, bar, baz)")
options.AddCommonDevPodFlags(cmd)
return cmd
}
// Run implements this command
func (o *CreateDevPodOptions) Run() error {
if o.Persist && o.Sync {
return errors.New("cannot specify --persist and --sync")
}
if o.ImportURL != "" && o.Sync {
return errors.New("cannot specify --import-url && --sync")
}
client, curNs, err := o.KubeClientAndNamespace()
if err != nil {
return errors.Wrap(err, "creating kubernetes client")
}
ns, _, err := kube.GetDevNamespace(client, curNs)
if err != nil {
return errors.Wrap(err, "getting the dev namespce")
}
if o.AutoExpose && !o.isAutoExposeSecure(client, ns) {
return errors.New("skipping creating the DevPod because auto-expose is insecure")
}
userName, err := o.GetUsername(o.Username)
if err != nil {
return errors.Wrap(err, "getting the current user")
}
dir := o.Dir
importURL := o.ImportURL
if o.TempDir {
if dir != "" {
return fmt.Errorf("you cannot specify --dir and --temp-dir")
}
dir, err = ioutil.TempDir("", username+"-")
if err != nil {
return errors.Wrapf(err, "creating a temp dir")
}
defer os.RemoveAll(dir)
o.Dir = dir
}
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return errors.Wrap(err, "getting the working directory")
}
}
if importURL == "" {
gitInfo, err := o.FindGitInfo(dir)
if err != nil {
log.Logger().Warnf("could not find git URL in dir %s due to: %s", dir, err.Error())
} else {
gitKind, _ := o.GitServerKind(gitInfo)
importURL = gits.HttpCloneURL(gitInfo, gitKind)
}
}
label := o.Label
workingDir := o.WorkingDir
if workingDir == "" {
workingDir = "/workspace"
if o.Sync {
// lets check for GOPATH stuff if we are in --sync mode so that we sync into gopath
gopath := os.Getenv("GOPATH")
if gopath != "" {
rel, err := filepath.Rel(gopath, dir)
if err == nil && rel != "" {
workingDir = filepath.Join(devPodGoPath, rel)
}
}
}
}
setupWorkspaceCommand := "jx step create devpod workspace"
create := true
var pod *corev1.Pod
var editEnv *v1.Environment
name := ""
podResources := client.CoreV1().Pods(ns)
var exposeServicePorts []int
var gitLabels devPodLabels
if importURL != "" && o.Reuse {
gitInfo, err := gits.ParseGitURL(importURL)
if err != nil {
log.Logger().Warnf("could not parse the git URL %s: %s", importURL, err.Error())
} else {
err = gitLabels.populateFromGitInfo(gitInfo)
if err != nil {
return errors.Wrapf(err, "populating labels from git info %s", err.Error())
}
// lets query to see if there is a DevPod already for this URL
matchLabels := map[string]string{
kube.LabelDevPodUsername: userName,
}
matchLabels = gitLabels.mergeLabels(matchLabels)
pod, err = o.findDevPodBySelector(podResources, matchLabels, dir)
if err != nil {
return errors.Wrapf(err, "finding DevPod by selector: %v", matchLabels)
}
if pod != nil {
name = pod.Name
create = false
}
}
}
idePort := int32(3000)
ideContainerName := "ide"
if create {
if o.TempDir && importURL != "" {
o.NotifyProgressf(opts.LogInfo, "cloning git URL: %s\n", importURL)
err = o.Git().ShallowClone(dir, importURL, "master", "")
if err != nil {
return errors.Wrapf(err, "git cloning shallow: %s into dir %s", importURL, dir)
}
}
podTemplates, err := kube.LoadPodTemplates(client, ns)
if err != nil {
return errors.Wrapf(err, "loading the pod templates from namesapce %q", ns)
}
podTemplateKeys := map[string]string{}
for k := range podTemplates {
podTemplateKeys[k] = k
}
labels := util.SortedMapKeys(podTemplateKeys)
if label == "" {
label, err = o.guessDevPodLabel(dir, labels)
if err != nil {
return errors.Wrap(err, "guessing the DevPod label")
}
if label == "javascript" {
label = "nodejs"
}
}
if label == "" {
label, err = util.PickName(labels, "Pick which kind of DevPod you wish to create: ", "", o.GetIOFileHandles())
if err != nil {
return errors.Wrap(err, "picking the kind of DevPod to create")
}
}
pod = podTemplates[label]
if pod == nil {
return util.InvalidOption(opts.OptionLabel, label, labels)
}
editEnv, err = o.getOrCreateEditEnvironment()
if err != nil {
return errors.Wrap(err, "getting or creating the edit environment")
}
// If the user passed in Image Pull Secrets, patch them in to the edit env's default service account
if o.PullSecrets != "" {
imagePullSecrets := strings.Fields(o.PullSecrets)
err = serviceaccount.PatchImagePullSecrets(client, editEnv.Spec.Namespace, "default", imagePullSecrets)
if err != nil {
return fmt.Errorf("failed to add pull secrets %s to service account default in namespace %s: %v", imagePullSecrets, editEnv.Spec.Namespace, err)
}
}
if pod.Labels == nil {
pod.Labels = map[string]string{}
}
if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
name = naming.ToValidName(userName + "-" + label)
if o.Suffix != "" {
name += "-" + o.Suffix
}
names, err := kube.GetPodNames(client, ns, "")
if err != nil {
return errors.Wrap(err, "getting pod names")
}
name = uniquePodName(names, name)
o.Results.PodName = name
pod.Name = name
pod.Labels[kube.LabelPodTemplate] = label
pod.Labels[kube.LabelDevPodName] = name
pod.Labels[kube.LabelDevPodUsername] = userName
pod.Labels = gitLabels.mergeLabels(pod.Labels)
if len(pod.Spec.Containers) == 0 {
return fmt.Errorf("no containers specified for label %s with pod: %#v", label, pod)
}
container1 := &pod.Spec.Containers[0]
// lets use a canonical name for the devpod container
container1.Name = devPodContainerName
workspaceVolumeName := "workspace-volume"
// lets remove the default workspace volume as we don't need it
for i, v := range pod.Spec.Volumes {
if v.Name == workspaceVolumeName {
pod.Spec.Volumes = append(pod.Spec.Volumes[:i], pod.Spec.Volumes[i+1:]...)
break
}
}
for ci, c := range pod.Spec.Containers {
for i, v := range c.VolumeMounts {
if v.Name == workspaceVolumeName {
pod.Spec.Containers[ci].VolumeMounts = append(c.VolumeMounts[:i], c.VolumeMounts[i+1:]...)
break
}
}
}
// Trying to reuse workspace-volume as a name seems to prevent us modifying the volumes!
workspaceVolumeName = "ws-volume"
var workspaceVolume corev1.Volume
workspaceClaimName := fmt.Sprintf("%s-pvc", pod.Name)
workspaceVolumeMount := corev1.VolumeMount{
Name: workspaceVolumeName,
MountPath: "/workspace",
}
if o.Persist {
workspaceVolume = corev1.Volume{
Name: workspaceVolumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: workspaceClaimName,
},
},
}
} else {
workspaceVolume = corev1.Volume{
Name: workspaceVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}
}
if pod.Spec.ServiceAccountName == "" {
sa := o.ServiceAccount
if sa == "" {
prow, err := o.IsProw()
if err != nil {
return errors.Wrap(err, "checking if prow is active")
}
settings, err := o.TeamSettings()
if err != nil {
return errors.Wrap(err, "getting the team settings")
}
sa = "jenkins"
if settings.IsJenkinsXPipelines() {
sa = tekton.DefaultPipelineSA
} else if prow {
sa = "knative-build-bot"
}
}
pod.Spec.ServiceAccountName = sa
}
if !o.Sync {
pod.Spec.Volumes = append(pod.Spec.Volumes, workspaceVolume)
container1.VolumeMounts = append(container1.VolumeMounts, workspaceVolumeMount)
cpuLimit, _ := resource.ParseQuantity("400m")
cpuRequest, _ := resource.ParseQuantity("200m")
memoryLimit, _ := resource.ParseQuantity("1Gi")
memoryRequest, _ := resource.ParseQuantity("128Mi")
// disable input for replacing the version stream git repo
batch := o.BatchMode
o.BatchMode = true
resolver, err := o.GetVersionResolver()
if err != nil {
return errors.Wrap(err, "getting the version stream resolver")
}
o.BatchMode = batch
// web IDEs won't work in --sync mode as we can't share a volume
if o.Theia {
image, err := resolver.ResolveDockerImage("theiaide/theia-full")
if err != nil {
return errors.Wrap(err, "resolving the container image for Theia IDE")
}
editorContainer := corev1.Container{
Name: ideContainerName,
Image: image,
Ports: []corev1.ContainerPort{
{
ContainerPort: idePort,
},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": cpuLimit,
"memory": memoryLimit,
},
Requests: corev1.ResourceList{
"cpu": cpuRequest,
"memory": memoryRequest,
},
},
VolumeMounts: []corev1.VolumeMount{
workspaceVolumeMount,
},
LivenessProbe: &corev1.Probe{
InitialDelaySeconds: 60,
PeriodSeconds: 10,
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Port: intstr.FromInt(int(idePort)),
},
},
},
SecurityContext: &corev1.SecurityContext{
RunAsUser: func(i int64) *int64 { return &i }(0),
},
Command: []string{"yarn", "theia", "start", "/workspace", "--hostname=0.0.0.0"},
}
pod.Spec.Containers = append(pod.Spec.Containers, editorContainer)
} else {
setupWorkspaceCommand += " --vscode"
idePort = 8443
image, err := resolver.ResolveDockerImage("codercom/code-server")
if err != nil {
return errors.Wrap(err, "resolving the container image for VS Code")
}
editorContainer := corev1.Container{
Name: ideContainerName,
Image: image,
Ports: []corev1.ContainerPort{
{
ContainerPort: idePort,
},
},
Env: []corev1.EnvVar{
{
Name: "HOME",
Value: workingDir + "/idehome",
},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": cpuLimit,
"memory": memoryLimit,
},
Requests: corev1.ResourceList{
"cpu": cpuRequest,
"memory": memoryRequest,
},
},
VolumeMounts: []corev1.VolumeMount{
workspaceVolumeMount,
},
LivenessProbe: &corev1.Probe{
InitialDelaySeconds: 60,
PeriodSeconds: 10,
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Port: intstr.FromInt(int(idePort)),
},
},
},
SecurityContext: &corev1.SecurityContext{
RunAsUser: func(i int64) *int64 { return &i }(0),
},
Args: []string{"--allow-http", "--no-auth", workingDir},
}
pod.Spec.Containers = append(pod.Spec.Containers, editorContainer)
}
}
if o.RequestCpu != "" {
q, err := resource.ParseQuantity(o.RequestCpu)
if err != nil {
return util.InvalidOptionError(optionRequestCPU, o.RequestCpu, err)
}
container1.Resources.Requests[corev1.ResourceCPU] = q
}
if o.RequestMemory != "" {
q, err := resource.ParseQuantity(o.RequestMemory)
if err != nil {
return util.InvalidOptionError(optionRequestMemory, o.RequestMemory, err)
}
container1.Resources.Requests[corev1.ResourceMemory] = q
}
//Set the devpods gopath properly
container1.Env = append(container1.Env, corev1.EnvVar{
Name: "GOPATH",
Value: devPodGoPath,
})
pod.Annotations[kube.AnnotationWorkingDir] = workingDir
if importURL != "" {
gitURLs := pod.Annotations[kube.AnnotationGitURLs]
if gitURLs == "" {
gitURLs = importURL
} else {
const separator = "\n"
slice := strings.Split(gitURLs, separator)
if util.StringArrayIndex(slice, importURL) < 0 {
slice = append(slice, importURL)
gitURLs = strings.Join(slice, separator)
}
}
pod.Annotations[kube.AnnotationGitURLs] = gitURLs
}
if o.Sync {
pod.Annotations[kube.AnnotationLocalDir] = dir
}
container1.Env = append(container1.Env, corev1.EnvVar{
Name: "WORK_DIR",
Value: workingDir,
})
container1.Stdin = true
// If a Docker registry override was passed in, set it as an env var.
if o.DockerRegistry != "" {
container1.Env = append(container1.Env, corev1.EnvVar{
Name: "DOCKER_REGISTRY",
Value: o.DockerRegistry,
})
}
// If a tiller namespace was passed in, set it as an env var.
if o.TillerNamespace != "" {
container1.Env = append(container1.Env, corev1.EnvVar{
Name: "TILLER_NAMESPACE",
Value: o.TillerNamespace,
})
}
if editEnv != nil {
container1.Env = append(container1.Env, corev1.EnvVar{
Name: "SKAFFOLD_DEPLOY_NAMESPACE",
Value: editEnv.Spec.Namespace,
})
}
// Assign the container the ports provided as input
for _, port := range o.Ports {
cp := corev1.ContainerPort{
Name: fmt.Sprintf("port-%d", port),
ContainerPort: int32(port),
}
container1.Ports = append(container1.Ports, cp)
}
// Assign the container the ports provided automatically
exposeServicePorts = o.Ports
if portsStr, ok := pod.Annotations["jenkins-x.io/devpodPorts"]; ok {
ports := strings.Split(portsStr, ", ")
for _, portStr := range ports {
port, _ := strconv.ParseInt(portStr, 10, 32)
exposeServicePorts = append(exposeServicePorts, int(port))
cp := corev1.ContainerPort{
Name: fmt.Sprintf("port-%d", port),
ContainerPort: int32(port),
}
container1.Ports = append(container1.Ports, cp)
}
}
if o.Reuse {
matchLabels := map[string]string{
kube.LabelPodTemplate: label,
kube.LabelDevPodUsername: userName,
}
foundPod, err := o.findDevPodBySelector(podResources, matchLabels, dir)
if err != nil {
return err
}
if foundPod != nil {
pod = foundPod
name = pod.Name
create = false
newLabels := gitLabels.mergeLabels(pod.Labels)
if !reflect.DeepEqual(pod.Labels, newLabels) {
pod.Labels = newLabels
_, err = podResources.Update(pod)
if err != nil {
log.Logger().Warnf("Failed to update git labels of pod %s: %s", pod.Name, err.Error())
}
}
}
}
}
ideServiceName := name + "-ide"
if create {
o.NotifyProgressf(opts.LogInfo, "Creating a DevPod of label: %s\n", util.ColorInfo(label))
_, err = podResources.Create(pod)
if err != nil {
return fmt.Errorf("failed to create pod %s\npod: %#v", err, pod)
}
if o.AutoExpose {
err := o.ensureEditEnvironmentHasExposeController(editEnv)
if err != nil {
return errors.Wrap(err, "ensuring that edit environment has expose-controller")
}
}
o.NotifyProgressf(opts.LogInfo, "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 errors.Wrapf(err, "waiting for POD %q to be ready", name)
}
// Get the pod UID
pod, err = client.CoreV1().Pods(ns).Get(name, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "getting the POD %q", name)
}
// Create PVC if needed
if o.Persist {
storageRequest, _ := resource.ParseQuantity("2Gi")
workspaceClaimName := fmt.Sprintf("%s-pvc", pod.Name)
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: workspaceClaimName,
OwnerReferences: []metav1.OwnerReference{
kube.PodOwnerRef(pod),
},
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
corev1.ReadWriteOnce,
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"storage": storageRequest,
},
},
},
}
_, err = client.CoreV1().PersistentVolumeClaims(curNs).Create(&pvc)
if err != nil {
return errors.Wrapf(err, "creating the persistent volume claim %q in namespace %q", pvc.Name, curNs)
}
}
// Create services
var addedServices []string
svcAnotations := map[string]string{}
if o.AutoExpose {
svcAnotations = map[string]string{
"fabric8.io/expose": "true",
}
}
// Create a service for every port we expose
if len(exposeServicePorts) > 0 {
for _, port := range exposeServicePorts {
portName := fmt.Sprintf("%s-%d", pod.Name, port)
servicePorts := []corev1.ServicePort{
{
Name: portName,
Port: int32(80),
TargetPort: intstr.FromInt(port),
},
}
service := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: svcAnotations,
Name: fmt.Sprintf("%s-port-%d", pod.Name, port),
OwnerReferences: []metav1.OwnerReference{
kube.PodOwnerRef(pod),
},
},
Spec: corev1.ServiceSpec{
Ports: servicePorts,
Selector: map[string]string{
"jenkins.io/devpod": pod.Name,
},
},
}
_, err = client.CoreV1().Services(curNs).Create(&service)
if err != nil {
return errors.Wrapf(err, "creating service %q in namespace %q", service.Name, curNs)
}
addedServices = append(addedServices, service.Name)
}
}
if !o.Sync {
// Create a service for the IDE
ideService := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: svcAnotations,
Name: ideServiceName,
OwnerReferences: []metav1.OwnerReference{
kube.PodOwnerRef(pod),
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: ideServiceName,
Port: 80,
TargetPort: intstr.FromInt(int(idePort)),
},
},
Selector: map[string]string{
"jenkins.io/devpod": pod.Name,
},
},
}
_, err = client.CoreV1().Services(curNs).Create(&ideService)
if err != nil {
return errors.Wrapf(err, "creating the service %q for IDE in namespace %q", ideService.Name, curNs)
}
addedServices = append(addedServices, ideServiceName)
}
if len(addedServices) > 0 && o.AutoExpose {
err = o.updateExposeController(client, ns, ns, addedServices...)
if err != nil {
return errors.Wrapf(err, "updating the expose controller in namespace %s", ns)
}
}
}
o.NotifyProgressf(opts.LogInfo, "Pod %s is now ready!\n", util.ColorInfo(pod.Name))
log.Logger().Infof("You can open other shells into this DevPod via %s", util.ColorInfo("jx create devpod"))
if !o.Sync {
if o.AutoExpose {
ideServiceURL, err := services.FindServiceURL(client, curNs, ideServiceName)
if err != nil {
return errors.Wrapf(err, "finding the URL for service %q", ideServiceName)
}
if ideServiceURL != "" {
pod, err = client.CoreV1().Pods(curNs).Get(name, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "getting the POD %q in namespace %q", name, curNs)
}
pod.Annotations["jenkins-x.io/devpod_IDE_URL"] = ideServiceURL
pod, err = client.CoreV1().Pods(curNs).Update(pod)
if err != nil {
return errors.Wrapf(err, "updating the POD %q in namespace %q", name, curNs)
}
log.Logger().Infof("\nYou can edit your app using the Web IDE at: %s", util.ColorInfo(ideServiceURL))
o.Results.TheaServiceURL = ideServiceURL
} else {
o.NotifyProgressf(opts.LogWarning, "Could not find service with name %s in namespace %s\n", ideServiceName, curNs)
}
} else {
exposeCmd := fmt.Sprintf("kubectl port-forward svc/%s 8080:80", ideServiceName)
log.Logger().Info("\nYou can access the Web IDE to edit your app with command:")
log.Logger().Infof("* %s", util.ColorInfo(exposeCmd))
}
}
if o.AutoExpose {
exposePortServices, err := services.GetServiceNames(client, curNs, fmt.Sprintf("%s-port-", pod.Name))
if err != nil {
return errors.Wrapf(err, "getting the exposed services for POD %q in namespace %q", pod.Name, curNs)
}
var exposePortURLs []string
for _, svcName := range exposePortServices {
u, err := services.GetServiceURLFromName(client, svcName, curNs)
if err != nil {
return errors.Wrapf(err, "getting the service URL from service name %q in namespace %q", svcName, curNs)
}
exposePortURLs = append(exposePortURLs, u)
}
if len(exposePortURLs) > 0 {
log.Logger().Infof("\nYou can access the DevPod from your browser via the following URLs:")
for _, u := range exposePortURLs {
log.Logger().Infof("* %s", util.ColorInfo(u))
}
log.Logger().Info("")
o.Results.ExposePortURLs = exposePortURLs
}
} else {
exposePortServices, err := services.GetServiceNames(client, curNs, fmt.Sprintf("%s-port-", pod.Name))
if err != nil {
return errors.Wrapf(err, "getting the exposed services for POD %q in namesapce %q", pod.Name, curNs)
}
localPort := 8081
log.Logger().Info("\nYou can access the DevPod locally with the following commands:")
for _, svcName := range exposePortServices {
exposeCmd := fmt.Sprintf("kubectl port-forward svc/%s %d:80", svcName, localPort)
log.Logger().Infof("* %s", util.ColorInfo(exposeCmd))
localPort++
}
log.Logger().Info("")
}
if o.Sync {
syncOptions := &sync.SyncOptions{
CommonOptions: o.CommonOptions,
Namespace: ns,
Pod: pod.Name,
Daemon: true,
Dir: dir,
}
err = syncOptions.CreateKsync(client, ns, pod.Name, dir, workingDir, userName)
if err != nil {
return errors.Wrap(err, "creating ksync")
}
}
var rshExec []string
if create {
// Let install bash-completion to make life better
o.NotifyProgressf(opts.LogInfo, "Attempting to install Bash Completion into DevPod\n")
rshExec = append(rshExec,
"if which yum &> /dev/null; then yum install -q -y bash-completion bash-completion-extra; fi",
"if which apt-get &> /dev/null; then apt-get install -qq bash-completion; fi",
"mkdir -p ~/.jx", "jx completion bash > ~/.jx/bash", "echo \"source ~/.jx/bash\" >> ~/.bashrc",
)
// Only add git secrets to the Theia container when sync flag is missing (otherwise Theia container won't exist)
if !o.Sync {
gha, err := o.IsGitHubAppMode()
if err != nil {
return err
}
// Add Git Secrets to Theia container
var authConfigSvc auth.ConfigService
if gha {
authConfigSvc, err = o.GitAuthConfigServiceGitHubAppMode("")
if err != nil {
return errors.Wrap(err, "when creating auth config service using GitAuthConfigServiceGitHubAppMode")
}
} else {
authConfigSvc, err = o.GitAuthConfigService()
if err != nil {
return errors.Wrap(err, "when creating auth config service using GitAuthConfigService")
}
}
gitCredentials, err := o.GitCredentials.CreateGitCredentialsFromAuthService(authConfigSvc, gha)
if err != nil {
return errors.Wrap(err, "creating git credentials")
}
data, err := o.GitCredentials.GitCredentialsFileData(gitCredentials)
if err != nil {
return errors.Wrap(err, "creating git credentials")
}
theiaRshExec := []string{
fmt.Sprintf("echo \"%s\" >> ~/.git-credentials", string(data)),
"git config --global credential.helper store",
}
// Configure remote username and email for git
username, _ := o.Git().Username("")
email, _ := o.Git().Email("")
if username != "" {
theiaRshExec = append(theiaRshExec, fmt.Sprintf("git config --global user.name \"%s\"", username))
}
if email != "" {
theiaRshExec = append(theiaRshExec, fmt.Sprintf("git config --global user.email \"%s\"", email))
}
// remove annoying warning
theiaRshExec = append(theiaRshExec, " git config --global push.default simple")