Skip to content

Commit ac268f1

Browse files
committed
Implementation of #250, #242, #249, #136
1 parent 63ce92a commit ac268f1

8 files changed

Lines changed: 72 additions & 23 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
# History Plugin for VS Code
1616
.history/
1717

18-
devspace
18+
/devspace
1919
debug
2020
.DS_STORE

cmd/up.go

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ type UpCmdFlags struct {
5757
open string
5858
initRegistries bool
5959
build bool
60-
shell string
6160
sync bool
6261
deploy bool
6362
portforwarding bool
6463
noSleep bool
64+
verboseSync bool
65+
container string
6566
}
6667

6768
//UpFlagsDefault are the default flags for UpCmdFlags
@@ -74,6 +75,8 @@ var UpFlagsDefault = &UpCmdFlags{
7475
deploy: false,
7576
portforwarding: true,
7677
noSleep: false,
78+
verboseSync: false,
79+
container: "",
7780
}
7881

7982
const clusterRoleBindingName = "devspace-users"
@@ -97,16 +100,16 @@ Starts and connects your DevSpace:
97100
4. Starts the sync client
98101
5. Enters the container shell
99102
#######################################################`,
100-
Args: cobra.NoArgs,
101-
Run: cmd.Run,
103+
Run: cmd.Run,
102104
}
103105
rootCmd.AddCommand(cobraCmd)
104106

105107
cobraCmd.Flags().BoolVar(&cmd.flags.tiller, "tiller", cmd.flags.tiller, "Install/upgrade tiller")
106108
cobraCmd.Flags().BoolVar(&cmd.flags.initRegistries, "init-registries", cmd.flags.initRegistries, "Initialize registries (and install internal one)")
107109
cobraCmd.Flags().BoolVarP(&cmd.flags.build, "build", "b", cmd.flags.build, "Build image if Dockerfile has been modified")
108-
cobraCmd.Flags().StringVarP(&cmd.flags.shell, "shell", "s", "", "Shell command (default: bash, fallback: sh)")
110+
cobraCmd.Flags().StringVarP(&cmd.flags.container, "container", "c", cmd.flags.container, "Container name where to open the shell")
109111
cobraCmd.Flags().BoolVar(&cmd.flags.sync, "sync", cmd.flags.sync, "Enable code synchronization")
112+
cobraCmd.Flags().BoolVar(&cmd.flags.verboseSync, "verbose-sync", cmd.flags.verboseSync, "When enabled the sync will log every file change")
110113
cobraCmd.Flags().BoolVar(&cmd.flags.portforwarding, "portforwarding", cmd.flags.portforwarding, "Enable port forwarding")
111114
cobraCmd.Flags().BoolVarP(&cmd.flags.deploy, "deploy", "d", cmd.flags.deploy, "Deploy chart")
112115
cobraCmd.Flags().BoolVar(&cmd.flags.noSleep, "no-sleep", cmd.flags.noSleep, "Enable no-sleep")
@@ -180,7 +183,7 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
180183
}()
181184
}
182185

183-
cmd.enterTerminal()
186+
cmd.enterTerminal(args)
184187
}
185188

