-
Notifications
You must be signed in to change notification settings - Fork 72
/
config.go
71 lines (56 loc) · 1.72 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
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Config struct {
CniPluginDir string `json:"cni_plugin_dir"`
CniConfigDir string `json:"cni_config_dir"`
BindMountDir string `json:"bind_mount_dir"`
OverlayNetwork string `json:"overlay_network"`
StateFilePath string `json:"state_file"`
StartPort int `json:"start_port"`
TotalPorts int `json:"total_ports"`
IPTablesLockFile string `json:"iptables_lock_file"`
}
func New(configFilePath string) (Config, error) {
cfg := Config{}
if _, err := os.Stat(configFilePath); err != nil {
return cfg, fmt.Errorf("file does not exist: %s", err)
}
configBytes, err := ioutil.ReadFile(configFilePath)
if err != nil {
return cfg, fmt.Errorf("reading config file: %s", err)
}
err = json.Unmarshal(configBytes, &cfg)
if err != nil {
return cfg, fmt.Errorf("parsing config (%s): %s", configFilePath, err)
}
if cfg.CniPluginDir == "" {
return cfg, fmt.Errorf("missing required config 'cni_plugin_dir'")
}
if cfg.CniConfigDir == "" {
return cfg, fmt.Errorf("missing required config 'cni_config_dir'")
}
if cfg.BindMountDir == "" {
return cfg, fmt.Errorf("missing required config 'bind_mount_dir'")
}
if cfg.OverlayNetwork == "" {
return cfg, fmt.Errorf("missing required config 'overlay_network'")
}
if cfg.StateFilePath == "" {
return cfg, fmt.Errorf("missing required config 'state_file'")
}
if cfg.StartPort == 0 {
return cfg, fmt.Errorf("missing required config 'start_port'")
}
if cfg.TotalPorts == 0 {
return cfg, fmt.Errorf("missing required config 'total_ports'")
}
if cfg.IPTablesLockFile == "" {
return cfg, fmt.Errorf("missing required config 'iptables_lock_file'")
}
return cfg, nil
}