diff --git a/otelcollector/main/main.go b/otelcollector/main/main.go index 6c6daee73..099d4e530 100644 --- a/otelcollector/main/main.go +++ b/otelcollector/main/main.go @@ -67,8 +67,8 @@ func main() { shared.WaitForTokenAdapter(ccpMetricsEnabled) if ccpMetricsEnabled != "true" { - shared.SetEnvAndSourceBashrc("ME_CONFIG_FILE", meConfigFile, true) - shared.SetEnvAndSourceBashrc("customResourceId", cluster, true) + shared.SetEnvAndSourceBashrc("ME_CONFIG_FILE", meConfigFile, true, false) + shared.SetEnvAndSourceBashrc("customResourceId", cluster, true, false) } else { os.Setenv("ME_CONFIG_FILE", meConfigFile) os.Setenv("customResourceId", cluster) @@ -76,7 +76,7 @@ func main() { trimmedRegion := strings.ToLower(strings.ReplaceAll(aksRegion, " ", "")) if ccpMetricsEnabled != "true" { - shared.SetEnvAndSourceBashrc("customRegion", trimmedRegion, true) + shared.SetEnvAndSourceBashrc("customRegion", trimmedRegion, true, false) } else { os.Setenv("customRegion", trimmedRegion) } diff --git a/otelcollector/shared/configmap/ccp/configmapparserforccp.go b/otelcollector/shared/configmap/ccp/configmapparserforccp.go index d60eac601..caf145eaa 100644 --- a/otelcollector/shared/configmap/ccp/configmapparserforccp.go +++ b/otelcollector/shared/configmap/ccp/configmapparserforccp.go @@ -25,7 +25,7 @@ func Configmapparserforccp() { configVersion = configVersion[:10] } // Set the environment variable - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", configVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", configVersion, true, false) } // Set agent config file version @@ -41,7 +41,7 @@ func Configmapparserforccp() { configSchemaVersion = configSchemaVersion[:10] } // Set the environment variable - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", configSchemaVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", configSchemaVersion, true, false) } // Parse the configmap to set the right environment variables for prometheus collector settings @@ -65,11 +65,11 @@ func Configmapparserforccp() { prometheusCcpConfigMerger() - shared.SetEnvAndSourceBashrc("AZMON_INVALID_CUSTOM_PROMETHEUS_CONFIG", "false", true) - shared.SetEnvAndSourceBashrc("CONFIG_VALIDATOR_RUNNING_IN_AGENT", "true", true) + shared.SetEnvAndSourceBashrc("AZMON_INVALID_CUSTOM_PROMETHEUS_CONFIG", "false", true, false) + shared.SetEnvAndSourceBashrc("CONFIG_VALIDATOR_RUNNING_IN_AGENT", "true", true, false) // No need to merge custom prometheus config, only merging in the default configs - shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true) + shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true, false) shared.StartCommandAndWait("/opt/promconfigvalidator", "--config", "/opt/defaultsMergedConfig.yml", "--output", "/opt/ccp-collector-config-with-defaults.yml", "--otelTemplate", "/opt/microsoft/otelcollector/ccp-collector-config-template.yml") if !shared.Exists("/opt/ccp-collector-config-with-defaults.yml") { fmt.Printf("prom-config-validator::Prometheus default scrape config validation failed. No scrape configs will be used") diff --git a/otelcollector/shared/configmap/mp/configmapparser.go b/otelcollector/shared/configmap/mp/configmapparser.go index d55b2f6d0..9c870e9e5 100644 --- a/otelcollector/shared/configmap/mp/configmapparser.go +++ b/otelcollector/shared/configmap/mp/configmapparser.go @@ -12,19 +12,19 @@ import ( const ( defaultConfigSchemaVersion = "v1" - defaultConfigFileVersion = "ver1" + defaultConfigFileVersion = "ver1" ) func setConfigSchemaVersionEnv() { fileInfo, err := os.Stat(schemaVersionFile) if err != nil || fileInfo.Size() == 0 { - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", defaultConfigSchemaVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", defaultConfigSchemaVersion, true, false) return } content, err := os.ReadFile(schemaVersionFile) if err != nil { shared.EchoError("Error reading schema version file:" + err.Error()) - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", defaultConfigSchemaVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", defaultConfigSchemaVersion, true, false) return } trimmedContent := strings.TrimSpace(string(content)) @@ -32,19 +32,19 @@ func setConfigSchemaVersionEnv() { if len(configSchemaVersion) > 10 { configSchemaVersion = configSchemaVersion[:10] } - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", configSchemaVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_SCHEMA_VERSION", configSchemaVersion, true, false) } func setConfigFileVersionEnv() { fileInfo, err := os.Stat(configVersionFile) if err != nil || fileInfo.Size() == 0 { - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", defaultConfigFileVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", defaultConfigFileVersion, true, false) return } content, err := os.ReadFile(configVersionFile) if err != nil { shared.EchoError("Error reading config version file:" + err.Error()) - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", defaultConfigFileVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", defaultConfigFileVersion, true, false) return } trimmedContent := strings.TrimSpace(string(content)) @@ -52,7 +52,7 @@ func setConfigFileVersionEnv() { if len(configFileVersion) > 10 { configFileVersion = configFileVersion[:10] } - shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", configFileVersion, true) + shared.SetEnvAndSourceBashrc("AZMON_AGENT_CFG_FILE_VERSION", configFileVersion, true, false) } func parseSettingsForPodAnnotations() { @@ -61,10 +61,52 @@ func parseSettingsForPodAnnotations() { fmt.Printf("%v\n", err) return } - handleEnvFileError(podAnnotationEnvVarPath) + handlePodAnnotationsFile(podAnnotationEnvVarPath) shared.EchoSectionDivider("End Processing - parseSettingsForPodAnnotations") } +func handlePodAnnotationsFile(filename string) { + // Check if the file exists + _, e := os.Stat(filename) + if os.IsNotExist(e) { + fmt.Printf("File does not exist: %s\n", filename) + return + } + + // Open the file for reading + file, err := os.Open(filename) + if err != nil { + fmt.Printf("Error opening file: %s\n", err) + return + } + defer file.Close() + + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + line := scanner.Text() + index := strings.Index(line, "=") + if index == -1 { + fmt.Printf("Skipping invalid line: %s\n", line) + continue + } + + key := line[:index] + value := line[index+1:] + + if key == "AZMON_PROMETHEUS_POD_ANNOTATION_NAMESPACES_REGEX" { + shared.SetEnvAndSourceBashrc(key, value, false, true) + } else { + shared.SetEnvAndSourceBashrc(key, value, false, false) + } + + } + + if err := scanner.Err(); err != nil { + fmt.Printf("Error reading file: %s\n", err) + } +} + func parsePrometheusCollectorConfig() { shared.EchoSectionDivider("Start Processing - parsePrometheusCollectorConfig") parseConfigAndSetEnvInFile() @@ -116,8 +158,8 @@ func Configmapparser() { prometheusConfigMerger(false) } - shared.SetEnvAndSourceBashrc("AZMON_INVALID_CUSTOM_PROMETHEUS_CONFIG", "false", true) - shared.SetEnvAndSourceBashrc("CONFIG_VALIDATOR_RUNNING_IN_AGENT", "true", true) + shared.SetEnvAndSourceBashrc("AZMON_INVALID_CUSTOM_PROMETHEUS_CONFIG", "false", true, false) + shared.SetEnvAndSourceBashrc("CONFIG_VALIDATOR_RUNNING_IN_AGENT", "true", true, false) // Running promconfigvalidator if promMergedConfig.yml exists if shared.FileExists("/opt/promMergedConfig.yml") { @@ -130,7 +172,7 @@ func Configmapparser() { if err != nil { fmt.Println("prom-config-validator::Prometheus custom config validation failed. The custom config will not be used") fmt.Printf("Command execution failed: %v\n", err) - shared.SetEnvAndSourceBashrc("AZMON_INVALID_CUSTOM_PROMETHEUS_CONFIG", "true", true) + shared.SetEnvAndSourceBashrc("AZMON_INVALID_CUSTOM_PROMETHEUS_CONFIG", "true", true, false) if shared.FileExists(mergedDefaultConfigPath) { fmt.Println("prom-config-validator::Running validator on just default scrape configs") shared.StartCommandAndWait("/opt/promconfigvalidator", "--config", mergedDefaultConfigPath, "--output", "/opt/collector-config-with-defaults.yml", "--otelTemplate", "/opt/microsoft/otelcollector/collector-config-template.yml") @@ -140,7 +182,7 @@ func Configmapparser() { shared.CopyFile("/opt/collector-config-with-defaults.yml", "/opt/microsoft/otelcollector/collector-config-default.yml") } } - shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true) + shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true, false) } } } else if _, err := os.Stat(mergedDefaultConfigPath); err == nil { @@ -153,11 +195,11 @@ func Configmapparser() { fmt.Println("prom-config-validator::Prometheus default scrape config validation succeeded, using this as collector config") shared.CopyFile("/opt/collector-config-with-defaults.yml", "/opt/microsoft/otelcollector/collector-config-default.yml") } - shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true) + shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true, false) } else { // This else block is needed, when there is no custom config mounted as config map or default configs enabled fmt.Println("prom-config-validator::No custom config via configmap or default scrape configs enabled.") - shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true) + shared.SetEnvAndSourceBashrc("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG", "true", true, false) } if _, err := os.Stat("/opt/microsoft/prom_config_validator_env_var"); err == nil { diff --git a/otelcollector/shared/file_utilities.go b/otelcollector/shared/file_utilities.go index 899b37ffc..25f666a71 100644 --- a/otelcollector/shared/file_utilities.go +++ b/otelcollector/shared/file_utilities.go @@ -127,7 +127,7 @@ func SetEnvVarsFromFile(filename string) error { key := parts[0] value := parts[1] - SetEnvAndSourceBashrc(key, value, false) + SetEnvAndSourceBashrc(key, value, false, false) } if err := scanner.Err(); err != nil { diff --git a/otelcollector/shared/process_utilities.go b/otelcollector/shared/process_utilities.go index b97efd5f6..6356e110f 100644 --- a/otelcollector/shared/process_utilities.go +++ b/otelcollector/shared/process_utilities.go @@ -44,8 +44,8 @@ func IsProcessRunning(processName string) bool { } // SetEnvAndSourceBashrc sets a key-value pair as an environment variable in the .bashrc file -// and sources the file to apply changes immediately. If echo is true, it calls EchoVar. -func SetEnvAndSourceBashrc(key, value string, echo bool) error { +// and sources the file to apply changes immediately. If echo is true, it calls EchoVar. If quotes is true, it add quotes around the value +func SetEnvAndSourceBashrc(key, value string, echo bool, quotes bool) error { // Get user's home directory homeDir, err := os.UserHomeDir() if err != nil { @@ -72,7 +72,11 @@ func SetEnvAndSourceBashrc(key, value string, echo bool) error { defer file.Close() // Write the export statement to the .bashrc file - _, err = fmt.Fprintf(file, "export %s=%s\n", key, value) + if quotes { + _, err = fmt.Fprintf(file, "export %s=\\\"%s\\\"\n", key, value) + } else { + _, err = fmt.Fprintf(file, "export %s=%s\n", key, value) + } if err != nil { return fmt.Errorf("failed to write to .bashrc file: %v", err) } diff --git a/otelcollector/shared/proxy_settings.go b/otelcollector/shared/proxy_settings.go index 7728b99e3..f38159347 100644 --- a/otelcollector/shared/proxy_settings.go +++ b/otelcollector/shared/proxy_settings.go @@ -51,8 +51,8 @@ func addNoProxy(target string) { noProxy := os.Getenv("NO_PROXY") noProxy = strings.TrimSpace(noProxy) noProxy += "," + target - SetEnvAndSourceBashrc("NO_PROXY", noProxy, true) - SetEnvAndSourceBashrc("no_proxy", noProxy, true) + SetEnvAndSourceBashrc("NO_PROXY", noProxy, true, false) + SetEnvAndSourceBashrc("no_proxy", noProxy, true, false) } func setHTTPProxyEnabled() { @@ -60,7 +60,7 @@ func setHTTPProxyEnabled() { if os.Getenv("HTTP_PROXY") != "" { httpProxyEnabled = "true" } - SetEnvAndSourceBashrc("HTTP_PROXY_ENABLED", httpProxyEnabled, true) + SetEnvAndSourceBashrc("HTTP_PROXY_ENABLED", httpProxyEnabled, true, false) } func ConfigureEnvironment() error { @@ -69,7 +69,7 @@ func ConfigureEnvironment() error { // Remove trailing '/' character from HTTP_PROXY and HTTPS_PROXY proxyVariables := []string{"http_proxy", "HTTP_PROXY", "https_proxy", "HTTPS_PROXY"} for _, v := range proxyVariables { - SetEnvAndSourceBashrc(v, removeTrailingSlash(os.Getenv(v)), true) + SetEnvAndSourceBashrc(v, removeTrailingSlash(os.Getenv(v)), true, false) } addNoProxy("ama-metrics-operator-targets.kube-system.svc.cluster.local") @@ -92,11 +92,11 @@ func ConfigureEnvironment() error { password := base64.StdEncoding.EncodeToString([]byte(strings.SplitN(urlParts[0], ":", 2)[1])) os.WriteFile("/opt/microsoft/proxy_password", []byte(password), 0644) - SetEnvAndSourceBashrc("MDSD_PROXY_MODE", "application", true) - SetEnvAndSourceBashrc("MDSD_PROXY_ADDRESS", os.Getenv("HTTPS_PROXY"), true) + SetEnvAndSourceBashrc("MDSD_PROXY_MODE", "application", true, false) + SetEnvAndSourceBashrc("MDSD_PROXY_ADDRESS", os.Getenv("HTTPS_PROXY"), true, false) if user := strings.SplitN(urlParts[0], ":", 2)[0]; user != "" { - SetEnvAndSourceBashrc("MDSD_PROXY_USERNAME", user, true) - SetEnvAndSourceBashrc("MDSD_PROXY_PASSWORD_FILE", "/opt/microsoft/proxy_password", true) + SetEnvAndSourceBashrc("MDSD_PROXY_USERNAME", user, true, false) + SetEnvAndSourceBashrc("MDSD_PROXY_PASSWORD_FILE", "/opt/microsoft/proxy_password", true, false) } } diff --git a/otelcollector/shared/telemetry.go b/otelcollector/shared/telemetry.go index 53896e766..d7d5382e4 100644 --- a/otelcollector/shared/telemetry.go +++ b/otelcollector/shared/telemetry.go @@ -45,7 +45,7 @@ func SetupTelemetry(customEnvironment string) { } // Export APPLICATIONINSIGHTS_AUTH - err := SetEnvAndSourceBashrc("APPLICATIONINSIGHTS_AUTH", encodedAIKey, false) + err := SetEnvAndSourceBashrc("APPLICATIONINSIGHTS_AUTH", encodedAIKey, false, false) if err != nil { fmt.Println("Error setting APPLICATIONINSIGHTS_AUTH environment variable:", err) return @@ -84,7 +84,7 @@ func SetupTelemetry(customEnvironment string) { } aiKey = string(aiKeyBytes) - err = SetEnvAndSourceBashrc("TELEMETRY_APPLICATIONINSIGHTS_KEY", aiKey, false) + err = SetEnvAndSourceBashrc("TELEMETRY_APPLICATIONINSIGHTS_KEY", aiKey, false, false) if err != nil { fmt.Println("Error setting TELEMETRY_APPLICATIONINSIGHTS_KEY environment variable:", err) return