Class: K8y::Kubeconfig::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/k8y/kubeconfig/config.rb

Constant Summary collapse

ContextNotFoundError =
Class.new(Error)
ClusterNotFoundError =
Class.new(Error)
UserNotFoundError =
Class.new(Error)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_version: "v1", kind: "Config", preferences: {}, clusters:, contexts:, users:, current_context:) ⇒ Config

Returns a new instance of Config.



31
32
33
34
35
36
37
38
39
# File 'lib/k8y/kubeconfig/config.rb', line 31

def initialize(api_version: "v1", kind: "Config", preferences: {}, clusters:, contexts:, users:, current_context:)
  @api_version = api_version
  @kind = kind
  @preferences = preferences
  @clusters = clusters
  @contexts = contexts
  @users = users
  @current_context = current_context
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



15
16
17
# File 'lib/k8y/kubeconfig/config.rb', line 15

def api_version
  @api_version
end

#clustersObject (readonly)

Returns the value of attribute clusters.



15
16
17
# File 'lib/k8y/kubeconfig/config.rb', line 15

def clusters
  @clusters
end

#contextsObject (readonly)

Returns the value of attribute contexts.



15
16
17
# File 'lib/k8y/kubeconfig/config.rb', line 15

def contexts
  @contexts
end

#current_contextObject (readonly)

Returns the value of attribute current_context.



15
16
17
# File 'lib/k8y/kubeconfig/config.rb', line 15

def current_context
  @current_context
end

#kindObject (readonly)

Returns the value of attribute kind.



15
16
17
# File 'lib/k8y/kubeconfig/config.rb', line 15

def kind
  @kind
end

#preferencesObject (readonly)

Returns the value of attribute preferences.



15
16
17
# File 'lib/k8y/kubeconfig/config.rb', line 15

def preferences
  @preferences
end

#usersObject (readonly)

Returns the value of attribute users.



15
16
17
# File 'lib/k8y/kubeconfig/config.rb', line 15

def users
  @users
end

Class Method Details

.from_hash(hash) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/k8y/kubeconfig/config.rb', line 17

def self.from_hash(hash)
  new(
    api_version: hash.fetch("apiVersion"),
    kind: hash.fetch("kind"),
    preferences: hash.fetch("preferences"),
    clusters: hash.fetch("clusters").map { |cluster| Cluster.from_hash(cluster) },
    contexts: hash.fetch("contexts").map { |context| Context.from_hash(context) },
    current_context: hash.fetch("current-context"),
    users: hash.fetch("users").map { |user| User.from_hash(user) },
  )
rescue Psych::Exception, KeyError => e
  raise Error, e
end

Instance Method Details

#cluster(name) ⇒ Object



49
50
51
52
53
# File 'lib/k8y/kubeconfig/config.rb', line 49

def cluster(name)
  cluster = clusters.find { |c| c.name == name }
  raise ClusterNotFoundError, "Could not find cluster #{name} in config" unless cluster
  cluster
end

#cluster_for_context(context) ⇒ Object



61
62
63
64
# File 'lib/k8y/kubeconfig/config.rb', line 61

def cluster_for_context(context)
  cluster_name = context(context).cluster
  cluster(cluster_name)
end

#context(context) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/k8y/kubeconfig/config.rb', line 41

def context(context)
  return context if context.is_a?(Context)

  context = contexts.find { |c| c.name == context }
  raise ContextNotFoundError, "Could not find context #{context} in config" unless context
  context
end

#user(name) ⇒ Object

Raises:



55
56
57
58
59
# File 'lib/k8y/kubeconfig/config.rb', line 55

def user(name)
  user = users.find { |user| user.name == name }
  raise UserNotFoundError, "User #{name} not found in config" unless user
  user
end

#user_for_context(context) ⇒ Object



66
67
68
# File 'lib/k8y/kubeconfig/config.rb', line 66

def user_for_context(context)
  user(context(context).user)
end