1616require 'kubernetes/config/error'
1717
1818module Kubernetes
19-
19+ # The InClusterConfig class represents configuration for authn/authz in a
20+ # Kubernetes cluster.
2021 class InClusterConfig
21-
22- SERVICE_HOST_ENV_NAME = "KUBERNETES_SERVICE_HOST"
23- SERVICE_PORT_ENV_NAME = "KUBERNETES_SERVICE_PORT"
24- SERVICE_TOKEN_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/token"
25- SERVICE_CA_CERT_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
22+ # rubocop:disable LineLength
23+ SERVICE_HOST_ENV_NAME = 'KUBERNETES_SERVICE_HOST' . freeze
24+ SERVICE_PORT_ENV_NAME = 'KUBERNETES_SERVICE_PORT' . freeze
25+ SERVICE_TOKEN_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/token' . freeze
26+ SERVICE_CA_CERT_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' . freeze
27+ # rubocop:enable LineLength
2628
2729 attr_accessor :host
2830 attr_accessor :port
2931 attr_accessor :token
3032
3133 def validate
32- unless ( self . host = self . env [ SERVICE_HOST_ENV_NAME ] ) && ( self . port = self . env [ SERVICE_PORT_ENV_NAME ] )
33- raise ConfigError . new ( "Service host/port is not set" )
34+ unless ( self . host = env [ SERVICE_HOST_ENV_NAME ] ) &&
35+ ( self . port = env [ SERVICE_PORT_ENV_NAME ] )
36+ raise ConfigError , 'Service host/port is not set'
3437 end
35- raise ConfigError . new ( "Service token file does not exists" ) unless File . file? ( self . token_file )
36- raise ConfigError . new ( "Service token file does not exists" ) unless File . file? ( self . ca_cert )
38+
39+ # rubocop:disable LineLength
40+ raise ConfigError , 'Service token file does not exists' unless File . file? ( token_file )
41+ raise ConfigError , 'Service token file does not exists' unless File . file? ( ca_cert )
42+ # rubocop:enable LineLength
3743 end
3844
39- def self . in_cluster? ( )
40- return File . exist? ( SERVICE_TOKEN_FILENAME ) && File . exist? ( SERVICE_CA_CERT_FILENAME )
45+ def self . in_cluster?
46+ File . exist? ( SERVICE_TOKEN_FILENAME ) &&
47+ File . exist? ( SERVICE_CA_CERT_FILENAME )
4148 end
4249
4350 def env
@@ -56,19 +63,18 @@ def token_file
5663 end
5764
5865 def load_token
59- open ( self . token_file ) do |io |
66+ File . open ( token_file ) do |io |
6067 self . token = io . read . chomp
6168 end
6269 end
6370
6471 def configure ( configuration )
6572 validate
6673 load_token
67- configuration . api_key [ 'authorization' ] = "Bearer #{ self . token } "
74+ configuration . api_key [ 'authorization' ] = "Bearer #{ token } "
6875 configuration . scheme = 'https'
69- configuration . host = "#{ self . host } :#{ self . port } "
70- configuration . ssl_ca_cert = self . ca_cert
76+ configuration . host = "#{ host } :#{ port } "
77+ configuration . ssl_ca_cert = ca_cert
7178 end
7279 end
73-
7480end
0 commit comments