-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
common.go
156 lines (140 loc) · 4.24 KB
/
common.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package commands
import (
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/argoproj/argo/cmd/argo/commands/client"
"github.com/argoproj/argo/pkg/apiclient/workflow"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned"
"github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
"github.com/argoproj/argo/workflow/templateresolution"
)
// Global variables
var (
// DEPRECATED
restConfig *rest.Config
// DEPRECATED
clientset *kubernetes.Clientset
// DEPRECATED
wfClientset *versioned.Clientset
// DEPRECATED
wfClient v1alpha1.WorkflowInterface
// DEPRECATED
wftmplClient v1alpha1.WorkflowTemplateInterface
jobStatusIconMap map[wfv1.NodePhase]string
noColor bool
// DEPRECATED
namespace string
)
func init() {
cobra.OnInitialize(initializeSession)
}
// ANSI escape codes
const (
escape = "\x1b"
noFormat = 0
Bold = 1
FgBlack = 30
FgRed = 31
FgGreen = 32
FgYellow = 33
FgBlue = 34
FgMagenta = 35
FgCyan = 36
FgWhite = 37
FgDefault = 39
)
// Default status for printWorkflow
const DefaultStatus = ""
func initializeSession() {
jobStatusIconMap = map[wfv1.NodePhase]string{
wfv1.NodePending: ansiFormat("◷", FgYellow),
wfv1.NodeRunning: ansiFormat("●", FgCyan),
wfv1.NodeSucceeded: ansiFormat("✔", FgGreen),
wfv1.NodeSkipped: ansiFormat("○", FgDefault),
wfv1.NodeFailed: ansiFormat("✖", FgRed),
wfv1.NodeError: ansiFormat("⚠", FgRed),
}
}
// DEPRECATED
func InitKubeClient() *kubernetes.Clientset {
if clientset != nil {
return clientset
}
var err error
restConfig, err = client.Config.ClientConfig()
if err != nil {
log.Fatal(err)
}
// create the clientset
clientset, err = kubernetes.NewForConfig(restConfig)
if err != nil {
log.Fatal(err)
}
return clientset
}
// InitWorkflowClient creates a new client for the Kubernetes Workflow CRD.
// DEPRECATED
func InitWorkflowClient(ns ...string) v1alpha1.WorkflowInterface {
if wfClient != nil && (len(ns) == 0 || ns[0] == namespace) {
return wfClient
}
InitKubeClient()
var err error
if len(ns) > 0 {
namespace = ns[0]
} else {
namespace, _, err = client.Config.Namespace()
if err != nil {
log.Fatal(err)
}
}
wfClientset = versioned.NewForConfigOrDie(restConfig)
wfClient = wfClientset.ArgoprojV1alpha1().Workflows(namespace)
wftmplClient = wfClientset.ArgoprojV1alpha1().WorkflowTemplates(namespace)
return wfClient
}
// ansiFormat wraps ANSI escape codes to a string to format the string to a desired color.
// NOTE: we still apply formatting even if there is no color formatting desired.
// The purpose of doing this is because when we apply ANSI color escape sequences to our
// output, this confuses the tabwriter library which miscalculates widths of columns and
// misaligns columns. By always applying a ANSI escape sequence (even when we don't want
// color, it provides more consistent string lengths so that tabwriter can calculate
// widths correctly.
func ansiFormat(s string, codes ...int) string {
if noColor || os.Getenv("TERM") == "dumb" || len(codes) == 0 {
return s
}
codeStrs := make([]string, len(codes))
for i, code := range codes {
codeStrs[i] = strconv.Itoa(code)
}
sequence := strings.Join(codeStrs, ";")
return fmt.Sprintf("%s[%sm%s%s[%dm", escape, sequence, s, escape, noFormat)
}
// LazyWorkflowTemplateGetter is a wrapper of v1alpha1.WorkflowTemplateInterface which
// supports lazy initialization.
// DEPRECATED
type LazyWorkflowTemplateGetter struct{}
// Get initializes it just before it's actually used and returns a retrieved workflow template.
// DEPRECATED
func (c LazyWorkflowTemplateGetter) Get(name string) (*wfv1.WorkflowTemplate, error) {
if wftmplClient == nil {
_ = InitWorkflowClient()
}
return templateresolution.WrapWorkflowTemplateInterface(wftmplClient).Get(name)
}
// DEPRECATED
var _ templateresolution.WorkflowTemplateNamespacedGetter = &LazyWorkflowTemplateGetter{}
// DEPRECATED
func GetWFApiServerGRPCClient(conn *grpc.ClientConn) (workflow.WorkflowServiceClient, context.Context) {
return workflow.NewWorkflowServiceClient(conn), client.GetContext()
}