Skip to content

Commit 0a35c92

Browse files
committed
feat: add wait_pod
1 parent 3d41239 commit 0a35c92

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/jessevdk/go-flags"
6+
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
7+
"github.com/loft-sh/devspace/pkg/devspace/services/targetselector"
8+
"github.com/pkg/errors"
9+
"time"
10+
)
11+
12+
type WaitPodOptions struct {
13+
ImageSelector string `long:"image-selector" description:"The image selector to use to select the container"`
14+
LabelSelector string `long:"label-selector" description:"The label selector to use to select the container"`
15+
Container string `long:"container" description:"The container to use"`
16+
17+
Namespace string `long:"namespace" short:"n" description:"The namespace to use"`
18+
DisableWait bool `long:"disable-wait" description:"If true, will not wait for the container to become ready"`
19+
Timeout int64 `long:"timeout" description:"The timeout to wait. Defaults to 5 minutes"`
20+
}
21+
22+
func WaitPod(ctx devspacecontext.Context, args []string) error {
23+
if ctx.KubeClient() == nil {
24+
return errors.Errorf(ErrMsg)
25+
}
26+
options := &WaitPodOptions{
27+
Namespace: ctx.KubeClient().Namespace(),
28+
}
29+
args, err := flags.ParseArgs(options, args)
30+
if err != nil {
31+
return errors.Wrap(err, "parse args")
32+
}
33+
if len(args) != 0 {
34+
return fmt.Errorf("usage: wait_pod [--image-selector|--label-selector]")
35+
}
36+
if options.ImageSelector == "" && options.LabelSelector == "" {
37+
return fmt.Errorf("usage: wait_pod [--image-selector|--label-selector]")
38+
}
39+
40+
logger := ctx.Log().ErrorStreamOnly()
41+
selectorOptions := targetselector.NewOptionsFromFlags(options.Container, options.LabelSelector, []string{options.ImageSelector}, options.Namespace, "")
42+
if options.Timeout != 0 {
43+
selectorOptions = selectorOptions.WithTimeout(options.Timeout)
44+
}
45+
selectorOptions.WithWaitingStrategy(targetselector.NewUntilNewestRunningWaitingStrategy(time.Millisecond * 100))
46+
_, err = targetselector.NewTargetSelector(selectorOptions).SelectSingleContainer(ctx.Context(), ctx.KubeClient(), logger)
47+
if err != nil {
48+
return err
49+
}
50+
51+
return nil
52+
}

pkg/devspace/pipeline/engine/pipelinehandler/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ var PipelineCommands = map[string]func(devCtx devspacecontext.Context, pipeline
7676
"select_pod": func(devCtx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
7777
return commands.SelectPod(devCtx, args)
7878
},
79+
"wait_pod": func(devCtx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
80+
return commands.WaitPod(devCtx, args)
81+
},
7982
}
8083

8184
func init() {

0 commit comments

Comments
 (0)