-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
78 lines (71 loc) · 1.84 KB
/
wait.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
package commands
import (
"fmt"
"os"
"sync"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/pkg/errors"
"github.com/spf13/cobra"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
)
func NewWaitCommand() *cobra.Command {
var (
ignoreNotFound bool
)
var command = &cobra.Command{
Use: "wait WORKFLOW1 WORKFLOW2..,",
Short: "waits for a workflow to complete",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
InitWorkflowClient()
WaitWorkflows(args, ignoreNotFound, false)
},
}
command.Flags().BoolVar(&ignoreNotFound, "ignore-not-found", false, "Ignore the wait if the workflow is not found")
return command
}
// WaitWorkflows waits for the given workflowNames.
func WaitWorkflows(workflowNames []string, ignoreNotFound, quiet bool) {
var wg sync.WaitGroup
for _, workflowName := range workflowNames {
wg.Add(1)
go func(name string) {
waitOnOne(name, ignoreNotFound, quiet)
wg.Done()
}(workflowName)
}
wg.Wait()
}
func waitOnOne(workflowName string, ignoreNotFound, quiet bool) {
fieldSelector := fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", workflowName))
opts := metav1.ListOptions{
FieldSelector: fieldSelector.String(),
}
_, err := wfClient.Get(workflowName, metav1.GetOptions{})
if err != nil {
if apierr.IsNotFound(err) && ignoreNotFound {
return
}
errors.CheckError(err)
}
watchIf, err := wfClient.Watch(opts)
errors.CheckError(err)
defer watchIf.Stop()
for next := range watchIf.ResultChan() {
wf, ok := next.Object.(*wfv1.Workflow)
if !ok {
continue
}
if !wf.Status.FinishedAt.IsZero() {
if !quiet {
fmt.Printf("%s completed at %v\n", workflowName, wf.Status.FinishedAt)
}
return
}
}
}