Class: Kubeclient::Config

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

Defined Under Namespace

Classes: Context

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kcfg, kcfg_path) ⇒ Config

Returns a new instance of Config.



20
21
22
23
24
# File 'lib/kubeclient/config.rb', line 20

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

Class Method Details

.read(filename) ⇒ Object



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

def self.read(filename)
  Config.new(YAML.load_file(filename), File.dirname(filename))
end

Instance Method Details

#context(context_name = nil) ⇒ Object



34
35
36
37
38
39
40
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
# File 'lib/kubeclient/config.rb', line 34

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

  ca_cert_data     = fetch_cluster_ca_data(cluster)
  client_cert_data = fetch_user_cert_data(user)
  client_key_data  = fetch_user_key_data(user)
  client_token     = fetch_user_token(user)

  ssl_options = {}
  auth_options = {}

  unless 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[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE
    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

  unless client_token.nil?
    auth_options[:bearer_token] = client_token
  end

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

#contextsObject



30
31
32
# File 'lib/kubeclient/config.rb', line 30

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