diff options
author | Tiger <twatson@gitlab.com> | 2019-05-16 17:49:12 -0500 |
---|---|---|
committer | Tiger <twatson@gitlab.com> | 2019-05-21 11:38:11 -0500 |
commit | 101c4480b32044682e453753c6bb18c2a296b044 (patch) | |
tree | 46b6ac3df55c748a6854bdb5c02a696228a7e9fc /spec | |
parent | 0702d4b6899648ba12a747824f3db86c2f4aa42e (diff) | |
download | gitlab-ce-101c4480b32044682e453753c6bb18c2a296b044.tar.gz |
Remove legacy Kubernetes #actual_namespace61935-remove-code-left-over-from-when-clusters-were-always-project-specific
When Kubernetes clusters were originally built they could only
exist at the project level, and so there was logic included
that assumed there would only ever be a single Kubernetes
namespace per cluster. We now support clusters at the group
and instance level, which allows multiple namespaces.
This change consolidates various project-specific fallbacks to
generate namespaces, and hands all responsibility to the
Clusters::KubernetesNamespace model. There is now no concept of
a single namespace for a Clusters::Platforms::Kubernetes; to
retrieve a namespace a project must now be supplied in all cases.
This simplifies upcoming work to use a separate Kubernetes
namespace per project environment (instead of a namespace
per project).
Diffstat (limited to 'spec')
9 files changed, 47 insertions, 38 deletions
diff --git a/spec/lib/gitlab/prometheus/query_variables_spec.rb b/spec/lib/gitlab/prometheus/query_variables_spec.rb index 048f4af6020..6dc99ef26ec 100644 --- a/spec/lib/gitlab/prometheus/query_variables_spec.rb +++ b/spec/lib/gitlab/prometheus/query_variables_spec.rb @@ -23,7 +23,7 @@ describe Gitlab::Prometheus::QueryVariables do context 'with deployment platform' do context 'with project cluster' do - let(:kube_namespace) { environment.deployment_platform.actual_namespace } + let(:kube_namespace) { environment.deployment_platform.cluster.kubernetes_namespace_for(project) } before do create(:cluster, :project, :provided_by_user, projects: [project]) diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 58203da5b22..60065ff22af 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -17,7 +17,6 @@ describe Clusters::Cluster do it { is_expected.to have_one(:application_prometheus) } it { is_expected.to have_one(:application_runner) } it { is_expected.to have_many(:kubernetes_namespaces) } - it { is_expected.to have_one(:kubernetes_namespace) } it { is_expected.to have_one(:cluster_project) } it { is_expected.to delegate_method(:status).to(:provider) } diff --git a/spec/models/clusters/platforms/kubernetes_spec.rb b/spec/models/clusters/platforms/kubernetes_spec.rb index e35d14f2282..c485850c16e 100644 --- a/spec/models/clusters/platforms/kubernetes_spec.rb +++ b/spec/models/clusters/platforms/kubernetes_spec.rb @@ -15,10 +15,8 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching it { is_expected.to validate_presence_of(:api_url) } it { is_expected.to validate_presence_of(:token) } - it { is_expected.to delegate_method(:project).to(:cluster) } it { is_expected.to delegate_method(:enabled?).to(:cluster) } it { is_expected.to delegate_method(:provided_by_user?).to(:cluster) } - it { is_expected.to delegate_method(:kubernetes_namespace).to(:cluster) } it_behaves_like 'having unique enum values' @@ -209,7 +207,7 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching it { is_expected.to be_truthy } end - describe '#actual_namespace' do + describe '#kubernetes_namespace_for' do let(:cluster) { create(:cluster, :project) } let(:project) { cluster.project } @@ -219,7 +217,7 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching namespace: namespace) end - subject { platform.actual_namespace } + subject { platform.kubernetes_namespace_for(project) } context 'with a namespace assigned' do let(:namespace) { 'namespace-123' } @@ -305,8 +303,6 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching end context 'no namespace provided' do - let(:namespace) { kubernetes.actual_namespace } - it_behaves_like 'setting variables' it 'sets KUBE_TOKEN' do @@ -389,7 +385,7 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching end context 'with valid pods' do - let(:pod) { kube_pod(environment_slug: environment.slug, project_slug: project.full_path_slug) } + let(:pod) { kube_pod(environment_slug: environment.slug, namespace: cluster.kubernetes_namespace_for(project), project_slug: project.full_path_slug) } let(:pod_with_no_terminal) { kube_pod(environment_slug: environment.slug, project_slug: project.full_path_slug, status: "Pending") } let(:terminals) { kube_terminals(service, pod) } @@ -419,6 +415,7 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching let!(:cluster) { create(:cluster, :project, enabled: enabled, platform_kubernetes: service) } let(:service) { create(:cluster_platform_kubernetes, :configured) } let(:enabled) { true } + let(:namespace) { cluster.kubernetes_namespace_for(cluster.project) } context 'when cluster is disabled' do let(:enabled) { false } @@ -428,8 +425,8 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching context 'when kubernetes responds with valid pods and deployments' do before do - stub_kubeclient_pods - stub_kubeclient_deployments + stub_kubeclient_pods(namespace) + stub_kubeclient_deployments(namespace) end it { is_expected.to include(pods: [kube_pod]) } @@ -437,8 +434,8 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching context 'when kubernetes responds with 500s' do before do - stub_kubeclient_pods(status: 500) - stub_kubeclient_deployments(status: 500) + stub_kubeclient_pods(namespace, status: 500) + stub_kubeclient_deployments(namespace, status: 500) end it { expect { subject }.to raise_error(Kubeclient::HttpError) } @@ -446,12 +443,18 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching context 'when kubernetes responds with 404s' do before do - stub_kubeclient_pods(status: 404) - stub_kubeclient_deployments(status: 404) + stub_kubeclient_pods(namespace, status: 404) + stub_kubeclient_deployments(namespace, status: 404) end it { is_expected.to include(pods: []) } end + + context 'when the cluster is not project level' do + let(:cluster) { create(:cluster, :group, platform_kubernetes: service) } + + it { is_expected.to include(pods: []) } + end end describe '#update_kubernetes_namespace' do diff --git a/spec/models/clusters/project_spec.rb b/spec/models/clusters/project_spec.rb index 2f017e69251..671af085d10 100644 --- a/spec/models/clusters/project_spec.rb +++ b/spec/models/clusters/project_spec.rb @@ -6,5 +6,4 @@ describe Clusters::Project do it { is_expected.to belong_to(:cluster) } it { is_expected.to belong_to(:project) } it { is_expected.to have_many(:kubernetes_namespaces) } - it { is_expected.to have_one(:kubernetes_namespace) } end diff --git a/spec/models/project_services/kubernetes_service_spec.rb b/spec/models/project_services/kubernetes_service_spec.rb index 3a381cb405d..2fce120381b 100644 --- a/spec/models/project_services/kubernetes_service_spec.rb +++ b/spec/models/project_services/kubernetes_service_spec.rb @@ -161,8 +161,8 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do end end - describe '#actual_namespace' do - subject { service.actual_namespace } + describe '#kubernetes_namespace_for' do + subject { service.kubernetes_namespace_for(project) } shared_examples 'a correctly formatted namespace' do it 'returns a valid Kubernetes namespace name' do @@ -298,7 +298,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do end context 'no namespace provided' do - let(:namespace) { subject.actual_namespace } + let(:namespace) { subject.kubernetes_namespace_for(project) } it_behaves_like 'setting variables' @@ -325,7 +325,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do end context 'with valid pods' do - let(:pod) { kube_pod(environment_slug: environment.slug, project_slug: project.full_path_slug) } + let(:pod) { kube_pod(environment_slug: environment.slug, namespace: service.kubernetes_namespace_for(project), project_slug: project.full_path_slug) } let(:pod_with_no_terminal) { kube_pod(environment_slug: environment.slug, project_slug: project.full_path_slug, status: "Pending") } let(:terminals) { kube_terminals(service, pod) } @@ -352,6 +352,8 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do describe '#calculate_reactive_cache' do subject { service.calculate_reactive_cache } + let(:namespace) { service.kubernetes_namespace_for(project) } + context 'when service is inactive' do before do service.active = false @@ -362,8 +364,8 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do context 'when kubernetes responds with valid pods' do before do - stub_kubeclient_pods - stub_kubeclient_deployments # Used by EE + stub_kubeclient_pods(namespace) + stub_kubeclient_deployments(namespace) # Used by EE end it { is_expected.to include(pods: [kube_pod]) } @@ -371,8 +373,8 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do context 'when kubernetes responds with 500s' do before do - stub_kubeclient_pods(status: 500) - stub_kubeclient_deployments(status: 500) # Used by EE + stub_kubeclient_pods(namespace, status: 500) + stub_kubeclient_deployments(namespace, status: 500) # Used by EE end it { expect { subject }.to raise_error(Kubeclient::HttpError) } @@ -380,8 +382,8 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do context 'when kubernetes responds with 404s' do before do - stub_kubeclient_pods(status: 404) - stub_kubeclient_deployments(status: 404) # Used by EE + stub_kubeclient_pods(namespace, status: 404) + stub_kubeclient_deployments(namespace, status: 404) # Used by EE end it { is_expected.to include(pods: []) } diff --git a/spec/requests/api/project_clusters_spec.rb b/spec/requests/api/project_clusters_spec.rb index 5357be3cdee..fc0381159dd 100644 --- a/spec/requests/api/project_clusters_spec.rb +++ b/spec/requests/api/project_clusters_spec.rb @@ -351,7 +351,7 @@ describe API::ProjectClusters do it 'does not update cluster attributes' do expect(cluster.domain).not_to eq('new_domain.com') expect(cluster.platform_kubernetes.namespace).not_to eq('invalid_namespace') - expect(cluster.kubernetes_namespace.namespace).not_to eq('invalid_namespace') + expect(cluster.kubernetes_namespace_for(project)).not_to eq('invalid_namespace') end it 'returns validation errors' do diff --git a/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb b/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb index 18f218fc236..be052a07da7 100644 --- a/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb +++ b/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb @@ -113,7 +113,7 @@ describe Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService, '#execute' d it 'does not create any Clusters::KubernetesNamespace' do subject - expect(cluster.kubernetes_namespace).to eq(kubernetes_namespace) + expect(cluster.kubernetes_namespaces).to eq([kubernetes_namespace]) end it 'creates project service account' do diff --git a/spec/support/helpers/kubernetes_helpers.rb b/spec/support/helpers/kubernetes_helpers.rb index ac52acb6570..78b7ae9c00c 100644 --- a/spec/support/helpers/kubernetes_helpers.rb +++ b/spec/support/helpers/kubernetes_helpers.rb @@ -24,30 +24,34 @@ module KubernetesHelpers WebMock.stub_request(:get, api_url + '/apis/serving.knative.dev/v1alpha1').to_return(kube_response(kube_v1alpha1_serving_knative_discovery_body)) end - def stub_kubeclient_service_pods(response = nil) + def stub_kubeclient_service_pods(status: nil) stub_kubeclient_discover(service.api_url) pods_url = service.api_url + "/api/v1/pods" + response = { status: status } if status WebMock.stub_request(:get, pods_url).to_return(response || kube_pods_response) end - def stub_kubeclient_pods(response = nil) + def stub_kubeclient_pods(namespace, status: nil) stub_kubeclient_discover(service.api_url) - pods_url = service.api_url + "/api/v1/namespaces/#{service.actual_namespace}/pods" + pods_url = service.api_url + "/api/v1/namespaces/#{namespace}/pods" + response = { status: status } if status WebMock.stub_request(:get, pods_url).to_return(response || kube_pods_response) end - def stub_kubeclient_logs(pod_name, response = nil) + def stub_kubeclient_logs(pod_name, namespace, status: nil) stub_kubeclient_discover(service.api_url) - logs_url = service.api_url + "/api/v1/namespaces/#{service.actual_namespace}/pods/#{pod_name}/log?tailLines=#{Clusters::Platforms::Kubernetes::LOGS_LIMIT}" + logs_url = service.api_url + "/api/v1/namespaces/#{namespace}/pods/#{pod_name}/log?tailLines=#{Clusters::Platforms::Kubernetes::LOGS_LIMIT}" + response = { status: status } if status WebMock.stub_request(:get, logs_url).to_return(response || kube_logs_response) end - def stub_kubeclient_deployments(response = nil) + def stub_kubeclient_deployments(namespace, status: nil) stub_kubeclient_discover(service.api_url) - deployments_url = service.api_url + "/apis/extensions/v1beta1/namespaces/#{service.actual_namespace}/deployments" + deployments_url = service.api_url + "/apis/extensions/v1beta1/namespaces/#{namespace}/deployments" + response = { status: status } if status WebMock.stub_request(:get, deployments_url).to_return(response || kube_deployments_response) end @@ -250,10 +254,11 @@ module KubernetesHelpers # This is a partial response, it will have many more elements in reality but # these are the ones we care about at the moment - def kube_pod(name: "kube-pod", environment_slug: "production", project_slug: "project-path-slug", status: "Running", track: nil) + def kube_pod(name: "kube-pod", environment_slug: "production", namespace: "project-namespace", project_slug: "project-path-slug", status: "Running", track: nil) { "metadata" => { "name" => name, + "namespace" => namespace, "generate_name" => "generated-name-with-suffix", "creationTimestamp" => "2016-11-25T19:55:19Z", "annotations" => { @@ -369,12 +374,13 @@ module KubernetesHelpers def kube_terminals(service, pod) pod_name = pod['metadata']['name'] + pod_namespace = pod['metadata']['namespace'] containers = pod['spec']['containers'] containers.map do |container| terminal = { selectors: { pod: pod_name, container: container['name'] }, - url: container_exec_url(service.api_url, service.actual_namespace, pod_name, container['name']), + url: container_exec_url(service.api_url, pod_namespace, pod_name, container['name']), subprotocols: ['channel.k8s.io'], headers: { 'Authorization' => ["Bearer #{service.token}"] }, created_at: DateTime.parse(pod['metadata']['creationTimestamp']), diff --git a/spec/support/prometheus/additional_metrics_shared_examples.rb b/spec/support/prometheus/additional_metrics_shared_examples.rb index 0fd67531c3b..8044b061ca5 100644 --- a/spec/support/prometheus/additional_metrics_shared_examples.rb +++ b/spec/support/prometheus/additional_metrics_shared_examples.rb @@ -46,7 +46,7 @@ RSpec.shared_examples 'additional metrics query' do describe 'project has Kubernetes service' do shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do let(:environment) { create(:environment, slug: 'environment-slug', project: project) } - let(:kube_namespace) { project.deployment_platform.actual_namespace } + let(:kube_namespace) { project.deployment_platform.kubernetes_namespace_for(project) } it_behaves_like 'query context containing environment slug and filter' |