Class: Krane::ClusterResourceDiscovery

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

Instance Method Summary collapse

Constructor Details

#initialize(task_config:, namespace_tags: []) ⇒ ClusterResourceDiscovery

Returns a new instance of ClusterResourceDiscovery.


8
9
10
11
12
# File 'lib/krane/cluster_resource_discovery.rb', line 8

def initialize(task_config:, namespace_tags: [])
  @task_config = task_config
  @namespace_tags = namespace_tags
  @api_path_cache = {}
end

Instance Method Details

#crdsObject


14
15
16
17
18
19
# File 'lib/krane/cluster_resource_discovery.rb', line 14

def crds
  @crds ||= fetch_crds.map do |cr_def|
    CustomResourceDefinition.new(namespace: namespace, context: context, logger: logger,
      definition: cr_def, statsd_tags: @namespace_tags)
  end
end

#fetch_mutating_webhook_configurationsObject


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/krane/cluster_resource_discovery.rb', line 40

def fetch_mutating_webhook_configurations
  command = %w(get mutatingwebhookconfigurations)
  raw_json, err, st = kubectl.run(*command, output: "json", attempts: 5, use_namespace: false)
  if st.success?
    JSON.parse(raw_json)["items"].map do |definition|
      Krane::MutatingWebhookConfiguration.new(namespace: namespace, context: context, logger: logger,
        definition: definition, statsd_tags: @namespace_tags)
    end
  else
    raise FatalKubeAPIError, "Error retrieving mutatingwebhookconfigurations: #{err}"
  end
end

#fetch_resources(namespaced: false) ⇒ Object


30
31
32
33
34
35
36
37
38
# File 'lib/krane/cluster_resource_discovery.rb', line 30

def fetch_resources(namespaced: false)
  responses = Concurrent::Hash.new
  Krane::Concurrency.split_across_threads(api_paths) do |path|
    responses[path] = fetch_api_path(path)["resources"] || []
  end
  responses.flat_map do |path, resources|
    resources.map { |r| resource_hash(path, namespaced, r) }
  end.compact.uniq { |r| "#{r['apigroup']}/#{r['kind']}" }
end

#prunable_resources(namespaced:) ⇒ Object


21
22
23
24
25
26
27
28
# File 'lib/krane/cluster_resource_discovery.rb', line 21

def prunable_resources(namespaced:)
  black_list = %w(Namespace Node ControllerRevision)
  fetch_resources(namespaced: namespaced).map do |resource|
    next unless resource["verbs"].one? { |v| v == "delete" }
    next if black_list.include?(resource["kind"])
    [resource["apigroup"], resource["version"], resource["kind"]].compact.join("/")
  end.compact
end