Commit 69b91d38 authored by Rémy Coutable's avatar Rémy Coutable Committed by Lin Jen-Shin

Ensure we set 'tiller-namespace' for 'helm' commands

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent d008a870
...@@ -7,7 +7,7 @@ module Quality ...@@ -7,7 +7,7 @@ module Quality
class HelmClient class HelmClient
CommandFailedError = Class.new(StandardError) CommandFailedError = Class.new(StandardError)
attr_reader :namespace attr_reader :tiller_namespace, :namespace
RELEASE_JSON_ATTRIBUTES = %w[Name Revision Updated Status Chart AppVersion Namespace].freeze RELEASE_JSON_ATTRIBUTES = %w[Name Revision Updated Status Chart AppVersion Namespace].freeze
...@@ -24,7 +24,8 @@ module Quality ...@@ -24,7 +24,8 @@ module Quality
# A single page of data and the corresponding page number. # A single page of data and the corresponding page number.
Page = Struct.new(:releases, :number) Page = Struct.new(:releases, :number)
def initialize(namespace:) def initialize(tiller_namespace:, namespace:)
@tiller_namespace = tiller_namespace
@namespace = namespace @namespace = namespace
end end
...@@ -35,7 +36,7 @@ module Quality ...@@ -35,7 +36,7 @@ module Quality
def delete(release_name:) def delete(release_name:)
run_command([ run_command([
'delete', 'delete',
%(--tiller-namespace "#{namespace}"), %(--tiller-namespace "#{tiller_namespace}"),
'--purge', '--purge',
release_name release_name
]) ])
...@@ -60,7 +61,7 @@ module Quality ...@@ -60,7 +61,7 @@ module Quality
command = [ command = [
'list', 'list',
%(--namespace "#{namespace}"), %(--namespace "#{namespace}"),
%(--tiller-namespace "#{namespace}" --output json), %(--tiller-namespace "#{tiller_namespace}" --output json),
*args *args
] ]
json = JSON.parse(run_command(command)) json = JSON.parse(run_command(command))
......
...@@ -25,7 +25,6 @@ class AutomatedCleanup ...@@ -25,7 +25,6 @@ class AutomatedCleanup
def initialize(project_path: ENV['CI_PROJECT_PATH'], gitlab_token: ENV['GITLAB_BOT_REVIEW_APPS_CLEANUP_TOKEN']) def initialize(project_path: ENV['CI_PROJECT_PATH'], gitlab_token: ENV['GITLAB_BOT_REVIEW_APPS_CLEANUP_TOKEN'])
@project_path = project_path @project_path = project_path
@gitlab_token = gitlab_token @gitlab_token = gitlab_token
ENV['TILLER_NAMESPACE'] ||= review_apps_namespace
end end
def gitlab def gitlab
...@@ -45,7 +44,9 @@ class AutomatedCleanup ...@@ -45,7 +44,9 @@ class AutomatedCleanup
end end
def helm def helm
@helm ||= Quality::HelmClient.new(namespace: review_apps_namespace) @helm ||= Quality::HelmClient.new(
tiller_namespace: review_apps_namespace,
namespace: review_apps_namespace)
end end
def kubernetes def kubernetes
......
This diff is collapsed.
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
require 'fast_spec_helper' require 'fast_spec_helper'
RSpec.describe Quality::HelmClient do RSpec.describe Quality::HelmClient do
let(:namespace) { 'review-apps-ee' } let(:tiller_namespace) { 'review-apps-ee' }
let(:namespace) { tiller_namespace }
let(:release_name) { 'my-release' } let(:release_name) { 'my-release' }
let(:raw_helm_list_page1) do let(:raw_helm_list_page1) do
<<~OUTPUT <<~OUTPUT
...@@ -30,12 +31,12 @@ RSpec.describe Quality::HelmClient do ...@@ -30,12 +31,12 @@ RSpec.describe Quality::HelmClient do
OUTPUT OUTPUT
end end
subject { described_class.new(namespace: namespace) } subject { described_class.new(tiller_namespace: tiller_namespace, namespace: namespace) }
describe '#releases' do describe '#releases' do
it 'raises an error if the Helm command fails' do it 'raises an error if the Helm command fails' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{namespace}" --output json)]) .with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{tiller_namespace}" --output json)])
.and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false))) .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
expect { subject.releases.to_a }.to raise_error(described_class::CommandFailedError) expect { subject.releases.to_a }.to raise_error(described_class::CommandFailedError)
...@@ -43,7 +44,7 @@ RSpec.describe Quality::HelmClient do ...@@ -43,7 +44,7 @@ RSpec.describe Quality::HelmClient do
it 'calls helm list with default arguments' do it 'calls helm list with default arguments' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{namespace}" --output json)]) .with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{tiller_namespace}" --output json)])
.and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true))) .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
subject.releases.to_a subject.releases.to_a
...@@ -51,7 +52,7 @@ RSpec.describe Quality::HelmClient do ...@@ -51,7 +52,7 @@ RSpec.describe Quality::HelmClient do
it 'calls helm list with extra arguments' do it 'calls helm list with extra arguments' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{namespace}" --output json --deployed)]) .with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{tiller_namespace}" --output json --deployed)])
.and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true))) .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
subject.releases(args: ['--deployed']).to_a subject.releases(args: ['--deployed']).to_a
...@@ -59,7 +60,7 @@ RSpec.describe Quality::HelmClient do ...@@ -59,7 +60,7 @@ RSpec.describe Quality::HelmClient do
it 'returns a list of Release objects' do it 'returns a list of Release objects' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{namespace}" --output json --deployed)]) .with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{tiller_namespace}" --output json --deployed)])
.and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true))) .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true)))
releases = subject.releases(args: ['--deployed']).to_a releases = subject.releases(args: ['--deployed']).to_a
...@@ -78,10 +79,10 @@ RSpec.describe Quality::HelmClient do ...@@ -78,10 +79,10 @@ RSpec.describe Quality::HelmClient do
it 'automatically paginates releases' do it 'automatically paginates releases' do
expect(Gitlab::Popen).to receive(:popen_with_detail).ordered expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
.with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{namespace}" --output json)]) .with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{tiller_namespace}" --output json)])
.and_return(Gitlab::Popen::Result.new([], raw_helm_list_page1, '', double(success?: true))) .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page1, '', double(success?: true)))
expect(Gitlab::Popen).to receive(:popen_with_detail).ordered expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
.with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{namespace}" --output json --offset review-6709-group-t40qbv)]) .with([%(helm list --namespace "#{namespace}" --tiller-namespace "#{tiller_namespace}" --output json --offset review-6709-group-t40qbv)])
.and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true))) .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true)))
releases = subject.releases.to_a releases = subject.releases.to_a
...@@ -94,7 +95,7 @@ RSpec.describe Quality::HelmClient do ...@@ -94,7 +95,7 @@ RSpec.describe Quality::HelmClient do
describe '#delete' do describe '#delete' do
it 'raises an error if the Helm command fails' do it 'raises an error if the Helm command fails' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm delete --tiller-namespace "#{namespace}" --purge #{release_name})]) .with([%(helm delete --tiller-namespace "#{tiller_namespace}" --purge #{release_name})])
.and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false))) .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError) expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
...@@ -102,7 +103,7 @@ RSpec.describe Quality::HelmClient do ...@@ -102,7 +103,7 @@ RSpec.describe Quality::HelmClient do
it 'calls helm delete with default arguments' do it 'calls helm delete with default arguments' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm delete --tiller-namespace "#{namespace}" --purge #{release_name})]) .with([%(helm delete --tiller-namespace "#{tiller_namespace}" --purge #{release_name})])
.and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true))) .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
expect(subject.delete(release_name: release_name)).to eq('') expect(subject.delete(release_name: release_name)).to eq('')
...@@ -113,7 +114,7 @@ RSpec.describe Quality::HelmClient do ...@@ -113,7 +114,7 @@ RSpec.describe Quality::HelmClient do
it 'raises an error if the Helm command fails' do it 'raises an error if the Helm command fails' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm delete --tiller-namespace "#{namespace}" --purge #{release_name.join(' ')})]) .with([%(helm delete --tiller-namespace "#{tiller_namespace}" --purge #{release_name.join(' ')})])
.and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false))) .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError) expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
...@@ -121,7 +122,7 @@ RSpec.describe Quality::HelmClient do ...@@ -121,7 +122,7 @@ RSpec.describe Quality::HelmClient do
it 'calls helm delete with multiple release names' do it 'calls helm delete with multiple release names' do
expect(Gitlab::Popen).to receive(:popen_with_detail) expect(Gitlab::Popen).to receive(:popen_with_detail)
.with([%(helm delete --tiller-namespace "#{namespace}" --purge #{release_name.join(' ')})]) .with([%(helm delete --tiller-namespace "#{tiller_namespace}" --purge #{release_name.join(' ')})])
.and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true))) .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
expect(subject.delete(release_name: release_name)).to eq('') expect(subject.delete(release_name: release_name)).to eq('')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment