2018-09-25 10:32:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../gitlab/popen' unless defined?(Gitlab::Popen)
|
|
|
|
|
|
|
|
module Quality
|
|
|
|
class KubernetesClient
|
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
|
|
|
|
|
|
|
|
def cleanup(release_name:)
|
2018-10-15 12:06:44 -04:00
|
|
|
command = [
|
|
|
|
%(--namespace "#{namespace}"),
|
|
|
|
'delete',
|
|
|
|
'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa',
|
|
|
|
'--now',
|
2019-04-18 10:05:21 -04:00
|
|
|
'--ignore-not-found',
|
|
|
|
'--include-uninitialized',
|
2018-10-15 12:06:44 -04:00
|
|
|
%(-l release="#{release_name}")
|
|
|
|
]
|
2018-09-25 10:32:53 -04:00
|
|
|
|
|
|
|
run_command(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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
|