-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
util.go
346 lines (314 loc) · 11.9 KB
/
util.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package common
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os/exec"
"runtime"
"sort"
"strings"
"time"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/remotecommand"
"github.com/argoproj/argo-workflows/v3/errors"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/util"
"github.com/argoproj/argo-workflows/v3/util/template"
)
// FindOverlappingVolume looks an artifact path, checks if it overlaps with any
// user specified volumeMounts in the template, and returns the deepest volumeMount
// (if any). A return value of nil indicates the path is not under any volumeMount.
func FindOverlappingVolume(tmpl *wfv1.Template, path string) *apiv1.VolumeMount {
volumeMounts := tmpl.GetVolumeMounts()
sort.Slice(volumeMounts, func(i, j int) bool {
return len(volumeMounts[i].MountPath) > len(volumeMounts[j].MountPath)
})
for _, mnt := range volumeMounts {
normalizedMountPath := strings.TrimRight(mnt.MountPath, "/")
if path == normalizedMountPath || isSubPath(path, normalizedMountPath) {
return &mnt
}
}
return nil
}
func isSubPath(path string, normalizedMountPath string) bool {
return strings.HasPrefix(path, normalizedMountPath+"/")
}
type RoundTripCallback func(conn *websocket.Conn, resp *http.Response, err error) error
type WebsocketRoundTripper struct {
Dialer *websocket.Dialer
Do RoundTripCallback
}
func (d *WebsocketRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
conn, resp, err := d.Dialer.Dial(r.URL.String(), r.Header)
if err == nil {
defer util.Close(conn)
}
return resp, d.Do(conn, resp, err)
}
// ExecPodContainer runs a command in a container in a pod and returns the remotecommand.Executor
func ExecPodContainer(restConfig *rest.Config, namespace string, pod string, container string, stdout bool, stderr bool, command ...string) (remotecommand.Executor, error) {
clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, errors.InternalWrapError(err)
}
execRequest := clientset.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod).
Namespace(namespace).
SubResource("exec").
Param("container", container).
Param("stdout", fmt.Sprintf("%v", stdout)).
Param("stderr", fmt.Sprintf("%v", stderr)).
Param("tty", "false")
for _, cmd := range command {
execRequest = execRequest.Param("command", cmd)
}
log.Info(execRequest.URL())
exec, err := remotecommand.NewSPDYExecutor(restConfig, "POST", execRequest.URL())
if err != nil {
return nil, errors.InternalWrapError(err)
}
return exec, nil
}
// GetExecutorOutput returns the output of an remotecommand.Executor
func GetExecutorOutput(exec remotecommand.Executor) (*bytes.Buffer, *bytes.Buffer, error) {
var stdOut bytes.Buffer
var stdErr bytes.Buffer
err := exec.Stream(remotecommand.StreamOptions{
Stdout: &stdOut,
Stderr: &stdErr,
Tty: false,
})
if err != nil {
return nil, nil, errors.InternalWrapError(err)
}
return &stdOut, &stdErr, nil
}
// ProcessArgs sets in the inputs, the values either passed via arguments, or the hardwired values
// It substitutes:
// * parameters in the template from the arguments
// * global parameters (e.g. {{workflow.parameters.XX}}, {{workflow.name}}, {{workflow.status}})
// * local parameters (e.g. {{pod.name}})
func ProcessArgs(tmpl *wfv1.Template, args wfv1.ArgumentsProvider, globalParams, localParams Parameters, validateOnly bool, namespace string, configMapInformer cache.SharedIndexInformer) (*wfv1.Template, error) {
// For each input parameter:
// 1) check if was supplied as argument. if so use the supplied value from arg
// 2) if not, use default value.
// 3) if no default value, it is an error
newTmpl := tmpl.DeepCopy()
for i, inParam := range newTmpl.Inputs.Parameters {
if inParam.Value == nil && inParam.Default != nil {
// first set to default value
inParam.Value = inParam.Default
}
// overwrite value from argument (if supplied)
argParam := args.GetParameterByName(inParam.Name)
if argParam != nil {
if argParam.Value != nil {
inParam.Value = argParam.Value
} else {
inParam.ValueFrom = argParam.ValueFrom
}
}
if inParam.ValueFrom != nil && inParam.ValueFrom.ConfigMapKeyRef != nil {
if configMapInformer != nil {
// SubstituteParams is called only at the end of this method. To support parametrization of the configmap
// we need to perform a substitution here over the name and the key of the ConfigMapKeyRef.
cmName, err := substituteConfigMapKeyRefParam(inParam.ValueFrom.ConfigMapKeyRef.Name, globalParams)
if err != nil {
log.WithError(err).Error("unable to substitute name for ConfigMapKeyRef")
return nil, err
}
cmKey, err := substituteConfigMapKeyRefParam(inParam.ValueFrom.ConfigMapKeyRef.Key, globalParams)
if err != nil {
log.WithError(err).Error("unable to substitute key for ConfigMapKeyRef")
return nil, err
}
cmValue, err := GetConfigMapValue(configMapInformer, namespace, cmName, cmKey)
if err != nil {
if inParam.ValueFrom.Default != nil && errors.IsCode(errors.CodeNotFound, err) {
inParam.Value = inParam.ValueFrom.Default
} else {
return nil, errors.Errorf(errors.CodeBadRequest, "unable to retrieve inputs.parameters.%s from ConfigMap: %s", inParam.Name, err)
}
} else {
inParam.Value = wfv1.AnyStringPtr(cmValue)
}
}
} else {
if inParam.Value == nil {
return nil, errors.Errorf(errors.CodeBadRequest, "inputs.parameters.%s was not supplied", inParam.Name)
}
}
newTmpl.Inputs.Parameters[i] = inParam
}
// Performs substitutions of input artifacts
artifacts := newTmpl.Inputs.Artifacts
for i, inArt := range artifacts {
argArt := args.GetArtifactByName(inArt.Name)
if !inArt.Optional && !inArt.HasLocationOrKey() {
// artifact must be supplied
if argArt == nil {
return nil, errors.Errorf(errors.CodeBadRequest, "inputs.artifacts.%s was not supplied", inArt.Name)
}
if (argArt.From == "" || argArt.FromExpression == "") && !argArt.HasLocationOrKey() && !validateOnly {
return nil, errors.Errorf(errors.CodeBadRequest, "inputs.artifacts.%s missing location information", inArt.Name)
}
}
if argArt != nil {
artifacts[i] = *argArt
artifacts[i].Path = inArt.Path
artifacts[i].Mode = inArt.Mode
artifacts[i].RecurseMode = inArt.RecurseMode
}
}
return SubstituteParams(newTmpl, globalParams, localParams)
}
// substituteConfigMapKeyRefParams check if ConfigMapKeyRef's key is a param and perform the substitution.
func substituteConfigMapKeyRefParam(in string, globalParams Parameters) (string, error) {
if strings.HasPrefix(in, "{{") && strings.HasSuffix(in, "}}") {
k := strings.TrimSuffix(strings.TrimPrefix(in, "{{"), "}}")
k = strings.Trim(k, " ")
v, ok := globalParams[k]
if !ok {
err := errors.InternalError(fmt.Sprintf("parameter %s not found", k))
log.WithError(err).Error()
return "", err
}
return v, nil
}
return in, nil
}
// SubstituteParams returns a new copy of the template with global, pod, and input parameters substituted
func SubstituteParams(tmpl *wfv1.Template, globalParams, localParams Parameters) (*wfv1.Template, error) {
tmplBytes, err := json.Marshal(tmpl)
if err != nil {
return nil, errors.InternalWrapError(err)
}
// First replace globals & locals, then replace inputs because globals could be referenced in the inputs
replaceMap := globalParams.Merge(localParams)
globalReplacedTmplStr, err := template.Replace(string(tmplBytes), replaceMap, true)
if err != nil {
return nil, err
}
var globalReplacedTmpl wfv1.Template
err = json.Unmarshal([]byte(globalReplacedTmplStr), &globalReplacedTmpl)
if err != nil {
return nil, errors.InternalWrapError(err)
}
// Now replace the rest of substitutions (the ones that can be made) in the template
replaceMap = make(map[string]string)
for _, inParam := range globalReplacedTmpl.Inputs.Parameters {
if inParam.Value == nil && inParam.ValueFrom == nil {
return nil, errors.InternalErrorf("inputs.parameters.%s had no value", inParam.Name)
} else if inParam.Value != nil {
replaceMap["inputs.parameters."+inParam.Name] = inParam.Value.String()
}
}
// allow {{inputs.parameters}} to fetch the entire input parameters list as JSON
jsonInputParametersBytes, err := json.Marshal(globalReplacedTmpl.Inputs.Parameters)
if err != nil {
return nil, errors.InternalWrapError(err)
}
replaceMap["inputs.parameters"] = string(jsonInputParametersBytes)
for _, inArt := range globalReplacedTmpl.Inputs.Artifacts {
if inArt.Path != "" {
replaceMap["inputs.artifacts."+inArt.Name+".path"] = inArt.Path
}
}
for _, outArt := range globalReplacedTmpl.Outputs.Artifacts {
if outArt.Path != "" {
replaceMap["outputs.artifacts."+outArt.Name+".path"] = outArt.Path
}
}
for _, param := range globalReplacedTmpl.Outputs.Parameters {
if param.ValueFrom != nil && param.ValueFrom.Path != "" {
replaceMap["outputs.parameters."+param.Name+".path"] = param.ValueFrom.Path
}
}
s, err := template.Replace(globalReplacedTmplStr, replaceMap, true)
if err != nil {
return nil, err
}
var newTmpl wfv1.Template
err = json.Unmarshal([]byte(s), &newTmpl)
if err != nil {
return nil, errors.InternalWrapError(err)
}
return &newTmpl, nil
}
// RunCommand is a convenience function to run/log a command and log the stderr upon failure
func RunCommand(name string, arg ...string) ([]byte, error) {
cmd := exec.Command(name, arg...)
cmdStr := strings.Join(cmd.Args, " ")
log.Info(cmdStr)
out, err := cmd.Output()
if err != nil {
if exErr, ok := err.(*exec.ExitError); ok {
errOutput := string(exErr.Stderr)
log.Errorf("`%s` failed: %s", cmdStr, errOutput)
return nil, errors.InternalError(strings.TrimSpace(errOutput))
}
return nil, errors.InternalWrapError(err)
}
return out, nil
}
// RunShellCommand is a convenience function to use RunCommand for shell executions. It's os-specific
// and runs `cmd` in windows.
func RunShellCommand(arg ...string) ([]byte, error) {
name := "sh"
shellFlag := "-c"
if runtime.GOOS == "windows" {
name = "cmd"
shellFlag = "/c"
}
arg = append([]string{shellFlag}, arg...)
return RunCommand(name, arg...)
}
const deleteRetries = 3
// DeletePod deletes a pod. Ignores NotFound error
func DeletePod(ctx context.Context, c kubernetes.Interface, podName, namespace string) error {
var err error
for attempt := 0; attempt < deleteRetries; attempt++ {
err = c.CoreV1().Pods(namespace).Delete(ctx, podName, metav1.DeleteOptions{})
if err == nil || apierr.IsNotFound(err) {
return nil
}
time.Sleep(100 * time.Millisecond)
}
return err
}
// GetTemplateGetterString returns string of TemplateHolder.
func GetTemplateGetterString(getter wfv1.TemplateHolder) string {
return fmt.Sprintf("%T (namespace=%s,name=%s)", getter, getter.GetNamespace(), getter.GetName())
}
// GetTemplateHolderString returns string of TemplateReferenceHolder.
func GetTemplateHolderString(tmplHolder wfv1.TemplateReferenceHolder) string {
if tmplHolder.GetTemplate() != nil {
return fmt.Sprintf("%T inlined", tmplHolder)
} else if x := tmplHolder.GetTemplateName(); x != "" {
return fmt.Sprintf("%T (%s)", tmplHolder, x)
} else if x := tmplHolder.GetTemplateRef(); x != nil {
return fmt.Sprintf("%T (%s/%s#%v)", tmplHolder, x.Name, x.Template, x.ClusterScope)
} else {
return fmt.Sprintf("%T invalid (https://argoproj.github.io/argo-workflows/templates/)", tmplHolder)
}
}
func GenerateOnExitNodeName(parentNodeName string) string {
return fmt.Sprintf("%s.onExit", parentNodeName)
}
func IsDone(un *unstructured.Unstructured) bool {
return un.GetDeletionTimestamp() == nil &&
un.GetLabels()[LabelKeyCompleted] == "true" &&
un.GetLabels()[LabelKeyWorkflowArchivingStatus] != "Pending"
}