Commit cc5fd26d authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'move-kubernetes-variables-to-contextable' into 'master'

Move kubernetes_variables to scoped_variables

See merge request gitlab-org/gitlab!73706
parents 0387e943 b341c4b0
...@@ -539,7 +539,6 @@ module Ci ...@@ -539,7 +539,6 @@ module Ci
.concat(persisted_variables) .concat(persisted_variables)
.concat(dependency_proxy_variables) .concat(dependency_proxy_variables)
.concat(job_jwt_variables) .concat(job_jwt_variables)
.concat(kubernetes_variables)
.concat(scoped_variables) .concat(scoped_variables)
.concat(job_variables) .concat(job_variables)
.concat(persisted_environment_variables) .concat(persisted_environment_variables)
...@@ -1161,22 +1160,6 @@ module Ci ...@@ -1161,22 +1160,6 @@ module Ci
end end
end end
def kubernetes_variables
::Gitlab::Ci::Variables::Collection.new.tap do |collection|
# A cluster deployemnt may also define a KUBECONFIG variable, so to keep existing
# configurations working we shouldn't overwrite it here.
# This check will be removed when Cluster and Agent configurations are
# merged in https://gitlab.com/gitlab-org/gitlab/-/issues/335089
break collection if deployment&.deployment_cluster
template = ::Ci::GenerateKubeconfigService.new(self).execute # rubocop: disable CodeReuse/ServiceClass
if template.valid?
collection.append(key: 'KUBECONFIG', value: template.to_yaml, public: false, file: true)
end
end
end
def conditionally_allow_failure!(exit_code) def conditionally_allow_failure!(exit_code)
return unless exit_code return unless exit_code
......
...@@ -17,6 +17,7 @@ module Ci ...@@ -17,6 +17,7 @@ module Ci
variables.concat(project.predefined_variables) variables.concat(project.predefined_variables)
variables.concat(pipeline.predefined_variables) variables.concat(pipeline.predefined_variables)
variables.concat(runner.predefined_variables) if runnable? && runner variables.concat(runner.predefined_variables) if runnable? && runner
variables.concat(kubernetes_variables)
variables.concat(deployment_variables(environment: environment)) variables.concat(deployment_variables(environment: environment))
variables.concat(yaml_variables) variables.concat(yaml_variables)
variables.concat(user_variables) variables.concat(user_variables)
...@@ -88,6 +89,18 @@ module Ci ...@@ -88,6 +89,18 @@ module Ci
end end
end end
def kubernetes_variables
::Gitlab::Ci::Variables::Collection.new.tap do |collection|
# Should get merged with the cluster kubeconfig in deployment_variables, see
# https://gitlab.com/gitlab-org/gitlab/-/issues/335089
template = ::Ci::GenerateKubeconfigService.new(self).execute
if template.valid?
collection.append(key: 'KUBECONFIG', value: template.to_yaml, public: false, file: true)
end
end
end
def deployment_variables(environment:) def deployment_variables(environment:)
return [] unless environment return [] unless environment
......
...@@ -3398,31 +3398,6 @@ RSpec.describe Ci::Build do ...@@ -3398,31 +3398,6 @@ RSpec.describe Ci::Build do
it { is_expected.to include(key: job_variable.key, value: job_variable.value, public: false, masked: false) } it { is_expected.to include(key: job_variable.key, value: job_variable.value, public: false, masked: false) }
end end
describe 'kubernetes variables' do
let(:service) { double(execute: template) }
let(:template) { double(to_yaml: 'example-kubeconfig', valid?: template_valid) }
let(:template_valid) { true }
before do
allow(Ci::GenerateKubeconfigService).to receive(:new).with(build).and_return(service)
end
it { is_expected.to include(key: 'KUBECONFIG', value: 'example-kubeconfig', public: false, file: true) }
context 'job is deploying to a cluster' do
let(:deployment) { create(:deployment, deployment_cluster: create(:deployment_cluster)) }
let(:build) { create(:ci_build, pipeline: pipeline, deployment: deployment) }
it { is_expected.not_to include(key: 'KUBECONFIG', value: 'example-kubeconfig', public: false, file: true) }
end
context 'generated config is invalid' do
let(:template_valid) { false }
it { is_expected.not_to include(key: 'KUBECONFIG', value: 'example-kubeconfig', public: false, file: true) }
end
end
end end
describe '#scoped_variables' do describe '#scoped_variables' do
...@@ -3631,6 +3606,27 @@ RSpec.describe Ci::Build do ...@@ -3631,6 +3606,27 @@ RSpec.describe Ci::Build do
include_examples "secret CI variables" include_examples "secret CI variables"
end end
describe '#kubernetes_variables' do
let(:build) { create(:ci_build) }
let(:service) { double(execute: template) }
let(:template) { double(to_yaml: 'example-kubeconfig', valid?: template_valid) }
let(:template_valid) { true }
subject { build.kubernetes_variables }
before do
allow(Ci::GenerateKubeconfigService).to receive(:new).with(build).and_return(service)
end
it { is_expected.to include(key: 'KUBECONFIG', value: 'example-kubeconfig', public: false, file: true) }
context 'generated config is invalid' do
let(:template_valid) { false }
it { is_expected.not_to include(key: 'KUBECONFIG', value: 'example-kubeconfig', public: false, file: true) }
end
end
describe '#deployment_variables' do describe '#deployment_variables' do
let(:build) { create(:ci_build, environment: environment) } let(:build) { create(:ci_build, environment: environment) }
let(:environment) { 'production' } let(:environment) { 'production' }
......
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