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, UserExec
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 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::Cluster
- #context(name = current_context) ⇒ K8s::Config::Context
-
#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.
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', **) 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 |
.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
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
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.(path)), permitted_classes: [Time, DateTime, Date], permitted_symbols: [], aliases: true)) end |
Instance Method Details
#cluster(name = context.cluster) ⇒ K8s::Config::Cluster
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
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
Merges configuration according to the rules specified in kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#merging-kubeconfig-files
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
207 208 209 |
# File 'lib/k8s/config.rb', line 207 def user(name = context.user) users.find{ |user| user.name == name }.user end |