forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
116 lines (103 loc) · 3.44 KB
/
get.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package cron
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/argoproj/pkg/errors"
"github.com/argoproj/pkg/humanize"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
"github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
func NewGetCommand() *cobra.Command {
var output string
command := &cobra.Command{
Use: "get CRON_WORKFLOW...",
Short: "display details about a cron workflow",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
ctx, apiClient := client.NewAPIClient()
serviceClient, err := apiClient.NewCronWorkflowServiceClient()
errors.CheckError(err)
namespace := client.Namespace()
for _, arg := range args {
cronWf, err := serviceClient.GetCronWorkflow(ctx, &cronworkflow.GetCronWorkflowRequest{
Name: arg,
Namespace: namespace,
})
errors.CheckError(err)
printCronWorkflow(cronWf, output)
}
},
}
command.Flags().StringVarP(&output, "output", "o", "", "Output format. One of: json|yaml|wide")
return command
}
func printCronWorkflow(wf *wfv1.CronWorkflow, outFmt string) {
switch outFmt {
case "name":
fmt.Println(wf.ObjectMeta.Name)
case "json":
outBytes, _ := json.MarshalIndent(wf, "", " ")
fmt.Println(string(outBytes))
case "yaml":
outBytes, _ := yaml.Marshal(wf)
fmt.Print(string(outBytes))
case "wide", "":
fmt.Print(getCronWorkflowGet(wf))
default:
log.Fatalf("Unknown output format: %s", outFmt)
}
}
func getCronWorkflowGet(cwf *wfv1.CronWorkflow) string {
const fmtStr = "%-30s %v\n"
out := ""
out += fmt.Sprintf(fmtStr, "Name:", cwf.ObjectMeta.Name)
out += fmt.Sprintf(fmtStr, "Namespace:", cwf.ObjectMeta.Namespace)
out += fmt.Sprintf(fmtStr, "Created:", humanize.Timestamp(cwf.ObjectMeta.CreationTimestamp.Time))
out += fmt.Sprintf(fmtStr, "Schedule:", cwf.Spec.Schedule)
out += fmt.Sprintf(fmtStr, "Suspended:", cwf.Spec.Suspend)
if cwf.Spec.Timezone != "" {
out += fmt.Sprintf(fmtStr, "Timezone:", cwf.Spec.Timezone)
}
if cwf.Spec.StartingDeadlineSeconds != nil {
out += fmt.Sprintf(fmtStr, "StartingDeadlineSeconds:", *cwf.Spec.StartingDeadlineSeconds)
}
if cwf.Spec.ConcurrencyPolicy != "" {
out += fmt.Sprintf(fmtStr, "ConcurrencyPolicy:", cwf.Spec.ConcurrencyPolicy)
}
if cwf.Status.LastScheduledTime != nil {
out += fmt.Sprintf(fmtStr, "LastScheduledTime:", humanize.Timestamp(cwf.Status.LastScheduledTime.Time))
}
next, err := GetNextRuntime(cwf)
if err == nil {
out += fmt.Sprintf(fmtStr, "NextScheduledTime:", humanize.Timestamp(next)+" (assumes workflow-controller is in UTC)")
}
if len(cwf.Status.Active) > 0 {
var activeWfNames []string
for _, activeWf := range cwf.Status.Active {
activeWfNames = append(activeWfNames, activeWf.Name)
}
out += fmt.Sprintf(fmtStr, "Active Workflows:", strings.Join(activeWfNames, ", "))
}
if len(cwf.Status.Conditions) > 0 {
out += cwf.Status.Conditions.DisplayString(fmtStr, map[wfv1.ConditionType]string{wfv1.ConditionTypeSubmissionError: "✖"})
}
if len(cwf.Spec.WorkflowSpec.Arguments.Parameters) > 0 {
out += fmt.Sprintf(fmtStr, "Workflow Parameters:", "")
for _, param := range cwf.Spec.WorkflowSpec.Arguments.Parameters {
if !param.HasValue() {
continue
}
out += fmt.Sprintf(fmtStr, " "+param.Name+":", param.GetValue())
}
}
return out
}