Class: VolumeSweeper::Kube::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/volume_sweeper/kube/client.rb

Overview

This class enables interation with the kube apis using 2 different modes.

1. In-cluster acccess.
     Assuming the code will run in the cluster it targets,
     So the main defaults that pods that mount from service account
     secret like `token` and `ca.cert` are used in this case.

2. External cluster access.
    Pass the environment variables below to access outside cluster
      * KUBE_API_URL
      * KUBE_API_TOKEN
      * KUBE_API_CA_PATH
    Or their kwargs counterpart to constructor.
      * :kube_api_url
      * :kube_api_token [base64 formatted]
      * :kube_api_ca_path

The env vars take the highest precedence.

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
# File 'lib/volume_sweeper/kube/client.rb', line 30

def initialize **kwargs
  @run_mode = kwargs[:mode]&.to_sym || :audit
  @api_url = kwargs[:kube_api_url]
  @api_token = kwargs[:kube_api_token]
  @api_ca_path = kwargs[:kube_api_ca_path]

  @log = Utils::Log.instance
  prepare_kube_client
end

Instance Method Details

#delete_released_persistent_volumes(age_in_days: 10) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/volume_sweeper/kube/client.rb', line 50

def delete_released_persistent_volumes age_in_days: 10
  names = []
  make_api_call :get_persistent_volumes do |i|
    names << i[:metadat][:name] if i.dig(:status, :phase) == "Released"
  end
  @log.msg "kube: Collected #{resources.size} released persisted volumes."

  return if names.blank? || @run_mode != :delete

  # Do actual deletion for released persistent volumes.
  # TODO: use last transistion timestamp if possible.
  @log.msg "kube: Looping over #{names.size} persisted volumes to be deleted sequentially."
  names.each do |pv|
    @log.msg "kube: deleting #{names} persisted volume .."
    @client.delete_pesistent_volume pv
    sleep 2
  end
  @log.msg "kube: completed deletion of #{names.size} persisted volume."
end

#fetch_pesistent_volumesObject



40
41
42
43
44
45
46
47
48
# File 'lib/volume_sweeper/kube/client.rb', line 40

def fetch_pesistent_volumes
  resources = []
  make_api_call :get_persistent_volumes do |i|
    resources << format_response_attrs(i)
  end
  @log.msg "kube: Collected #{resources.size} persisted volumes from the cluster."

  resources
end