Commit 76faa18c authored by Kamil Trzciński's avatar Kamil Trzciński 🔴

Merge branch 'ee-49952-port-upgrade-command-to-ce' into 'master'

Port UpgradeCommand to CE

See merge request gitlab-org/gitlab-ee!7526
parents b5757b49 83eaf2e1
Pipeline #31812123 passed with stages
in 47 minutes and 49 seconds
......@@ -6,8 +6,6 @@ module Clusters
extend ActiveSupport::Concern
included do
prepend ::EE::Clusters::ApplicationStatus
scope :installed, -> { where(status: self.state_machines[:status].states[:installed].value) }
state_machine :status, initial: :not_installable do
......@@ -17,6 +15,9 @@ module Clusters
state :scheduled, value: 1
state :installing, value: 2
state :installed, value: 3
state :updating, value: 4
state :updated, value: 5
state :update_errored, value: 6
event :make_scheduled do
transition [:installable, :errored] => :scheduled
......@@ -34,6 +35,18 @@ module Clusters
transition any => :errored
end
event :make_updating do
transition [:installed, :updated, :update_errored] => :updating
end
event :make_updated do
transition [:updating] => :updated
end
event :make_update_errored do
transition any => :update_errored
end
before_transition any => [:scheduled] do |app_status, _|
app_status.status_reason = nil
end
......@@ -42,6 +55,15 @@ module Clusters
status_reason = transition.args.first
app_status.status_reason = status_reason if status_reason
end
before_transition any => [:updating] do |app_status, _|
app_status.status_reason = nil
end
before_transition any => [:update_errored] do |app_status, transition|
status_reason = transition.args.first
app_status.status_reason = status_reason if status_reason
end
end
end
end
......
module EE
module Clusters
module ApplicationStatus
extend ActiveSupport::Concern
prepended do
state_machine :status, initial: :not_installable do
state :updating, value: 4
state :updated, value: 5
state :update_errored, value: 6
event :make_updating do
transition [:installed, :updated, :update_errored] => :updating
end
event :make_updated do
transition [:updating] => :updated
end
event :make_update_errored do
transition any => :update_errored
end
before_transition any => [:updating] do |app_status, _|
app_status.status_reason = nil
end
before_transition any => [:update_errored] do |app_status, transition|
status_reason = transition.args.first
app_status.status_reason = status_reason if status_reason
end
end
end
end
end
end
module EE
module Gitlab
module Kubernetes
module Helm
module Api
def get_config_map(config_map_name)
namespace.ensure_exists!
kubeclient.get_config_map(config_map_name, namespace.name)
end
def update(command)
namespace.ensure_exists!
update_config_map(command)
kubeclient.create_pod(command.pod_resource)
end
private
def update_config_map(command)
command.config_map_resource.tap do |config_map_resource|
kubeclient.update_config_map(config_map_resource)
end
end
end
end
end
end
end
require 'spec_helper'
describe Gitlab::Kubernetes::Helm::Api do
let(:kubeclient) { spy }
let(:namespace) { spy }
let(:application) { build(:clusters_applications_prometheus) }
subject { described_class.new(kubeclient) }
before do
allow(Gitlab::Kubernetes::Namespace)
.to receive(:new)
.with(Gitlab::Kubernetes::Helm::NAMESPACE, kubeclient)
.and_return(namespace)
end
describe '#get_config_map' do
it 'ensures the namespace exists before retrieving the config map' do
expect(namespace).to receive(:ensure_exists!).once
subject.get_config_map(application.name)
end
it 'gets the config map on kubeclient' do
expect(kubeclient).to receive(:get_config_map)
.with("example-config-map-name", namespace.name)
.once
subject.get_config_map("example-config-map-name")
end
end
describe '#update' do
let(:rbac) { false }
let(:command) do
Gitlab::Kubernetes::Helm::UpgradeCommand.new(
application.name,
chart: application.chart,
files: application.files,
rbac: rbac
)
end
it 'ensures the namespace exists before creating the pod' do
expect(namespace).to receive(:ensure_exists!).once.ordered
expect(kubeclient).to receive(:create_pod).once.ordered
subject.update(command)
end
it 'updates the config map on kubeclient when one exists' do
resource = Gitlab::Kubernetes::ConfigMap.new(
application.name, application.files
).generate
expect(kubeclient).to receive(:update_config_map).with(resource).once
subject.update(command)
end
end
end
......@@ -2,8 +2,6 @@ module Gitlab
module Kubernetes
module Helm
class Api
prepend EE::Gitlab::Kubernetes::Helm::Api
def initialize(kubeclient)
@kubeclient = kubeclient
@namespace = Gitlab::Kubernetes::Namespace.new(Gitlab::Kubernetes::Helm::NAMESPACE, kubeclient)
......@@ -19,6 +17,12 @@ module Gitlab
kubeclient.create_pod(command.pod_resource)
end
def update(command)
namespace.ensure_exists!
update_config_map(command)
kubeclient.create_pod(command.pod_resource)
end
##
# Returns Pod phase
#
......@@ -38,6 +42,12 @@ module Gitlab
kubeclient.delete_pod(pod_name, namespace.name)
end
def get_config_map(config_map_name)
namespace.ensure_exists!
kubeclient.get_config_map(config_map_name, namespace.name)
end
private
attr_reader :kubeclient, :namespace
......@@ -48,6 +58,12 @@ module Gitlab
end
end
def update_config_map(command)
command.config_map_resource.tap do |config_map_resource|
kubeclient.update_config_map(config_map_resource)
end
end
def create_service_account(command)
command.service_account_resource.tap do |service_account_resource|
break unless service_account_resource
......
require_dependency 'gitlab/kubernetes/helm.rb'
# frozen_string_literal: true
module Gitlab
module Kubernetes
......
......@@ -150,6 +150,43 @@ describe Gitlab::Kubernetes::Helm::Api do
end
end
describe '#update' do
let(:rbac) { false }
let(:command) do
Gitlab::Kubernetes::Helm::UpgradeCommand.new(
application_name,
chart: 'chart-name',
files: files,
rbac: rbac
)
end
before do
allow(namespace).to receive(:ensure_exists!).once
allow(client).to receive(:update_config_map).and_return(nil)
allow(client).to receive(:create_pod).and_return(nil)
end
it 'ensures the namespace exists before creating the pod' do
expect(namespace).to receive(:ensure_exists!).once.ordered
expect(client).to receive(:create_pod).once.ordered
subject.update(command)
end
it 'updates the config map on kubeclient when one exists' do
resource = Gitlab::Kubernetes::ConfigMap.new(
application_name, files
).generate
expect(client).to receive(:update_config_map).with(resource).once
subject.update(command)
end
end
describe '#status' do
let(:phase) { Gitlab::Kubernetes::Pod::RUNNING }
let(:pod) { Kubeclient::Resource.new(status: { phase: phase }) } # partial representation
......@@ -179,4 +216,25 @@ describe Gitlab::Kubernetes::Helm::Api do
subject.delete_pod!(command.pod_name)
end
end
describe '#get_config_map' do
before do
allow(namespace).to receive(:ensure_exists!).once
allow(client).to receive(:get_config_map).and_return(nil)
end
it 'ensures the namespace exists before retrieving the config map' do
expect(namespace).to receive(:ensure_exists!).once
subject.get_config_map('example-config-map-name')
end
it 'gets the config map on kubeclient' do
expect(client).to receive(:get_config_map)
.with('example-config-map-name', namespace.name)
.once
subject.get_config_map('example-config-map-name')
end
end
end
# frozen_string_literal: true
require 'rails_helper'
describe Gitlab::Kubernetes::Helm::UpgradeCommand do
......
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