-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
packer.go
98 lines (86 loc) · 2.16 KB
/
packer.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
package packer
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/util/file"
)
const envVarName = "MAX_WORKFLOW_SIZE"
func getMaxWorkflowSize() int {
s, _ := strconv.Atoi(os.Getenv(envVarName))
if s == 0 {
s = 1024 * 1024
}
return s
}
func SetMaxWorkflowSize(s int) func() {
_ = os.Setenv(envVarName, strconv.Itoa(s))
return func() { _ = os.Unsetenv(envVarName) }
}
func DecompressWorkflow(wf *wfv1.Workflow) error {
if len(wf.Status.Nodes) == 0 && wf.Status.CompressedNodes != "" {
nodeContent, err := file.DecodeDecompressString(wf.Status.CompressedNodes)
if err != nil {
return err
}
err = json.Unmarshal([]byte(nodeContent), &wf.Status.Nodes)
wf.Status.CompressedNodes = ""
return err
}
return nil
}
// getSize return the entire workflow json string size
func getSize(wf *wfv1.Workflow) (int, error) {
nodeContent, err := json.Marshal(wf)
if err != nil {
return 0, err
}
return len(nodeContent), nil
}
func IsLargeWorkflow(wf *wfv1.Workflow) (bool, error) {
size, err := getSize(wf)
return size > getMaxWorkflowSize(), err
}
const tooLarge = "workflow is longer than maximum allowed size."
func IsTooLargeError(err error) bool {
return err != nil && strings.HasPrefix(err.Error(), tooLarge)
}
func CompressWorkflowIfNeeded(wf *wfv1.Workflow) error {
large, err := IsLargeWorkflow(wf)
if err != nil {
return err
}
if !large {
return nil
}
return compressWorkflow(wf)
}
func compressWorkflow(wf *wfv1.Workflow) error {
nodes := wf.Status.Nodes
nodeContent, err := json.Marshal(nodes)
if err != nil {
return err
}
wf.Status.CompressedNodes = file.CompressEncodeString(string(nodeContent))
wf.Status.Nodes = nil
// still too large?
large, err := IsLargeWorkflow(wf)
if err != nil {
wf.Status.CompressedNodes = ""
wf.Status.Nodes = nodes
return err
}
if large {
compressedSize, err := getSize(wf)
wf.Status.CompressedNodes = ""
wf.Status.Nodes = nodes
if err != nil {
return err
}
return fmt.Errorf("%s compressed size %d > maxSize %d", tooLarge, compressedSize, getMaxWorkflowSize())
}
return nil
}