Class: Kubes::CLI::Prune
Constant Summary
collapse
- KINDS =
%w[ConfigMap Secret]
Instance Method Summary
collapse
Methods included from Util::Sure
#sure?
#run_hooks
Methods inherited from Base
#compile, #initialize, #pod_name
Methods included from Logging
#logger
Instance Method Details
#anything_to_prune? ⇒ 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_kinds ⇒ Object
IE: “Secret”=>{“demo-secret”=>“ebd93b58dd”}
#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
|
#get_all_items ⇒ Object
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
|
#namespace ⇒ Object
23
24
25
|
# File 'lib/kubes/cli/prune.rb', line 23
def namespace
fetcher.namespace
end
|
#old?(item) ⇒ 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] || [] built.each do |original_name,hash|
return false unless name.include?(original_name)
current = "#{original_name}-#{hash}"
return false if name == current
regexp = Regexp.new("#{original_name}-\\w{10}$") return true if name.match(regexp)
end
false
end
|
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
|
#run ⇒ Object
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_items ⇒ Object
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
|