2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../gitlab/popen' unless defined?(Gitlab::Popen)
|
|
|
|
|
|
|
|
module Quality
|
|
|
|
class KubernetesClient
|
2018-12-13 13:39:08 +05:30
|
|
|
CommandFailedError = Class.new(StandardError)
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
attr_reader :namespace
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def initialize(namespace:)
|
2018-12-05 23:21:45 +05:30
|
|
|
@namespace = namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup(release_name:)
|
2018-12-13 13:39:08 +05:30
|
|
|
command = [
|
|
|
|
%(--namespace "#{namespace}"),
|
|
|
|
'delete',
|
|
|
|
'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa',
|
|
|
|
'--now',
|
|
|
|
%(-l release="#{release_name}")
|
|
|
|
]
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
run_command(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def run_command(command)
|
2018-12-13 13:39:08 +05:30
|
|
|
final_command = ['kubectl', *command].join(' ')
|
|
|
|
puts "Running command: `#{final_command}`" # rubocop:disable Rails/Output
|
|
|
|
|
|
|
|
result = Gitlab::Popen.popen_with_detail([final_command])
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
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-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|