-
Notifications
You must be signed in to change notification settings - Fork 55
/
metadata.go
63 lines (52 loc) · 1.73 KB
/
metadata.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
package payloads
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/jellydator/validation"
)
type BuildMetadata struct {
Annotations map[string]string `json:"annotations"`
Labels map[string]string `json:"labels"`
}
func (m BuildMetadata) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Annotations, validation.Empty),
validation.Field(&m.Labels, validation.Empty),
)
}
type Metadata struct {
Annotations map[string]string `json:"annotations" yaml:"annotations"`
Labels map[string]string `json:"labels" yaml:"labels"`
}
func (m Metadata) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Annotations, validation.Map().Keys(validation.By(cloudfoundryKeyCheck)).AllowExtraKeys()),
validation.Field(&m.Labels, validation.Map().Keys(validation.By(cloudfoundryKeyCheck)).AllowExtraKeys()),
)
}
type MetadataPatch struct {
Annotations map[string]*string `json:"annotations"`
Labels map[string]*string `json:"labels"`
}
func (p MetadataPatch) Validate() error {
return validation.ValidateStruct(&p,
validation.Field(&p.Annotations, validation.Map().Keys(validation.By(cloudfoundryKeyCheck)).AllowExtraKeys()),
validation.Field(&p.Labels, validation.Map().Keys(validation.By(cloudfoundryKeyCheck)).AllowExtraKeys()),
)
}
func cloudfoundryKeyCheck(key any) error {
keyStr, ok := key.(string)
if !ok {
return fmt.Errorf("expected string key, got %T", key)
}
u, err := url.ParseRequestURI("https://" + keyStr) // without the scheme, the hostname will be parsed as a path
if err != nil {
return nil
}
if strings.HasSuffix(u.Hostname(), "cloudfoundry.org") {
return errors.New("label/annotation key cannot use the cloudfoundry.org domain")
}
return nil
}