Skip to content

Commit 8c393e2

Browse files
authored
Merge pull request #2296 from FabianKramm/main
refactor: improve sleep mode exit
2 parents fbff37e + 5cea0e2 commit 8c393e2

10 files changed

Lines changed: 94 additions & 37 deletions

File tree

cmd/root.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
"github.com/loft-sh/devspace/pkg/devspace/kill"
78
"io/ioutil"
89
"os"
910
"strings"
@@ -209,6 +210,13 @@ func BuildRoot(f factory.Factory, excludePlugins bool) *cobra.Command {
209210
})
210211
persistentFlags := rootCmd.PersistentFlags()
211212
globalFlags = flags.SetGlobalFlags(persistentFlags)
213+
kill.SetStopFunction(func(message string) {
214+
if message == "" {
215+
os.Exit(1)
216+
} else {
217+
f.GetLog().Fatal(message)
218+
}
219+
})
212220

213221
rootCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
214222
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}

cmd/run_pipeline.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/loft-sh/devspace/pkg/devspace/dev"
1616
"github.com/loft-sh/devspace/pkg/devspace/devpod"
1717
"github.com/loft-sh/devspace/pkg/devspace/hook"
18+
"github.com/loft-sh/devspace/pkg/devspace/kill"
1819
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
1920
"github.com/loft-sh/devspace/pkg/devspace/pipeline"
2021
"github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
@@ -24,6 +25,7 @@ import (
2425
"github.com/loft-sh/devspace/pkg/util/interrupt"
2526
"github.com/loft-sh/devspace/pkg/util/log"
2627
"github.com/loft-sh/devspace/pkg/util/message"
28+
"github.com/mgutz/ansi"
2729
"github.com/pkg/errors"
2830
"github.com/sirupsen/logrus"
2931
"github.com/spf13/cobra"
@@ -459,6 +461,16 @@ func runPipeline(ctx devspacecontext.Context, args []string, options *CommandOpt
459461

460462
// get deploy pipeline
461463
pipe := pipeline.NewPipeline(ctx.Config().Config().Name, devPodManager, dependencyRegistry, configPipeline, options.Options)
464+
kill.SetStopFunction(func(message string) {
465+
if message != "" {
466+
ctx.Log().WriteString(logrus.FatalLevel, "\n"+ansi.Color("fatal", "red+b")+" "+message+"\n")
467+
}
468+
469+
err = pipe.Close()
470+
if err != nil {
471+
ctx.Log().Debugf("Error closing pipeline: %v", err)
472+
}
473+
})
462474

463475
// start ui & open
464476
serv, err := dev.UI(ctx, options.UIPort, options.ShowUI, pipe)
@@ -478,13 +490,21 @@ func runPipeline(ctx devspacecontext.Context, args []string, options *CommandOpt
478490
// start pipeline
479491
err = pipe.Run(ctx.WithLogger(log.NewStreamLoggerWithFormat(stdoutWriter, stderrWriter, ctx.Log().GetLevel(), log.TimeFormat)), args)
480492
if err != nil {
493+
if err == context.Canceled {
494+
return nil
495+
}
496+
481497
return err
482498
}
483499
ctx.Log().Debugf("Wait for dev to finish")
484500

485501
// wait for dev
486502
err = pipe.WaitDev()
487503
if err != nil {
504+
if err == context.Canceled {
505+
return nil
506+
}
507+
488508
return err
489509
}
490510

pkg/devspace/devpod/devpod.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"fmt"
7+
"github.com/loft-sh/devspace/pkg/devspace/kill"
78
"io"
89
"net/http"
910
"os"
@@ -420,8 +421,8 @@ func (d *devPod) startAttach(ctx devspacecontext.Context, devContainer *latest.D
420421
}
421422

422423
// kill ourselves here
423-
if !opts.ContinueOnTerminalExit && opts.KillApplication != nil {
424-
go opts.KillApplication()
424+
if !opts.ContinueOnTerminalExit {
425+
kill.StopDevSpace("")
425426
} else {
426427
parent.Kill(nil)
427428
}
@@ -460,8 +461,8 @@ func (d *devPod) startTerminal(ctx devspacecontext.Context, devContainer *latest
460461
}
461462

462463
// kill ourselves here
463-
if !opts.ContinueOnTerminalExit && opts.KillApplication != nil {
464-
go opts.KillApplication()
464+
if !opts.ContinueOnTerminalExit {
465+
kill.StopDevSpace("")
465466
} else {
466467
parent.Kill(nil)
467468
}

pkg/devspace/devpod/manager.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ type Options struct {
2222
DisablePortForwarding bool `long:"disable-port-forwarding" description:"If enabled will not start any port forwarding configuration"`
2323
DisablePodReplace bool `long:"disable-pod-replace" description:"If enabled will not replace any pods"`
2424
DisableOpen bool `long:"disable-open" description:"If enabled will not replace any pods"`
25-
26-
// KillApplication kills the whole pipeline including all dev pods
27-
KillApplication func()
2825
}
2926

3027
type Manager interface {

pkg/devspace/kill/kill.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package kill
2+
3+
import "sync"
4+
5+
// stopDevSpace can be used to stop DevSpace globally
6+
var stopDevSpace func(message string)
7+
var stopDevSpaceMutex sync.Mutex
8+
9+
func StopDevSpace(message string) {
10+
stopDevSpaceMutex.Lock()
11+
defer stopDevSpaceMutex.Unlock()
12+
13+
// don't block here
14+
go stopDevSpace(message)
15+
}
16+
17+
func SetStopFunction(stopFn func(message string)) {
18+
stopDevSpaceMutex.Lock()
19+
defer stopDevSpaceMutex.Unlock()
20+
21+
stopDevSpace = stopFn
22+
}

pkg/devspace/kubectl/client.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
7+
"github.com/loft-sh/devspace/pkg/devspace/kill"
78
"github.com/loft-sh/devspace/pkg/devspace/kubectl/util"
89
"github.com/loft-sh/devspace/pkg/devspace/upgrade"
910
"github.com/loft-sh/devspace/pkg/util/idle"
@@ -118,7 +119,7 @@ func NewClientFromContext(context, namespace string, switchContext bool, kubeLoa
118119
requestType: "Regular",
119120
callback: func(response *http.Response) {
120121
if response.Header.Get("X-DevSpace-Response-Type") == "Blocked" {
121-
log.GetInstance().Fatalf("Targeted Kubernetes environment has begun sleeping. Please restart DevSpace to wake up the environment")
122+
kill.StopDevSpace("Targeted Kubernetes environment has begun sleeping. Please restart DevSpace to wake up the environment")
122123
}
123124
},
124125
}
@@ -329,7 +330,7 @@ func wakeUpAndPing(ctx context.Context, client Client, log log.Logger) error {
329330
requestType: "Ping",
330331
callback: func(response *http.Response) {
331332
if response.Header.Get("X-DevSpace-Response-Type") == "Blocked" {
332-
log.Fatalf("Targeted Kubernetes environment has begun sleeping. Please restart DevSpace to wake up the environment")
333+
kill.StopDevSpace("Targeted Kubernetes environment has begun sleeping. Please restart DevSpace to wake up the environment")
333334
}
334335
},
335336
}
@@ -356,7 +357,7 @@ func wakeUpAndPing(ctx context.Context, client Client, log log.Logger) error {
356357
if err != nil {
357358
log.Debugf("Error pinging Kubernetes environment: %v", err)
358359
}
359-
}, time.Minute*3)
360+
}, time.Minute)
360361
}()
361362

362363
return nil
@@ -416,9 +417,16 @@ func wakeUp(ctx context.Context, client Client, log log.Logger) error {
416417
log.Infof("DevSpace is waking up the Kubernetes environment, please wait a moment...")
417418

418419
// wake up the environment
419-
_, err = kubeClient.CoreV1().Pods(client.Namespace()).List(ctx, metav1.ListOptions{LabelSelector: "devspace=wakeup"})
420-
if err != nil {
421-
return errors.Wrap(err, "error waking up the environment")
420+
waitErr := wait.PollImmediate(time.Second, time.Second*30, func() (done bool, err error) {
421+
_, err = kubeClient.CoreV1().Pods(client.Namespace()).List(ctx, metav1.ListOptions{LabelSelector: "devspace=wakeup"})
422+
if err != nil {
423+
return false, nil
424+
}
425+
426+
return true, nil
427+
})
428+
if waitErr != nil {
429+
return errors.Wrap(err, "wake up environment")
422430
}
423431

424432
return nil

pkg/devspace/pipeline/engine/pipelinehandler/commands/get_config_value.go

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

33
import (
44
"fmt"
5+
"github.com/jessevdk/go-flags"
56
"github.com/loft-sh/devspace/pkg/util/yamlutil"
7+
"github.com/pkg/errors"
68
"strings"
79

810
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
@@ -11,23 +13,28 @@ import (
1113
"mvdan.cc/sh/v3/interp"
1214
)
1315

16+
type GetConfigValueOptions struct{}
17+
1418
func GetConfigValue(ctx devspacecontext.Context, args []string) error {
1519
ctx = ctx.WithLogger(ctx.Log().ErrorStreamOnly())
1620
ctx.Log().Debugf("get_config_value %s", strings.Join(args, " "))
17-
if len(args) != 1 {
21+
options := &GetConfigValueOptions{}
22+
args, err := flags.ParseArgs(options, args)
23+
if err != nil {
24+
return errors.Wrap(err, "parse args")
25+
} else if len(args) != 1 {
1826
return fmt.Errorf("usage: get_config_value deployments.my-deployment.helm.chart.name")
1927
}
2028

2129
hc := interp.HandlerCtx(ctx.Context())
22-
rawConfig := ctx.Config().Raw()
23-
30+
config := ctx.Config().RawBeforeConversion()
2431
nodePath, err := yamlpath.NewPath(args[0])
2532
if err != nil {
2633
ctx.Log().Debugf("%v", err)
2734
return nil
2835
}
2936

30-
out, err := yaml.Marshal(rawConfig)
37+
out, err := yaml.Marshal(config)
3138
if err != nil {
3239
ctx.Log().Debugf("%v", err)
3340
return nil
@@ -45,11 +52,19 @@ func GetConfigValue(ctx devspacecontext.Context, args []string) error {
4552
ctx.Log().Debugf("%v", err)
4653
return nil
4754
}
48-
4955
if len(nodes) < 1 {
5056
return nil
5157
}
5258

53-
_, _ = hc.Stdout.Write([]byte(nodes[0].Value))
59+
if nodes[0].Kind == yaml.ScalarNode {
60+
_, _ = hc.Stdout.Write([]byte(nodes[0].Value))
61+
return nil
62+
}
63+
64+
out, err = yaml.Marshal(nodes[0])
65+
if err != nil {
66+
return err
67+
}
68+
_, _ = hc.Stdout.Write(out)
5469
return nil
5570
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/start_dev.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
77
"github.com/loft-sh/devspace/pkg/devspace/devpod"
88
"github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
9-
logpkg "github.com/loft-sh/devspace/pkg/util/log"
109
"github.com/loft-sh/devspace/pkg/util/stringutil"
1110
"github.com/pkg/errors"
1211
"strings"
@@ -72,19 +71,5 @@ func StartDev(ctx devspacecontext.Context, pipeline types.Pipeline, args []strin
7271
} else {
7372
return fmt.Errorf("either specify 'start_dev --all' or 'dev devConfig1 devConfig2'")
7473
}
75-
options.Options.KillApplication = func() {
76-
killApplication(pipeline)
77-
}
7874
return pipeline.DevPodManager().StartMultiple(ctx, args, options.Options)
7975
}
80-
81-
func killApplication(pipeline types.Pipeline) {
82-
for pipeline.Parent() != nil {
83-
pipeline = pipeline.Parent()
84-
}
85-
86-
err := pipeline.Close()
87-
if err != nil {
88-
logpkg.GetInstance().Errorf("error closing pipeline: %v", err)
89-
}
90-
}

pkg/util/extract/unzip.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"compress/gzip"
77
"fmt"
88
"io"
9-
"log"
109
"os"
1110
"path/filepath"
1211
)
@@ -34,7 +33,7 @@ func (e *extractor) UntarGz(src, dest string) error {
3433

3534
uncompressedStream, err := gzip.NewReader(gzipStream)
3635
if err != nil {
37-
log.Fatal("ExtractTarGz: NewReader failed")
36+
return fmt.Errorf("ExtractTarGz: NewReader failed")
3837
}
3938

4039
tarReader := tar.NewReader(uncompressedStream)

pkg/util/idle/idle.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package idle
22

33
import (
4+
"fmt"
5+
"github.com/loft-sh/devspace/pkg/devspace/kill"
46
"time"
57

68
"github.com/loft-sh/devspace/pkg/util/log"
@@ -48,7 +50,7 @@ func (m *monitor) Start(timeout time.Duration, log log.Logger) {
4850
return
4951
} else if duration > timeout {
5052
// we exit here
51-
log.Fatalf("Automatically exit DevSpace, because the user is inactive for %s. To disable automatic exiting, run with --inactivity-timeout=0", duration.String())
53+
kill.StopDevSpace(fmt.Sprintf("Automatically exit DevSpace, because the user is inactive for %s. To disable automatic exiting, run with --inactivity-timeout=0", duration.String()))
5254
}
5355
}, time.Second*10)
5456
}()

0 commit comments

Comments
 (0)