/
validate_node_pool.go
49 lines (39 loc) · 1.52 KB
/
validate_node_pool.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
package manifest
import (
"fmt"
"github.com/go-playground/validator/v10"
)
// Validate validates the parsed data inside the NodePool section of the manifest.
// It checks for missing/invalid filled out values defined in the NodePool section of
// the manifest.
func (p *NodePool) Validate(m *Manifest) error {
// check for name uniqueness across node pools.
names := make(map[string]bool)
for _, n := range p.Dynamic {
if err := n.Validate(); err != nil {
return fmt.Errorf("failed to validate DynamicNodePool %q: %w", n.Name, err)
}
// check if the provider is defined in the manifest
if _, err := m.GetProvider(n.ProviderSpec.Name); err != nil {
return fmt.Errorf("provider %q specified for DynamicNodePool %q doesn't exists", n.ProviderSpec.Name, n.Name)
}
// check if the name is already used by a different node pool
if _, ok := names[n.Name]; ok {
return fmt.Errorf("name %q is used across multiple node pools, must be unique", n.Name)
}
names[n.Name] = true
}
for _, n := range p.Static {
if err := n.Validate(); err != nil {
return fmt.Errorf("failed to validate StaticNodePool %q: %w", n.Name, err)
}
// check if the name is already used by a different node pool
if _, ok := names[n.Name]; ok {
return fmt.Errorf("name %q is used across multiple node pools, must be unique", n.Name)
}
names[n.Name] = true
}
return nil
}
func (d *DynamicNodePool) Validate() error { return validator.New().Struct(d) }
func (s *StaticNodePool) Validate() error { return validator.New().Struct(s) }