Class: K8s::Config
- Inherits:
-
RecursiveOpenStruct
- Object
- RecursiveOpenStruct
- K8s::Config
- 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
-
.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.
- .defaults ⇒ Object
-
.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.
-
.load_file(path) ⇒ K8s::Config
Loads a configuration from a YAML file.
Instance Method Summary collapse
- #cluster(name = context&.cluster) ⇒ K8s::Config::Child
- #context(name = current_context) ⇒ K8s::Config::Child
-
#merge(other) ⇒ K8s::Config
Merges configuration according to the rules specified in kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#merging-kubeconfig-files.
- #user(name = context&.user) ⇒ K8s::Config::User
Methods included from KeyTransformations
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.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/k8s/config.rb', line 77 def self.build(server:, ca:, auth_token:, cluster_name: 'kubernetes', user: 'k8s-client', context: 'k8s-client', **) 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() ) end |
.defaults ⇒ Object
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
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/k8s/config.rb', line 57 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
49 50 51 |
# File 'lib/k8s/config.rb', line 49 def self.load_file(path) new(YAML.safe_load(File.read(File.(path)), [Time, DateTime, Date], [], true)) end |
Instance Method Details
#cluster(name = context&.cluster) ⇒ K8s::Config::Child
134 135 136 137 138 |
# File 'lib/k8s/config.rb', line 134 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
125 126 127 128 129 |
# File 'lib/k8s/config.rb', line 125 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
Merges configuration according to the rules specified in kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#merging-kubeconfig-files
93 94 95 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 |
# File 'lib/k8s/config.rb', line 93 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
143 144 145 146 147 |
# File 'lib/k8s/config.rb', line 143 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 |