Commit 1ca9aaf8 authored by Alessio Caiazza's avatar Alessio Caiazza

Merge branch 'refactor-clusters' into 38464-k8s-apps

parents 08752e5d 694cfeb7
...@@ -92,11 +92,7 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -92,11 +92,7 @@ class Projects::ClustersController < Projects::ApplicationController
params.require(:cluster).permit( params.require(:cluster).permit(
:enabled, :enabled,
:name, :name,
:platform_type,
:provider_type, :provider_type,
platform_kubernetes_attributes: [
:namespace
],
provider_gcp_attributes: [ provider_gcp_attributes: [
:gcp_project_id, :gcp_project_id,
:zone, :zone,
...@@ -106,11 +102,7 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -106,11 +102,7 @@ class Projects::ClustersController < Projects::ApplicationController
end end
def update_params def update_params
params.require(:cluster).permit( params.require(:cluster).permit(:enabled)
:enabled,
platform_kubernetes_attributes: [
:namespace
])
end end
def authorize_google_api def authorize_google_api
......
...@@ -27,9 +27,15 @@ module Clusters ...@@ -27,9 +27,15 @@ module Clusters
validates :name, cluster_name: true validates :name, cluster_name: true
validate :restrict_modification, on: :update validate :restrict_modification, on: :update
# TODO: Move back this into Clusters::Platforms::Kubernetes in 10.3
# We need callback here because `enabled` belongs to Clusters::Cluster
# Callbacks in Clusters::Platforms::Kubernetes will not be called after update
after_save :update_kubernetes_integration!
delegate :status, to: :provider, allow_nil: true delegate :status, to: :provider, allow_nil: true
delegate :status_reason, to: :provider, allow_nil: true delegate :status_reason, to: :provider, allow_nil: true
delegate :on_creation?, to: :provider, allow_nil: true delegate :on_creation?, to: :provider, allow_nil: true
delegate :update_kubernetes_integration!, to: :platform, allow_nil: true
enum platform_type: { enum platform_type: {
kubernetes: 1 kubernetes: 1
......
module Clusters module Clusters
module Platforms module Platforms
class Kubernetes < ActiveRecord::Base class Kubernetes < ActiveRecord::Base
include Gitlab::CurrentSettings
self.table_name = 'cluster_platforms_kubernetes' self.table_name = 'cluster_platforms_kubernetes'
belongs_to :cluster, inverse_of: :platform_kubernetes, class_name: 'Clusters::Cluster' belongs_to :cluster, inverse_of: :platform_kubernetes, class_name: 'Clusters::Cluster'
...@@ -28,13 +26,10 @@ module Clusters ...@@ -28,13 +26,10 @@ module Clusters
} }
# We expect to be `active?` only when enabled and cluster is created (the api_url is assigned) # We expect to be `active?` only when enabled and cluster is created (the api_url is assigned)
with_options presence: true, if: :enabled? do validates :api_url, url: true, presence: true
validates :api_url, url: true, presence: true validates :token, presence: true
validates :token, presence: true
end
# TODO: Glue code till we migrate Kubernetes Integration into Platforms::Kubernetes # TODO: Glue code till we migrate Kubernetes Integration into Platforms::Kubernetes
after_save :update_kubernetes_integration!
after_destroy :destroy_kubernetes_integration! after_destroy :destroy_kubernetes_integration!
alias_attribute :ca_pem, :ca_cert alias_attribute :ca_pem, :ca_cert
...@@ -64,6 +59,21 @@ module Clusters ...@@ -64,6 +59,21 @@ module Clusters
@kubeclient ||= kubernetes_service.kubeclient if manages_kubernetes_service? @kubeclient ||= kubernetes_service.kubeclient if manages_kubernetes_service?
end end
def update_kubernetes_integration!
raise 'Kubernetes service already configured' unless manages_kubernetes_service?
# This is neccesary, otheriwse enabled? returns true even though cluster updated with enabled: false
cluster.reload
ensure_kubernetes_service&.update!(
active: enabled?,
api_url: api_url,
namespace: namespace,
token: token,
ca_pem: ca_cert
)
end
private private
def enforce_namespace_to_lower_case def enforce_namespace_to_lower_case
...@@ -80,27 +90,15 @@ module Clusters ...@@ -80,27 +90,15 @@ module Clusters
def destroy_kubernetes_integration! def destroy_kubernetes_integration!
return unless manages_kubernetes_service? return unless manages_kubernetes_service?
kubernetes_service.destroy! kubernetes_service&.destroy!
end
def update_kubernetes_integration!
return raise 'Kubernetes service already configured' unless manages_kubernetes_service?
ensure_kubernetes_service.update!(
active: enabled?,
api_url: api_url,
namespace: namespace,
token: token,
ca_pem: ca_cert
)
end end
def kubernetes_service def kubernetes_service
@kubernetes_service ||= project.kubernetes_service || project.build_kubernetes_service @kubernetes_service ||= project&.kubernetes_service
end end
def ensure_kubernetes_service def ensure_kubernetes_service
@kubernetes_service ||= kubernetes_service || project.build_kubernetes_service @kubernetes_service ||= kubernetes_service || project&.build_kubernetes_service
end end
end end
end end
......
...@@ -13,11 +13,7 @@ module Clusters ...@@ -13,11 +13,7 @@ module Clusters
private private
def create_cluster def create_cluster
Clusters::Cluster.create!( Clusters::Cluster.create(cluster_params)
cluster_params.merge(
projects: [project]))
rescue ActiveRecord::RecordInvalid => e
e.record
end end
def cluster_params def cluster_params
...@@ -27,7 +23,7 @@ module Clusters ...@@ -27,7 +23,7 @@ module Clusters
provider[:access_token] = access_token provider[:access_token] = access_token
end end
@cluster_params = params.merge(user: current_user) @cluster_params = params.merge(user: current_user, projects: [project])
end end
end end
end end
...@@ -9,11 +9,9 @@ module Clusters ...@@ -9,11 +9,9 @@ module Clusters
configure_provider configure_provider
configure_kubernetes configure_kubernetes
provider.make_created! cluster.save!
rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e
provider.make_errored!("Failed to request to CloudPlatform; #{e.message}") provider.make_errored!("Failed to request to CloudPlatform; #{e.message}")
rescue KubeException => e
provider.make_errored!("Failed to request to Kubernetes; #{e.message}")
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
provider.make_errored!("Failed to configure GKE Cluster: #{e.message}") provider.make_errored!("Failed to configure GKE Cluster: #{e.message}")
end end
...@@ -22,6 +20,7 @@ module Clusters ...@@ -22,6 +20,7 @@ module Clusters
def configure_provider def configure_provider
provider.endpoint = gke_cluster.endpoint provider.endpoint = gke_cluster.endpoint
provider.status_event = :make_created
end end
def configure_kubernetes def configure_kubernetes
...@@ -39,7 +38,7 @@ module Clusters ...@@ -39,7 +38,7 @@ module Clusters
'https://' + gke_cluster.endpoint, 'https://' + gke_cluster.endpoint,
Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate), Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate),
gke_cluster.master_auth.username, gke_cluster.master_auth.username,
gke_cluster.master_auth.password) gke_cluster.master_auth.password).execute
end end
def gke_cluster def gke_cluster
......
...@@ -3,7 +3,7 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration ...@@ -3,7 +3,7 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration
def change def change
create_table :clusters do |t| create_table :clusters do |t|
t.references :user, null: false, index: true, foreign_key: { on_delete: :nullify } t.references :user, index: true, foreign_key: { on_delete: :nullify }
t.integer :provider_type t.integer :provider_type
t.integer :platform_type t.integer :platform_type
......
...@@ -464,7 +464,9 @@ ActiveRecord::Schema.define(version: 20171031100710) do ...@@ -464,7 +464,9 @@ ActiveRecord::Schema.define(version: 20171031100710) do
create_table "cluster_platforms_kubernetes", force: :cascade do |t| create_table "cluster_platforms_kubernetes", force: :cascade do |t|
t.integer "cluster_id", null: false t.integer "cluster_id", null: false
t.string "api_url" t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", null: false
t.text "api_url"
t.text "ca_cert" t.text "ca_cert"
t.string "namespace" t.string "namespace"
t.string "username" t.string "username"
...@@ -491,6 +493,9 @@ ActiveRecord::Schema.define(version: 20171031100710) do ...@@ -491,6 +493,9 @@ ActiveRecord::Schema.define(version: 20171031100710) do
create_table "cluster_providers_gcp", force: :cascade do |t| create_table "cluster_providers_gcp", force: :cascade do |t|
t.integer "cluster_id", null: false t.integer "cluster_id", null: false
t.integer "status" t.integer "status"
t.integer "num_nodes", null: false
t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", null: false
t.text "status_reason" t.text "status_reason"
t.string "gcp_project_id", null: false t.string "gcp_project_id", null: false
t.string "zone", null: false t.string "zone", null: false
...@@ -508,6 +513,10 @@ ActiveRecord::Schema.define(version: 20171031100710) do ...@@ -508,6 +513,10 @@ ActiveRecord::Schema.define(version: 20171031100710) do
create_table "clusters", force: :cascade do |t| create_table "clusters", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.integer "provider_type"
t.integer "platform_type"
t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", null: false
t.boolean "enabled", default: true t.boolean "enabled", default: true
t.string "name", null: false t.string "name", null: false
t.integer "provider_type" t.integer "provider_type"
......
...@@ -172,7 +172,6 @@ describe Projects::ClustersController do ...@@ -172,7 +172,6 @@ describe Projects::ClustersController do
{ {
cluster: { cluster: {
name: 'new-cluster', name: 'new-cluster',
platform_type: :kubernetes,
provider_type: :gcp, provider_type: :gcp,
provider_gcp_attributes: { provider_gcp_attributes: {
gcp_project_id: '111' gcp_project_id: '111'
...@@ -202,44 +201,50 @@ describe Projects::ClustersController do ...@@ -202,44 +201,50 @@ describe Projects::ClustersController do
end end
end end
context 'when adds a cluster manually' do # TODO: Activate in 10.3
let(:params) do # context 'when adds a cluster manually' do
{ # let(:params) do
cluster: { # {
name: 'new-cluster', # cluster: {
platform_type: :kubernetes, # name: 'new-cluster',
provider_type: :user, # platform_type: :kubernetes,
platform_kubernetes_attributes: { # provider_type: :user,
namespace: 'custom-namespace', # platform_kubernetes_attributes: {
api_url: 'https://111.111.111.111', # namespace: 'custom-namespace',
token: 'token' # api_url: 'https://111.111.111.111',
} # token: 'token'
} # }
} # }
end # }
# end
it 'creates a new cluster' do
expect(ClusterProvisionWorker).to receive(:perform_async) # it 'creates a new cluster' do
expect { go }.to change { Clusters::Cluster.count } # expect(ClusterProvisionWorker).to receive(:perform_async)
expect(response).to redirect_to(project_cluster_path(project, project.cluster)) # expect { go }.to change { Clusters::Cluster.count }
end # expect(response).to redirect_to(project_cluster_path(project, project.cluster))
end # end
# end
context 'when not all required parameters are set' do
let(:params) do # TODO: We should fix this in 10.2
{ # Maybe
cluster: { # - validates :provider_gcp, presence: true, if: :gcp?
name: 'new-cluster' # - validates :provider_type, presence: true
} # are required in Clusters::Cluster
} # context 'when not all required parameters are set' do
end # let(:params) do
# {
it 'shows an error message' do # cluster: {
expect { go }.not_to change { Clusters::Cluster.count } # name: 'new-cluster'
expect(assigns(:cluster).errors).not_to be_empty # }
expect(response).to render_template(:new) # }
end # end
end
# it 'shows an error message' do
# expect { go }.not_to change { Clusters::Cluster.count }
# expect(assigns(:cluster).errors).not_to be_empty
# expect(response).to render_template(:new)
# end
# end
end end
context 'when access token is expired' do context 'when access token is expired' do
...@@ -393,40 +398,41 @@ describe Projects::ClustersController do ...@@ -393,40 +398,41 @@ describe Projects::ClustersController do
end end
end end
context 'when update namespace' do # TODO: Activate in 10.3
let(:namespace) { 'namespace-123' } # context 'when update namespace' do
# let(:namespace) { 'namespace-123' }
let(:params) do
{ # let(:params) do
cluster: { # {
platform_kubernetes_attributes: { # cluster: {
namespace: namespace # platform_kubernetes_attributes: {
} # namespace: namespace
} # }
} # }
end # }
# end
it "updates and redirects back to show page" do
go # it "updates and redirects back to show page" do
# go
cluster.reload
expect(response).to redirect_to(project_cluster_path(project, project.cluster)) # cluster.reload
expect(flash[:notice]).to eq('Cluster was successfully updated.') # expect(response).to redirect_to(project_cluster_path(project, project.cluster))
expect(cluster.platform.namespace).to eq(namespace) # expect(flash[:notice]).to eq('Cluster was successfully updated.')
end # expect(cluster.platform.namespace).to eq(namespace)
# end
context 'when namespace is invalid' do
let(:namespace) { 'my Namespace 321321321 #' } # context 'when namespace is invalid' do
# let(:namespace) { 'my Namespace 321321321 #' }
it "rejects changes" do
go # it "rejects changes" do
# go
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:show) # expect(response).to have_gitlab_http_status(:ok)
expect(cluster.platform.namespace).not_to eq(namespace) # expect(response).to render_template(:show)
end # expect(cluster.platform.namespace).not_to eq(namespace)
end # end
end # end
# end
end end
describe 'security' do describe 'security' do
...@@ -481,7 +487,6 @@ describe Projects::ClustersController do ...@@ -481,7 +487,6 @@ describe Projects::ClustersController do
it "destroys and redirects back to clusters list" do it "destroys and redirects back to clusters list" do
expect { go } expect { go }
.to change { Clusters::Cluster.count }.by(-1) .to change { Clusters::Cluster.count }.by(-1)
.and change { Clusters::Platforms::Kubernetes.count }.by(-1)
.and change { Clusters::Providers::Gcp.count }.by(-1) .and change { Clusters::Providers::Gcp.count }.by(-1)
expect(response).to redirect_to(project_clusters_path(project)) expect(response).to redirect_to(project_clusters_path(project))
......
...@@ -2,8 +2,6 @@ FactoryGirl.define do ...@@ -2,8 +2,6 @@ FactoryGirl.define do
factory :cluster, class: Clusters::Cluster do factory :cluster, class: Clusters::Cluster do
user user
name 'test-cluster' name 'test-cluster'
provider_type :user
platform_type :kubernetes
trait :project do trait :project do
after(:create) do |cluster, evaluator| after(:create) do |cluster, evaluator|
...@@ -24,28 +22,18 @@ FactoryGirl.define do ...@@ -24,28 +22,18 @@ FactoryGirl.define do
provider_type :gcp provider_type :gcp
platform_type :kubernetes platform_type :kubernetes
platform_kubernetes do before(:create) do |cluster, evaluator|
create(:platform_kubernetes, :configured) cluster.platform_kubernetes = build(:platform_kubernetes, :configured)
end cluster.provider_gcp = build(:provider_gcp, :created)
provider_gcp do
create(:provider_gcp, :created)
end end
end end
trait :providing_by_gcp do trait :providing_by_gcp do
provider_type :gcp provider_type :gcp
platform_type :kubernetes
provider_gcp do provider_gcp do
create(:provider_gcp, :creating) create(:provider_gcp, :creating)
end end
after(:create) do |cluster, evaluator|
build(:platform_kubernetes, cluster: cluster).tap do |platform|
platform.save!(validate: false)
end
end
end end
end end
end end
...@@ -6,7 +6,7 @@ describe Projects::ClustersController, '(JavaScript fixtures)', type: :controlle ...@@ -6,7 +6,7 @@ describe Projects::ClustersController, '(JavaScript fixtures)', type: :controlle
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let(:namespace) { create(:namespace, name: 'frontend-fixtures' )} let(:namespace) { create(:namespace, name: 'frontend-fixtures' )}
let(:project) { create(:project, :repository, namespace: namespace) } let(:project) { create(:project, :repository, namespace: namespace) }
let(:cluster) { project.create_cluster!(gcp_cluster_name: "gke-test-creation-1", gcp_project_id: 'gitlab-internal-153318', gcp_cluster_zone: 'us-central1-a', gcp_cluster_size: '1', project_namespace: 'aaa', gcp_machine_type: 'n1-standard-1')} let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
render_views render_views
......
...@@ -147,10 +147,16 @@ deploy_keys: ...@@ -147,10 +147,16 @@ deploy_keys:
- user - user
- deploy_keys_projects - deploy_keys_projects
- projects - projects
cluster: clusters:
- project - projects
- user - user
- service cluster_projects:
- projects
- clusters
provider_gcp:
- cluster
platform_kubernetes:
- cluster
services: services:
- project - project
- service_hook - service_hook
......
...@@ -356,32 +356,6 @@ Clusters::Platforms::Kubernetes: ...@@ -356,32 +356,6 @@ Clusters::Platforms::Kubernetes:
- encrypted_token_iv - encrypted_token_iv
- created_at - created_at
- updated_at - updated_at
Gcp::Cluster:
- id
- project_id
- user_id
- service_id
- enabled
- status
- status_reason
- project_namespace
- endpoint
- ca_cert
- encrypted_kubernetes_token
- encrypted_kubernetes_token_iv
- username
- encrypted_password
- encrypted_password_iv
- gcp_project_id
- gcp_cluster_zone
- gcp_cluster_name
- gcp_cluster_size
- gcp_machine_type
- gcp_operation_id
- encrypted_gcp_token
- encrypted_gcp_token_iv
- created_at
- updated_at
DeployKey: DeployKey:
- id - id
- user_id - user_id
......
...@@ -9,9 +9,8 @@ describe Clusters::Cluster do ...@@ -9,9 +9,8 @@ describe Clusters::Cluster do
it { is_expected.to delegate_method(:status_reason).to(:provider) } it { is_expected.to delegate_method(:status_reason).to(:provider) }
it { is_expected.to delegate_method(:status_name).to(:provider) } it { is_expected.to delegate_method(:status_name).to(:provider) }
it { is_expected.to delegate_method(:on_creation?).to(:provider) } it { is_expected.to delegate_method(:on_creation?).to(:provider) }
it { is_expected.to delegate_method(:update_kubernetes_integration!).to(:platform) }
it { is_expected.to respond_to :project } it { is_expected.to respond_to :project }
it { is_expected.to validate_presence_of(:provider_type) }
it { is_expected.to validate_presence_of(:platform_type) }
describe '.enabled' do describe '.enabled' do
subject { described_class.enabled } subject { described_class.enabled }
......
...@@ -5,8 +5,6 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching ...@@ -5,8 +5,6 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
include ReactiveCachingHelpers include ReactiveCachingHelpers
it { is_expected.to belong_to(:cluster) } it { is_expected.to belong_to(:cluster) }
it { is_expected.to be_kind_of(Gitlab::Kubernetes) }
it { is_expected.to be_kind_of(ReactiveCaching) }
it { is_expected.to respond_to :ca_pem } it { is_expected.to respond_to :ca_pem }
describe 'before_validation' do describe 'before_validation' do
...@@ -92,6 +90,55 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching ...@@ -92,6 +90,55 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
end end
end end
describe 'after_save from Clusters::Cluster' do
context 'when platform_kubernetes is being cerated' do
let(:enabled) { true }
let(:project) { create(:project) }
let(:cluster) { build(:cluster, provider_type: :gcp, platform_type: :kubernetes, platform_kubernetes: platform, provider_gcp: provider, enabled: enabled, projects: [project]) }
let(:platform) { build(:platform_kubernetes, :configured) }
let(:provider) { build(:provider_gcp) }
let(:kubernetes_service) { project.kubernetes_service }
it 'updates KubernetesService' do
cluster.save!
expect(kubernetes_service.active).to eq(enabled)
expect(kubernetes_service.api_url).to eq(platform.api_url)
expect(kubernetes_service.namespace).to eq(platform.namespace)
expect(kubernetes_service.ca_pem).to eq(platform.ca_cert)
end
end
context 'when platform_kubernetes has been created' do
let(:enabled) { false }
let!(:project) { create(:project) }
let!(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:platform) { cluster.platform }
let(:kubernetes_service) { project.kubernetes_service }
it 'updates KubernetesService' do
cluster.update(enabled: enabled)
expect(kubernetes_service.active).to eq(enabled)
end
end
context 'when kubernetes_service has been configured without cluster integration' do
let!(:project) { create(:project) }
let(:cluster) { build(:cluster, provider_type: :gcp, platform_type: :kubernetes, platform_kubernetes: platform, provider_gcp: provider, projects: [project]) }
let(:platform) { build(:platform_kubernetes, :configured, api_url: 'https://111.111.111.111') }
let(:provider) { build(:provider_gcp) }
before do
create(:kubernetes_service, project: project)
end
it 'raises an error' do
expect { cluster.save! }.to raise_error('Kubernetes service already configured')
end
end
end
describe '#actual_namespace' do describe '#actual_namespace' do
subject { kubernetes.actual_namespace } subject { kubernetes.actual_namespace }
...@@ -138,137 +185,4 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching ...@@ -138,137 +185,4 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end
end end
describe '#predefined_variables' do
let!(:cluster) { create(:cluster, :project, platform_kubernetes: kubernetes) }
let(:kubernetes) { create(:platform_kubernetes, api_url: api_url, ca_cert: ca_pem, token: token) }
let(:api_url) { 'https://kube.domain.com' }
let(:ca_pem) { 'CA PEM DATA' }
let(:token) { 'token' }
let(:kubeconfig) do
config_file = expand_fixture_path('config/kubeconfig.yml')
config = YAML.load(File.read(config_file))
config.dig('users', 0, 'user')['token'] = token
config.dig('contexts', 0, 'context')['namespace'] = namespace
config.dig('clusters', 0, 'cluster')['certificate-authority-data'] =
Base64.strict_encode64(ca_pem)
YAML.dump(config)
end
shared_examples 'setting variables' do
it 'sets the variables' do
expect(kubernetes.predefined_variables).to include(
{ key: 'KUBE_URL', value: api_url, public: true },
{ key: 'KUBE_TOKEN', value: token, public: false },
{ key: 'KUBE_NAMESPACE', value: namespace, public: true },
{ key: 'KUBECONFIG', value: kubeconfig, public: false, file: true },
{ key: 'KUBE_CA_PEM', value: ca_pem, public: true },
{ key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true }
)
end
end
context 'namespace is provided' do
let(:namespace) { 'my-project' }
before do
kubernetes.namespace = namespace
end
it_behaves_like 'setting variables'
end
context 'no namespace provided' do
let(:namespace) { kubernetes.actual_namespace }
it_behaves_like 'setting variables'
it 'sets the KUBE_NAMESPACE' do
kube_namespace = kubernetes.predefined_variables.find { |h| h[:key] == 'KUBE_NAMESPACE' }
expect(kube_namespace).not_to be_nil
expect(kube_namespace[:value]).to match(/\A#{Gitlab::PathRegex::PATH_REGEX_STR}-\d+\z/)
end
end
end
describe '#terminals' do
subject { service.terminals(environment) }
let!(:cluster) { create(:cluster, :project, platform_kubernetes: service) }
let(:project) { cluster.project }
let(:service) { create(:platform_kubernetes, :configured) }
let(:environment) { build(:environment, project: project, name: "env", slug: "env-000000") }
context 'with invalid pods' do
it 'returns no terminals' do
stub_reactive_cache(service, pods: [{ "bad" => "pod" }])
is_expected.to be_empty
end
end
context 'with valid pods' do
let(:pod) { kube_pod(app: environment.slug) }
let(:terminals) { kube_terminals(service, pod) }
before do
stub_reactive_cache(
service,
pods: [pod, pod, kube_pod(app: "should-be-filtered-out")]
)
end
it 'returns terminals' do
is_expected.to eq(terminals + terminals)
end
it 'uses max session time from settings' do
stub_application_setting(terminal_max_session_time: 600)
times = subject.map { |terminal| terminal[:max_session_time] }
expect(times).to eq [600, 600, 600, 600]
end
end
end
describe '#calculate_reactive_cache' do
subject { service.calculate_reactive_cache }
let!(:cluster) { create(:cluster, :project, enabled: enabled, platform_kubernetes: service) }
let(:service) { create(:platform_kubernetes, :configured) }
let(:enabled) { true }
context 'when cluster is disabled' do
let(:enabled) { false }
it { is_expected.to be_nil }
end
context 'when kubernetes responds with valid pods' do
before do
stub_kubeclient_pods
end
it { is_expected.to eq(pods: [kube_pod]) }
end
context 'when kubernetes responds with 500s' do
before do
stub_kubeclient_pods(status: 500)
end
it { expect { subject }.to raise_error(KubeException) }
end
context 'when kubernetes responds with 404s' do
before do
stub_kubeclient_pods(status: 404)
end
it { is_expected.to eq(pods: []) }
end
end
end end
...@@ -11,11 +11,7 @@ describe Clusters::CreateService do ...@@ -11,11 +11,7 @@ describe Clusters::CreateService do
let(:params) do let(:params) do
{ {
name: 'test-cluster', name: 'test-cluster',
platform_type: :kubernetes,
provider_type: :gcp, provider_type: :gcp,
platform_kubernetes_attributes: {
namespace: 'custom-namespace'
},
provider_gcp_attributes: { provider_gcp_attributes: {
gcp_project_id: 'gcp-project', gcp_project_id: 'gcp-project',
zone: 'us-central1-a', zone: 'us-central1-a',
...@@ -30,7 +26,6 @@ describe Clusters::CreateService do ...@@ -30,7 +26,6 @@ describe Clusters::CreateService do
expect { result } expect { result }
.to change { Clusters::Cluster.count }.by(1) .to change { Clusters::Cluster.count }.by(1)
.and change { Clusters::Platforms::Kubernetes.count }.by(1)
.and change { Clusters::Providers::Gcp.count }.by(1) .and change { Clusters::Providers::Gcp.count }.by(1)
expect(result.name).to eq('test-cluster') expect(result.name).to eq('test-cluster')
...@@ -41,9 +36,7 @@ describe Clusters::CreateService do ...@@ -41,9 +36,7 @@ describe Clusters::CreateService do
expect(result.provider.num_nodes).to eq(1) expect(result.provider.num_nodes).to eq(1)
expect(result.provider.machine_type).to eq('machine_type-a') expect(result.provider.machine_type).to eq('machine_type-a')
expect(result.provider.access_token).to eq(access_token) expect(result.provider.access_token).to eq(access_token)
expect(result.platform.namespace).to eq('custom-namespace') expect(result.platform).to be_nil
expect(result.platform.api_url).to eq(Clusters::CreateService::TEMPOLARY_API_URL)
expect(result.platform.token).to eq(Clusters::CreateService::TEMPOLARY_TOKEN)
end end
end end
...@@ -51,11 +44,7 @@ describe Clusters::CreateService do ...@@ -51,11 +44,7 @@ describe Clusters::CreateService do
let(:params) do let(:params) do
{ {
name: 'test-cluster', name: 'test-cluster',
platform_type: :kubernetes,
provider_type: :gcp, provider_type: :gcp,
platform_kubernetes_attributes: {
namespace: 'custom-namespace'
},
provider_gcp_attributes: { provider_gcp_attributes: {
gcp_project_id: '!!!!!!!', gcp_project_id: '!!!!!!!',
zone: 'us-central1-a', zone: 'us-central1-a',
...@@ -73,55 +62,56 @@ describe Clusters::CreateService do ...@@ -73,55 +62,56 @@ describe Clusters::CreateService do
end end
end end
context 'when provider is user' do # TODO: This will be active in 10.3
context 'when correct params' do # context 'when provider is user' do
let(:params) do # context 'when correct params' do
{ # let(:params) do
name: 'test-cluster', # {
platform_type: :kubernetes, # name: 'test-cluster',
provider_type: :user, # platform_type: :kubernetes,
platform_kubernetes_attributes: { # provider_type: :user,
namespace: 'custom-namespace', # platform_kubernetes_attributes: {
api_url: 'https://111.111.111.111', # namespace: 'custom-namespace',
token: 'token' # api_url: 'https://111.111.111.111',
} # token: 'token'
} # }
end # }
# end
it 'creates a cluster object and performs a worker' do # it 'creates a cluster object and performs a worker' do
expect(ClusterProvisionWorker).to receive(:perform_async) # expect(ClusterProvisionWorker).to receive(:perform_async)
expect { result } # expect { result }
.to change { Clusters::Cluster.count }.by(1) # .to change { Clusters::Cluster.count }.by(1)
.and change { Clusters::Platforms::Kubernetes.count }.by(1) # .and change { Clusters::Platforms::Kubernetes.count }.by(1)
expect(result.name).to eq('test-cluster') # expect(result.name).to eq('test-cluster')
expect(result.user).to eq(user) # expect(result.user).to eq(user)
expect(result.project).to eq(project) # expect(result.project).to eq(project)
expect(result.provider).to be_nil # expect(result.provider).to be_nil
expect(result.platform.namespace).to eq('custom-namespace') # expect(result.platform.namespace).to eq('custom-namespace')
end # end
end # end
context 'when invalid params' do # context 'when invalid params' do
let(:params) do # let(:params) do
{ # {
name: 'test-cluster', # name: 'test-cluster',
platform_type: :kubernetes, # platform_type: :kubernetes,
provider_type: :user, # provider_type: :user,
platform_kubernetes_attributes: { # platform_kubernetes_attributes: {
namespace: 'custom-namespace', # namespace: 'custom-namespace',
api_url: '!!!!!', # api_url: '!!!!!',
token: 'token' # token: 'token'
} # }
} # }
end # end
it 'returns an error' do # it 'returns an error' do
# expect(ClusterProvisionWorker).not_to receive(:perform_async) # # expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { result }.to change { Clusters::Cluster.count }.by(0) # expect { result }.to change { Clusters::Cluster.count }.by(0)
expect(result.errors[:"platform_kubernetes.api_url"]).to be_present # expect(result.errors[:"platform_kubernetes.api_url"]).to be_present
end # end
end # end
end # end
end end
...@@ -62,6 +62,7 @@ describe Clusters::Gcp::FinalizeCreationService do ...@@ -62,6 +62,7 @@ describe Clusters::Gcp::FinalizeCreationService do
it 'has corresponded data' do it 'has corresponded data' do
described_class.new.execute(provider) described_class.new.execute(provider)
cluster.reload
provider.reload provider.reload
platform.reload platform.reload
......
...@@ -27,7 +27,7 @@ module KubernetesHelpers ...@@ -27,7 +27,7 @@ module KubernetesHelpers
def stub_kubeclient_get_secrets_error(api_url) def stub_kubeclient_get_secrets_error(api_url)
WebMock.stub_request(:get, api_url + '/api/v1/secrets') WebMock.stub_request(:get, api_url + '/api/v1/secrets')
.to_return(status: [500, "Internal Server Error"]) .to_return(status: [404, "Internal Server Error"])
end end
def kube_v1_secrets_body(**options) def kube_v1_secrets_body(**options)
......
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