class KuberKit::ServiceDeployer::Strategies::Kubernetes

Constants

STRATEGY_OPTIONS

Public Instance Methods

deploy(shell, service) click to toggle source
# File lib/kuber_kit/service_deployer/strategies/kubernetes.rb, line 17
def deploy(shell, service)
  service_config = reader.read(shell, service)
  config_path    = "#{configs.service_config_dir}/#{service.name}.yml"
  shell.write(config_path, service_config)

  kubeconfig_path = KuberKit.current_configuration.kubeconfig_path
  namespace       = KuberKit.current_configuration.deployer_namespace

  strategy_options = service.attribute(:deployer, default: {})
  unknown_options  = strategy_options.keys.map(&:to_sym) - STRATEGY_OPTIONS
  if unknown_options.any?
    raise KuberKit::Error, "Unknow options for deploy strategy: #{unknown_options}. Available options: #{STRATEGY_OPTIONS}"
  end
  
  resource_type = strategy_options.fetch(:resource_type, "deployment")
  resource_name = strategy_options.fetch(:resource_name, service.uri)
  
  resource_exists = kubectl_commands.resource_exists?(
    shell, resource_type, resource_name, kubeconfig_path: kubeconfig_path, namespace: namespace
  )

  delete_enabled = strategy_options.fetch(:delete_if_exists, false)
  if delete_enabled && resource_exists
    kubectl_commands.delete_resource(shell, resource_type, resource_name, kubeconfig_path: kubeconfig_path, namespace: namespace)
  end

  apply_result = kubectl_commands.apply_file(shell, config_path, kubeconfig_path: kubeconfig_path, namespace: namespace)

  restart_enabled  = strategy_options.fetch(:restart_if_exists, true)
  wait_for_rollout = strategy_options.fetch(:wait_for_rollout, true)
  if restart_enabled && resource_exists
    kubectl_commands.rolling_restart(
      shell, resource_type, resource_name, 
      kubeconfig_path: kubeconfig_path, namespace: namespace
    )
    
    kubectl_commands.rollout_status(
      shell, resource_type, resource_name, wait: true,
      kubeconfig_path: kubeconfig_path, namespace: namespace
    ) if wait_for_rollout
  end
  
  apply_result
end