Class: Kubes::CLI::Prune

Inherits:
Base
  • Object
show all
Extended by:
Memoist
Includes:
Hooks::Concern, Util::Sure
Defined in:
lib/kubes/cli/prune.rb

Constant Summary collapse

KINDS =
%w[ConfigMap Secret]

Instance Method Summary collapse

Methods included from Util::Sure

#sure?

Methods included from Hooks::Concern

#run_hooks

Methods inherited from Base

#compile, #initialize, #pod_name

Methods included from Logging

#logger

Constructor Details

This class inherits a constructor from Kubes::CLI::Base

Instance Method Details

#anything_to_prune?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
# File 'lib/kubes/cli/prune.rb', line 27

def anything_to_prune?
  items = []
  return unless namespace

  with_old_items { |i| items << i }
  if items.empty?
    logger.info("There are no old resources that need pruning.") unless @options[:quiet]
  end
  !items.empty?
end

#built_kindsObject

IE: “Secret”=>{“demo-secret”=>“ebd93b58dd”}



80
81
82
83
# File 'lib/kubes/cli/prune.rb', line 80

def built_kinds
  compile # compile so built kinds are in memory
  Kubes::Compiler::Decorator::Hashable::Storage.md5s
end

#capture_items(kind) ⇒ Object



74
75
76
77
# File 'lib/kubes/cli/prune.rb', line 74

def capture_items(kind)
  data = Kubes::Kubectl.capture("get #{kind} -o json -n #{namespace}")
  data['items'] || []
end

#fetcherObject



18
19
20
# File 'lib/kubes/cli/prune.rb', line 18

def fetcher
  Kubes::Kubectl::Fetch::Base.new(@options)
end

#get_all_itemsObject



68
69
70
71
72
# File 'lib/kubes/cli/prune.rb', line 68

def get_all_items
  secrets = capture_items('secret')
  config_maps = capture_items('configmap')
  secrets + config_maps
end

#namespaceObject



23
24
25
# File 'lib/kubes/cli/prune.rb', line 23

def namespace
  fetcher.namespace
end

#old?(item) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kubes/cli/prune.rb', line 52

def old?(item)
  name = item['metadata']['name']
  kind = item['kind']
  built = built_kinds[kind] || [] # IE: {"demo-secret"=>"ebd93b58dd"}
  built.each do |original_name,hash|
    return false unless name.include?(original_name)
    current = "#{original_name}-#{hash}"
    return false if name == current
    # Spec cover the tricky regexp logic
    regexp = Regexp.new("#{original_name}-\\w{10}$") # IE: # demo-secret-\w{10}$
    return true if name.match(regexp)
  end

  false
end

#perform(preview:) ⇒ Object



38
39
40
41
42
# File 'lib/kubes/cli/prune.rb', line 38

def perform(preview:)
  with_old_items do |item|
    prune(item, preview)
  end
end

#prune(item, preview = false) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/kubes/cli/prune.rb', line 86

def prune(item, preview=false)
  kind = item['kind']
  name = item['metadata']['name']
  args = "delete #{kind} #{name} -n #{namespace}"
  if preview
    logger.info "    kubectl #{args}"
  else
    Kubes::Kubectl.execute(args)
  end
end

#runObject



8
9
10
11
12
13
14
15
16
# File 'lib/kubes/cli/prune.rb', line 8

def run
  return unless anything_to_prune?
  logger.info "Pruning old resources: #{KINDS.join(', ')}"
  perform(preview: true) unless @options[:yes]
  sure?("This will prune/delete resources. Are you sure?")
  run_hooks("kubes.rb", name: "prune") do
    perform(preview: false)
  end
end

#with_old_itemsObject



44
45
46
47
48
49
50
# File 'lib/kubes/cli/prune.rb', line 44

def with_old_items
  items = get_all_items
  items.each do |i|
    next unless old?(i)
    yield(i)
  end
end