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



27
28
29
30
31
# File 'lib/kubeclient/config.rb', line 27

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.



34
35
36
37
38
39
40
41
42
# File 'lib/kubeclient/config.rb', line 34

def self.read(filename)
  parsed =
    if RUBY_VERSION >= '2.6'
      YAML.safe_load(File.read(filename), permitted_classes: [Date, Time])
    else
      YAML.safe_load(File.read(filename), [Date, Time])
    end
  Config.new(parsed, File.dirname(filename))
end

Instance Method Details

#context(context_name = nil) ⇒ Object



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
75
76
77
78
79
80
81
82
83
# File 'lib/kubeclient/config.rb', line 48

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

  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 = {}

  ssl_options[:verify_ssl] = if cluster['insecure-skip-tls-verify'] == true
                               OpenSSL::SSL::VERIFY_NONE
                             else
                               OpenSSL::SSL::VERIFY_PEER
                             end

  if cluster_ca_data?(cluster)
    cert_store = OpenSSL::X509::Store.new
    populate_cert_store_from_cluster_ca_data(cluster, cert_store)
    ssl_options[:cert_store] = cert_store
  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



44
45
46
# File 'lib/kubeclient/config.rb', line 44

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