2018-09-25 10:32:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../gitlab/popen' unless defined?(Gitlab::Popen)
|
|
|
|
|
|
|
|
module Quality
|
|
|
|
class KubernetesClient
|
2019-11-27 13:06:30 -05:00
|
|
|
RESOURCE_LIST = 'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa,crd'
|
2018-10-15 12:06:44 -04:00
|
|
|
CommandFailedError = Class.new(StandardError)
|
|
|
|
|
2018-09-25 10:32:53 -04:00
|
|
|
attr_reader :namespace
|
|
|
|
|
2018-10-15 12:06:44 -04:00
|
|
|
def initialize(namespace:)
|
2018-09-25 10:32:53 -04:00
|
|
|
@namespace = namespace
|
|
|
|
end
|
|
|
|
|
2019-10-29 11:07:20 -04:00
|
|
|
def cleanup(release_name:, wait: true)
|
2019-11-27 13:06:30 -05:00
|
|
|
delete_by_selector(release_name: release_name, wait: wait)
|
|
|
|
delete_by_matching_name(release_name: release_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def delete_by_selector(release_name:, wait:)
|
2019-10-24 11:06:02 -04:00
|
|
|
selector = case release_name
|
|
|
|
when String
|
|
|
|
%(-l release="#{release_name}")
|
|
|
|
when Array
|
|
|
|
%(-l 'release in (#{release_name.join(', ')})')
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'release_name must be a string or an array'
|
|
|
|
end
|
|
|
|
|
2018-10-15 12:06:44 -04:00
|
|
|
command = [
|
|
|
|
'delete',
|
2019-11-27 13:06:30 -05:00
|
|
|
RESOURCE_LIST,
|
|
|
|
%(--namespace "#{namespace}"),
|
2018-10-15 12:06:44 -04:00
|
|
|
'--now',
|
2019-04-18 10:05:21 -04:00
|
|
|
'--ignore-not-found',
|
|
|
|
'--include-uninitialized',
|
2019-10-29 11:07:20 -04:00
|
|
|
%(--wait=#{wait}),
|
2019-10-24 11:06:02 -04:00
|
|
|
selector
|
2018-10-15 12:06:44 -04:00
|
|
|
]
|
2018-09-25 10:32:53 -04:00
|
|
|
|
|
|
|
run_command(command)
|
|
|
|
end
|
|
|
|
|
2019-11-27 13:06:30 -05:00
|
|
|
def delete_by_matching_name(release_name:)
|
|
|
|
resource_names = raw_resource_names
|
|
|
|
command = [
|
|
|
|
'delete',
|
|
|
|
%(--namespace "#{namespace}")
|
|
|
|
]
|
|
|
|
|
|
|
|
Array(release_name).each do |release|
|
|
|
|
resource_names
|
|
|
|
.select { |resource_name| resource_name.include?(release) }
|
|
|
|
.each { |matching_resource| run_command(command + [matching_resource]) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_resource_names
|
|
|
|
command = [
|
|
|
|
'get',
|
|
|
|
RESOURCE_LIST,
|
|
|
|
%(--namespace "#{namespace}"),
|
|
|
|
'-o custom-columns=NAME:.metadata.name'
|
|
|
|
]
|
|
|
|
run_command(command).lines.map(&:strip)
|
|
|
|
end
|
2018-09-25 10:32:53 -04:00
|
|
|
|
|
|
|
def run_command(command)
|
2018-10-15 12:06:44 -04:00
|
|
|
final_command = ['kubectl', *command].join(' ')
|
|
|
|
puts "Running command: `#{final_command}`" # rubocop:disable Rails/Output
|
|
|
|
|
|
|
|
result = Gitlab::Popen.popen_with_detail([final_command])
|
2018-09-25 10:32:53 -04:00
|
|
|
|
2018-10-15 12:06:44 -04:00
|
|
|
if result.status.success?
|
|
|
|
result.stdout.chomp.freeze
|
|
|
|
else
|
|
|
|
raise CommandFailedError, "The `#{final_command}` command failed (status: #{result.status}) with the following error:\n#{result.stderr}"
|
|
|
|
end
|
2018-09-25 10:32:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|