Skip to content

Commit 8cf84d3

Browse files
committed
chaning sidecar code
1 parent a949253 commit 8cf84d3

File tree

6 files changed

+351
-227
lines changed

6 files changed

+351
-227
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ azure-vnet-telemetry-binary:
220220

221221
# Build the Azure CNI Telemetry Sidecar binary.
222222
cni-telemetry-sidecar-binary:
223-
cd $(CNI_TELEMETRY_SIDECAR_DIR) && CGO_ENABLED=0 go build -v -o $(CNI_TELEMETRY_SIDECAR_BUILD_DIR)/azure-cni-telemetry-sidecar$(EXE_EXT) -ldflags "-X main.version=$(CNI_TELEMETRY_SIDECAR_VERSION) -X $(CNI_AI_PATH)=$(CNI_TELEMETRY_SIDECAR_AI_ID)" -gcflags="-dwarflocationlists=true"
223+
$(MKDIR) $(CNI_TELEMETRY_SIDECAR_BUILD_DIR)
224+
cd $(CNI_TELEMETRY_SIDECAR_DIR) && CGO_ENABLED=0 go build -v -o $(CNI_TELEMETRY_SIDECAR_BUILD_DIR)/azure-cni-telemetry-sidecar$(EXE_EXT) -ldflags "-X main.version=$(CNI_TELEMETRY_SIDECAR_VERSION) -X main.aiKey=$(CNI_TELEMETRY_SIDECAR_AI_ID)" -gcflags="-dwarflocationlists=true"
224225

225226
# Build the Azure CLI network binary.
226227
acncli-binary:
@@ -564,7 +565,7 @@ cni-telemetry-sidecar-image: ## build cni-telemetry-sidecar container image.
564565
$(MAKE) container \
565566
DOCKERFILE=cns/cni-telemetry-sidecar/Dockerfile \
566567
IMAGE=$(CNI_TELEMETRY_SIDECAR_IMAGE) \
567-
EXTRA_BUILD_ARGS='--build-arg CNI_AI_PATH=$(CNI_AI_PATH) --build-arg CNI_AI_ID=$(CNI_TELEMETRY_SIDECAR_AI_ID)' \
568+
EXTRA_BUILD_ARGS='--build-arg CNI_AI_PATH=$(CNI_AI_PATH) --build-arg CNI_AI_ID=$(CNI_TELEMETRY_SIDECAR_AI_ID) --build-arg VERSION=$(CNI_TELEMETRY_SIDECAR_VERSION)' \
568569
PLATFORM=$(PLATFORM) \
569570
TAG=$(CNI_TELEMETRY_SIDECAR_PLATFORM_TAG) \
570571
TARGET=$(OS) \
Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
# Azure CNI Telemetry Sidecar Dockerfile
2-
# Based on proven CNI Dockerfile pattern from azure-container-networking
31
ARG ARCH
42
ARG OS_VERSION
53
ARG OS
64

7-
# Use the same proven base images as CNI
8-
# mcr.microsoft.com/oss/go/microsoft/golang:1.23-azurelinux3.0
5+
# Base Go image
96
FROM --platform=linux/${ARCH} mcr.microsoft.com/oss/go/microsoft/golang@sha256:8f60e85f4b2f567c888d0b3a4cd12dc74bee534d94c528655546452912d90c74 AS go
107

11-
# mcr.microsoft.com/azurelinux/base/core:3.0
12-
FROM --platform=linux/${ARCH} mcr.microsoft.com/azurelinux/base/core@sha256:9948138108a3d69f1dae62104599ac03132225c3b7a5ac57b85a214629c8567d AS mariner-core
13-
14-
FROM go AS azure-cni-telemetry-sidecar
8+
FROM go AS cni-telemetry-sidecar
159
ARG OS
1610
ARG VERSION
1711
ARG CNI_AI_PATH
@@ -20,40 +14,21 @@ ARG CNI_AI_ID
2014
WORKDIR /azure-container-networking
2115
COPY . .
2216

