Class: K8s::Config

Inherits:
RecursiveOpenStruct
  • Object
show all
Includes:
KeyTransformations
Defined in:
lib/k8s/config.rb

Overview

Common struct type for kubeconfigs:

  • converts string keys to symbols

  • normalizes foo-bar to foo_bar

Defined Under Namespace

Modules: KeyTransformations Classes: Child

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KeyTransformations

#initialize, #to_h

Class Method Details

.build(server:, ca:, auth_token:, cluster_name: 'kubernetes', user: 'k8s-client', context: 'k8s-client', **options) ⇒ Object

Build a minimal configuration from at least a server address, server certificate authority data and an access token.

Parameters:

  • server (String)

    kubernetes server address

  • ca (String)

    server certificate authority data (base64 encoded)

  • token (String)

    access token

  • cluster_name (String) (defaults to: 'kubernetes')

    cluster name

  • user (String) (defaults to: 'k8s-client')

    user name

  • context (String) (defaults to: 'k8s-client')

    context name

  • options (Hash)

    (see #initialize)



80
81
82
83
84
85
86
87
88
89
# File 'lib/k8s/config.rb', line 80

def self.build(server:, ca:, auth_token:, cluster_name: 'kubernetes', user: 'k8s-client', context: 'k8s-client', **options)
  new(
    {
      clusters: [{ name: cluster_name, cluster: { server: server, certificate_authority_data: ca } }],
      users: [{ name: user, user: { token: auth_token } }],
      contexts: [{ name: context, context: { cluster: cluster_name, user: user } }],
      current_context: context
    }.merge(options)
  )
end

.defaultsObject



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

def self.defaults
  {
    :apiVersion => 'v1',
    :clusters=> [],
    :contexts => [],
    :current_context => nil,
    :kind => 'Config',
    :preferences => {},
    :users => []
  }
end

.from_kubeconfig_env(kubeconfig = nil) ⇒ Object

Loads configuration files listed in KUBE_CONFIG environment variable and merge using the configuration merge rules, @see K8s::Config.merge

Parameters:

  • kubeconfig (String) (defaults to: nil)

    by default read from ENV

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
# File 'lib/k8s/config.rb', line 60

def self.from_kubeconfig_env(kubeconfig = nil)
  kubeconfig ||= ENV.fetch('KUBECONFIG', '')
  raise ArgumentError, "KUBECONFIG not set" if kubeconfig.empty?

  paths = kubeconfig.split(/(?!\\):/)

  paths.inject(load_file(paths.shift)) do |memo, other_cfg|
    memo.merge(load_file(other_cfg))
  end
end

.load_file(path) ⇒ K8s::Config

Loads a configuration from a YAML file

Parameters:

  • path (String)

Returns:



49
50
51
52
53
54
# File 'lib/k8s/config.rb', line 49

def self.load_file(path)
  path = File.expand_path(path)
  yaml = File.read(path)
  hash = YAML.safe_load yaml, permitted_classes: [Time, DateTime, Date], aliases: true
  new(hash)
end

Instance Method Details

#cluster(name = context&.cluster) ⇒ K8s::Config::Child

Parameters:

  • name (String) (defaults to: context&.cluster)

Returns:

Raises:



137
138
139
140
141
# File 'lib/k8s/config.rb', line 137

def cluster(name = context&.cluster)
  return nil if name.nil?

  clusters.find { |cluster| cluster.name == name }&.cluster || raise(K8s::Error::Configuration, "cluster not found: #{name.inspect}")
end

#context(name = current_context) ⇒ K8s::Config::Child

Parameters:

  • name (String) (defaults to: current_context)

Returns:

Raises:



128
129
130
131
132
# File 'lib/k8s/config.rb', line 128

def context(name = current_context)
  return nil if name.nil?

  contexts.find { |context| context.name == name }&.context || raise(K8s::Error::Configuration, "context not found: #{name.inspect}")
end

#merge(other) ⇒ K8s::Config

Parameters:

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/k8s/config.rb', line 96

def merge(other)
  old_attributes = to_h
  other_attributes = other.is_a?(Hash) ? other : other.to_h

  old_attributes.merge!(other_attributes) do |key, old_value, new_value|
    case key
    when :clusters, :contexts, :users
      old_value + new_value.reject do |new_mapping|
        old_value.any? { |old_mapping| old_mapping[:name] == new_mapping[:name] }
      end
    else
      case old_value
      when Array
        (old_value + new_value).uniq
      when Hash
        old_value.merge(new_value) do |_key, inner_old_value, inner_new_value|
          inner_old_value.nil? ? inner_new_value : inner_old_value
        end
      when NilClass
        new_value
      else
        old_value
      end
    end
  end

  self.class.new(old_attributes)
end

#user(name = context&.user) ⇒ K8s::Config::User

Parameters:

  • name (String) (defaults to: context&.user)

Returns:

  • (K8s::Config::User)

Raises:



146
147
148
149
150
# File 'lib/k8s/config.rb', line 146

def user(name = context&.user)
  return nil if name.nil?

  users.find { |user| user.name == name }&.user || raise(K8s::Error::Configuration, "user not found: #{name.inspect}")
end