-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
129 lines (109 loc) · 2.83 KB
/
config.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package config
import (
"fmt"
"os"
"strings"
"github.com/go-playground/validator/v10"
"github.com/squarefactory/cloud-burster/logger"
"github.com/squarefactory/cloud-burster/validate"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
const APIVersion = "cloud-burster.squarefactory.io/v1alpha1"
// equalAPIVersion is a validator to check API version compatibility
func equalAPIVersion(fl validator.FieldLevel) bool {
return fl.Field().String() == APIVersion
}
func init() {
if err := validate.I.RegisterValidation("equalAPI", equalAPIVersion); err != nil {
logger.I.Fatal("couldn't register validation", zap.Any("validator", equalAPIVersion))
}
}
type Config struct {
APIVersion string `yaml:"apiVersion" validate:"equalAPI"`
Clouds []Cloud `yaml:"clouds" validate:"dive"`
SuffixSearch []string `yaml:"suffixSearch"`
}
func (c *Config) Validate() error {
return validate.I.Struct(c)
}
func (c *Config) SearchHostByHostName(hostname string) (*Host, *Cloud, error) {
var found bool
var foundCloud *Cloud
var foundHost *Host
// Search the corresponding cloud
for _, cloud := range c.Clouds {
// Search the corresponding host
for _, host := range cloud.Hosts {
if hostname == host.Name {
found = true
foundHost = &host
foundCloud = &cloud
break
}
}
if found {
break
}
// Not found yet, search the corresponding group host
for _, groupsHost := range cloud.GroupsHost {
hosts, err := groupsHost.GenerateHosts()
if err != nil {
return nil, nil, err
}
for _, host := range hosts {
if hostname == host.Name {
found = true
foundHost = &host
foundCloud = &cloud
break
}
}
if found {
break
}
}
if found {
break
}
}
return foundHost, foundCloud, nil
}
// GenerateHosts generates hosts for the DNS server
func (c *Config) GenerateHosts() (string, error) {
var sb strings.Builder
// Search in all clouds
for _, cloud := range c.Clouds {
logger.I.Debug("found cloud", zap.Any("cloud", cloud))
// Search in all hosts
for _, host := range cloud.Hosts {
logger.I.Debug("found host", zap.Any("host", host))
sb.WriteString(fmt.Sprintf("%s %s\n", host.IP, host.Name))
}
// Search in all groups host
for _, groupsHost := range cloud.GroupsHost {
logger.I.Debug("found groupsHost", zap.Any("groupsHost", groupsHost))
hosts, err := groupsHost.GenerateHosts()
if err != nil {
return "", err
}
for _, host := range hosts {
logger.I.Debug("found host", zap.Any("host", host))
sb.WriteString(fmt.Sprintf("%s %s\n", host.IP, host.Name))
}
}
}
return sb.String(), nil
}
func ParseFile(filePath string) (*Config, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
config := &Config{}
err = yaml.Unmarshal(data, config)
if err != nil {
return nil, err
}
return config, nil
}