23-
# Debug: Check if the source file exists and show Go version
24-
RUN ls -la cns/cni-telemetry-sidecar/ && go version
25-
26-
# Build the Azure CNI telemetry sidecar binary for Linux
27-
RUN CGO_ENABLED=0 GOOS=linux go build \
28-
-a \
29-
-o /go/bin/azure-cni-telemetry-sidecar \
30-
-trimpath \
31-
-ldflags "-X main.version=${VERSION:-unknown} -X ${CNI_AI_PATH:-main.aiMetadata}=${CNI_AI_ID:-unknown}" \
32-
-gcflags="-dwarflocationlists=true" \
33-
./cns/cni-telemetry-sidecar/
34-
35-
# Build the Azure CNI telemetry sidecar binary for Windows
36-
RUN CGO_ENABLED=0 GOOS=windows go build \
37-
-a \
38-
-o /go/bin/azure-cni-telemetry-sidecar.exe \
17+
# Build the telemetry sidecar with embedded AppInsights key (same approach as azure-vnet-telemetry)
18+
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/azure-cni-telemetry-sidecar \
3919
-trimpath \
40-
-ldflags "-X main.version=${VERSION:-unknown} -X ${CNI_AI_PATH:-main.aiMetadata}=${CNI_AI_ID:-unknown}" \
20+
-ldflags "-X main.version=$VERSION -X main.aiKey=$CNI_AI_ID" \
4121
-gcflags="-dwarflocationlists=true" \
42-
./cns/cni-telemetry-sidecar/
43-
44-
# Verify both binaries were built
45-
RUN ls -la /go/bin/azure-cni-telemetry-sidecar*
22+
./cns/cni-telemetry-sidecar
4623

