-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
parse.go
169 lines (152 loc) · 4.85 KB
/
parse.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
package common
import (
"regexp"
"strings"
jsonpkg "github.com/argoproj/pkg/json"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
var yamlSeparator = regexp.MustCompile(`\n---`)
type ParseResult struct {
Object metav1.Object
Err error
}
func ParseObjects(body []byte, strict bool) []ParseResult {
res := []ParseResult{}
if jsonpkg.IsJSON(body) {
un := &unstructured.Unstructured{}
err := jsonpkg.Unmarshal(body, un)
if un.GetKind() != "" && err != nil {
// only return an error if this is a kubernetes object, otherwise, ignore
return append(res, ParseResult{nil, err})
}
v, err := toWorkflowTypeJSON(body, un.GetKind(), strict)
return append(res, ParseResult{v, err})
}
for _, text := range yamlSeparator.Split(string(body), -1) {
if strings.TrimSpace(text) == "" {
continue
}
un := &unstructured.Unstructured{}
err := yaml.Unmarshal([]byte(text), un)
if un.GetKind() != "" && err != nil {
// only return an error if this is a kubernetes object, otherwise, ignore
res = append(res, ParseResult{nil, err})
continue
}
v, err := toWorkflowTypeYAML([]byte(text), un.GetKind(), strict)
if v != nil {
// only append when this is a Kubernetes object
res = append(res, ParseResult{v, err})
}
}
return res
}
func objectForKind(kind string) metav1.Object {
switch kind {
case wf.CronWorkflowKind:
return &wfv1.CronWorkflow{}
case wf.ClusterWorkflowTemplateKind:
return &wfv1.ClusterWorkflowTemplate{}
case wf.WorkflowKind:
return &wfv1.Workflow{}
case wf.WorkflowEventBindingKind:
return &wfv1.WorkflowEventBinding{}
case wf.WorkflowTemplateKind:
return &wfv1.WorkflowTemplate{}
default:
return &metav1.ObjectMeta{}
}
}
func toWorkflowTypeYAML(body []byte, kind string, strict bool) (metav1.Object, error) {
var json []byte
var err error
if strict {
json, err = yaml.YAMLToJSONStrict(body)
} else {
json, err = yaml.YAMLToJSON(body)
}
if err != nil {
return nil, err
}
return toWorkflowTypeJSON(json, kind, strict)
}
func toWorkflowTypeJSON(body []byte, kind string, strict bool) (metav1.Object, error) {
v := objectForKind(kind)
if strict {
return v, jsonpkg.UnmarshalStrict(body, v)
}
return v, jsonpkg.Unmarshal(body, v)
}
// SplitWorkflowYAMLFile is a helper to split a body into multiple workflow objects
func SplitWorkflowYAMLFile(body []byte, strict bool) ([]wfv1.Workflow, error) {
manifests := make([]wfv1.Workflow, 0)
for _, res := range ParseObjects(body, strict) {
obj, err := res.Object, res.Err
v, ok := obj.(*wfv1.Workflow)
if !ok {
log.Warnf("%s is not of kind Workflow. Ignoring...", obj.GetName())
continue
}
if err != nil { // only returns parsing errors for workflow types
return nil, err
}
manifests = append(manifests, *v)
}
return manifests, nil
}
// SplitWorkflowTemplateYAMLFile is a helper to split a body into multiple workflow template objects
func SplitWorkflowTemplateYAMLFile(body []byte, strict bool) ([]wfv1.WorkflowTemplate, error) {
manifests := make([]wfv1.WorkflowTemplate, 0)
for _, res := range ParseObjects(body, strict) {
obj, err := res.Object, res.Err
v, ok := obj.(*wfv1.WorkflowTemplate)
if !ok {
log.Warnf("%s is not of kind WorkflowTemplate. Ignoring...", obj.GetName())
continue
}
if err != nil { // only returns parsing errors for template types
return nil, err
}
manifests = append(manifests, *v)
}
return manifests, nil
}
// SplitCronWorkflowYAMLFile is a helper to split a body into multiple workflow template objects
func SplitCronWorkflowYAMLFile(body []byte, strict bool) ([]wfv1.CronWorkflow, error) {
manifests := make([]wfv1.CronWorkflow, 0)
for _, res := range ParseObjects(body, strict) {
obj, err := res.Object, res.Err
v, ok := obj.(*wfv1.CronWorkflow)
if !ok {
log.Warnf("%s is not of kind CronWorkflow. Ignoring...", obj.GetName())
continue
}
if err != nil { // only returns parsing errors for cron types
return nil, err
}
manifests = append(manifests, *v)
}
return manifests, nil
}
// SplitClusterWorkflowTemplateYAMLFile is a helper to split a body into multiple cluster workflow template objects
func SplitClusterWorkflowTemplateYAMLFile(body []byte, strict bool) ([]wfv1.ClusterWorkflowTemplate, error) {
manifests := make([]wfv1.ClusterWorkflowTemplate, 0)
for _, res := range ParseObjects(body, strict) {
obj, err := res.Object, res.Err
v, ok := obj.(*wfv1.ClusterWorkflowTemplate)
if !ok {
log.Warnf("%s is not of kind ClusterWorkflowTemplate. Ignoring...", obj.GetName())
continue
}
if err != nil { // only returns parsing errors for cwft types
return nil, err
}
manifests = append(manifests, *v)
}
return manifests, nil
}