Class: KuberKit::ServiceDeployer::Strategies::Kubernetes

Inherits:
Abstract show all
Defined in:
lib/kuber_kit/service_deployer/strategies/kubernetes.rb

Constant Summary collapse

STRATEGY_OPTIONS =
[
  :resource_type,
  :resource_name,
  :delete_if_exists,
  :restart_if_exists,
  :wait_for_rollout,
  :apply_command
]

Instance Method Summary collapse

Instance Method Details

#deploy(shell, service) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kuber_kit/service_deployer/strategies/kubernetes.rb', line 18

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,
    apply_command: strategy_options.fetch(:apply_command, "apply")
  )

  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