Class: ClusterDisconnectService

Inherits:
Object
  • Object
show all
Defined in:
lib/uffizzi/services/cluster/disconnect_service.rb

Class Method Summary collapse

Class Method Details

.handle(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/uffizzi/services/cluster/disconnect_service.rb', line 9

def handle(options)
  kubeconfig_path = options[:kubeconfig] || KubeconfigService.default_path
  is_ask_origin_current_context = options[:ask]
  kubeconfig = KubeconfigService.read_kubeconfig(kubeconfig_path)

  if kubeconfig.nil?
    return Uffizzi.ui.say("Kubeconfig does not exist by path #{kubeconfig_path}")
  end

  contexts = KubeconfigService.get_cluster_contexts(kubeconfig)

  if contexts.empty?
    return Uffizzi.ui.say("No contexts by kubeconfig path #{kubeconfig_path}")
  end

  prev_current_context = Uffizzi::ConfigHelper.previous_current_context_by_path(kubeconfig_path)&.fetch(:current_context, nil)
  current_context = KubeconfigService.get_current_context(kubeconfig)

  if KubeconfigService.find_cluster_contexts_by_name(kubeconfig, prev_current_context).present? &&
      prev_current_context != current_context &&
      !is_ask_origin_current_context
    update_current_context_by_filepath(kubeconfig_path, prev_current_context)
    say_success(prev_current_context, kubeconfig_path)
    return
  end

  new_current_context = ask_context(contexts, current_context)
  update_current_context_by_filepath(kubeconfig_path, new_current_context)
  set_previous_current_context_to_config(kubeconfig_path, new_current_context)
  say_success(new_current_context, kubeconfig_path)
end