Class: Krane::ResourceCache

Inherits:
Object
  • Object
show all
Defined in:
lib/krane/resource_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(task_config) ⇒ ResourceCache

Returns a new instance of ResourceCache.



9
10
11
12
13
14
15
# File 'lib/krane/resource_cache.rb', line 9

def initialize(task_config)
  @task_config = task_config

  @kind_fetcher_locks = Concurrent::Hash.new { |hash, key| hash[key] = Mutex.new }
  @data = Concurrent::Hash.new
  @kubectl = Kubectl.new(task_config: @task_config, log_failure_by_default: false)
end

Instance Method Details

#get_all(kind, selector = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/krane/resource_cache.rb', line 27

def get_all(kind, selector = nil)
  instances = use_or_populate_cache(kind).values
  return instances unless selector

  instances.select do |r|
    labels = r.dig("metadata", "labels") || {}
    labels >= selector
  end
rescue KubectlError
  []
end

#get_instance(kind, resource_name, raise_if_not_found: false) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/krane/resource_cache.rb', line 17

def get_instance(kind, resource_name, raise_if_not_found: false)
  instance = use_or_populate_cache(kind).fetch(resource_name, {})
  if instance.blank? && raise_if_not_found
    raise Krane::Kubectl::ResourceNotFoundError, "Resource does not exist (used cache for kind #{kind})"
  end
  instance
rescue KubectlError
  {}
end

#prewarm(resources) ⇒ Object



39
40
41
42
43
# File 'lib/krane/resource_cache.rb', line 39

def prewarm(resources)
  sync_dependencies = resources.flat_map { |r| r.class.const_get(:SYNC_DEPENDENCIES) }
  kinds = (resources.map(&:type) + sync_dependencies).uniq
  Krane::Concurrency.split_across_threads(kinds, max_threads: kinds.count) { |kind| get_all(kind) }
end