Class: K8s::Config

Inherits:
ConfigStruct show all
Defined in:
lib/k8s/config.rb

Overview

Defined Under Namespace

Classes: Cluster, Context, NamedCluster, NamedContext, NamedUser, Types, User, UserAuthProvider, UserExec

Class Method Summary collapse

Instance Method Summary collapse

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)



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

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

.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)


124
125
126
127
128
129
130
131
132
133
# File 'lib/k8s/config.rb', line 124

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:



113
114
115
116
117
118
# File 'lib/k8s/config.rb', line 113

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

Instance Method Details

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

Parameters:

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

Returns:



201
202
203
# File 'lib/k8s/config.rb', line 201

def cluster(name = context.cluster)
  clusters.find{ |cluster| cluster.name == name }.cluster
end

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

Parameters:

  • name (String) (defaults to: current_context)

Returns:

Raises:



192
193
194
195
196
197
# File 'lib/k8s/config.rb', line 192

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

  found.context
end

#merge(other) ⇒ K8s::Config

Parameters:

Returns:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/k8s/config.rb', line 160

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

  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:



207
208
209
# File 'lib/k8s/config.rb', line 207

def user(name = context.user)
  users.find{ |user| user.name == name }.user
end