Class: Dapp::Kube::Kubernetes::Config

Inherits:
Object
  • Object
show all
Extended by:
Helper::YAML, Shellout::Base
Defined in:
lib/dapp/kube/kubernetes/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::YAML

yaml_load, yaml_load_file

Constructor Details

#initialize(config_hash, config_path) ⇒ Config

Returns a new instance of Config.



59
60
61
62
# File 'lib/dapp/kube/kubernetes/config.rb', line 59

def initialize(config_hash, config_path)
  @config_hash = config_hash
  @config_path = config_path
end

Instance Attribute Details

#config_hashObject (readonly)

<< self



57
58
59
# File 'lib/dapp/kube/kubernetes/config.rb', line 57

def config_hash
  @config_hash
end

#config_pathObject (readonly)

<< self



57
58
59
# File 'lib/dapp/kube/kubernetes/config.rb', line 57

def config_path
  @config_path
end

Class Method Details

.kubectl_available?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dapp/kube/kubernetes/config.rb', line 41

def kubectl_available?
  shellout("kubectl").exitstatus.zero?
end

.new_autoObject



22
23
24
25
26
27
28
29
# File 'lib/dapp/kube/kubernetes/config.rb', line 22

def new_auto
  new_auto_if_available.tap do |cfg|
    raise(Kubernetes::Error::Default,
      code: :config_not_found,
      data: { },
    ) if cfg.nil?
  end
end

.new_auto_if_availableObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dapp/kube/kubernetes/config.rb', line 9

def new_auto_if_available
  if Kubernetes::Config.kubectl_available?
    Kubernetes::Config.new_from_kubectl
  elsif ENV['KUBECONFIG']
    Kubernetes::Config.new_from_kubeconfig(ENV['KUBECONFIG'])
  else
    default_path = File.join(ENV['HOME'], '.kube/config')
    if File.exists? default_path
      Kubernetes::Config.new_from_kubeconfig(default_path)
    end
  end
end

.new_from_kubeconfig(path) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/dapp/kube/kubernetes/config.rb', line 31

def new_from_kubeconfig(path)
  unless File.exists?(path)
    raise(Kubernetes::Error::Default,
      code: :config_file_not_found,
      data: { config_path: path }
    )
  end
  self.new yaml_load_file(path), path
end

.new_from_kubectlObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/dapp/kube/kubernetes/config.rb', line 45

def new_from_kubectl
  cmd_res = shellout(
    "kubectl config view --raw",
    env: {"KUBECONFIG" => ENV["KUBECONFIG"]}
  )

  shellout_cmd_should_succeed! cmd_res

  self.new YAML.load(cmd_res.stdout), "kubectl config view --raw"
end

Instance Method Details

#cluster_config(cluster_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dapp/kube/kubernetes/config.rb', line 106

def cluster_config(cluster_name)
  res = config_hash.fetch('clusters', [])
    .find {|cluster| cluster['name'] == cluster_name}

  raise(Kubernetes::Error::Default,
    code: :cluster_config_not_found,
    data: {config_path: config_path,
          cluster: cluster_name}
  ) if res.nil?

  res['cluster']
end

#cluster_name(context_name) ⇒ Object



119
120
121
122
# File 'lib/dapp/kube/kubernetes/config.rb', line 119

def cluster_name(context_name)
  cfg = context_config(context_name)
  cfg['cluster'] if cfg
end

#context_config(context_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dapp/kube/kubernetes/config.rb', line 79

def context_config(context_name)
  res = config_hash.fetch('contexts', [])
    .find {|context| context['name'] == context_name}

  raise(Kubernetes::Error::Default,
    code: :context_config_not_found,
    data: {config_path: config_path,
            config: config_hash,
            context_name: context_name}
  ) if res.nil?

  res['context']
end

#context_namesObject



64
65
66
# File 'lib/dapp/kube/kubernetes/config.rb', line 64

def context_names
  config_hash.fetch('contexts', []).map {|context| context['name']}
end

#current_context_nameObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/dapp/kube/kubernetes/config.rb', line 68

def current_context_name
  @current_context_name ||= begin
    config_hash['current-context'] || begin
      if (context = config_hash.fetch('contexts', []).first)
        warn "[WARN] .kube/config current-context is not set, using first context '#{context['name']}'"
        context['name']
      end
    end
  end
end

#namespace(context_name) ⇒ Object



124
125
126
127
# File 'lib/dapp/kube/kubernetes/config.rb', line 124

def namespace(context_name)
  cfg = context_config(context_name)
  cfg['namespace'] if cfg
end

#user_config(user_name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dapp/kube/kubernetes/config.rb', line 93

def user_config(user_name)
  res = config_hash.fetch('users', [])
    .find {|user| user['name'] == user_name}

  raise(Kubernetes::Error::Default,
    code: :user_config_not_found,
    data: {config_path: config_path,
            user: user_name}
  ) if res.nil?

  res['user']
end