Module: Util::KubeClient

Extended by:
Logging
Defined in:
lib/util/kube_client.rb

Constant Summary

Constants included from Logging

Logging::SEV_LABEL, Logging::TRACE

Class Method Summary collapse

Methods included from Logging

logger, logger=

Class Method Details

.create_client_by_object_type(api_endpoint, api_version) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/util/kube_client.rb', line 57

def create_client_by_object_type(api_endpoint, api_version)
  if !@current_kube_context.nil?
    create_kube_client_config(api_endpoint, api_version)
  else
    create_kube_client_default(api_endpoint, api_version)
  end
end

.create_clients(kube_config_hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/util/kube_client.rb', line 18

def create_clients(kube_config_hash)
  begin
    initialize_config(kube_config_hash)
    @core_client = create_client_by_object_type('', @api_version)
    @apps_client = create_client_by_object_type('/apis/apps', @api_version)
    @batch_client = create_client_by_object_type('/apis/batch', @api_version)
    @discover_v1_client = create_client_by_object_type('/apis/discovery.k8s.io', @api_version)
  rescue StandardError => e
    logger.error("Error while creating kube clients. Error - #{e}")
    raise e
  end

  set_clients(@core_client, @apps_client, @batch_client, @discover_v1_client)
end

.create_kube_client_config(api_endpoint, api_version) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/util/kube_client.rb', line 65

def create_kube_client_config(api_endpoint, api_version)
  logger.debug("Endpoint: #{@current_kube_context.api_endpoint + api_endpoint} and api_version: #{api_version}")
  begin
    # TODO: Test with SSL options.
    client = Kubeclient::Client.new(
      @current_kube_context.api_endpoint + api_endpoint,
      api_version,
      ssl_options: @current_kube_context.ssl_options,
      auth_options: @current_kube_context.auth_options
    )
  rescue StandardError => e
    logger.error("Error while creating kube client based on config - #{e}")
    raise e
  end
  client
end

.create_kube_client_default(api_endpoint, api_version) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
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
121
122
123
124
125
126
# File 'lib/util/kube_client.rb', line 82

def create_kube_client_default(api_endpoint, api_version)
  # Reference: https://github.com/splunk/fluent-plugin-kubernetes-objects/blob/develop/lib/fluent/plugin/in_kubernetes_objects.rb#L139
  kubernetes_url = nil
  if @kubernetes_url.nil?
    # Use Kubernetes default service account if we're in a pod.
    env_host = ENV['KUBERNETES_SERVICE_HOST']
    env_host = "[#{env_host}]" if env_host =~ Resolv::IPv6::Regex
    env_port = ENV['KUBERNETES_SERVICE_PORT']
    if env_host && env_port
      # kubernetes_url = "https://#{env_host}:#{env_port}/#{@api_endpoint.delete_prefix('/')}#{api_endpoint}"
      kubernetes_url = "https://#{env_host}:#{env_port}#{api_endpoint}"
    end
  else
    kubernetes_url = @kubernetes_url + api_endpoint
  end

  # Use SSL certificate and bearer token from Kubernetes service account.
  if Dir.exist?(@secret_dir)
    secret_ca_file = File.join(@secret_dir, 'ca.crt')
    secret_token_file = File.join(@secret_dir, 'token')

    @ca_file = secret_ca_file if @ca_file.nil? && File.exist?(secret_ca_file)

    @bearer_token_file = secret_token_file if @bearer_token_file.nil? && File.exist?(secret_token_file)
  end

  ssl_options = {
    client_cert: @client_cert && OpenSSL::X509::Certificate.new(File.read(@client_cert)),
    client_key: @client_key && OpenSSL::PKey::RSA.new(File.read(@client_key)),
    ca_file: @ca_file,
    verify_ssl: @verify_ssl ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
  }

  auth_options = {}
  auth_options[:bearer_token] = File.read(@bearer_token_file) if @bearer_token_file
  logger.debug("kubernetes_url : #{kubernetes_url} and api_version: #{api_version}")
  Kubeclient::Client.new(
    kubernetes_url, @api_version,
    ssl_options: ssl_options,
    auth_options: auth_options
  )
rescue StandardError => e
  logger.error("Error while creating default kube client - #{e}")
  raise e
end

.get_clientsObject



137
138
139
# File 'lib/util/kube_client.rb', line 137

def get_clients
  @kube_clients
end

.initialize_config(kube_config_hash) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/util/kube_client.rb', line 33

def initialize_config(kube_config_hash)
  @kube_config_hash = kube_config_hash

  unless @kube_config_hash[:kube_config_location].nil?
    begin
      config = Kubeclient::Config.read @kube_config_hash[:kube_config_location]
      @current_kube_context = config.context
    rescue StandardError => e
      logger.error("Error while initializing config params for kube client - #{e}")
      raise e
    end
  end

  @kubernetes_url = @kube_config_hash[:kubernetes_url]
  @api_endpoint = @kube_config_hash[:api_endpoint]
  @api_version = @kube_config_hash[:api_version]
  @client_cert = @kube_config_hash[:client_cert]
  @client_key = @kube_config_hash[:client_key]
  @ca_file = @kube_config_hash[:ca_file]
  @verify_ssl = @kube_config_hash[:verify_ssl]
  @bearer_token_file = @kube_config_hash[:bearer_token_file]
  @secret_dir = @kube_config_hash[:secret_dir]
end

.set_clients(core_client, apps_client, batch_client, discover_v1_client) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/util/kube_client.rb', line 128

def set_clients(core_client, apps_client, batch_client, discover_v1_client)
  @kube_clients = {
    core_client: core_client,
    apps_client: apps_client,
    batch_client: batch_client,
    discover_v1_client: discover_v1_client
  }
end