class Dapp::Kube::Kubernetes::Config

Attributes

config_hash[R]
config_path[R]

Public Class Methods

kubectl_available?() click to toggle source
# File lib/dapp/kube/kubernetes/config.rb, line 41
def kubectl_available?
  shellout("kubectl").exitstatus.zero?
end
new(config_hash, config_path) click to toggle source
# File lib/dapp/kube/kubernetes/config.rb, line 59
def initialize(config_hash, config_path)
  @config_hash = config_hash
  @config_path = config_path
end
new_auto() click to toggle source
# 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_available() click to toggle source
# 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) click to toggle source
# 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_kubectl() click to toggle source
# 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

Public Instance Methods

cluster_config(cluster_name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_names() click to toggle source
# File lib/dapp/kube/kubernetes/config.rb, line 64
def context_names
  config_hash.fetch('contexts', []).map {|context| context['name']}
end
current_context_name() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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