-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.go
More file actions
114 lines (98 loc) · 2.76 KB
/
config.go
File metadata and controls
114 lines (98 loc) · 2.76 KB
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
package config
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"text/template"
"time"
"github.com/api7/api7-ingress-controller/internal/types"
"gopkg.in/yaml.v2"
)
var (
ControllerConfig = NewDefaultConfig()
)
func SetControllerConfig(cfg *Config) {
ControllerConfig = cfg
}
// NewDefaultConfig creates a Config object which fills all config items with
// default value.
func NewDefaultConfig() *Config {
return &Config{
LogLevel: DefaultLogLevel,
ControllerName: DefaultControllerName,
LeaderElectionID: DefaultLeaderElectionID,
ProbeAddr: DefaultProbeAddr,
MetricsAddr: DefaultMetricsAddr,
LeaderElection: NewLeaderElection(),
}
}
func NewLeaderElection() *LeaderElection {
return &LeaderElection{
LeaseDuration: types.TimeDuration{Duration: 15 * time.Second},
RenewDeadline: types.TimeDuration{Duration: 10 * time.Second},
RetryPeriod: types.TimeDuration{Duration: 2 * time.Second},
Disable: false,
}
}
// NewConfigFromFile creates a Config object and fills all config items according
// to the configuration file. The file can be in JSON/YAML format, which will be
// distinguished according to the file suffix.
func NewConfigFromFile(filename string) (*Config, error) {
cfg := NewDefaultConfig()
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
envVarMap := map[string]string{}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
envVarMap[pair[0]] = pair[1]
}
tpl := template.New("text").Option("missingkey=error")
tpl, err = tpl.Parse(string(data))
if err != nil {
return nil, fmt.Errorf("error parsing configuration template %v", err)
}
buf := bytes.NewBufferString("")
err = tpl.Execute(buf, envVarMap)
if err != nil {
return nil, fmt.Errorf("error execute configuration template %v", err)
}
if strings.HasSuffix(filename, ".yaml") || strings.HasSuffix(filename, ".yml") {
err = yaml.Unmarshal(buf.Bytes(), cfg)
} else {
err = json.Unmarshal(buf.Bytes(), cfg)
}
if err != nil {
return nil, err
}
return cfg, nil
}
func (c *Config) Validate() error {
if c.ControllerName == "" {
return fmt.Errorf("controller_name is required")
}
return nil
}
//nolint:unused
func (c *Config) validateGatewayConfig(gc *GatewayConfig) error {
if gc.Name == "" {
return fmt.Errorf("control_planesp[].gateway_name is required")
}
if gc.ControlPlane.AdminKey == "" {
return fmt.Errorf("control_planes[].admin_api.admin_key is required")
}
if len(gc.ControlPlane.Endpoints) == 0 {
return fmt.Errorf("control_planes[].admin_api.endpoints is required")
}
if gc.ControlPlane.TLSVerify == nil {
gc.ControlPlane.TLSVerify = new(bool)
*gc.ControlPlane.TLSVerify = true
}
return nil
}
func GetControllerName() string {
return ControllerConfig.ControllerName
}