Class: Kubernetes::KubeConfig

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

Overview

rubocop:disable ClassLength The KubeConfig class represents configuration based on a YAML representation.

Constant Summary collapse

KUBE_CONFIG_DEFAULT_LOCATION =
File.expand_path('~/.kube/config')
AUTH_KEY =
'authorization'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, config_hash = nil) ⇒ KubeConfig

Returns a new instance of KubeConfig.



43
44
45
46
# File 'lib/kubernetes/config/kube_config.rb', line 43

def initialize(path = nil, config_hash = nil)
  @path = path
  @config = config_hash
end

Instance Attribute Details

#configObject



52
53
54
55
56
# File 'lib/kubernetes/config/kube_config.rb', line 52

def config
  @config ||= File.open(path) do |io|
    ::YAML.safe_load(io.read)
  end
end

#pathObject

Returns the value of attribute path.



40
41
42
# File 'lib/kubernetes/config/kube_config.rb', line 40

def path
  @path
end

Class Method Details

.list_context_names(config_file = KUBE_CONFIG_DEFAULT_LOCATION) ⇒ Object



34
35
36
37
# File 'lib/kubernetes/config/kube_config.rb', line 34

def list_context_names(config_file = KUBE_CONFIG_DEFAULT_LOCATION)
  config = new(config_file)
  config.list_context_names
end

Instance Method Details

#base_pathObject



48
49
50
# File 'lib/kubernetes/config/kube_config.rb', line 48

def base_path
  File.dirname(path)
end

#configure(configuration, context_name = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kubernetes/config/kube_config.rb', line 58

def configure(configuration, context_name = nil)
  context = context_name ? find_context(context_name) : current_context
  return unless context

  user = context['user'] || {}
  cluster = context['cluster'] || {}

  configuration.tap do |c|
    c.api_key[AUTH_KEY] = user[AUTH_KEY] if user[AUTH_KEY]

    init_server(cluster, user, c)
  end
end

#current_contextObject



155
156
157
158
159
# File 'lib/kubernetes/config/kube_config.rb', line 155

def current_context
  return unless config

  find_context(config['current-context'])
end

#find_cluster(name) ⇒ Object



98
99
100
101
102
103
# File 'lib/kubernetes/config/kube_config.rb', line 98

def find_cluster(name)
  find_by_name(config['clusters'], 'cluster', name).tap do |cluster|
    Kubernetes.create_temp_file_and_set(cluster, 'certificate-authority')
    cluster['verify_ssl'] = !cluster['insecure-skip-tls-verify']
  end
end

#find_context(name) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/kubernetes/config/kube_config.rb', line 144

def find_context(name)
  find_by_name(config['contexts'], 'context', name).tap do |context|
    if context['cluster']
      context['cluster'] = find_cluster(context['cluster'])
    end
    if context['user'] && !context['user'].empty?
      context['user'] = find_user(context['user'])
    end
  end
end

#find_user(name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/kubernetes/config/kube_config.rb', line 105

def find_user(name)
  find_by_name(config['users'], 'user', name).tap do |user|
    next unless user

    Kubernetes.create_temp_file_and_set(user, 'client-certificate')
    Kubernetes.create_temp_file_and_set(user, 'client-key')
    load_token_file(user)
    setup_auth(user)
  end
end

#init_server(cluster, user, config) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kubernetes/config/kube_config.rb', line 72

def init_server(cluster, user, config)
  return unless (server = cluster['server'])

  server = URI.parse(server)
  config.scheme = server.scheme
  host = "#{server.host}:#{server.port}"
  host = "#{server.userinfo}@#{host}" if server.userinfo
  config.host = host
  config.base_path = server.path

  return unless server.scheme == 'https'

  setup_ssl(cluster, user, config)
end

#list_context_namesObject

rubocop:enable AbcSize



140
141
142
# File 'lib/kubernetes/config/kube_config.rb', line 140

def list_context_names
  config['contexts'].map { |e| e['name'] }
end

#load_token_file(user) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/kubernetes/config/kube_config.rb', line 116

def load_token_file(user)
  # If tokenFile is specified, then set token
  return unless !user['token'] && user['tokenFile']

  File.open(user['tokenFile']) do |io|
    user['token'] = io.read.chomp
  end
end

#setup_auth(user) ⇒ Object

rubocop:disable AbcSize



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kubernetes/config/kube_config.rb', line 126

def setup_auth(user)
  # Convert token field to http header
  if user['token']
    user['authorization'] = "Bearer #{user['token']}"
  elsif user['username'] && user['password']
    user_pass = "#{user['username']}:#{user['password']}"
    user['authorization'] = "Basic #{Base64.strict_encode64(user_pass)}"
  elsif user['auth-provider'] && user['auth-provider']['name'] == 'azure'
    token = user['auth-provider']['config']['access-token']
    user['authorization'] = "Bearer #{token}"
  end
end

#setup_ssl(cluster, user, config) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/kubernetes/config/kube_config.rb', line 87

def setup_ssl(cluster, user, config)
  # rubocop:disable DoubleNegation
  config.verify_ssl = !!cluster['verify-ssl']
  config.verify_ssl_host = !!cluster['verify-ssl']
  # rubocop:enable DoubleNegation

  config.ssl_ca_cert = cluster['certificate-authority']
  config.cert_file = user['client-certificate']
  config.key_file = user['client-key']
end