class Krane::ResourceCache

Public Class Methods

new(task_config) click to toggle source
# 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

Public Instance Methods

get_all(kind, selector = nil) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

fetch_by_kind(kind) click to toggle source
# File lib/krane/resource_cache.rb, line 52
def fetch_by_kind(kind)
  resource_class = KubernetesResource.class_for_kind(kind)
  global_kind = @task_config.global_kinds.map(&:downcase).include?(kind.downcase)
  output_is_sensitive = resource_class.nil? ? false : resource_class::SENSITIVE_TEMPLATE_CONTENT
  raw_json, _, st = @kubectl.run("get", kind, "--chunk-size=0", attempts: 5, output: "json",
     output_is_sensitive: output_is_sensitive, use_namespace: !global_kind)
  raise KubectlError unless st.success?

  instances = {}
  JSON.parse(raw_json)["items"].each do |resource|
    resource_name = resource.dig("metadata", "name")
    instances[resource_name] = resource
  end
  instances
end
statsd_tags() click to toggle source
# File lib/krane/resource_cache.rb, line 41
def statsd_tags
  { namespace: namespace, context: context }
end
use_or_populate_cache(kind) click to toggle source
# File lib/krane/resource_cache.rb, line 45
def use_or_populate_cache(kind)
  @kind_fetcher_locks[kind].synchronize do
    return @data[kind] if @data.key?(kind)
    @data[kind] = fetch_by_kind(kind)
  end
end