Commit ce897f11 authored by Dylan Griffith's avatar Dylan Griffith

Refactor Cluster Application classes to pass through a has of config files

This is refactoring in the lead up to passing mutual TLS certs for helm applications. As such we expect all applications to need config files so we can remove the logic about which applications need and do not need this (ie `#config_map?`).
parent 87f03f01
...@@ -15,7 +15,10 @@ module Clusters ...@@ -15,7 +15,10 @@ module Clusters
end end
def install_command def install_command
Gitlab::Kubernetes::Helm::InitCommand.new(name) Gitlab::Kubernetes::Helm::InitCommand.new(
name: name,
files: {}
)
end end
end end
end end
......
...@@ -32,9 +32,9 @@ module Clusters ...@@ -32,9 +32,9 @@ module Clusters
def install_command def install_command
Gitlab::Kubernetes::Helm::InstallCommand.new( Gitlab::Kubernetes::Helm::InstallCommand.new(
name, name: name,
chart: chart, chart: chart,
values: values files: files
) )
end end
......
...@@ -35,9 +35,9 @@ module Clusters ...@@ -35,9 +35,9 @@ module Clusters
def install_command def install_command
Gitlab::Kubernetes::Helm::InstallCommand.new( Gitlab::Kubernetes::Helm::InstallCommand.new(
name, name: name,
chart: chart, chart: chart,
values: values, files: files,
repository: repository repository: repository
) )
end end
......
...@@ -43,10 +43,10 @@ module Clusters ...@@ -43,10 +43,10 @@ module Clusters
def install_command def install_command
Gitlab::Kubernetes::Helm::InstallCommand.new( Gitlab::Kubernetes::Helm::InstallCommand.new(
name, name: name,
chart: chart, chart: chart,
version: version, version: version,
values: values files: files
) )
end end
......
...@@ -28,9 +28,9 @@ module Clusters ...@@ -28,9 +28,9 @@ module Clusters
def install_command def install_command
Gitlab::Kubernetes::Helm::InstallCommand.new( Gitlab::Kubernetes::Helm::InstallCommand.new(
name, name: name,
chart: chart, chart: chart,
values: values, files: files,
repository: repository repository: repository
) )
end end
......
...@@ -12,6 +12,12 @@ module Clusters ...@@ -12,6 +12,12 @@ module Clusters
File.read(chart_values_file) File.read(chart_values_file)
end end
def files
{
'values.yaml': values
}
end
private private
def chart_values_file def chart_values_file
......
module Gitlab module Gitlab
module Kubernetes module Kubernetes
class ConfigMap class ConfigMap
def initialize(name, values = "") def initialize(name, files)
@name = name @name = name
@values = values @files = files
end end
def generate def generate
resource = ::Kubeclient::Resource.new resource = ::Kubeclient::Resource.new
resource.metadata = metadata resource.metadata = metadata
resource.data = { values: values } resource.data = files
resource resource
end end
...@@ -19,7 +19,7 @@ module Gitlab ...@@ -19,7 +19,7 @@ module Gitlab
private private
attr_reader :name, :values attr_reader :name, :files
def metadata def metadata
{ {
......
...@@ -9,7 +9,7 @@ module Gitlab ...@@ -9,7 +9,7 @@ module Gitlab
def install(command) def install(command)
namespace.ensure_exists! namespace.ensure_exists!
create_config_map(command) if command.config_map? create_config_map(command)
kubeclient.create_pod(command.pod_resource) kubeclient.create_pod(command.pod_resource)
end end
......
module Gitlab module Gitlab
module Kubernetes module Kubernetes
module Helm module Helm
class BaseCommand module BaseCommand
attr_reader :name
def initialize(name)
@name = name
end
def pod_resource def pod_resource
Gitlab::Kubernetes::Helm::Pod.new(self, namespace).generate Gitlab::Kubernetes::Helm::Pod.new(self, namespace).generate
end end
...@@ -24,14 +18,22 @@ module Gitlab ...@@ -24,14 +18,22 @@ module Gitlab
HEREDOC HEREDOC
end end
def config_map?
false
end
def pod_name def pod_name
"install-#{name}" "install-#{name}"
end end
def config_map_resource
Gitlab::Kubernetes::ConfigMap.new(name, files).generate
end
def name
raise "Not implemented"
end
def files
raise "Not implemented"
end
private private
def namespace def namespace
......
module Gitlab module Gitlab
module Kubernetes module Kubernetes
module Helm module Helm
class InitCommand < BaseCommand class InitCommand
include BaseCommand
attr_reader :name, :files
def initialize(name:, files:)
@name = name
@files = files
end
def generate_script def generate_script
super + [ super + [
init_helm_command init_helm_command
......
module Gitlab module Gitlab
module Kubernetes module Kubernetes
module Helm module Helm
class InstallCommand < BaseCommand class InstallCommand
attr_reader :name, :chart, :version, :repository, :values include BaseCommand
def initialize(name, chart:, values:, version: nil, repository: nil) attr_reader :name, :files
attr_reader :chart, :version, :repository
def initialize(name:, chart:, files:, version: nil, repository: nil)
@name = name @name = name
@chart = chart @chart = chart
@version = version @version = version
@values = values @files = files
@repository = repository @repository = repository
end end
...@@ -20,14 +23,6 @@ module Gitlab ...@@ -20,14 +23,6 @@ module Gitlab
].compact.join("\n") ].compact.join("\n")
end end
def config_map?
true
end
def config_map_resource
Gitlab::Kubernetes::ConfigMap.new(name, values).generate
end
private private
def init_command def init_command
......
...@@ -10,10 +10,8 @@ module Gitlab ...@@ -10,10 +10,8 @@ module Gitlab
def generate def generate
spec = { containers: [container_specification], restartPolicy: 'Never' } spec = { containers: [container_specification], restartPolicy: 'Never' }
if command.config_map? spec[:volumes] = volumes_specification
spec[:volumes] = volumes_specification spec[:containers][0][:volumeMounts] = volume_mounts_specification
spec[:containers][0][:volumeMounts] = volume_mounts_specification
end
::Kubeclient::Resource.new(metadata: metadata, spec: spec) ::Kubeclient::Resource.new(metadata: metadata, spec: spec)
end end
...@@ -61,7 +59,7 @@ module Gitlab ...@@ -61,7 +59,7 @@ module Gitlab
name: 'configuration-volume', name: 'configuration-volume',
configMap: { configMap: {
name: "values-content-configuration-#{command.name}", name: "values-content-configuration-#{command.name}",
items: [{ key: 'values', path: 'values.yaml' }] items: command.files.map { |name, _| { key: name, path: name } }
} }
} }
] ]
......
...@@ -3,7 +3,7 @@ require 'spec_helper' ...@@ -3,7 +3,7 @@ require 'spec_helper'
describe Gitlab::Kubernetes::ConfigMap do describe Gitlab::Kubernetes::ConfigMap do
let(:kubeclient) { double('kubernetes client') } let(:kubeclient) { double('kubernetes client') }
let(:application) { create(:clusters_applications_prometheus) } let(:application) { create(:clusters_applications_prometheus) }
let(:config_map) { described_class.new(application.name, application.values) } let(:config_map) { described_class.new(application.name, application.files) }
let(:namespace) { Gitlab::Kubernetes::Helm::NAMESPACE } let(:namespace) { Gitlab::Kubernetes::Helm::NAMESPACE }
let(:metadata) do let(:metadata) do
...@@ -15,7 +15,7 @@ describe Gitlab::Kubernetes::ConfigMap do ...@@ -15,7 +15,7 @@ describe Gitlab::Kubernetes::ConfigMap do
end end
describe '#generate' do describe '#generate' do
let(:resource) { ::Kubeclient::Resource.new(metadata: metadata, data: { values: application.values }) } let(:resource) { ::Kubeclient::Resource.new(metadata: metadata, data: application.files) }
subject { config_map.generate } subject { config_map.generate }
it 'should build a Kubeclient Resource' do it 'should build a Kubeclient Resource' do
......
...@@ -39,7 +39,7 @@ describe Gitlab::Kubernetes::Helm::Api do ...@@ -39,7 +39,7 @@ describe Gitlab::Kubernetes::Helm::Api do
end end
context 'with a ConfigMap' do context 'with a ConfigMap' do
let(:resource) { Gitlab::Kubernetes::ConfigMap.new(application.name, application.values).generate } let(:resource) { Gitlab::Kubernetes::ConfigMap.new(application.name, application.files).generate }
it 'creates a ConfigMap on kubeclient' do it 'creates a ConfigMap on kubeclient' do
expect(client).to receive(:create_config_map).with(resource).once expect(client).to receive(:create_config_map).with(resource).once
......
require 'spec_helper' require 'spec_helper'
class TestClass
include Gitlab::Kubernetes::Helm::BaseCommand
def name
"test-class-name"
end
def files
{
some: 'value'
}
end
end
describe Gitlab::Kubernetes::Helm::BaseCommand do describe Gitlab::Kubernetes::Helm::BaseCommand do
let(:application) { create(:clusters_applications_helm) } let(:application) { create(:clusters_applications_helm) }
let(:base_command) { described_class.new(application.name) } let(:base_command) { TestClass.new }
subject { base_command } subject { base_command }
it_behaves_like 'helm commands' do it_behaves_like 'helm commands' do
...@@ -18,15 +30,9 @@ describe Gitlab::Kubernetes::Helm::BaseCommand do ...@@ -18,15 +30,9 @@ describe Gitlab::Kubernetes::Helm::BaseCommand do
end end
end end
describe '#config_map?' do
subject { base_command.config_map? }
it { is_expected.to be_falsy }
end
describe '#pod_name' do describe '#pod_name' do
subject { base_command.pod_name } subject { base_command.pod_name }
it { is_expected.to eq('install-helm') } it { is_expected.to eq('install-test-class-name') }
end end
end end
...@@ -4,7 +4,7 @@ describe Gitlab::Kubernetes::Helm::InitCommand do ...@@ -4,7 +4,7 @@ describe Gitlab::Kubernetes::Helm::InitCommand do
let(:application) { create(:clusters_applications_helm) } let(:application) { create(:clusters_applications_helm) }
let(:commands) { 'helm init >/dev/null' } let(:commands) { 'helm init >/dev/null' }
subject { described_class.new(application.name) } subject { described_class.new(name: application.name, files: {}) }
it_behaves_like 'helm commands' it_behaves_like 'helm commands'
end end
...@@ -62,12 +62,6 @@ describe Gitlab::Kubernetes::Helm::InstallCommand do ...@@ -62,12 +62,6 @@ describe Gitlab::Kubernetes::Helm::InstallCommand do
end end
end end
describe '#config_map?' do
subject { install_command.config_map? }
it { is_expected.to be_truthy }
end
describe '#config_map_resource' do describe '#config_map_resource' do
let(:metadata) do let(:metadata) do
{ {
...@@ -77,7 +71,7 @@ describe Gitlab::Kubernetes::Helm::InstallCommand do ...@@ -77,7 +71,7 @@ describe Gitlab::Kubernetes::Helm::InstallCommand do
} }
end end
let(:resource) { ::Kubeclient::Resource.new(metadata: metadata, data: { values: application.values }) } let(:resource) { ::Kubeclient::Resource.new(metadata: metadata, data: application.files) }
subject { install_command.config_map_resource } subject { install_command.config_map_resource }
......
...@@ -9,7 +9,7 @@ describe Gitlab::Kubernetes::Helm::Pod do ...@@ -9,7 +9,7 @@ describe Gitlab::Kubernetes::Helm::Pod do
subject { described_class.new(command, namespace) } subject { described_class.new(command, namespace) }
shared_examples 'helm pod' do context 'with a command' do
it 'should generate a Kubeclient::Resource' do it 'should generate a Kubeclient::Resource' do
expect(subject.generate).to be_a_kind_of(Kubeclient::Resource) expect(subject.generate).to be_a_kind_of(Kubeclient::Resource)
end end
...@@ -41,10 +41,6 @@ describe Gitlab::Kubernetes::Helm::Pod do ...@@ -41,10 +41,6 @@ describe Gitlab::Kubernetes::Helm::Pod do
spec = subject.generate.spec spec = subject.generate.spec
expect(spec.restartPolicy).to eq('Never') expect(spec.restartPolicy).to eq('Never')
end end
end
context 'with a install command' do
it_behaves_like 'helm pod'
it 'should include volumes for the container' do it 'should include volumes for the container' do
container = subject.generate.spec.containers.first container = subject.generate.spec.containers.first
...@@ -60,24 +56,8 @@ describe Gitlab::Kubernetes::Helm::Pod do ...@@ -60,24 +56,8 @@ describe Gitlab::Kubernetes::Helm::Pod do
it 'should mount configMap specification in the volume' do it 'should mount configMap specification in the volume' do
volume = subject.generate.spec.volumes.first volume = subject.generate.spec.volumes.first
expect(volume.configMap['name']).to eq("values-content-configuration-#{app.name}") expect(volume.configMap['name']).to eq("values-content-configuration-#{app.name}")
expect(volume.configMap['items'].first['key']).to eq('values') expect(volume.configMap['items'].first['key']).to eq(:'values.yaml')
expect(volume.configMap['items'].first['path']).to eq('values.yaml') expect(volume.configMap['items'].first['path']).to eq(:'values.yaml')
end
end
context 'with a init command' do
let(:app) { create(:clusters_applications_helm, cluster: cluster) }
it_behaves_like 'helm pod'
it 'should not include volumeMounts inside the container' do
container = subject.generate.spec.containers.first
expect(container.volumeMounts).to be_nil
end
it 'should not a volume inside the specification' do
spec = subject.generate.spec
expect(spec.volumes).to be_nil
end end
end end
end end
......
...@@ -74,18 +74,18 @@ describe Clusters::Applications::Ingress do ...@@ -74,18 +74,18 @@ describe Clusters::Applications::Ingress do
expect(subject.name).to eq('ingress') expect(subject.name).to eq('ingress')
expect(subject.chart).to eq('stable/nginx-ingress') expect(subject.chart).to eq('stable/nginx-ingress')
expect(subject.version).to be_nil expect(subject.version).to be_nil
expect(subject.values).to eq(ingress.values) expect(subject.files).to eq(ingress.files)
end end
end end
describe '#values' do describe '#files' do
subject { ingress.values } let(:values) { ingress.files[:'values.yaml'] }
it 'should include ingress valid keys' do it 'should include ingress valid keys in values' do
is_expected.to include('image') expect(values).to include('image')
is_expected.to include('repository') expect(values).to include('repository')
is_expected.to include('stats') expect(values).to include('stats')
is_expected.to include('podAnnotations') expect(values).to include('podAnnotations')
end end
end end
end end
...@@ -38,23 +38,23 @@ describe Clusters::Applications::Jupyter do ...@@ -38,23 +38,23 @@ describe Clusters::Applications::Jupyter do
expect(subject.chart).to eq('jupyter/jupyterhub') expect(subject.chart).to eq('jupyter/jupyterhub')
expect(subject.version).to be_nil expect(subject.version).to be_nil
expect(subject.repository).to eq('https://jupyterhub.github.io/helm-chart/') expect(subject.repository).to eq('https://jupyterhub.github.io/helm-chart/')
expect(subject.values).to eq(jupyter.values) expect(subject.files).to eq(jupyter.files)
end end
end end
describe '#values' do describe '#files' do
let(:jupyter) { create(:clusters_applications_jupyter) } let(:jupyter) { create(:clusters_applications_jupyter) }
subject { jupyter.values } let(:values) { jupyter.files[:'values.yaml'] }
it 'should include valid values' do it 'should include valid values' do
is_expected.to include('ingress') expect(values).to include('ingress')
is_expected.to include('hub') expect(values).to include('hub')
is_expected.to include('rbac') expect(values).to include('rbac')
is_expected.to include('proxy') expect(values).to include('proxy')
is_expected.to include('auth') expect(values).to include('auth')
is_expected.to include("clientId: #{jupyter.oauth_application.uid}") expect(values).to match(/clientId: '?#{jupyter.oauth_application.uid}/)
is_expected.to include("callbackUrl: #{jupyter.callback_url}") expect(values).to match(/callbackUrl: '?#{jupyter.callback_url}/)
end end
end end
end end
...@@ -153,21 +153,21 @@ describe Clusters::Applications::Prometheus do ...@@ -153,21 +153,21 @@ describe Clusters::Applications::Prometheus do
expect(command.name).to eq('prometheus') expect(command.name).to eq('prometheus')
expect(command.chart).to eq('stable/prometheus') expect(command.chart).to eq('stable/prometheus')
expect(command.version).to eq('6.7.3') expect(command.version).to eq('6.7.3')
expect(command.values).to eq(prometheus.values) expect(command.files).to eq(prometheus.files)
end end
end end
describe '#values' do describe '#files' do
let(:prometheus) { create(:clusters_applications_prometheus) } let(:prometheus) { create(:clusters_applications_prometheus) }
subject { prometheus.values } let(:values) { prometheus.files[:'values.yaml'] }
it 'should include prometheus valid values' do it 'should include prometheus valid values' do
is_expected.to include('alertmanager') expect(values).to include('alertmanager')
is_expected.to include('kubeStateMetrics') expect(values).to include('kubeStateMetrics')
is_expected.to include('nodeExporter') expect(values).to include('nodeExporter')
is_expected.to include('pushgateway') expect(values).to include('pushgateway')
is_expected.to include('serverFiles') expect(values).to include('serverFiles')
end end
end end
end end
...@@ -33,25 +33,26 @@ describe Clusters::Applications::Runner do ...@@ -33,25 +33,26 @@ describe Clusters::Applications::Runner do
expect(subject.chart).to eq('runner/gitlab-runner') expect(subject.chart).to eq('runner/gitlab-runner')
expect(subject.version).to be_nil expect(subject.version).to be_nil
expect(subject.repository).to eq('https://charts.gitlab.io') expect(subject.repository).to eq('https://charts.gitlab.io')
expect(subject.values).to eq(gitlab_runner.values) expect(subject.files).to eq(gitlab_runner.files)
end end
end end
describe '#values' do describe '#files' do
let(:gitlab_runner) { create(:clusters_applications_runner, runner: ci_runner) } let(:gitlab_runner) { create(:clusters_applications_runner, runner: ci_runner) }
subject { gitlab_runner.values } subject { gitlab_runner.files }
let(:values) { subject[:'values.yaml'] }
it 'should include runner valid values' do it 'should include runner valid values' do
is_expected.to include('concurrent') expect(values).to include('concurrent')
is_expected.to include('checkInterval') expect(values).to include('checkInterval')
is_expected.to include('rbac') expect(values).to include('rbac')
is_expected.to include('runners') expect(values).to include('runners')
is_expected.to include('privileged: true') expect(values).to include('privileged: true')
is_expected.to include('image: ubuntu:16.04') expect(values).to include('image: ubuntu:16.04')
is_expected.to include('resources') expect(values).to include('resources')
is_expected.to include("runnerToken: #{ci_runner.token}") expect(values).to match(/runnerToken: '?#{ci_runner.token}/)
is_expected.to include("gitlabUrl: #{Gitlab::Routing.url_helpers.root_url}") expect(values).to match(/gitlabUrl: '?#{Gitlab::Routing.url_helpers.root_url}/)
end end
context 'without a runner' do context 'without a runner' do
...@@ -66,7 +67,7 @@ describe Clusters::Applications::Runner do ...@@ -66,7 +67,7 @@ describe Clusters::Applications::Runner do
end end
it 'uses the new runner token' do it 'uses the new runner token' do
expect(subject).to include("runnerToken: #{gitlab_runner.reload.runner.token}") expect(values).to match(/runnerToken: '?#{gitlab_runner.reload.runner.token}/)
end end
it 'assigns the new runner to runner' do it 'assigns the new runner to runner' do
...@@ -77,7 +78,7 @@ describe Clusters::Applications::Runner do ...@@ -77,7 +78,7 @@ describe Clusters::Applications::Runner do
end end
context 'with duplicated values on vendor/runner/values.yaml' do context 'with duplicated values on vendor/runner/values.yaml' do
let(:values) do let(:stub_values) do
{ {
"concurrent" => 4, "concurrent" => 4,
"checkInterval" => 3, "checkInterval" => 3,
...@@ -96,11 +97,11 @@ describe Clusters::Applications::Runner do ...@@ -96,11 +97,11 @@ describe Clusters::Applications::Runner do
end end
before do before do
allow(gitlab_runner).to receive(:chart_values).and_return(values) allow(gitlab_runner).to receive(:chart_values).and_return(stub_values)
end end
it 'should overwrite values.yaml' do it 'should overwrite values.yaml' do
is_expected.to include("privileged: #{gitlab_runner.privileged}") expect(values).to match(/privileged: '?#{gitlab_runner.privileged}/)
end end
end end
end end
......
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