Class: GlooIngressAdapter::ClientFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo_ingress_adapter/client_factory.rb

Overview

Factory for Kubernetes clients

Constant Summary collapse

KUBERNETES_API_URL =
"https://kubernetes.default.svc"

Instance Method Summary collapse

Constructor Details

#initialize(kubeconfig:, logger:) ⇒ ClientFactory

Returns a new instance of ClientFactory.



10
11
12
13
# File 'lib/gloo_ingress_adapter/client_factory.rb', line 10

def initialize(kubeconfig:, logger:)
  @kubeconfig = kubeconfig.freeze
  @logger = logger
end

Instance Method Details

#create_client(version:, api: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gloo_ingress_adapter/client_factory.rb', line 15

def create_client(version:, api: nil)
  suffix = api ? "/apis/#{api}" : ""

  options = {
    timeouts: { open: 30, read: 30 },
  }

  url = KUBERNETES_API_URL

  if @kubeconfig
    context = Kubeclient::Config.read(@kubeconfig).context

    url = context.api_endpoint

    options[:auth_options] = context.auth_options
    options[:ssl_options] = context.ssl_options
  else
    options[:auth_options] = { bearer_token_file: "/var/run/secrets/kubernetes.io/serviceaccount/token" }

    if File.exist?("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
      options[:ssl_options] = { ca_file: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" }
    end
  end

  Kubeclient::Client.new("#{url}#{suffix}", version, **options)
end