@@ -4,104 +4,98 @@ import (
44 "encoding/json"
55 "fmt"
66 "os"
7- "time"
87
98 "github.com/Azure/azure-container-networking/cns/configuration"
10- "github.com/Azure/azure-container-networking/cns/logger "
9+ "go.uber.org/zap "
1110)
1211
13- // ConfigManager handles Azure CNS configuration loading and validation
12+ // ConfigManager handles CNS configuration loading
1413type ConfigManager struct {
1514 configPath string
15+ logger * zap.Logger
1616}
1717
18- // NewConfigManager creates a new configuration manager for Azure CNS
18+ // NewConfigManager creates a new config manager
1919func NewConfigManager (configPath string ) * ConfigManager {
2020 return & ConfigManager {
2121 configPath : configPath ,
2222 }
2323}
2424
25- // LoadConfig loads and validates the Azure CNS configuration
25+ // SetLogger sets the zap logger for the config manager
26+ func (cm * ConfigManager ) SetLogger (logger * zap.Logger ) {
27+ cm .logger = logger
28+ }
29+
30+ // LoadConfig loads the CNS configuration from file
2631func (cm * ConfigManager ) LoadConfig () (* configuration.CNSConfig , error ) {
27- logger .Printf ("Loading Azure CNS configuration from: %s" , cm .configPath )
32+ // Use zap logger if available, otherwise create a default config
33+ if cm .logger != nil {
34+ cm .logger .Debug ("Loading CNS configuration" , zap .String ("path" , cm .configPath ))
35+ }
2836
29- // Wait for configuration file to become available (Kubernetes ConfigMap mount)
30- if err := cm .waitForConfigFile (); err != nil {
31- return nil , fmt .Errorf ("Azure CNS configuration file not available: %w" , err )
37+ // Check if config file exists
38+ if _ , err := os .Stat (cm .configPath ); os .IsNotExist (err ) {
39+ if cm .logger != nil {
40+ cm .logger .Info ("CNS config file not found, using default configuration" ,
41+ zap .String ("path" , cm .configPath ))
42+ }
43+
44+ // Return default configuration
45+ return & configuration.CNSConfig {
46+ TelemetrySettings : configuration.TelemetrySettings {
47+ DisableAll : false ,
48+ TelemetryBatchSizeBytes : 16384 ,
49+ TelemetryBatchIntervalInSecs : 15 ,
50+ RefreshIntervalInSecs : 15 ,
51+ DisableMetadataRefreshThread : false ,
52+ DebugMode : false ,
53+ DisableTrace : false ,
54+ DisableMetric : false ,
55+ DisableEvent : false ,
56+ AppInsightsInstrumentationKey : "" , // Will be set by environment or config
57+ },
58+ }, nil
3259 }
3360
34- // Read the configuration file from mounted volume
61+ // Read the config file
3562 data , err := os .ReadFile (cm .configPath )
3663 if err != nil {
37- return nil , fmt .Errorf ("failed to read Azure CNS configuration file: %w" , err )
64+ if cm .logger != nil {
65+ cm .logger .Error ("Failed to read CNS config file" ,
66+ zap .String ("path" , cm .configPath ),
67+ zap .Error (err ))
68+ }
69+ return nil , fmt .Errorf ("failed to read config file %s: %w" , cm .configPath , err )
3870 }
3971
40- // Parse JSON configuration into CNS config structure
72+ // Parse the JSON configuration
4173 var config configuration.CNSConfig
4274 if err := json .Unmarshal (data , & config ); err != nil {
43- return nil , fmt .Errorf ("failed to parse Azure CNS configuration: %w" , err )
44- }
45-
46- // Validate configuration for Azure telemetry requirements
47- if err := cm .validateConfig (& config ); err != nil {
48- return nil , fmt .Errorf ("Azure CNS configuration validation failed: %w" , err )
49- }
50-
51- logger .Printf ("Azure CNS configuration loaded and validated successfully" )
52- return & config , nil
53- }
54-
55- // waitForConfigFile waits for the configuration file to become available
56- // This is important in Kubernetes environments where ConfigMaps are mounted asynchronously
57- func (cm * ConfigManager ) waitForConfigFile () error {
58- const maxRetries = 30
59- const retryInterval = 2 * time .Second
60-
61- for i := 0 ; i < maxRetries ; i ++ {
62- if _ , err := os .Stat (cm .configPath ); err == nil {
63- return nil
64- }
65-
66- if i == 0 {
67- logger .Printf ("Waiting for Azure CNS configuration file to become available..." )
75+ if cm .logger != nil {
76+ cm .logger .Error ("Failed to parse CNS config file" ,
77+ zap .String ("path" , cm .configPath ),
78+ zap .Error (err ))
6879 }
69-
70- time .Sleep (retryInterval )
80+ return nil , fmt .Errorf ("failed to parse config file: %w" , err )
7181 }
7282
73- return fmt .Errorf ("Azure CNS configuration file not available after %d attempts (%v total wait time)" ,
74- maxRetries , time .Duration (maxRetries )* retryInterval )
75- }
76-
77- // validateConfig performs Azure-specific validation of the CNS configuration
78- func (cm * ConfigManager ) validateConfig (config * configuration.CNSConfig ) error {
79- // Validate that telemetry settings are properly configured
80- if config .TelemetrySettings .AppInsightsInstrumentationKey == "" && ! config .TelemetrySettings .DisableAll {
81- logger .Printf ("Warning: No Application Insights instrumentation key configured and telemetry not disabled" )
83+ // Set default values for telemetry settings if not specified
84+ if config .TelemetrySettings .TelemetryBatchSizeBytes == 0 {
85+ config .TelemetrySettings .TelemetryBatchSizeBytes = 16384
8286 }
83-
84- // Validate batch size settings for Azure Application Insights
85- if config .TelemetrySettings .TelemetryBatchSizeBytes <= 0 {
86- logger .Printf ("Warning: Invalid telemetry batch size, using default" )
87+ if config .TelemetrySettings .TelemetryBatchIntervalInSecs == 0 {
88+ config .TelemetrySettings .TelemetryBatchIntervalInSecs = 15
8789 }
88-
89- // Validate batch interval for optimal Azure ingestion
90- if config .TelemetrySettings .TelemetryBatchIntervalInSecs <= 0 {
91- logger .Printf ("Warning: Invalid telemetry batch interval, using default" )
90+ if config .TelemetrySettings .RefreshIntervalInSecs == 0 {
91+ config .TelemetrySettings .RefreshIntervalInSecs = 15
9292 }
9393
94- // Log configuration summary for Azure monitoring and debugging
95- logger .Printf ("Azure CNS Configuration Summary:" )
96- logger .Printf (" - Telemetry DisableAll: %t" , config .TelemetrySettings .DisableAll )
97- logger .Printf (" - Application Insights Key Present: %t" ,
98- config .TelemetrySettings .AppInsightsInstrumentationKey != "" )
99- logger .Printf (" - Batch Size: %d bytes" , config .TelemetrySettings .TelemetryBatchSizeBytes )
100- logger .Printf (" - Batch Interval: %d seconds" , config .TelemetrySettings .TelemetryBatchIntervalInSecs )
101- logger .Printf (" - Disable Trace: %t" , config .TelemetrySettings .DisableTrace )
102- logger .Printf (" - Disable Metric: %t" , config .TelemetrySettings .DisableMetric )
103- logger .Printf (" - Disable Event: %t" , config .TelemetrySettings .DisableEvent )
104- logger .Printf (" - Debug Mode: %t" , config .TelemetrySettings .DebugMode )
94+ if cm .logger != nil {
95+ cm .logger .Info ("Successfully loaded CNS configuration" ,
96+ zap .String ("path" , cm .configPath ),
97+ zap .Bool ("telemetryDisabled" , config .TelemetrySettings .DisableAll ))
98+ }
10599
106- return nil
100+ return & config , nil
107101}
0 commit comments