186189
func (cmd *UpCmd) ensureNamespace() error {
@@ -675,12 +678,36 @@ func (cmd *UpCmd) startSync() []*synctool.SyncConfig {
675678
if err != nil {
676679
log.Panicf("Unable to list devspace pods: %s", err.Error())
677680
} else if pod != nil {
681+
if len(pod.Spec.Containers) == 0 {
682+
log.Warnf("Cannot start sync on pod, because selected pod %s/%s has no containers", pod.Namespace, pod.Name)
683+
continue
684+
}
685+
686+
container := &pod.Spec.Containers[0]
687+
if syncPath.ContainerName != nil && *syncPath.ContainerName != "" {
688+
found := false
689+
690+
for _, c := range pod.Spec.Containers {
691+
if c.Name == *syncPath.ContainerName {
692+
container = &c
693+
found = true
694+
break
695+
}
696+
}
697+
698+
if found == false {
699+
log.Warnf("Couldn't start sync, because container %s wasn't found in pod %s/%s", *syncPath.ContainerName, pod.Namespace, pod.Name)
700+
continue
701+
}
702+
}
703+
678704
syncConfig := &synctool.SyncConfig{
679705
Kubectl: cmd.kubectl,
680706
Pod: pod,
681-
Container: &pod.Spec.Containers[0],
707+
Container: container,
682708
WatchPath: absLocalPath,
683709
DestPath: *syncPath.ContainerPath,
710+
Verbose: cmd.flags.verboseSync,
684711
}
685712

686713
if syncPath.ExcludePaths != nil {
@@ -700,7 +727,7 @@ func (cmd *UpCmd) startSync() []*synctool.SyncConfig {
700727
log.Fatalf("Sync error: %s", err.Error())
701728
}
702729

703-
log.Donef("Sync started on %s <-> %s", absLocalPath, *syncPath.ContainerPath)
730+
log.Donef("Sync started on %s <-> %s (Pod: %s/%s)", absLocalPath, *syncPath.ContainerPath, pod.Namespace, pod.Name)
704731
syncConfigs = append(syncConfigs, syncConfig)
705732
}
706733
}
@@ -756,24 +783,37 @@ func (cmd *UpCmd) startPortForwarding() {
756783
}
757784
}
758785

759-
func (cmd *UpCmd) enterTerminal() {
760-
var shell []string
786+
func (cmd *UpCmd) enterTerminal(args []string) {
787+
var command []string
788+
config := configutil.GetConfig(false)
761789

762-
if len(cmd.flags.shell) == 0 {
763-
shell = []string{
790+
if len(args) == 0 && (config.DevSpace.Terminal.Command == nil || len(*config.DevSpace.Terminal.Command) == 0) {
791+
command = []string{
764792
"sh",
765793
"-c",
766794
"command -v bash >/dev/null 2>&1 && exec bash || exec sh",
767795
}
768796
} else {
769-
shell = []string{cmd.flags.shell}
797+
if len(args) > 0 {
798+
command = args
799+
} else {
800+
for _, cmd := range *config.DevSpace.Terminal.Command {
801+
command = append(command, *cmd)
802+
}
803+
}
770804
}
771805

772-
_, _, _, terminalErr := kubectl.Exec(cmd.kubectl, cmd.pod, cmd.pod.Spec.Containers[0].Name, shell, true, nil)
806+
containerName := cmd.pod.Spec.Containers[0].Name
807+
if cmd.flags.container != "" {
808+
containerName = cmd.flags.container
809+
} else if config.DevSpace.Terminal.ContainerName != nil {
810+
containerName = *config.DevSpace.Terminal.ContainerName
811+
}
773812

813+
_, _, _, terminalErr := kubectl.Exec(cmd.kubectl, cmd.pod, containerName, command, true, nil)
774814
if terminalErr != nil {
775815
if _, ok := terminalErr.(kubectlExec.CodeExitError); ok == false {
776-
log.Fatalf("Unable to start terminal session: %s", terminalErr.Error())
816+
log.Fatalf("Unable to start terminal session: %v", terminalErr)
777817
}
778818
}
779819
}

pkg/devspace/config/configutil/make.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func makeConfig() *v1.Config {
1010
User: &v1.ClusterUser{},
1111
},
1212
DevSpace: &v1.DevSpaceConfig{
13+
Terminal: &v1.Terminal{},
1314
PortForwarding: &[]*v1.PortForwardingConfig{},
1415
Release: &v1.Release{},
1516
Sync: &[]*v1.SyncConfig{},

pkg/devspace/config/v1/devspace.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v1
22

33
//DevSpaceConfig defines the devspace deployment
44
type DevSpaceConfig struct {
5+
Terminal *Terminal `yaml:"terminal"`
56
Release *Release `yaml:"release"`
67
PortForwarding *[]*PortForwardingConfig `yaml:"portForwarding"`
78
Sync *[]*SyncConfig `yaml:"sync"`
@@ -28,6 +29,7 @@ type SyncConfig struct {
2829
LabelSelector *map[string]*string `yaml:"labelSelector"`
2930
LocalSubPath *string `yaml:"localSubPath"`
3031
ContainerPath *string `yaml:"containerPath"`
32+
ContainerName *string `yaml:"containerName"`
3133
ExcludePaths *[]string `yaml:"excludePaths"`
3234
DownloadExcludePaths *[]string `yaml:"downloadExcludePaths"`
3335
UploadExcludePaths *[]string `yaml:"uploadExcludePaths"`

pkg/devspace/config/v1/terminal.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package v1
2+
3+
// Terminal describes the terminal options
4+
type Terminal struct {
5+
ContainerName *string `yaml:"containerName"`
6+
Command *[]*string `yaml:"shell"`
7+
}

pkg/devspace/sync/downstream.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (d *downstream) downloadFiles(files []*fileInformation) (string, error) {
216216

217217
// Each file is represented in one line
218218
for _, element := range files {
219-
if lenFiles <= 3 || d.config.verbose {
219+
if lenFiles <= 3 || d.config.Verbose {
220220
d.config.Logf("[Downstream] Download file %s, size: %d", element.Name, element.Size)
221221
}
222222

@@ -335,7 +335,7 @@ func (d *downstream) removeFilesAndFolders(removeFiles map[string]*fileInformati
335335
absFilepath := filepath.Join(d.config.WatchPath, key)
336336

337337
if shouldRemoveLocal(absFilepath, value, d.config) {
338-
if numRemoveFiles <= 3 || d.config.verbose {
338+
if numRemoveFiles <= 3 || d.config.Verbose {
339339
d.config.Logf("[Downstream] Remove %s", key)
340340
}
341341

@@ -369,7 +369,7 @@ func (d *downstream) createFolders(createFolders []*fileInformation) {
369369

370370
for _, element := range createFolders {
371371
if element.IsDirectory {
372-
if numCreateFolders <= 3 || d.config.verbose {
372+
if numCreateFolders <= 3 || d.config.Verbose {
373373
d.config.Logln("[Downstream] Create folder: " + element.Name)
374374
}
375375

pkg/devspace/sync/sync_config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type SyncConfig struct {
3939
ExcludePaths []string
4040
DownloadExcludePaths []string
4141
UploadExcludePaths []string
42+
Verbose bool
4243

4344
fileIndex *fileIndex
4445

@@ -51,9 +52,7 @@ type SyncConfig struct {
5152
upstream *upstream
5253
downstream *downstream
5354

54-
silent bool
55-
verbose bool
56-
55+
silent bool
5756
stopOnce sync.Once
5857

5958
// Used for testing

pkg/devspace/sync/upstream.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func (u *upstream) applyCreates(files []*fileInformation) error {
270270
}
271271

272272
// Print changes
273-
if u.config.verbose {
273+
if u.config.Verbose {
274274
for _, c := range writtenFiles {
275275
if c.IsDirectory {
276276
u.config.Logf("[Upstream] Create Folder %s", c.Name)
@@ -387,7 +387,7 @@ func (u *upstream) applyRemoves(files []*fileInformation) error {
387387
}
388388

389389
// Print changes
390-
if u.config.verbose {
390+
if u.config.Verbose {
391391
u.config.Logf("[Upstream] Remove %s", relativePath)
392392
}
393393
}

0 commit comments

Comments
 (0)