-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
parametrizable.go
55 lines (49 loc) · 1.11 KB
/
parametrizable.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
package intstr
import (
"fmt"
"strconv"
"strings"
"k8s.io/apimachinery/pkg/util/intstr"
)
// These are utility functions when using IntOrString to hold either an Int or an Argo Variable
func Int(is *intstr.IntOrString) (*int, error) {
if is == nil {
return nil, nil
}
if is.Type == intstr.String {
i, err := strconv.Atoi(is.StrVal)
if err != nil {
return nil, fmt.Errorf("value '%s' cannot be resolved to an int", is.StrVal)
}
return &i, nil
}
i := int(is.IntVal)
return &i, nil
}
func Int32(is *intstr.IntOrString) (*int32, error) {
v, err := Int(is)
if v == nil || err != nil {
return nil, err
}
i := int32(*v)
return &i, err
}
func Int64(is *intstr.IntOrString) (*int64, error) {
v, err := Int(is)
if v == nil || err != nil {
return nil, err
}
i := int64(*v)
return &i, err
}
func IsValidIntOrArgoVariable(is *intstr.IntOrString) bool {
if is == nil {
return true
} else if is.Type == intstr.Int {
return true
} else if _, err := strconv.Atoi(is.StrVal); err == nil {
return true
} else {
return strings.HasPrefix(is.StrVal, "{{") && strings.HasSuffix(is.StrVal, "}}")
}
}