build/linux/installer/scripts/tomlparser-prom-customconfig.rb
# Use the ruby structure created after config parsing to set the right values to be used as environment variables
Use the ruby structure created after config parsing to set the right values to be used as environment variables
Use the ruby structure created after config parsing to set the right values to be used as environment variables
def populateSettingValuesFromConfigMap(parsedConfig)
  controller = ENV["CONTROLLER_TYPE"]
  if !controller.nil?
    if !parsedConfig.nil? && !parsedConfig[:prometheus_data_collection_settings].nil?
      if controller.casecmp(@replicaset) == 0 && !parsedConfig[:prometheus_data_collection_settings][:cluster].nil?
        begin
          interval = parsedConfig[:prometheus_data_collection_settings][:cluster][:interval]
          fieldPass = parsedConfig[:prometheus_data_collection_settings][:cluster][:fieldpass]
          fieldDrop = parsedConfig[:prometheus_data_collection_settings][:cluster][:fielddrop]
          urls = parsedConfig[:prometheus_data_collection_settings][:cluster][:urls]
          kubernetesServices = parsedConfig[:prometheus_data_collection_settings][:cluster][:kubernetes_services]
          monitorKubernetesPods = parsedConfig[:prometheus_data_collection_settings][:cluster][:monitor_kubernetes_pods]
          monitorKubernetesPodsNamespaces = parsedConfig[:prometheus_data_collection_settings][:cluster][:monitor_kubernetes_pods_namespaces]
          if checkForType(interval, String) &&
             checkForTypeArray(fieldPass, String) &&
             checkForTypeArray(fieldDrop, String) &&
             checkForTypeArray(kubernetesServices, String) &&
             checkForTypeArray(urls, String) &&
             (monitorKubernetesPods.nil? || (!monitorKubernetesPods.nil? && (!!monitorKubernetesPods == monitorKubernetesPods))) 
            puts "config::Successfully passed typecheck for config settings for replicaset"
            interval = (interval.nil?) ? @defaultRsInterval : interval
            fieldPass = (fieldPass.nil?) ? @defaultRsFieldPass : fieldPass
            fieldDrop = (fieldDrop.nil?) ? @defaultRsFieldDrop : fieldDrop
            kubernetesServices = (kubernetesServices.nil?) ? @defaultRsK8sServices : kubernetesServices
            urls = (urls.nil?) ? @defaultRsPromUrls : urls
            monitorKubernetesPods = (monitorKubernetesPods.nil?) ? @defaultRsMonitorPods : monitorKubernetesPods
            file_name = "/opt/telegraf-test-rs.conf"
            FileUtils.cp("/etc/opt/microsoft/docker-cimprov/telegraf-rs.conf", file_name)
            puts "config::Starting to substitute the placeholders in telegraf conf copy file for replicaset"
            text = File.read(file_name)
            new_contents = text.gsub("$AZMON_RS_PROM_INTERVAL", interval)
            fieldPassSetting = (fieldPass.length > 0) ? ("[\"" + fieldPass.join("\",\"") + "\"]") : "[]"
            new_contents = new_contents.gsub("$AZMON_RS_PROM_FIELDPASS", fieldPassSetting)
            fieldDropSetting = (fieldDrop.length > 0) ? ("[\"" + fieldDrop.join("\",\"") + "\"]") : "[]"
            new_contents = new_contents.gsub("$AZMON_RS_PROM_FIELDDROP", fieldDropSetting)
            new_contents = new_contents.gsub("$AZMON_RS_PROM_URLS", ((urls.length > 0) ? ("[\"" + urls.join("\",\"") + "\"]") : "[]"))
            new_contents = new_contents.gsub("$AZMON_RS_PROM_K8S_SERVICES", ((kubernetesServices.length > 0) ? ("[\"" + kubernetesServices.join("\",\"") + "\"]") : "[]"))
            if monitorKubernetesPods && !monitorKubernetesPodsNamespaces.nil? && checkForTypeArray(monitorKubernetesPodsNamespaces, String)
              new_contents = createPrometheusPluginsWithNamespaceSetting(monitorKubernetesPods, monitorKubernetesPodsNamespaces, new_contents, interval, fieldPassSetting, fieldDropSetting)
              monitorKubernetesPodsNamespacesLength = monitorKubernetesPodsNamespaces.length
            else
              new_contents = replaceDefaultMonitorPodSettings(new_contents, monitorKubernetesPods)
              monitorKubernetesPodsNamespacesLength = 0
            end
            File.open(file_name, "w") { |file| file.puts new_contents }
            puts "config::Successfully substituted the placeholders in telegraf conf file for replicaset"
            file = File.open("telemetry_prom_config_env_var", "w")
            if !file.nil?
              file.write("export TELEMETRY_RS_PROM_INTERVAL=\"#{interval}\"\n")
              file.write("export TELEMETRY_RS_PROM_FIELDPASS_LENGTH=\"#{fieldPass.length}\"\n")
              file.write("export TELEMETRY_RS_PROM_FIELDDROP_LENGTH=\"#{fieldDrop.length}\"\n")
              file.write("export TELEMETRY_RS_PROM_K8S_SERVICES_LENGTH=#{kubernetesServices.length}\n")
              file.write("export TELEMETRY_RS_PROM_URLS_LENGTH=#{urls.length}\n")
              file.write("export TELEMETRY_RS_PROM_MONITOR_PODS=\"#{monitorKubernetesPods}\"\n")
              file.write("export TELEMETRY_RS_PROM_MONITOR_PODS_NS_LENGTH=\"#{monitorKubernetesPodsNamespacesLength}\"\n")
              file.close
              puts "config::Successfully created telemetry file for replicaset"
            end
          else
            ConfigParseErrorLogger.logError("Typecheck failed for prometheus config settings for replicaset, using defaults, please use right types for all settings")
          end 
        rescue => errorStr
          ConfigParseErrorLogger.logError("Exception while parsing config file for prometheus config for replicaset: #{errorStr}, using defaults")
          setRsPromDefaults
          puts "****************End Prometheus Config Processing********************"
        end
      elsif controller.casecmp(@daemonset) == 0 && !parsedConfig[:prometheus_data_collection_settings][:node].nil?
        begin
          interval = parsedConfig[:prometheus_data_collection_settings][:node][:interval]
          fieldPass = parsedConfig[:prometheus_data_collection_settings][:node][:fieldpass]
          fieldDrop = parsedConfig[:prometheus_data_collection_settings][:node][:fielddrop]
          urls = parsedConfig[:prometheus_data_collection_settings][:node][:urls]
          if checkForType(interval, String) &&
             checkForTypeArray(fieldPass, String) &&
             checkForTypeArray(fieldDrop, String) &&
             checkForTypeArray(urls, String)
            puts "config::Successfully passed typecheck for config settings for daemonset"
            interval = (interval.nil?) ? @defaultDsInterval : interval
            fieldPass = (fieldPass.nil?) ? @defaultDsFieldPass : fieldPass
            fieldDrop = (fieldDrop.nil?) ? @defaultDsFieldDrop : fieldDrop
            urls = (urls.nil?) ? @defaultDsPromUrls : urls
            file_name = "/opt/telegraf-test.conf"
            FileUtils.cp("/etc/opt/microsoft/docker-cimprov/telegraf.conf", file_name)
            puts "config::Starting to substitute the placeholders in telegraf conf copy file for daemonset"
            text = File.read(file_name)
            new_contents = text.gsub("$AZMON_DS_PROM_INTERVAL", interval)
            new_contents = new_contents.gsub("$AZMON_DS_PROM_FIELDPASS", ((fieldPass.length > 0) ? ("[\"" + fieldPass.join("\",\"") + "\"]") : "[]"))
            new_contents = new_contents.gsub("$AZMON_DS_PROM_FIELDDROP", ((fieldDrop.length > 0) ? ("[\"" + fieldDrop.join("\",\"") + "\"]") : "[]"))
            new_contents = new_contents.gsub("$AZMON_DS_PROM_URLS", ((urls.length > 0) ? ("[\"" + urls.join("\",\"") + "\"]") : "[]"))
            File.open(file_name, "w") { |file| file.puts new_contents }
            puts "config::Successfully substituted the placeholders in telegraf conf file for daemonset"
            file = File.open("telemetry_prom_config_env_var", "w")
            if !file.nil?
              file.write("export TELEMETRY_DS_PROM_INTERVAL=\"#{interval}\"\n")
              file.write("export TELEMETRY_DS_PROM_FIELDPASS_LENGTH=\"#{fieldPass.length}\"\n")
              file.write("export TELEMETRY_DS_PROM_FIELDDROP_LENGTH=\"#{fieldDrop.length}\"\n")
              file.write("export TELEMETRY_DS_PROM_URLS_LENGTH=#{urls.length}\n")
              file.close
              puts "config::Successfully created telemetry file for daemonset"
            end
          else
            ConfigParseErrorLogger.logError("Typecheck failed for prometheus config settings for daemonset, using defaults, please use right types for all settings")
          end 
        rescue => errorStr
          ConfigParseErrorLogger.logError("Exception while parsing config file for prometheus config for daemonset: #{errorStr}, using defaults, please check correctness of configmap")
          puts "****************End Prometheus Config Processing********************"
        end
      end 
    end
  else
    ConfigParseErrorLogger.logError("Controller undefined while processing prometheus config, using defaults")
  end
