Commit 061b2714 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '2525-ee-kubernetes' into 'master'

Move some EE-only KubernetesService code to a prepended module

Closes #2525

See merge request !2168
parents 8aaa2a00 b78ce713
module EE
module KubernetesService
def rollout_status(environment)
with_reactive_cache do |data|
specs = filter_by_label(data[:deployments], app: environment.slug)
::Gitlab::Kubernetes::RolloutStatus.from_specs(*specs)
end
end
def calculate_reactive_cache
result = super
result[:deployments] = read_deployments if result
result
end
def read_deployments
kubeclient = build_kubeclient!(api_path: 'apis/extensions', api_version: 'v1beta1')
kubeclient.get_deployments(namespace: actual_namespace).as_json
rescue KubeException => err
raise err unless err.error_code == 404
[]
end
end
end
...@@ -3,6 +3,8 @@ class KubernetesService < DeploymentService ...@@ -3,6 +3,8 @@ class KubernetesService < DeploymentService
include Gitlab::Kubernetes include Gitlab::Kubernetes
include ReactiveCaching include ReactiveCaching
prepend EE::KubernetesService
self.reactive_cache_key = ->(service) { [service.class.model_name.singular, service.project_id] } self.reactive_cache_key = ->(service) { [service.class.model_name.singular, service.project_id] }
# Namespace defaults to the project path, but can be overridden in case that # Namespace defaults to the project path, but can be overridden in case that
...@@ -122,21 +124,13 @@ class KubernetesService < DeploymentService ...@@ -122,21 +124,13 @@ class KubernetesService < DeploymentService
end end
end end
def rollout_status(environment)
with_reactive_cache do |data|
specs = filter_by_label(data[:deployments], app: environment.slug)
::Gitlab::Kubernetes::RolloutStatus.from_specs(*specs)
end
end
# Caches resources in the namespace so other calls don't need to block on # Caches resources in the namespace so other calls don't need to block on
# network access # network access
def calculate_reactive_cache def calculate_reactive_cache
return unless active? && project && !project.pending_delete? return unless active? && project && !project.pending_delete?
# We may want to cache extra things in the future # We may want to cache extra things in the future
{ pods: read_pods, deployments: read_deployments } { pods: read_pods }
end end
TEMPLATE_PLACEHOLDER = 'Kubernetes namespace'.freeze TEMPLATE_PLACEHOLDER = 'Kubernetes namespace'.freeze
...@@ -173,15 +167,6 @@ class KubernetesService < DeploymentService ...@@ -173,15 +167,6 @@ class KubernetesService < DeploymentService
[] []
end end
def read_deployments
kubeclient = build_kubeclient!(api_path: 'apis/extensions', api_version: 'v1beta1')
kubeclient.get_deployments(namespace: actual_namespace).as_json
rescue KubeException => err
raise err unless err.error_code == 404
[]
end
def kubeclient_ssl_options def kubeclient_ssl_options
opts = { verify_ssl: OpenSSL::SSL::VERIFY_PEER } opts = { verify_ssl: OpenSSL::SSL::VERIFY_PEER }
......
require 'spec_helper'
describe KubernetesService, models: true, caching: true do
include KubernetesHelpers
include ReactiveCachingHelpers
let(:project) { build_stubbed(:kubernetes_project) }
let(:service) { project.kubernetes_service }
describe '#rollout_status' do
let(:environment) { build(:environment, project: project, name: "env", slug: "env-000000") }
subject(:rollout_status) { service.rollout_status(environment) }
context 'with valid deployments' do
before do
stub_reactive_cache(
service,
deployments: [kube_deployment(app: environment.slug), kube_deployment]
)
end
it 'creates a matching RolloutStatus' do
expect(rollout_status).to be_kind_of(::Gitlab::Kubernetes::RolloutStatus)
expect(rollout_status.deployments.map(&:labels)).to eq([{ 'app' => 'env-000000' }])
end
end
end
end
...@@ -280,25 +280,6 @@ describe KubernetesService, models: true, caching: true do ...@@ -280,25 +280,6 @@ describe KubernetesService, models: true, caching: true do
end end
end end
describe '#rollout_status' do
let(:environment) { build(:environment, project: project, name: "env", slug: "env-000000") }
subject(:rollout_status) { service.rollout_status(environment) }
context 'with valid deployments' do
before do
stub_reactive_cache(
service,
deployments: [kube_deployment(app: environment.slug), kube_deployment]
)
end
it 'creates a matching RolloutStatus' do
expect(rollout_status).to be_kind_of(::Gitlab::Kubernetes::RolloutStatus)
expect(rollout_status.deployments.map(&:labels)).to eq([{ 'app' => 'env-000000' }])
end
end
end
describe '#calculate_reactive_cache' do describe '#calculate_reactive_cache' do
subject { service.calculate_reactive_cache } subject { service.calculate_reactive_cache }
......
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