This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
utils.go
155 lines (125 loc) · 3.61 KB
/
utils.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
package k8s
import (
"math"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/golang/protobuf/ptypes"
)
func refInt(i int) *int {
return &i
}
func refStr(s string) *string {
return &s
}
func computeRetryStrategy(n *core.Node, t *core.TaskTemplate) *v1alpha1.RetryStrategy {
if n.GetMetadata() != nil && n.GetMetadata().GetRetries() != nil {
return &v1alpha1.RetryStrategy{
MinAttempts: refInt(int(n.GetMetadata().GetRetries().Retries + 1)),
}
}
if t != nil && t.GetMetadata() != nil && t.GetMetadata().GetRetries() != nil {
return &v1alpha1.RetryStrategy{
MinAttempts: refInt(int(t.GetMetadata().GetRetries().Retries + 1)),
}
}
return nil
}
func computeDeadline(n *core.Node) (*v1.Duration, error) {
var deadline *v1.Duration
if n.GetMetadata() != nil && n.GetMetadata().GetTimeout() != nil {
duration, err := ptypes.Duration(n.GetMetadata().GetTimeout())
if err != nil {
return nil, err
}
deadline = &v1.Duration{
Duration: duration,
}
}
return deadline, nil
}
func getResources(task *core.TaskTemplate) *core.Resources {
if task == nil {
return nil
}
if task.GetContainer() == nil {
return nil
}
return task.GetContainer().Resources
}
func toAliasValueArray(aliases []*core.Alias) []v1alpha1.Alias {
if aliases == nil {
return nil
}
res := make([]v1alpha1.Alias, 0, len(aliases))
for _, alias := range aliases {
res = append(res, v1alpha1.Alias{Alias: *alias})
}
return res
}
func toBindingValueArray(bindings []*core.Binding) []*v1alpha1.Binding {
if bindings == nil {
return nil
}
res := make([]*v1alpha1.Binding, 0, len(bindings))
for _, binding := range bindings {
res = append(res, &v1alpha1.Binding{Binding: binding})
}
return res
}
func minInt(i, j int) int {
return int(math.Min(float64(i), float64(j)))
}
// StripTypeMetadata strips the type metadata from the given type.
func StripTypeMetadata(t *core.LiteralType) *core.LiteralType {
if t == nil {
return nil
}
c := *t
c.Metadata = nil
c.Structure = nil
c.Annotation = nil
switch underlyingType := c.Type.(type) {
case *core.LiteralType_UnionType:
variants := make([]*core.LiteralType, 0, len(c.GetUnionType().Variants))
for _, variant := range c.GetUnionType().Variants {
variants = append(variants, StripTypeMetadata(variant))
}
underlyingType.UnionType.Variants = variants
case *core.LiteralType_MapValueType:
underlyingType.MapValueType = StripTypeMetadata(c.GetMapValueType())
case *core.LiteralType_CollectionType:
underlyingType.CollectionType = StripTypeMetadata(c.GetCollectionType())
case *core.LiteralType_StructuredDatasetType:
columns := make([]*core.StructuredDatasetType_DatasetColumn, 0, len(c.GetStructuredDatasetType().Columns))
for _, column := range c.GetStructuredDatasetType().Columns {
columns = append(columns, &core.StructuredDatasetType_DatasetColumn{
Name: column.Name,
LiteralType: StripTypeMetadata(column.LiteralType),
})
}
underlyingType.StructuredDatasetType.Columns = columns
}
return &c
}
func StripInterfaceTypeMetadata(iface *core.TypedInterface) *core.TypedInterface {
if iface == nil {
return nil
}
newIface := *iface
if iface.Inputs != nil {
for name, i := range iface.Inputs.Variables {
i.Type = StripTypeMetadata(i.Type)
i.Description = ""
newIface.Inputs.Variables[name] = i
}
}
if iface.Outputs != nil {
for name, i := range iface.Outputs.Variables {
i.Type = StripTypeMetadata(i.Type)
i.Description = ""
iface.Outputs.Variables[name] = i
}
}
return &newIface
}