<commit_before>require 'stackbuilder/stacks/kubernetes/namespace'
require 'stackbuilder/stacks/maintainers'
require 'erb'

module Stacks::Services::BaseK8sApp
  include Stacks::Kubernetes::ResourceSetApp
  include Stacks::Maintainers

  attr_accessor :ports
  attr_accessor :ephemeral_storage_size
  attr_accessor :cpu_request
  attr_accessor :cpu_limit
  attr_accessor :memory_limit

  attr_accessor :command
  attr_accessor :args
  attr_accessor :capabilities
  attr_accessor :readiness

  attr_accessor :appconfig

  attr_accessor :alerts_channel
  attr_accessor :startup_alert_threshold
  attr_accessor :monitor_tucker
  attr_accessor :monitor_readiness_probe
  attr_accessor :page_on_critical

  attr_accessor :application
  attr_accessor :description
  attr_accessor :maintainers

  def self.extended(object)
    object.configure
  end

  def configure
    @ports = {}
    @ephemeral_storage_size = nil
    @maintainers = []
    @cpu_request = false
    @cpu_limit = false
    @monitor_readiness_probe = true
    @monitor_tucker = false
    @page_on_critical = false
    @artifact_from_nexus = false
    @memory_limit = '64Mi'
    @command = nil
    @args = nil
    @capabilities = nil
    @readiness = nil
    @appconfig = nil
  end

  def to_k8s(app_deployer, dns_resolver, hiera_provider)
    assert_k8s_requirements

    instances = if @instances.is_a?(Hash)
                  @instances
                else
                  { @environment.sites.first => @instances }
                end

    instances.map do |site, replicas|
      app_name = application.downcase
      group = @groups.first

      begin
        app_version = app_deployer.query_cmdb_for(:application => application,
                                                  :environment => @environment.name,
                                                  :group => group)[:target_version]
      rescue
        raise("Version not found in cmdb for application: '#{application}', group: '#{group}' in environment: '#{environment.name}'")
      end

      domain = "mgmt.#{environment.domain(site)}"

      hiera_scope = {
        'domain' => domain,
        'hostname' => kubernetes ? identity : children.first.hostname,
        'application' => application,
        'stackname' => @stack.name,
        'logicalenv' => @environment.name,
        'group' => group,
        'site' => site
      }

      fake_machine_instance = Struct.new(:index).new(0)
      erb_vars = {
        'dependencies' => dependency_config(site, fake_machine_instance),
        'credentials_selector' => hiera_provider.lookup(hiera_scope, 'stacks/application_credentials_selector', nil)
      }.merge(hiera_scope)

      config, used_secrets = generate_app_config(erb_vars, hiera_provider)

      output = super app_deployer, dns_resolver, hiera_provider, service_adjusted_labels
      output += app_generate_resources(app_deployer, dns_resolver, hiera_provider, hiera_scope, app_name, app_version, replicas, used_secrets, site, \
                                       standard_labels, service_adjusted_labels, k8s_app_resources_name, config)

      pp "base k8 app"
      pp output<commit_msg>wtaj/mshah - remove printouts (learnt they cause diffs in stack compile)<commit_after>require 'stackbuilder/stacks/kubernetes/namespace'
require 'stackbuilder/stacks/maintainers'
require 'erb'

module Stacks::Services::BaseK8sApp
  include Stacks::Kubernetes::ResourceSetApp
  include Stacks::Maintainers

  attr_accessor :ports
  attr_accessor :ephemeral_storage_size
  attr_accessor :cpu_request
  attr_accessor :cpu_limit
  attr_accessor :memory_limit

  attr_accessor :command
  attr_accessor :args
  attr_accessor :capabilities
  attr_accessor :readiness

  attr_accessor :appconfig

  attr_accessor :alerts_channel
  attr_accessor :startup_alert_threshold
  attr_accessor :monitor_tucker
  attr_accessor :monitor_readiness_probe
  attr_accessor :page_on_critical

  attr_accessor :application
  attr_accessor :description
  attr_accessor :maintainers

  def self.extended(object)
    object.configure
  end

  def configure
    @ports = {}
    @ephemeral_storage_size = nil
    @maintainers = []
    @cpu_request = false
    @cpu_limit = false
    @monitor_readiness_probe = true
    @monitor_tucker = false
    @page_on_critical = false
    @artifact_from_nexus = false
    @memory_limit = '64Mi'
    @command = nil
    @args = nil
    @capabilities = nil
    @readiness = nil
    @appconfig = nil
  end

  def to_k8s(app_deployer, dns_resolver, hiera_provider)
    assert_k8s_requirements

    instances = if @instances.is_a?(Hash)
                  @instances
                else
                  { @environment.sites.first => @instances }
                end

    instances.map do |site, replicas|
      app_name = application.downcase
      group = @groups.first

      begin
        app_version = app_deployer.query_cmdb_for(:application => application,
                                                  :environment => @environment.name,
                                                  :group => group)[:target_version]
      rescue
        raise("Version not found in cmdb for application: '#{application}', group: '#{group}' in environment: '#{environment.name}'")
      end

      domain = "mgmt.#{environment.domain(site)}"

      hiera_scope = {
        'domain' => domain,
        'hostname' => kubernetes ? identity : children.first.hostname,
        'application' => application,
        'stackname' => @stack.name,
        'logicalenv' => @environment.name,
        'group' => group,
        'site' => site
      }

      fake_machine_instance = Struct.new(:index).new(0)
      erb_vars = {
        'dependencies' => dependency_config(site, fake_machine_instance),
        'credentials_selector' => hiera_provider.lookup(hiera_scope, 'stacks/application_credentials_selector', nil)
      }.merge(hiera_scope)

      config, used_secrets = generate_app_config(erb_vars, hiera_provider)

      output = super app_deployer, dns_resolver, hiera_provider, service_adjusted_labels
      output += app_generate_resources(app_deployer, dns_resolver, hiera_provider, hiera_scope, app_name, app_version, replicas, used_secrets, site, \
                                       standard_labels, service_adjusted_labels, k8s_app_resources_name, config)

{'fraction_non_alphanumeric': 0.08621236133122029, 'fraction_numerical': 0.002377179080824089, 'mean_word_length': 2.9666876178504085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '247194', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1866, 'n_words': 376}