Method: K8s::Client.autoconfig
- Defined in:
- lib/k8s/client.rb
.autoconfig(namespace: nil, **options) ⇒ K8s::Client
Attempts to create a K8s::Client instance automatically using environment variables, existing configuration files or in cluster configuration.
Look-up order:
- KUBE_TOKEN, KUBE_CA, KUBE_SERVER environment variables
- KUBECONFIG environment variable
- $HOME/.kube/config file
- In cluster configuration
Will raise when no means of configuration is available
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/k8s/client.rb', line 70 def self.autoconfig(namespace: nil, **) if ENV.values_at('KUBE_TOKEN', 'KUBE_CA', 'KUBE_SERVER').none? { |v| v.nil? || v.empty? } unless Base64.decode64(ENV['KUBE_CA']).match?(/CERTIFICATE/) raise ArgumentError, 'KUBE_CA does not seem to be base64 encoded' end begin token = [:auth_token] || Base64.strict_decode64(ENV['KUBE_TOKEN']) rescue ArgumentError raise ArgumentError, 'KUBE_TOKEN does not seem to be base64 encoded' end configuration = K8s::Config.build(server: ENV['KUBE_SERVER'], ca: ENV['KUBE_CA'], auth_token: token) elsif !ENV['KUBECONFIG'].to_s.empty? configuration = K8s::Config.from_kubeconfig_env(ENV['KUBECONFIG']) else found_config = [ File.join(Dir.home, '.kube', 'config'), '/etc/kubernetes/admin.conf', '/etc/kubernetes/kubelet.conf' ].find { |f| File.exist?(f) && File.readable?(f) } configuration = K8s::Config.load_file(found_config) if found_config end if configuration config(configuration, namespace: namespace, **) else in_cluster_config(namespace: namespace, **) end end |