Class: KubeconfigService

Inherits:
Object
  • Object
show all
Extended by:
ApiClient
Defined in:
lib/uffizzi/services/kubeconfig_service.rb

Defined Under Namespace

Classes: InvalidKubeconfigError

Constant Summary collapse

KUBECONFIG_GENERAL_KEYS =
['apiVersion', 'clusters', 'contexts', 'current-context', 'kind', 'users'].freeze

Class Method Summary collapse

Methods included from ApiClient

bulk_create_secrets, check_can_install, check_credential, create_access_token, create_account_controller_settings, create_ci_session, create_cluster, create_credential, create_deployment, create_project, create_session, delete_account_controller_settings, delete_cluster, delete_credential, delete_deployment, delete_project, delete_secret, deploy_containers, describe_compose_file, describe_deployment, destroy_session, fetch_account, fetch_account_projects, fetch_accounts, fetch_credentials, fetch_deployment_service_logs, fetch_deployment_services, fetch_deployments, fetch_events, fetch_project, fetch_projects, fetch_secrets, get_access_token, get_account_clusters, get_account_controller_settings, get_activity_items, get_cluster, get_cluster_ingresses, get_k8s_container_description, get_project_clusters, scale_down_cluster, scale_up_cluster, set_compose_file, sync_cluster, unset_compose_file, update_account, update_account_controller_settings, update_credential, update_deployment, validate_compose_file

Methods included from ApiRoutes

#access_token_url, #access_tokens_url, #account_can_install_uri, #account_clusters_uri, #account_controller_setting_uri, #account_controller_settings_uri, #account_projects_uri, #account_uri, #accounts_uri, #activity_items_uri, #browser_sign_in_url, #check_credential_uri, #ci_session_uri, #cluster_uri, #compose_file_uri, #create_projects_uri, #credential_uri, #credentials_uri, #deploy_containers_uri, #deployment_uri, #deployments_uri, #events_uri, #k8s_container_description_uri, #preview_service_logs_uri, #preview_services_uri, #project_cluster_ingresses_uri, #project_clusters_uri, #project_uri, #projects_uri, #scale_down_cluster_uri, #scale_up_cluster_uri, #secret_uri, #secrets_uri, #session_uri, #sync_cluster_uri, #validate_compose_file_uri

Class Method Details

.default_pathObject



89
90
91
92
93
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 89

def default_path
  path = kubeconfig_env_path || Uffizzi.configuration.default_kubeconfig_path

  File.expand_path(path)
end

.exclude(target_kubeconfig, source_kubeconfig) ⇒ Object



32
33
34
35
36
37
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 32

def exclude(target_kubeconfig, source_kubeconfig)
  return if target_kubeconfig.nil?

  excludable_cluster_name = get_current_cluster_name(source_kubeconfig)
  exclude_by_cluster_name(target_kubeconfig, excludable_cluster_name)
end

.find_cluster_contexts_by_name(kubeconfig, context_name) ⇒ Object



63
64
65
66
67
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 63

def find_cluster_contexts_by_name(kubeconfig, context_name)
  return if context_name.nil?

  get_cluster_contexts(kubeconfig).detect { |c| c['name'] == context_name }
end

.get_cluster_contexts(kubeconfig) ⇒ Object



59
60
61
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 59

def get_cluster_contexts(kubeconfig)
  kubeconfig.fetch('contexts', [])
end

.get_current_cluster_name(kubeconfig) ⇒ Object



47
48
49
50
51
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 47

def get_current_cluster_name(kubeconfig)
  get_cluster_contexts(kubeconfig)
    .detect { |c| c['name'] == get_current_context(kubeconfig) }
    .dig('context', 'cluster')
end

.get_current_context(kubeconfig) ⇒ Object



39
40
41
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 39

def get_current_context(kubeconfig)
  kubeconfig['current-context']
end

.get_current_namespace(kubeconfig) ⇒ Object



53
54
55
56
57
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 53

def get_current_namespace(kubeconfig)
  get_cluster_contexts(kubeconfig)
    .detect { |c| c['name'] == get_current_context(kubeconfig) }
    .dig('context', 'namespace')
end

.get_first_context(kubeconfig) ⇒ Object



43
44
45
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 43

def get_first_context(kubeconfig)
  get_cluster_contexts(kubeconfig)[0]
end

.merge(target_kubeconfig, source_kubeconfig) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 20

def merge(target_kubeconfig, source_kubeconfig)
  return source_kubeconfig.deep_dup if target_kubeconfig.nil?

  new_cluster_name = get_current_cluster_name(source_kubeconfig)

  if cluster_exists_in_kubeconfig?(target_kubeconfig, new_cluster_name)
    replace_by_cluster_name(target_kubeconfig, source_kubeconfig, new_cluster_name)
  else
    add(target_kubeconfig, source_kubeconfig)
  end
end

.read_kubeconfig(filepath) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 95

def read_kubeconfig(filepath)
  real_file_path = File.expand_path(filepath)
  kubeconfig = File.exist?(real_file_path) ? Psych.safe_load(File.read(real_file_path)) : nil

  if kubeconfig.present? && !valid_kubeconfig?(kubeconfig)
    raise InvalidKubeconfigError.new(filepath)
  end

  kubeconfig
end

.save_to_filepath(filepath, kubeconfig = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 76

def save_to_filepath(filepath, kubeconfig = nil)
  target_kubeconfig = read_kubeconfig(filepath)

  if target_kubeconfig.present? && !valid_kubeconfig?(target_kubeconfig)
    raise InvalidKubeconfigError.new(filepath)
  end

  new_kubeconfig = block_given? ? yield(target_kubeconfig) : kubeconfig
  return if new_kubeconfig.nil?

  write_kubeconfig(filepath, new_kubeconfig)
end

.update_current_context(kubeconfig, current_context) ⇒ Object



69
70
71
72
73
74
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 69

def update_current_context(kubeconfig, current_context)
  new_kubeconfig = kubeconfig.deep_dup
  new_kubeconfig['current-context'] = current_context

  new_kubeconfig
end

.write_kubeconfig(filepath, kubeconfig) ⇒ Object



106
107
108
109
110
111
# File 'lib/uffizzi/services/kubeconfig_service.rb', line 106

def write_kubeconfig(filepath, kubeconfig)
  real_file_path = File.expand_path(filepath)
  dir_path = File.dirname(real_file_path)
  FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
  File.write(real_file_path, kubeconfig.to_yaml)
end