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
]

Instance Method Summary collapse

Instance Method Details

#deploy(shell, service) ⇒ Object



16
17
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
# File 'lib/kuber_kit/service_deployer/strategies/kubernetes.rb', line 16

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

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

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