-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
101 lines (92 loc) · 2.8 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
package main
import (
"encoding/json"
"fmt"
"time"
)
const (
defaultWGDeviceMTU = 1420
defaultWGListenPort = 51820
)
// Duration is a helper to unmarshal time.Duration from json
// https://stackoverflow.com/questions/48050945/how-to-unmarshal-json-into-durations/54571600#54571600
type Duration struct {
time.Duration
}
// MarshalJSON calls json Marshall on Duration
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// UnmarshalJSON provides handling of time.Duration when unmarshalling
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return err
}
d.Duration = tmp
return nil
default:
return fmt.Errorf("Invalid duration of type %v", value)
}
}
type localClusterConfig struct {
Name string `json:"name"`
KubeConfigPath string `json:"kubeConfigPath"`
}
type remoteClusterConfig struct {
Name string `json:"name"`
KubeConfigPath string `json:"kubeConfigPath"`
RemoteAPIURL string `json:"remoteAPIURL"`
RemoteCAURL string `json:"remoteCAURL"`
RemoteSATokenPath string `json:"remoteSATokenPath"`
WGDeviceMTU int `json:"wgDeviceMTU"`
WGListenPort int `json:"wgListenPort"`
PodSubnet string `json:"podSubnet"`
ResyncPeriod Duration `json:"resyncPeriod"`
}
// Config holds the application configuration
type Config struct {
Local localClusterConfig `json:"local"`
Remotes []*remoteClusterConfig `json:"remotes"`
}
func parseConfig(rawConfig []byte) (*Config, error) {
conf := &Config{}
if err := json.Unmarshal(rawConfig, conf); err != nil {
return nil, fmt.Errorf("error unmarshalling config: %v", err)
}
// Check for mandatory local config.
if conf.Local.Name == "" {
return nil, fmt.Errorf("Configuration is missing local cluster name")
}
if len(conf.Remotes) < 1 {
return nil, fmt.Errorf("No remote cluster configuration defined")
}
// Check for mandatory remote config.
for _, r := range conf.Remotes {
if r.Name == "" {
return nil, fmt.Errorf("Configuration is missing remote cluster name")
}
if (r.RemoteAPIURL == "" || r.RemoteCAURL == "" || r.RemoteSATokenPath == "") && r.KubeConfigPath == "" {
return nil, fmt.Errorf("Insufficient configuration to create remote cluster client. Set kubeConfigPath or remoteAPIURL and remoteCAURL and remoteSATokenPath")
}
if r.PodSubnet == "" {
return nil, fmt.Errorf("No pod subnet defined for remote cluster")
}
if r.WGDeviceMTU == 0 {
r.WGDeviceMTU = defaultWGDeviceMTU
}
if r.WGListenPort == 0 {
r.WGListenPort = defaultWGListenPort
}
}
return conf, nil
}