getContainerResourceRequestsAndLimits

in source/plugins/ruby/KubernetesApiClient.rb [411:672]


    def getContainerResourceRequestsAndLimits(pod, metricCategory, metricNameToCollect, metricNametoReturn, metricTime = Time.now.utc.iso8601)
      metricItems = []
      begin
        clusterId = getClusterId
        podNameSpace = pod["metadata"]["namespace"]
        podUid = getPodUid(podNameSpace, pod["metadata"])
        if podUid.nil?
          return metricItems
        end

        nodeName = ""
        
        if !pod["spec"]["nodeName"].nil?
          nodeName = pod["spec"]["nodeName"]
        end
        
        if isAROv3MasterOrInfraPod(nodeName)
          return metricItems
        end

        podContainers = []
        if !pod["spec"]["containers"].nil? && !pod["spec"]["containers"].empty?
          podContainers = podContainers + pod["spec"]["containers"]
        end
        
        if !pod["spec"]["initContainers"].nil? && !pod["spec"]["initContainers"].empty?
          podContainers = podContainers + pod["spec"]["initContainers"]
        end

        if (!podContainers.nil? && !podContainers.empty? && !pod["spec"]["nodeName"].nil?)
          podContainers.each do |container|
            containerName = container["name"]
            
            if (!container["resources"].nil? && !container["resources"].empty? && !container["resources"][metricCategory].nil? && !container["resources"][metricCategory][metricNameToCollect].nil?)
              metricValue = getMetricNumericValue(metricNameToCollect, container["resources"][metricCategory][metricNameToCollect])

              metricProps = {}
              metricProps["Timestamp"] = metricTime
              metricProps["Host"] = nodeName
              
              metricProps["Computer"] = nodeName
              metricProps["ObjectName"] = "K8SContainer"
              metricProps["InstanceName"] = clusterId + "/" + podUid + "/" + containerName

              metricCollection = {}
              metricCollection["CounterName"] = metricNametoReturn
              metricCollection["Value"] = metricValue
              
              metricProps["json_Collections"] = []
              metricCollections = []               
              metricCollections.push(metricCollection)        
              metricProps["json_Collections"] = metricCollections.to_json
              metricItems.push(metricProps)             
              
            else
              nodeMetricsHashKey = clusterId + "/" + nodeName + "_" + "allocatable" + "_" + metricNameToCollect
              if (metricCategory == "limits" && @@NodeMetrics.has_key?(nodeMetricsHashKey))
                metricValue = @@NodeMetrics[nodeMetricsHashKey]
                
                               
                metricProps = {}
                metricProps["Timestamp"] = metricTime
                metricProps["Host"] = nodeName
                
                metricProps["Computer"] = nodeName
                metricProps["ObjectName"] = "K8SContainer"
                metricProps["InstanceName"] = clusterId + "/" + podUid + "/" + containerName

                metricCollection = {}
                metricCollection["CounterName"] = metricNametoReturn
                metricCollection["Value"] = metricValue
                metricProps["json_Collections"] = []
                metricCollections = []                  
                metricCollections.push(metricCollection)        
                metricProps["json_Collections"] = metricCollections.to_json
                metricItems.push(metricProps)              
              end
            end
          end
        end
      rescue => error
        @Log.warn("getcontainerResourceRequestsAndLimits failed: #{error} for metric #{metricCategory} #{metricNameToCollect}")
        return metricItems
      end
      return metricItems
    end 

    def getContainerResourceRequestsAndLimitsAsInsightsMetrics(pod, metricCategory, metricNameToCollect, metricNametoReturn, metricTime = Time.now.utc.iso8601)
      metricItems = []
      begin
        clusterId = getClusterId
        clusterName = getClusterName
        podNameSpace = pod["metadata"]["namespace"]
        if podNameSpace.eql?("kube-system") && !pod["metadata"].key?("ownerReferences")
          
          
          
          
          
          if pod["metadata"]["annotations"].nil?
            return metricItems
          else
            podUid = pod["metadata"]["annotations"]["kubernetes.io/config.hash"]
          end
        else
          podUid = pod["metadata"]["uid"]
        end

        podContainers = []
        if !pod["spec"]["containers"].nil? && !pod["spec"]["containers"].empty?
          podContainers = podContainers + pod["spec"]["containers"]
        end
        
        if !pod["spec"]["initContainers"].nil? && !pod["spec"]["initContainers"].empty?
          podContainers = podContainers + pod["spec"]["initContainers"]
        end

        if (!podContainers.nil? && !podContainers.empty?)
          if (!pod["spec"]["nodeName"].nil?)
            nodeName = pod["spec"]["nodeName"]
          else
            nodeName = "" 
          end
          podContainers.each do |container|
            metricValue = nil
            containerName = container["name"]
            
            if (!container["resources"].nil? && !container["resources"].empty? && !container["resources"][metricCategory].nil? && !container["resources"][metricCategory][metricNameToCollect].nil?)
              metricValue = getMetricNumericValue(metricNameToCollect, container["resources"][metricCategory][metricNameToCollect])
            else
              
              if (metricNameToCollect.downcase != "nvidia.com/gpu") && (metricNameToCollect.downcase != "amd.com/gpu")
                nodeMetricsHashKey = clusterId + "/" + nodeName + "_" + "allocatable" + "_" + metricNameToCollect
                metricValue = @@NodeMetrics[nodeMetricsHashKey]
              end
            end
            if (!metricValue.nil?)
              metricItem = {}
              metricItem["CollectionTime"] = metricTime
              metricItem["Computer"] = nodeName
              metricItem["Name"] = metricNametoReturn
              metricItem["Value"] = metricValue
              metricItem["Origin"] = Constants::INSIGHTSMETRICS_TAGS_ORIGIN
              metricItem["Namespace"] = Constants::INSIGHTSMETRICS_TAGS_GPU_NAMESPACE

              metricTags = {}
              metricTags[Constants::INSIGHTSMETRICS_TAGS_CLUSTERID] = clusterId
              metricTags[Constants::INSIGHTSMETRICS_TAGS_CLUSTERNAME] = clusterName
              metricTags[Constants::INSIGHTSMETRICS_TAGS_CONTAINER_NAME] = podUid + "/" + containerName
              

              metricItem["Tags"] = metricTags

              metricItems.push(metricItem)
            end
          end
        end
      rescue => error
        @Log.warn("getcontainerResourceRequestsAndLimitsAsInsightsMetrics failed: #{error} for metric #{metricCategory} #{metricNameToCollect}")
        return metricItems
      end
      return metricItems
    end 

    def parseNodeLimits(metricJSON, metricCategory, metricNameToCollect, metricNametoReturn, metricTime = Time.now.utc.iso8601)
      metricItems = []
      begin
        metricInfo = metricJSON
        clusterId = getClusterId
        
        
        
        metricInfo["items"].each do |node|
          metricItem = parseNodeLimitsFromNodeItem(node, metricCategory, metricNameToCollect, metricNametoReturn, metricTime)
          if !metricItem.nil? && !metricItem.empty?
            metricItems.push(metricItem)
          end
        end
      rescue => error
        @Log.warn("parseNodeLimits failed: #{error} for metric #{metricCategory} #{metricNameToCollect}")
      end
      return metricItems
    end 

    def parseNodeLimitsFromNodeItem(node, metricCategory, metricNameToCollect, metricNametoReturn, metricTime = Time.now.utc.iso8601)
      metricItem = {}
      begin
        clusterId = getClusterId
        
        
        
        if (!node["status"][metricCategory].nil?) && (!node["status"][metricCategory][metricNameToCollect].nil?)
          
          metricValue = getMetricNumericValue(metricNameToCollect, node["status"][metricCategory][metricNameToCollect])

          metricItem["Timestamp"] = metricTime
          metricItem["Host"] = node["metadata"]["name"]
          
          metricItem["Computer"] = node["metadata"]["name"]
          metricItem["ObjectName"] = "K8SNode"
          metricItem["InstanceName"] = clusterId + "/" + node["metadata"]["name"]

          metricCollection = {}
          metricCollection["CounterName"] = metricNametoReturn
          metricCollection["Value"] = metricValue
          metricCollections = []
          metricCollections.push(metricCollection) 
         
          metricItem["json_Collections"] = []
          metricItem["json_Collections"] = metricCollections.to_json
         
          
          
          @@NodeMetrics[clusterId + "/" + node["metadata"]["name"] + "_" + metricCategory + "_" + metricNameToCollect] = metricValue
          
        end
      rescue => error
        @Log.warn("parseNodeLimitsFromNodeItem failed: #{error} for metric #{metricCategory} #{metricNameToCollect}")
      end
      return metricItem
    end 

    def parseNodeLimitsAsInsightsMetrics(node, metricCategory, metricNameToCollect, metricNametoReturn, metricTime = Time.now.utc.iso8601)
      metricItem = {}
      begin
        
        
        
        if (!node["status"][metricCategory].nil?) && (!node["status"][metricCategory][metricNameToCollect].nil?)
          clusterId = getClusterId
          clusterName = getClusterName

          
          metricValue = getMetricNumericValue(metricNameToCollect, node["status"][metricCategory][metricNameToCollect])

          metricItem["CollectionTime"] = metricTime
          metricItem["Computer"] = node["metadata"]["name"]
          metricItem["Name"] = metricNametoReturn
          metricItem["Value"] = metricValue
          metricItem["Origin"] = Constants::INSIGHTSMETRICS_TAGS_ORIGIN
          metricItem["Namespace"] = Constants::INSIGHTSMETRICS_TAGS_GPU_NAMESPACE

          metricTags = {}
          metricTags[Constants::INSIGHTSMETRICS_TAGS_CLUSTERID] = clusterId
          metricTags[Constants::INSIGHTSMETRICS_TAGS_CLUSTERNAME] = clusterName
          metricTags[Constants::INSIGHTSMETRICS_TAGS_GPU_VENDOR] = metricNameToCollect

          metricItem["Tags"] = metricTags

          
          
          if (metricNameToCollect.downcase != "nvidia.com/gpu") && (metricNameToCollect.downcase != "amd.com/gpu")
            @@NodeMetrics[clusterId + "/" + node["metadata"]["name"] + "_" + metricCategory + "_" + metricNameToCollect] = metricValue
            
          end
        end
      rescue => error
        @Log.warn("parseNodeLimitsAsInsightsMetrics failed: #{error} for metric #{metricCategory} #{metricNameToCollect}")
      end
      return metricItem
    end