4724
FROM scratch AS bins
48-
COPY --from=azure-cni-telemetry-sidecar /go/bin/* /
25+
COPY --from=cni-telemetry-sidecar /go/bin/azure-cni-telemetry-sidecar /
4926

50-
FROM scratch AS linux
51-
COPY --from=azure-cni-telemetry-sidecar /go/bin/azure-cni-telemetry-sidecar /azure-cni-telemetry-sidecar
52-
ENTRYPOINT [ "/azure-cni-telemetry-sidecar" ]
27+
FROM mcr.microsoft.com/azurelinux/base/core:3.0 AS linux
28+
COPY --from=cni-telemetry-sidecar /go/bin/azure-cni-telemetry-sidecar /usr/local/bin/
5329

54-
# Windows support following CNI pattern
55-
FROM --platform=windows/${ARCH} mcr.microsoft.com/oss/kubernetes/windows-host-process-containers-base-image@sha256:b4c9637e032f667c52d1eccfa31ad8c63f1b035e8639f3f48a510536bf34032b AS hpc
30+
# Create directories for logs and config
31+
RUN mkdir -p /var/log /etc/azure-cns /var/run
5632

57-
FROM hpc AS windows
58-
COPY --from=azure-cni-telemetry-sidecar /go/bin/azure-cni-telemetry-sidecar.exe /azure-cni-telemetry-sidecar.exe
59-
ENTRYPOINT [ "/azure-cni-telemetry-sidecar.exe" ]
33+
ENTRYPOINT ["/usr/local/bin/azure-cni-telemetry-sidecar"]
34+
CMD ["--config=/etc/azure-cns/cns_config.json", "--log-level=info"]

cns/cni-telemetry-sidecar/configmanager.go

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func (cm *ConfigManager) SetLogger(logger *zap.Logger) {
2929

3030
// LoadConfig loads the CNS configuration from file
3131
func (cm *ConfigManager) LoadConfig() (*configuration.CNSConfig, error) {
32-
// Use zap logger if available, otherwise create a default config
3332
if cm.logger != nil {
3433
cm.logger.Debug("Loading CNS configuration", zap.String("path", cm.configPath))
3534
}
@@ -40,22 +39,7 @@ func (cm *ConfigManager) LoadConfig() (*configuration.CNSConfig, error) {
4039
cm.logger.Info("CNS config file not found, using default configuration",
4140
zap.String("path", cm.configPath))
4241
}
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
42+
return cm.createDefaultConfig(), nil
5943
}
6044

6145
// Read the config file
@@ -80,22 +64,96 @@ func (cm *ConfigManager) LoadConfig() (*configuration.CNSConfig, error) {
8064
return nil, fmt.Errorf("failed to parse config file: %w", err)
8165
}
8266

67+
// Apply defaults and environment variable overrides
68+
cm.setConfigDefaults(&config)
69+
70+
if cm.logger != nil {
71+
cm.logger.Info("Successfully loaded CNS configuration",
72+
zap.String("path", cm.configPath),
73+
zap.Bool("telemetryDisabled", config.TelemetrySettings.DisableAll),
74+
zap.Bool("cniTelemetryEnabled", config.TelemetrySettings.EnableCNITelemetry),
75+
zap.String("socketPath", config.TelemetrySettings.CNITelemetrySocketPath),
76+
zap.Bool("hasAppInsightsKey", config.TelemetrySettings.AppInsightsInstrumentationKey != ""))
77+
}
78+
79+
return &config, nil
80+
}
81+
82+
// createDefaultConfig creates a default configuration
83+
func (cm *ConfigManager) createDefaultConfig() *configuration.CNSConfig {
84+
config := &configuration.CNSConfig{
85+
TelemetrySettings: configuration.TelemetrySettings{
86+
DisableAll: false,
87+
TelemetryBatchSizeBytes: defaultBatchSizeInBytes,
88+
TelemetryBatchIntervalInSecs: defaultBatchIntervalInSecs,
89+
RefreshIntervalInSecs: defaultRefreshTimeoutInSecs,
90+
DisableMetadataRefreshThread: false,
91+
DebugMode: false,
92+
DisableTrace: false,
93+
DisableMetric: false,
94+
DisableEvent: false,
95+
EnableCNITelemetry: false, // Default to false
96+
CNITelemetrySocketPath: "/var/run/azure-vnet-telemetry.sock",
97+
},
98+
}
99+
100+
// Set AppInsights key from environment variables
101+
cm.setAppInsightsKey(&config.TelemetrySettings)
102+
103+
return config
104+
}
105+
106+
// setConfigDefaults applies default values and environment variable overrides
107+
func (cm *ConfigManager) setConfigDefaults(config *configuration.CNSConfig) {
83108
// Set default values for telemetry settings if not specified
84109
if config.TelemetrySettings.TelemetryBatchSizeBytes == 0 {
85-
config.TelemetrySettings.TelemetryBatchSizeBytes = 16384
110+
config.TelemetrySettings.TelemetryBatchSizeBytes = defaultBatchSizeInBytes
86111
}
87112
if config.TelemetrySettings.TelemetryBatchIntervalInSecs == 0 {
88-
config.TelemetrySettings.TelemetryBatchIntervalInSecs = 15
113+
config.TelemetrySettings.TelemetryBatchIntervalInSecs = defaultBatchIntervalInSecs
89114
}
90115
if config.TelemetrySettings.RefreshIntervalInSecs == 0 {
91-
config.TelemetrySettings.RefreshIntervalInSecs = 15
116+
config.TelemetrySettings.RefreshIntervalInSecs = defaultRefreshTimeoutInSecs
92117
}
93118

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))
119+
// Set default CNI telemetry socket path
120+
if config.TelemetrySettings.CNITelemetrySocketPath == "" {
121+
config.TelemetrySettings.CNITelemetrySocketPath = "/var/run/azure-vnet-telemetry.sock"
98122
}
99123

100-
return &config, nil
124+
// Handle AppInsights instrumentation key from environment variables
125+
cm.setAppInsightsKey(&config.TelemetrySettings)
126+
}
127+
128+
// setAppInsightsKey sets the AppInsights instrumentation key from environment variables
129+
func (cm *ConfigManager) setAppInsightsKey(ts *configuration.TelemetrySettings) {
130+
// Try multiple environment variable names
131+
envKeys := []string{
132+
"APPINSIGHTS_INSTRUMENTATIONKEY",
133+
"APPLICATIONINSIGHTS_CONNECTION_STRING",
134+
"AI_INSTRUMENTATION_KEY",
135+
}
136+
137+
// If no key is set in config, try environment variables
138+
if ts.AppInsightsInstrumentationKey == "" {
139+
for _, envKey := range envKeys {
140+
if key := os.Getenv(envKey); key != "" {
141+
ts.AppInsightsInstrumentationKey = key
142+
if cm.logger != nil {
143+
cm.logger.Info("Using AppInsights key from environment variable",
144+
zap.String("envVar", envKey))
145+
}
146+
break
147+
}
148+
}
149+
}
150+
151+
// Log the status
152+
if cm.logger != nil {
153+
if ts.AppInsightsInstrumentationKey != "" {
154+
cm.logger.Info("AppInsights instrumentation key configured for CNI telemetry")
155+
} else {
156+
cm.logger.Warn("No AppInsights instrumentation key found - telemetry will work locally but not send to Azure")
157+
}
158+
}
101159
}

cns/cni-telemetry-sidecar/main.go

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,106 @@ package main
33
import (
44
"context"
55
"flag"
6+
"fmt"
67
"os"
78
"os/signal"
89
"syscall"
910

10-
"github.com/Azure/azure-container-networking/cns/logger/v2"
11-
cores "github.com/Azure/azure-container-networking/cns/logger/v2/cores"
1211
"go.uber.org/zap"
12+
"go.uber.org/zap/zapcore"
1313
)
1414

1515
var (
16-
version = "unknown"
17-
configPath = flag.String("config", "/etc/cns/cns-config.json", "Path to CNS configuration file")
16+
configPath = flag.String("config", "/etc/azure-cns/cns_config.json", "Path to CNS configuration file")
1817
logLevel = flag.String("log-level", "info", "Log level (debug, info, warn, error)")
18+
19+
// These variables are set at build time via ldflags from Makefile
20+
version = "1.0.0" // -X main.version=$(CNI_TELEMETRY_SIDECAR_VERSION)
21+
aiKey = "" // -X main.aiKey=$(CNI_TELEMETRY_SIDECAR_AI_ID)
1922
)
2023

2124
func main() {
2225
flag.Parse()
2326

24-
// Initialize main logger with correct path for shared volume
25-
zapLogger, cleanup, err := logger.New(&logger.Config{
26-
Level: *logLevel,
27-
File: &cores.FileConfig{
28-
Filepath: "/var/log/azure-cni-telemetry-sidecar.log", // This will write to host's /var/log/azure-cns/
29-
},
30-
})
27+
// Initialize logger
28+
logger, err := initializeLogger(*logLevel)
3129
if err != nil {
32-
panic("Failed to initialize logger: " + err.Error())
30+
fmt.Printf("Failed to initialize logger: %v\n", err)
31+
os.Exit(1)
3332
}
34-
defer cleanup()
33+
defer logger.Sync()
3534

36-
zapLogger.Info("Starting Azure CNI Telemetry Sidecar",
35+
logger.Info("Starting Azure CNI Telemetry Sidecar",
3736
zap.String("version", version),
3837
zap.String("configPath", *configPath),
39-
zap.String("logLevel", *logLevel))
38+
zap.String("logLevel", *logLevel),
39+
zap.Bool("hasBuiltInAIKey", aiKey != ""),
40+
zap.String("aiKeyPrefix", maskAIKey(aiKey)))
4041

41-
// Create telemetry sidecar service and pass the logger
42+
// Create telemetry sidecar
4243
sidecar := NewTelemetrySidecar(*configPath)
44+
if err := sidecar.SetLogger(logger); err != nil {
45+
logger.Fatal("Failed to set logger", zap.Error(err))
46+
}
4347

44-
// Set the logger for the sidecar to avoid nil pointer
45-
if err := sidecar.SetLogger(zapLogger); err != nil {
46-
zapLogger.Error("Failed to set logger for sidecar", zap.Error(err))
47-
os.Exit(1)
48+
// Set the build-time AI key if available
49+
if aiKey != "" {
50+
sidecar.SetBuiltInAIKey(aiKey)
51+
logger.Info("Using build-time embedded AppInsights key (from Makefile)")
52+
} else {
53+
logger.Warn("No build-time AppInsights key found - will try config file and environment variables")
4854
}
4955

50-
// Setup graceful shutdown context
56+
// Setup context for graceful shutdown
5157
ctx, cancel := context.WithCancel(context.Background())
5258
defer cancel()
5359

54-
// Handle OS signals for graceful shutdown
55-
sigCh := make(chan os.Signal, 1)
56-
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
57-
60+
// Handle shutdown signals
61+
sigChan := make(chan os.Signal, 1)
62+
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
5863
go func() {
59-
sig := <-sigCh
60-
zapLogger.Info("Received shutdown signal, initiating graceful shutdown",
61-
zap.String("signal", sig.String()))
64+
sig := <-sigChan
65+
logger.Info("Received shutdown signal", zap.String("signal", sig.String()))
6266
cancel()
6367
}()
6468

65-
// Run the telemetry sidecar (using the Run method from sidecar.go)
69+
// Run the sidecar
6670
if err := sidecar.Run(ctx); err != nil {
67-
zapLogger.Error("Azure CNI Telemetry Sidecar failed",
68-
zap.Error(err))
71+
logger.Error("Sidecar execution failed", zap.Error(err))
6972
os.Exit(1)
7073
}
7174

72-
zapLogger.Info("Azure CNI Telemetry Sidecar stopped gracefully")
75+
logger.Info("Azure CNI Telemetry Sidecar stopped gracefully")
76+
}
77+
78+
// maskAIKey masks the AI key for logging (show first 8 chars + "...")
79+
func maskAIKey(key string) string {
80+
if len(key) <= 8 {
81+
return "***"
82+
}
83+
return key[:8] + "..."
84+
}
85+
86+
// initializeLogger creates a zap logger with the specified level
87+
func initializeLogger(level string) (*zap.Logger, error) {
88+
var zapLevel zapcore.Level
89+
switch level {
90+
case "debug":
91+
zapLevel = zapcore.DebugLevel
92+
case "info":
93+
zapLevel = zapcore.InfoLevel
94+
case "warn":
95+
zapLevel = zapcore.WarnLevel
96+
case "error":
97+
zapLevel = zapcore.ErrorLevel
98+
default:
99+
zapLevel = zapcore.InfoLevel
100+
}
101+
102+
config := zap.NewProductionConfig()
103+
config.Level = zap.NewAtomicLevelAt(zapLevel)
104+
config.OutputPaths = []string{"stdout", "/var/log/azure-cni-telemetry-sidecar.log"}
105+
config.ErrorOutputPaths = []string{"stderr"}
106+
107+
return config.Build()
73108
}

0 commit comments

Comments
 (0)