<commit_before>module EmsRefresh::Parsers
  class Kubernetes
    def self.ems_inv_to_hashes(inventory)
      new.ems_inv_to_hashes(inventory)
    end

    def initialize
      @data = {}
    end

    def ems_inv_to_hashes(inventory)
      get_nodes(inventory)
      get_services(inventory)
      get_pods(inventory)
      EmsRefresh.log_inv_debug_trace(@data, "data:")
      @data
    end

    def get_nodes(inventory)
      process_collection(inventory["node"], :container_nodes) { |n| parse_node(n) }
    end

    def get_services(inventory)
      process_collection(inventory["service"], :container_services) { |s| parse_service(s) }
    end

    def get_pods(inventory)
      process_collection(inventory["pod"], :container_groups) { |n| parse_pod(n) }
    end

    def process_collection(collection, key, &block)
      @data[key] ||= []
      collection.each { |item| process_collection_item(item, key, &block) }
    end

    def process_collection_item(item, key)
      @data[key] ||= []

      new_result = yield(item)

      @data[key] << new_result
      new_result
    end

    def parse_node(node)
      new_result = parse_base_item(node)
      # TODO, add more properties such as resources
      new_result
    end

    def parse_service(service)
      new_result = parse_base_item(service)
      new_result.merge!(
        :port             => service.spec.port,
        :protocol         => service.spec.protocol,
        :portal_ip        => service.spec.portalIP,
        :container_port   => service.spec.containerPort,
        :namespace        => service.metadata.instance_values["table"][:namespace],
        :session_affinity => service.spec.sessionAffinity,

        :labels           => parse_labels(service),
        :selector_parts   => parse_selector_parts(service)
      )
      new_result
    end

    def parse_pod(pod)
      # pod in kubernetes is container group in manageiq
      new_result = parse_base_item(pod)
      new_result.merge!(
        # namespace is overriden in more_core_extensions and hence needs a non method access
        :namespace      => pod.metadata.instance_values["table"][:namespace],
        # workaround due to https://github.com/GoogleCloudPlatform/kubernetes/issues/3607
        :restart_policy => pod.spec.restartPolicy.to_h.keys.first.to_s,
        :dns_policy     => pod.spec.dnsPolicy
      )
      # TODO, map volumes
      # TODO, podIP
      containers = pod.spec.containers
      new_result[:container_definitions] = containers.collect do |container_def|
        parse_container_definition(container_def, pod.metadata.uid)
      end

      # container instances<commit_msg>kubernetes: handle new pod restartPolicy format<commit_after>module EmsRefresh::Parsers
  class Kubernetes
    def self.ems_inv_to_hashes(inventory)
      new.ems_inv_to_hashes(inventory)
    end

    def initialize
      @data = {}
    end

    def ems_inv_to_hashes(inventory)
      get_nodes(inventory)
      get_services(inventory)
      get_pods(inventory)
      EmsRefresh.log_inv_debug_trace(@data, "data:")
      @data
    end

    def get_nodes(inventory)
      process_collection(inventory["node"], :container_nodes) { |n| parse_node(n) }
    end

    def get_services(inventory)
      process_collection(inventory["service"], :container_services) { |s| parse_service(s) }
    end

    def get_pods(inventory)
      process_collection(inventory["pod"], :container_groups) { |n| parse_pod(n) }
    end

    def process_collection(collection, key, &block)
      @data[key] ||= []
      collection.each { |item| process_collection_item(item, key, &block) }
    end

    def process_collection_item(item, key)
      @data[key] ||= []

      new_result = yield(item)

      @data[key] << new_result
      new_result
    end

    def parse_node(node)
      new_result = parse_base_item(node)
      # TODO, add more properties such as resources
      new_result
    end

    def parse_service(service)
      new_result = parse_base_item(service)
      new_result.merge!(
        :port             => service.spec.port,
        :protocol         => service.spec.protocol,
        :portal_ip        => service.spec.portalIP,
        :container_port   => service.spec.containerPort,
        :namespace        => service.metadata.instance_values["table"][:namespace],
        :session_affinity => service.spec.sessionAffinity,

        :labels           => parse_labels(service),
        :selector_parts   => parse_selector_parts(service)
      )
      new_result
    end

    def parse_pod(pod)
      # pod in kubernetes is container group in manageiq
      new_result = parse_base_item(pod)
      # TODO: remove hash support when old versions are not in use anymore
      #       https://github.com/GoogleCloudPlatform/kubernetes/issues/3607
      if pod.spec.restartPolicy.kind_of?(Hash)
        pod_restart_policy = pod.spec.restartPolicy.keys.first.to_s
      else
        pod_restart_policy = pod.spec.restartPolicy
      end
      new_result.merge!(
        # namespace is overriden in more_core_extensions and hence needs a non method access
        :namespace      => pod.metadata.instance_values["table"][:namespace],
        :restart_policy => pod_restart_policy,
        :dns_policy     => pod.spec.dnsPolicy
      )
      # TODO, map volumes
      # TODO, podIP
      containers = pod.spec.containers
      new_result[:container_definitions] = containers.collect do |container_def|
        parse_container_definition(container_def, pod.metadata.uid)
      end

      # container instances
{'fraction_non_alphanumeric': 0.09599422591122339, 'fraction_numerical': 0.001443522194153735, 'mean_word_length': 2.775885558583106, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5520321', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1652, 'n_words': 321}