Class: Kubeclient::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/kubeclient/config.rb

Overview

Kubernetes client configuration class

Defined Under Namespace

Classes: Context

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, kcfg_path) ⇒ Config

data (Hash) - Parsed kubeconfig data. kcfg_path (string) - Base directory for resolving relative references to external files.

If set to nil, all external lookups & commands are disabled (even for absolute paths).

See also the more convenient Config.read



25
26
27
28
29
# File 'lib/kubeclient/config.rb', line 25

def initialize(data, kcfg_path)
  @kcfg = data
  @kcfg_path = kcfg_path
  raise 'Unknown kubeconfig version' if @kcfg['apiVersion'] != 'v1'
end

Class Method Details

.read(filename) ⇒ Object

Builds Config instance by parsing given file, with lookups relative to file’s directory.



32
33
34
35
# File 'lib/kubeclient/config.rb', line 32

def self.read(filename)
  parsed = YAML.safe_load(File.read(filename), [Date, Time])
  Config.new(parsed, File.dirname(filename))
end

Instance Method Details

#context(context_name = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kubeclient/config.rb', line 41

def context(context_name = nil)
  cluster, user, namespace = fetch_context(context_name || @kcfg['current-context'])

  if user.key?('exec')
    exec_opts = expand_command_option(user['exec'], 'command')
    user['exec_result'] = ExecCredentials.run(exec_opts)
  end

  ca_cert_data     = fetch_cluster_ca_data(cluster)
  client_cert_data = fetch_user_cert_data(user)
  client_key_data  = fetch_user_key_data(user)
  auth_options     = fetch_user_auth_options(user)

  ssl_options = {}

  if !ca_cert_data.nil?
    cert_store = OpenSSL::X509::Store.new
    cert_store.add_cert(OpenSSL::X509::Certificate.new(ca_cert_data))
    ssl_options[:verify_ssl] = OpenSSL::SSL::VERIFY_PEER
    ssl_options[:cert_store] = cert_store
  else
    ssl_options[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE
  end

  unless client_cert_data.nil?
    ssl_options[:client_cert] = OpenSSL::X509::Certificate.new(client_cert_data)
  end

  unless client_key_data.nil?
    ssl_options[:client_key] = OpenSSL::PKey.read(client_key_data)
  end

  Context.new(cluster['server'], @kcfg['apiVersion'], ssl_options, auth_options, namespace)
end

#contextsObject



37
38
39
# File 'lib/kubeclient/config.rb', line 37

def contexts
  @kcfg['contexts'].map { |x| x['name'] }
end