end
def populateSettingValuesFromConfigMap ( parsedConfig ) controller = ENV [ "CONTROLLER_TYPE" ] if ! controller . nil? if ! parsedConfig . nil? && ! parsedConfig [ :prometheus_data_collection_settings ] . nil? if controller . casecmp ( @replicaset ) == 0 && ! parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] . nil? begin interval = parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] [ :interval ] fieldPass = parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] [ :fieldpass ] fieldDrop = parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] [ :fielddrop ] urls = parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] [ :urls ] kubernetesServices = parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] [ :kubernetes_services ] monitorKubernetesPods = parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] [ :monitor_kubernetes_pods ] monitorKubernetesPodsNamespaces = parsedConfig [ :prometheus_data_collection_settings ] [ :cluster ] [ :monitor_kubernetes_pods_namespaces ] if checkForType ( interval , String ) && checkForTypeArray ( fieldPass , String ) && checkForTypeArray ( fieldDrop , String ) && checkForTypeArray ( kubernetesServices , String ) && checkForTypeArray ( urls , String ) && ( monitorKubernetesPods . nil? || ( ! monitorKubernetesPods . nil? && ( ! ! monitorKubernetesPods == monitorKubernetesPods ) ) ) puts "config::Successfully passed typecheck for config settings for replicaset" interval = ( interval . nil? ) ? @defaultRsInterval : interval fieldPass = ( fieldPass . nil? ) ? @defaultRsFieldPass : fieldPass fieldDrop = ( fieldDrop . nil? ) ? @defaultRsFieldDrop : fieldDrop kubernetesServices = ( kubernetesServices . nil? ) ? @defaultRsK8sServices : kubernetesServices urls = ( urls . nil? ) ? @defaultRsPromUrls : urls monitorKubernetesPods = ( monitorKubernetesPods . nil? ) ? @defaultRsMonitorPods : monitorKubernetesPods file_name = "/opt/telegraf-test-rs.conf" FileUtils . cp ( "/etc/opt/microsoft/docker-cimprov/telegraf-rs.conf" , file_name ) puts "config::Starting to substitute the placeholders in telegraf conf copy file for replicaset" text = File . read ( file_name ) new_contents = text . gsub ( "$AZMON_RS_PROM_INTERVAL" , interval ) fieldPassSetting = ( fieldPass . length > 0 ) ? ( "[\"" + fieldPass . join ( "\",\"" ) + "\"]" ) : "[]" new_contents = new_contents . gsub ( "$AZMON_RS_PROM_FIELDPASS" , fieldPassSetting ) fieldDropSetting = ( fieldDrop . length > 0 ) ? ( "[\"" + fieldDrop . join ( "\",\"" ) + "\"]" ) : "[]" new_contents = new_contents . gsub ( "$AZMON_RS_PROM_FIELDDROP" , fieldDropSetting ) new_contents = new_contents . gsub ( "$AZMON_RS_PROM_URLS" , ( ( urls . length > 0 ) ? ( "[\"" + urls . join ( "\",\"" ) + "\"]" ) : "[]" ) ) new_contents = new_contents . gsub ( "$AZMON_RS_PROM_K8S_SERVICES" , ( ( kubernetesServices . length > 0 ) ? ( "[\"" + kubernetesServices . join ( "\",\"" ) + "\"]" ) : "[]" ) ) if monitorKubernetesPods && ! monitorKubernetesPodsNamespaces . nil? && checkForTypeArray ( monitorKubernetesPodsNamespaces , String ) new_contents = createPrometheusPluginsWithNamespaceSetting ( monitorKubernetesPods , monitorKubernetesPodsNamespaces , new_contents , interval , fieldPassSetting , fieldDropSetting ) monitorKubernetesPodsNamespacesLength = monitorKubernetesPodsNamespaces . length else new_contents = replaceDefaultMonitorPodSettings ( new_contents , monitorKubernetesPods ) monitorKubernetesPodsNamespacesLength = 0 end File . open ( file_name , "w" ) { | file | file . puts new_contents } puts "config::Successfully substituted the placeholders in telegraf conf file for replicaset" file = File . open ( "telemetry_prom_config_env_var" , "w" ) if ! file . nil? file . write ( "export TELEMETRY_RS_PROM_INTERVAL=\"#{interval}\"\n" ) file . write ( "export TELEMETRY_RS_PROM_FIELDPASS_LENGTH=\"#{fieldPass.length}\"\n" ) file . write ( "export TELEMETRY_RS_PROM_FIELDDROP_LENGTH=\"#{fieldDrop.length}\"\n" ) file . write ( "export TELEMETRY_RS_PROM_K8S_SERVICES_LENGTH=#{kubernetesServices.length}\n" ) file . write ( "export TELEMETRY_RS_PROM_URLS_LENGTH=#{urls.length}\n" ) file . write ( "export TELEMETRY_RS_PROM_MONITOR_PODS=\"#{monitorKubernetesPods}\"\n" ) file . write ( "export TELEMETRY_RS_PROM_MONITOR_PODS_NS_LENGTH=\"#{monitorKubernetesPodsNamespacesLength}\"\n" ) file . close puts "config::Successfully created telemetry file for replicaset" end else ConfigParseErrorLogger . logError ( "Typecheck failed for prometheus config settings for replicaset, using defaults, please use right types for all settings" ) end rescue => errorStr ConfigParseErrorLogger . logError ( "Exception while parsing config file for prometheus config for replicaset: #{errorStr}, using defaults" ) setRsPromDefaults puts "****************End Prometheus Config Processing********************" end elsif controller . casecmp ( @daemonset ) == 0 && ! parsedConfig [ :prometheus_data_collection_settings ] [ :node ] . nil? begin interval = parsedConfig [ :prometheus_data_collection_settings ] [ :node ] [ :interval ] fieldPass = parsedConfig [ :prometheus_data_collection_settings ] [ :node ] [ :fieldpass ] fieldDrop = parsedConfig [ :prometheus_data_collection_settings ] [ :node ] [ :fielddrop ] urls = parsedConfig [ :prometheus_data_collection_settings ] [ :node ] [ :urls ] if checkForType ( interval , String ) && checkForTypeArray ( fieldPass , String ) && checkForTypeArray ( fieldDrop , String ) && checkForTypeArray ( urls , String ) puts "config::Successfully passed typecheck for config settings for daemonset" interval = ( interval . nil? ) ? @defaultDsInterval : interval fieldPass = ( fieldPass . nil? ) ? @defaultDsFieldPass : fieldPass fieldDrop = ( fieldDrop . nil? ) ? @defaultDsFieldDrop : fieldDrop urls = ( urls . nil? ) ? @defaultDsPromUrls : urls file_name = "/opt/telegraf-test.conf" FileUtils . cp ( "/etc/opt/microsoft/docker-cimprov/telegraf.conf" , file_name ) puts "config::Starting to substitute the placeholders in telegraf conf copy file for daemonset" text = File . read ( file_name ) new_contents = text . gsub ( "$AZMON_DS_PROM_INTERVAL" , interval ) new_contents = new_contents . gsub ( "$AZMON_DS_PROM_FIELDPASS" , ( ( fieldPass . length > 0 ) ? ( "[\"" + fieldPass . join ( "\",\"" ) + "\"]" ) : "[]" ) ) new_contents = new_contents . gsub ( "$AZMON_DS_PROM_FIELDDROP" , ( ( fieldDrop . length > 0 ) ? ( "[\"" + fieldDrop . join ( "\",\"" ) + "\"]" ) : "[]" ) ) new_contents = new_contents . gsub ( "$AZMON_DS_PROM_URLS" , ( ( urls . length > 0 ) ? ( "[\"" + urls . join ( "\",\"" ) + "\"]" ) : "[]" ) ) File . open ( file_name , "w" ) { | file | file . puts new_contents } puts "config::Successfully substituted the placeholders in telegraf conf file for daemonset" file = File . open ( "telemetry_prom_config_env_var" , "w" ) if ! file . nil? file . write ( "export TELEMETRY_DS_PROM_INTERVAL=\"#{interval}\"\n" ) file . write ( "export TELEMETRY_DS_PROM_FIELDPASS_LENGTH=\"#{fieldPass.length}\"\n" ) file . write ( "export TELEMETRY_DS_PROM_FIELDDROP_LENGTH=\"#{fieldDrop.length}\"\n" ) file . write ( "export TELEMETRY_DS_PROM_URLS_LENGTH=#{urls.length}\n" ) file . close puts "config::Successfully created telemetry file for daemonset" end else ConfigParseErrorLogger . logError ( "Typecheck failed for prometheus config settings for daemonset, using defaults, please use right types for all settings" ) end rescue => errorStr ConfigParseErrorLogger . logError ( "Exception while parsing config file for prometheus config for daemonset: #{errorStr}, using defaults, please check correctness of configmap" ) puts "****************End Prometheus Config Processing********************" end end end else ConfigParseErrorLogger . logError ( "Controller undefined while processing prometheus config, using defaults" ) end end
Use the ruby structure created after config parsing to set the right values to be used as environment variables
Use the ruby structure created after config parsing to set the right values to be used as environment variables
# Checking to see if this is the daemonset or replicaset to parse config accordingly #Get prometheus replicaset custom config settings # Check for the right datattypes to enforce right setting values #Checking for Boolean type, since 'Boolean' is not defined as a type in ruby #if setting is nil assign default values # Copy the telegraf config file to a temp file to run telegraf in test mode with this config #Replace the placeholder config values with values from custom config # Check to see if monitor_kubernetes_pods is set to true with a valid setting for monitor_kubernetes_namespaces to enable scraping for specific namespaces # Adding nil check here as well since checkForTypeArray returns true even if setting is nil to accomodate for other settings to be able - # - to use defaults in case of nil settings #Set environment variables for telemetry #Setting array lengths as environment variables for telemetry purposes # Close file after writing all environment variables # end of type check condition #Get prometheus daemonset custom config settings # Check for the right datattypes to enforce right setting values #if setting is nil assign default values # Copy the telegraf config file to a temp file to run telegraf in test mode with this config #Replace the placeholder config values with values from custom config #Set environment variables for telemetry #Setting array lengths as environment variables for telemetry purposes # Close file after writing all environment variables # end of type check condition # end of controller type check
parsedConfig
<not_specific>