forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_labels.go
75 lines (68 loc) · 2.93 KB
/
function_labels.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
package function
import (
"fmt"
"os"
"strings"
"knative.dev/kn-plugin-func/utils"
)
type Label struct {
// Key consist of optional prefix part (ended by '/') and name part
// Prefix part validation pattern: [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*
// Name part validation pattern: ([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]
Key *string `yaml:"key" jsonschema:"pattern=^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\\/)?([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$"`
Value *string `yaml:"value,omitempty" jsonschema:"pattern=^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$"`
}
func (l Label) String() string {
if l.Key != nil && l.Value == nil {
return fmt.Sprintf("Label with key \"%s\"", *l.Key)
} else if l.Key != nil && l.Value != nil {
match := regLocalEnv.FindStringSubmatch(*l.Value)
if len(match) == 2 {
return fmt.Sprintf("Label with key \"%s\" and value set from local env variable \"%s\"", *l.Key, match[1])
}
return fmt.Sprintf("Label with key \"%s\" and value \"%s\"", *l.Key, *l.Value)
}
return ""
}
// ValidateLabels checks that input labels are correct and contain all necessary fields.
// Returns array of error messages, empty if no errors are found
//
// Allowed settings:
// - key: EXAMPLE1 # label directly from a value
// value: value1
// - key: EXAMPLE2 # label from the local ENV var
// value: {{ env:MY_ENV }}
func ValidateLabels(labels []Label) (errors []string) {
for i, label := range labels {
if label.Key == nil && label.Value == nil {
errors = append(errors, fmt.Sprintf("label entry #%d is not properly set", i))
} else if label.Key == nil && label.Value != nil {
errors = append(errors, fmt.Sprintf("label entry #%d is missing key field, only value '%s' is set", i, *label.Value))
} else {
if err := utils.ValidateLabelKey(*label.Key); err != nil {
errors = append(errors, fmt.Sprintf("label entry #%d has invalid key set: %q; %s", i, *label.Key, err.Error()))
}
if label.Value != nil {
if err := utils.ValidateLabelValue(*label.Value); err != nil {
errors = append(errors, fmt.Sprintf("label entry #%d has invalid value set: %q; %s", i, *label.Value, err.Error()))
}
if strings.HasPrefix(*label.Value, "{{") {
// ENV from the local ENV var; {{ env:MY_ENV }}
if !regLocalEnv.MatchString(*label.Value) {
errors = append(errors,
fmt.Sprintf(
"label entry #%d with key '%s' has invalid value field set, it has '%s', but allowed is only '{{ env:MY_ENV }}'",
i, *label.Key, *label.Value))
} else {
match := regLocalEnv.FindStringSubmatch(*label.Value)
value := os.Getenv(match[1])
if err := utils.ValidateLabelValue(value); err != nil {
errors = append(errors, fmt.Sprintf("label entry #%d with key '%s' has invalid value when the environment is evaluated: '%s': %s", i, *label.Key, value, err.Error()))
}
}
}
}
}
}
return
}