Class: Krane::ClusterResourceDiscovery
- Inherits:
-
Object
- Object
- Krane::ClusterResourceDiscovery
- Defined in:
- lib/krane/cluster_resource_discovery.rb
Instance Method Summary collapse
- #crds ⇒ Object
-
#fetch_resources(namespaced: false) ⇒ Object
kubectl api-resources -o wide returns 5 columns NAME SHORTNAMES APIGROUP NAMESPACED KIND VERBS SHORTNAMES and APIGROUP may be blank VERBS is an array serviceaccounts sa <blank> true ServiceAccount [create delete deletecollection get list patch update watch].
-
#initialize(task_config:, namespace_tags: []) ⇒ ClusterResourceDiscovery
constructor
A new instance of ClusterResourceDiscovery.
- #prunable_resources(namespaced:) ⇒ Object
Constructor Details
#initialize(task_config:, namespace_tags: []) ⇒ ClusterResourceDiscovery
Returns a new instance of ClusterResourceDiscovery.
7 8 9 10 |
# File 'lib/krane/cluster_resource_discovery.rb', line 7 def initialize(task_config:, namespace_tags: []) @task_config = task_config @namespace_tags = end |
Instance Method Details
#crds ⇒ Object
12 13 14 15 16 17 |
# File 'lib/krane/cluster_resource_discovery.rb', line 12 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_resources(namespaced: false) ⇒ Object
kubectl api-resources -o wide returns 5 columns NAME SHORTNAMES APIGROUP NAMESPACED KIND VERBS SHORTNAMES and APIGROUP may be blank VERBS is an array serviceaccounts sa <blank> true ServiceAccount [create delete deletecollection get list patch update watch]
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/krane/cluster_resource_discovery.rb', line 37 def fetch_resources(namespaced: false) command = %w(api-resources) command << "--namespaced=#{namespaced}" raw, _, st = kubectl.run(*command, output: "wide", attempts: 5, use_namespace: false) if st.success? rows = raw.split("\n") header = rows[0] resources = rows[1..-1] full_width_field_names = header.downcase.scan(/[a-z]+[\W]*/) cursor = 0 fields = full_width_field_names.each_with_object({}) do |name, hash| start = cursor cursor = start + name.length # Last field should consume the remainder of the line cursor = 0 if full_width_field_names.last == name.strip hash[name.strip] = [start, cursor - 1] end resources.map do |resource| resource = fields.map { |k, (s, e)| [k.strip, resource[s..e].strip] }.to_h # Manually parse verbs: "[get list]" into %w(get list) resource["verbs"] = resource["verbs"][1..-2].split resource end else [] end end |
#prunable_resources(namespaced:) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/krane/cluster_resource_discovery.rb', line 19 def prunable_resources(namespaced:) black_list = %w(Namespace Node ControllerRevision) api_versions = fetch_api_versions fetch_resources(namespaced: namespaced).uniq { |r| r['kind'] }.map do |resource| next unless resource['verbs'].one? { |v| v == "delete" } next if black_list.include?(resource['kind']) group_versions = api_versions[resource['apigroup'].to_s] version = version_for_kind(group_versions, resource['kind']) [resource['apigroup'], version, resource['kind']].compact.join("/") end.compact end |