Commit 075afd7c authored by Tiger's avatar Tiger

Add Kubernetes option to environment CI config

Introduces a new CI configuration option that allows
specifying the Kubernetes namespace that an environment
will be deployed to. This option does not currently
have any effect on deployments, deploying to the
specified namespace will be added as part of
https://gitlab.com/gitlab-org/gitlab/issues/27630

Example usage:

environment:
  name: production
  url: https://production.example.com
  kubernetes:
    namespace: production
parent 4a525497
---
title: Allow specifying Kubernetes namespace for an environment in gitlab-ci.yml
merge_request: 20270
author:
type: added
......@@ -8,9 +8,11 @@ module Gitlab
# Entry that represents an environment.
#
class Environment < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Validatable
include ::Gitlab::Config::Entry::Configurable
ALLOWED_KEYS = %i[name url action on_stop].freeze
ALLOWED_KEYS = %i[name url action on_stop kubernetes].freeze
entry :kubernetes, Entry::Kubernetes, description: 'Kubernetes deployment configuration.'
validations do
validate do
......@@ -46,6 +48,7 @@ module Gitlab
allow_nil: true
validates :on_stop, type: String, allow_nil: true
validates :kubernetes, type: Hash, allow_nil: true
end
end
......@@ -73,6 +76,10 @@ module Gitlab
value[:on_stop]
end
def kubernetes
value[:kubernetes]
end
def value
case @config
when String then { name: @config, action: 'start' }
......@@ -80,6 +87,10 @@ module Gitlab
else {}
end
end
def skip_config_hash_validation?
true
end
end
end
end
......
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module Entry
class Kubernetes < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Validatable
include ::Gitlab::Config::Entry::Attributable
ALLOWED_KEYS = %i[namespace].freeze
attributes ALLOWED_KEYS
validations do
validates :config, type: Hash
validates :config, allowed_keys: ALLOWED_KEYS
validates :namespace, type: String, presence: true
end
end
end
end
end
end
......@@ -241,4 +241,28 @@ describe Gitlab::Ci::Config::Entry::Environment do
end
end
end
describe 'kubernetes' do
let(:config) do
{ name: 'production', kubernetes: kubernetes_config }
end
context 'is a string' do
let(:kubernetes_config) { 'production' }
it { expect(entry).not_to be_valid }
end
context 'is a hash' do
let(:kubernetes_config) { Hash(namespace: 'production') }
it { expect(entry).to be_valid }
end
context 'is nil' do
let(:kubernetes_config) { nil }
it { expect(entry).to be_valid }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Ci::Config::Entry::Kubernetes do
subject { described_class.new(config) }
describe 'attributes' do
it { is_expected.to respond_to(:namespace) }
it { is_expected.to respond_to(:has_namespace?) }
end
describe 'validations' do
describe 'config' do
context 'is a hash containing known keys' do
let(:config) { Hash(namespace: 'namespace') }
it { is_expected.to be_valid }
end
context 'is a hash containing an unknown key' do
let(:config) { Hash(unknown: 'attribute') }
it { is_expected.not_to be_valid }
end
context 'is a string' do
let(:config) { 'config' }
it { is_expected.not_to be_valid }
end
end
describe 'namespace' do
let(:config) { Hash(namespace: namespace) }
context 'is a string' do
let(:namespace) { 'namespace' }
it { is_expected.to be_valid }
end
context 'is a hash' do
let(:namespace) { Hash(key: 'namespace') }
it { is_expected.not_to be_valid }
end
context 'is not present' do
let(:namespace) { '' }
it { is_expected.not_to be_valid }
end
end
end
end
......@@ -801,6 +801,32 @@ describe Ci::CreatePipelineService do
end
end
context 'environment with Kubernetes configuration' do
let(:kubernetes_namespace) { 'custom-namespace' }
before do
config = YAML.dump(
deploy: {
environment: {
name: "environment-name",
kubernetes: { namespace: kubernetes_namespace }
},
script: 'ls'
}
)
stub_ci_pipeline_yaml_file(config)
end
it 'stores the requested namespace' do
result = execute_service
build = result.builds.first
expect(result).to be_persisted
expect(build.options.dig(:environment, :kubernetes, :namespace)).to eq(kubernetes_namespace)
end
end
context 'when environment with invalid name' do
before do
config = YAML.dump(deploy: { environment: { name: 'name,with,commas' }, script: 'ls' })
......
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