-
Notifications
You must be signed in to change notification settings - Fork 241
/
configuration.go
222 lines (199 loc) · 6.75 KB
/
configuration.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright Microsoft. All rights reserved.
package configuration
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/common"
"github.com/pkg/errors"
)
type SWIFTV2Mode string
const (
// EnvCNSConfig is the CNS_CONFIGURATION_PATH env var key
EnvCNSConfig = "CNS_CONFIGURATION_PATH"
defaultConfigName = "cns_config.json"
// Service Fabric SWIFTV2 mode
SFSWIFTV2 SWIFTV2Mode = "SFSWIFTV2"
// K8s SWIFTV2 mode
K8sSWIFTV2 SWIFTV2Mode = "K8sSWIFTV2"
)
type CNSConfig struct {
ChannelMode string
EnablePprof bool
EnableSubnetScarcity bool
EnableSwiftV2 bool
SWIFTV2Mode SWIFTV2Mode
InitializeFromCNI bool
ManagedSettings ManagedSettings
MetricsBindAddress string
SyncHostNCTimeoutMs int
SyncHostNCVersionIntervalMs int
TLSCertificatePath string
TLSEndpoint string
TLSPort string
TLSSubjectName string
TelemetrySettings TelemetrySettings
UseHTTPS bool
WireserverIP string
KeyVaultSettings KeyVaultSettings
MSISettings MSISettings
ProgramSNATIPTables bool
ManageEndpointState bool
CNIConflistScenario string
EnableCNIConflistGeneration bool
CNIConflistFilepath string
MellanoxMonitorIntervalSecs int
AZRSettings AZRSettings
WatchPods bool
EnableAsyncPodDelete bool
AsyncPodDeletePath string
}
type TelemetrySettings struct {
// Flag to disable the telemetry.
DisableAll bool
// Flag to Disable sending trace.
DisableTrace bool
// Flag to Disable sending metric.
DisableMetric bool
// Flag to Disable sending events.
DisableEvent bool
// Configure how many bytes can be sent in one call to the data collector
TelemetryBatchSizeBytes int
// Configure the maximum delay before sending queued telemetry in milliseconds
TelemetryBatchIntervalInSecs int
// Heartbeat interval for sending heartbeat metric
HeartBeatIntervalInMins int
// Enable thread for getting metadata from wireserver
DisableMetadataRefreshThread bool
// Refresh interval in milliseconds for metadata thread
RefreshIntervalInSecs int
// Disable debug logging for telemetry messages
DebugMode bool
// Interval for sending snapshot events.
SnapshotIntervalInMins int
// AppInsightsInstrumentationKey allows the user to override the default appinsights ikey
AppInsightsInstrumentationKey string
}
type ManagedSettings struct {
PrivateEndpoint string
InfrastructureNetworkID string
NodeID string
NodeSyncIntervalInSeconds int
}
type AZRSettings struct {
EnableAZR bool
PopulateHomeAzCacheRetryIntervalSecs int
}
type MSISettings struct {
ResourceID string
}
type KeyVaultSettings struct {
URL string
CertificateName string
RefreshIntervalInHrs int
}
func getConfigFilePath(cmdPath string) (string, error) {
// If config path is set from cmd line, return that.
if strings.TrimSpace(cmdPath) != "" {
return cmdPath, nil
}
// If config path is set from env, return that.
if envPath := os.Getenv(EnvCNSConfig); strings.TrimSpace(envPath) != "" {
return envPath, nil
}
// otherwise compose the default config path and return that.
dir, err := common.GetExecutableDirectory()
if err != nil {
return "", errors.Wrap(err, "failed to discover exec dir for config")
}
defaultPath := filepath.Join(dir, defaultConfigName)
return defaultPath, nil
}
// ReadConfig returns a CNS config from file or an error.
func ReadConfig(cmdLineConfigPath string) (*CNSConfig, error) {
configpath, err := getConfigFilePath(cmdLineConfigPath)
if err != nil {
return nil, err
}
logger.Printf("[Configuration] Using config path: %s", configpath)
return readConfigFromFile(configpath)
}
// readConfigFromFile attempts to read the file and unmarshal it in to a CNSConfig.
func readConfigFromFile(f string) (*CNSConfig, error) {
content, err := os.ReadFile(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to read config file %s", f)
}
var config CNSConfig
if err := json.Unmarshal(content, &config); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal config")
}
return &config, nil
}
// set telmetry setting defaults
func setTelemetrySettingDefaults(telemetrySettings *TelemetrySettings) {
if telemetrySettings.RefreshIntervalInSecs == 0 {
// set the default refresh interval of metadata thread to 15 seconds
telemetrySettings.RefreshIntervalInSecs = 15
}
if telemetrySettings.TelemetryBatchIntervalInSecs == 0 {
// set the default AI telemetry batch interval to 30 seconds
telemetrySettings.TelemetryBatchIntervalInSecs = 30
}
if telemetrySettings.TelemetryBatchSizeBytes == 0 {
// set the default AI telemetry batch size to 32768 bytes
telemetrySettings.TelemetryBatchSizeBytes = 32768
}
if telemetrySettings.HeartBeatIntervalInMins == 0 {
// set the default Heartbeat interval to 30 minutes
telemetrySettings.HeartBeatIntervalInMins = 30
}
if telemetrySettings.SnapshotIntervalInMins == 0 {
telemetrySettings.SnapshotIntervalInMins = 60
}
}
// set managed setting defaults
func setManagedSettingDefaults(managedSettings *ManagedSettings) {
if managedSettings.NodeSyncIntervalInSeconds == 0 {
managedSettings.NodeSyncIntervalInSeconds = 30
}
}
func setAZRSettingsDefaults(azrSettings *AZRSettings) {
if azrSettings.PopulateHomeAzCacheRetryIntervalSecs == 0 {
// set the default PopulateHomeAzCache retry interval to 60 seconds
azrSettings.PopulateHomeAzCacheRetryIntervalSecs = 60
}
}
func setKeyVaultSettingsDefaults(kvs *KeyVaultSettings) {
if kvs.RefreshIntervalInHrs == 0 {
kvs.RefreshIntervalInHrs = 12 //nolint:gomnd // default times
}
}
// SetCNSConfigDefaults set default values of CNS config if not specified
func SetCNSConfigDefaults(config *CNSConfig) {
setTelemetrySettingDefaults(&config.TelemetrySettings)
setManagedSettingDefaults(&config.ManagedSettings)
setKeyVaultSettingsDefaults(&config.KeyVaultSettings)
setAZRSettingsDefaults(&config.AZRSettings)
if config.ChannelMode == "" {
config.ChannelMode = cns.Direct
}
if config.MetricsBindAddress == "" {
config.MetricsBindAddress = ":9090"
}
if config.SyncHostNCVersionIntervalMs == 0 {
config.SyncHostNCVersionIntervalMs = 1000 //nolint:gomnd // default times
}
if config.SyncHostNCTimeoutMs == 0 {
config.SyncHostNCTimeoutMs = 500 //nolint:gomnd // default times
}
if config.WireserverIP == "" {
config.WireserverIP = "168.63.129.16"
}
if config.AsyncPodDeletePath == "" {
config.AsyncPodDeletePath = "/var/run/azure-vnet/deleteIDs"
}
}