Class: Scaltainer::KubeResource

Inherits:
ReplicaSetBase show all
Defined in:
lib/scaltainer/orchestrators/kubernetes.rb

Constant Summary collapse

PERMANENT_ERROR_STATUSES =

Statuses that no later attempt can recover from with the credentials at hand, as opposed to conflicts and outages which are worth retrying.

[401, 403].freeze

Instance Attribute Summary

Attributes inherited from ReplicaSetBase

#id, #name, #namespace, #type

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace) ⇒ KubeResource

Returns a new instance of KubeResource.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 9

def initialize(name, namespace)
  @@client ||= self.class.get_client
  type = ENV['KUBERNETES_CONTROLLER_KIND'] || 'deployment'
  # if namespace not specified, use the one found in configuration
  namespace ||= @@namespace || 'default'
  super(name, type, namespace)
  @resource = @@client.send("get_#{@type}", normalize_name(@name), @namespace)
  @id = @resource..uid
rescue Kubeclient::HttpError => e
  raise resource_error(e, 'find')
end

Class Method Details

.get_api_url(default_server) ⇒ Object



87
88
89
90
91
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 87

def self.get_api_url(default_server)
  server = ENV['KUBERNETES_API_SERVER'] || default_server
  endpoint = ENV['KUBERNETES_API_ENDPOINT'] || '/api'
  "#{server}#{endpoint}"
end

.get_api_version(default_version) ⇒ Object



93
94
95
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 93

def self.get_api_version(default_version)
  ENV['KUBERNETES_API_VERSION'] || default_version
end

.get_clientObject



46
47
48
49
50
51
52
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 46

def self.get_client
  if ENV['KUBECONFIG']
    get_client_from_kubeconfig ENV['KUBECONFIG']
  else
    get_client_from_serviceaccount '/var/run/secrets/kubernetes.io/serviceaccount'
  end
end

.get_client_from_kubeconfig(kubeconfig) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 54

def self.get_client_from_kubeconfig(kubeconfig)
  config = Kubeclient::Config.read(kubeconfig)
  url = get_api_url(config.context.api_endpoint)
  version = get_api_version(config.context.api_version)
  # @@namespace = config.context.namespace # wait till PR#308 merged into kubeclient
  Kubeclient::Client.new(
    url, version,
    ssl_options: config.context.ssl_options,
    auth_options: config.context.auth_options
  )
end

.get_client_from_serviceaccount(serviceaccount) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 66

def self.get_client_from_serviceaccount(serviceaccount)
  ssl_verify = if ENV['KUBERNETES_SKIP_SSL_VERIFY']
    OpenSSL::SSL::VERIFY_NONE
  else
    OpenSSL::SSL::VERIFY_PEER
  end
  ssl_options = {
    client_cert: OpenSSL::X509::Certificate.new(read_secret(serviceaccount, 'ca.crt')),
    verify_ssl: ssl_verify
  }
  auth_options = {bearer_token: read_secret(serviceaccount, 'token')}
  @@namespace = read_secret(serviceaccount, 'namespace')
  url = get_api_url('https://kubernetes.default:443')
  version = get_api_version('v1')
  Kubeclient::Client.new(
    url, version,
    ssl_options: ssl_options,
    auth_options: auth_options
  )
end

.read_secret(serviceaccount, secret) ⇒ Object



97
98
99
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 97

def self.read_secret(serviceaccount, secret)
  File.read("#{serviceaccount}/#{secret}")
end

Instance Method Details

#get_replicasObject



21
22
23
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 21

def get_replicas
  @resource.spec.replicas
end

#set_replicas(replicas) ⇒ Object



25
26
27
28
29
# File 'lib/scaltainer/orchestrators/kubernetes.rb', line 25

def set_replicas(replicas)
  @@client.send("patch_#{@type}", normalize_name(@name), {spec: {replicas: replicas}}, @namespace)
rescue Kubeclient::HttpError => e
  raise resource_error(e, 'scale')
end