|
| 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 | +} |
0 commit comments