Class: SidekiqAutoscale::KubernetesAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_autoscale/adapters/kubernetes_adapter.rb

Overview

Scale a Kubernetes deployment object.

Instance Method Summary collapse

Constructor Details

#initializeKubernetesAdapter

Returns a new instance of KubernetesAdapter.



6
7
8
9
10
11
12
13
14
# File 'lib/sidekiq_autoscale/adapters/kubernetes_adapter.rb', line 6

def initialize
  require "k8s-ruby"

  namespace = File.read("/run/secrets/kubernetes.io/serviceaccount/namespace")
  client = K8s::Client.autoconfig

  @deployment_name = SidekiqAutoscale.adapter_config[:deployment_name]
  @resources = client.api("apps/v1").resource("deployments", namespace: namespace)
end

Instance Method Details

#worker_countObject



16
17
18
19
20
21
22
23
24
# File 'lib/sidekiq_autoscale/adapters/kubernetes_adapter.rb', line 16

def worker_count
  @resources.get(@deployment_name).spec.replicas
rescue K8s::Error, K8s::Error::Forbidden => e
  SidekiqAutoscale.on_scaling_error(e)
  0
rescue Excon::Errors::Error, Excon::Error::Socket => e
  SidekiqAutoscale.logger.warn("[SIDEKIQ_SCALE][KUBERNETES_ACTION] Error connecting to Kubernetes: #{e}")
  0
end

#worker_count=(val) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/sidekiq_autoscale/adapters/kubernetes_adapter.rb', line 26

def worker_count=(val)
  return if val == worker_count

  @resources.merge_patch(@deployment_name, { spec: { replicas: val } })
  SidekiqAutoscale.logger.info("[SIDEKIQ_SCALE][KUBERNETES_ACTION] Set new worker count to #{val} (previously #{worker_count})")
rescue Excon::Errors::Error, K8s::Error, K8s::Error::Forbidden => e
  SidekiqAutoscale.on_scaling_error(e)
end