generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
90 lines (77 loc) · 2.82 KB
/
types.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
package config
import (
"fmt"
"path"
"regexp"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
)
// NamingPolicy represents naming policies for Namespaces created from SubNamespaces.
type NamingPolicy struct {
Root string `json:"root"`
Match string `json:"match"`
}
type NamingPolicyRegexp struct {
Root *regexp.Regexp
Match string
}
// Config represents the configuration file of Accurate.
type Config struct {
LabelKeys []string `json:"labelKeys,omitempty"`
AnnotationKeys []string `json:"annotationKeys,omitempty"`
SubNamespaceLabelKeys []string `json:"subNamespaceLabelKeys,omitempty"`
SubNamespaceAnnotationKeys []string `json:"subNamespaceAnnotationKeys,omitempty"`
Watches []metav1.GroupVersionKind `json:"watches,omitempty"`
NamingPolicies []NamingPolicy `json:"namingPolicies,omitempty"`
NamingPolicyRegexps []NamingPolicyRegexp
}
// Validate validates the configurations.
func (c *Config) Validate(mapper meta.RESTMapper) error {
for _, key := range c.LabelKeys {
// Verify that pattern is a valid format.
if _, err := path.Match(key, ""); err != nil {
return fmt.Errorf("malformed pattern for labelKeys %s: %w", key, err)
}
}
for _, key := range c.AnnotationKeys {
// Verify that pattern is a valid format.
if _, err := path.Match(key, ""); err != nil {
return fmt.Errorf("malformed pattern for annotationKeys %s: %w", key, err)
}
}
for _, key := range c.SubNamespaceLabelKeys {
// Verify that pattern is a valid format.
if _, err := path.Match(key, ""); err != nil {
return fmt.Errorf("malformed pattern for subNamespaceLabelKeys %s: %w", key, err)
}
}
for _, key := range c.SubNamespaceAnnotationKeys {
// Verify that pattern is a valid format.
if _, err := path.Match(key, ""); err != nil {
return fmt.Errorf("malformed pattern for subNamespaceAnnotationKeys %s: %w", key, err)
}
}
for _, gvk := range c.Watches {
mapping, err := mapper.RESTMapping(schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}, gvk.Version)
if err != nil {
return fmt.Errorf("invalid gvk %s: %w", gvk.String(), err)
}
if mapping.Scope.Name() != meta.RESTScopeNameNamespace {
return fmt.Errorf("%s is not namespace-scoped", gvk.String())
}
}
for _, policy := range c.NamingPolicies {
root, err := regexp.Compile(policy.Root)
if err != nil {
return fmt.Errorf("invalid naming policy: %w", err)
}
c.NamingPolicyRegexps = append(c.NamingPolicyRegexps, NamingPolicyRegexp{Root: root, Match: policy.Match})
}
return nil
}
// Load loads configurations.
func (c *Config) Load(data []byte) error {
return yaml.Unmarshal(data, c, yaml.DisallowUnknownFields)
}