Class: K8s::Config
- Inherits:
-
ConfigStruct
- Object
- Dry::Struct
- ConfigStruct
- K8s::Config
- Defined in:
- lib/k8s/config.rb
Overview
Defined Under Namespace
Classes: Cluster, Context, NamedCluster, NamedContext, NamedUser, Types, User, UserAuthProvider
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.
-
.from_kubeconfig_env(kubeconfig = nil) ⇒ Object
Loads configuration files listed in KUBE_CONFIG environment variable and merged 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::Cluster
-
#context(name = current_context) ⇒ K8s::Config::Context
TODO: raise error if not found.
-
#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
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.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/k8s/config.rb', line 134 def self.build(server:, ca:, auth_token:, cluster_name: 'kubernetes', user: 'k8s-client', context: 'k8s-client', **) begin decoded_token = Base64.strict_decode64(auth_token) rescue ArgumentError decoded_token = nil end new( { clusters: [{ name: cluster_name, cluster: { server: server, certificate_authority_data: ca } }], users: [{ name: user, user: { token: decoded_token || auth_token } }], contexts: [{ name: context, context: { cluster: cluster_name, user: user } }], current_context: context }.merge() ) end |
.from_kubeconfig_env(kubeconfig = nil) ⇒ Object
Loads configuration files listed in KUBE_CONFIG environment variable and merged using the configuration merge rules, @see K8s::Config.merge
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/k8s/config.rb', line 113 def self.from_kubeconfig_env(kubeconfig = nil) kubeconfig ||= ENV.fetch('KUBECONFIG', '') return if kubeconfig.empty? paths = kubeconfig.split(/(?!\\):/) return load_file(paths.first) if paths.size == 1 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
105 106 107 |
# File 'lib/k8s/config.rb', line 105 def self.load_file(path) new(YAML.safe_load(File.read(path))) end |
Instance Method Details
#cluster(name = context.cluster) ⇒ K8s::Config::Cluster
195 196 197 |
# File 'lib/k8s/config.rb', line 195 def cluster(name = context.cluster) clusters.find{ |cluster| cluster.name == name }.cluster end |
#context(name = current_context) ⇒ K8s::Config::Context
TODO: raise error if not found
189 190 191 |
# File 'lib/k8s/config.rb', line 189 def context(name = current_context) contexts.find{ |context| context.name == name }.context 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
156 157 158 159 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 |
# File 'lib/k8s/config.rb', line 156 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 STDERR.puts "key is #{key} old val is #{old_value.inspect} and new val is #{new_value.inspect}" old_value end end end self.class.new(old_attributes) end |
#user(name = context.user) ⇒ K8s::Config::User
201 202 203 |
# File 'lib/k8s/config.rb', line 201 def user(name = context.user) users.find{ |user| user.name == name }.user end |