diff options
author | Thong Kuah <tkuah@gitlab.com> | 2018-12-05 01:57:02 +0300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2018-12-05 01:57:02 +0300 |
commit | e3188eb13e3145e9bd4b123c304e43b18eeb1154 (patch) | |
tree | c7c5b90152770b348a7df072849c54bee914dbaa | |
parent | ba2d8a3f3483af053eea47f84c158509a91f7012 (diff) |
Shift to class methods for RefreshService
As we don't use any instance attributes and we don't seem to have any
commonalities between the cluster and the project variant.
-rw-r--r-- | app/services/clusters/refresh_service.rb | 30 | ||||
-rw-r--r-- | app/workers/cluster_platform_configure_worker.rb | 2 | ||||
-rw-r--r-- | app/workers/cluster_project_configure_worker.rb | 2 | ||||
-rw-r--r-- | spec/services/clusters/refresh_service_spec.rb | 8 |
4 files changed, 24 insertions, 18 deletions
diff --git a/app/services/clusters/refresh_service.rb b/app/services/clusters/refresh_service.rb index c203f495b7c..7c82b98a33f 100644 --- a/app/services/clusters/refresh_service.rb +++ b/app/services/clusters/refresh_service.rb @@ -2,27 +2,31 @@ module Clusters class RefreshService - def create_or_update_namespaces_for_cluster(cluster) - cluster_namespaces = cluster.kubernetes_namespaces - - # Create all namespaces that are missing for each project - cluster.all_projects.missing_kubernetes_namespace(cluster_namespaces).each do |project| + def self.create_or_update_namespaces_for_cluster(cluster) + projects_with_missing_kubernetes_namespaces_for_cluster(cluster).each do |project| create_or_update_namespace(cluster, project) end end - def create_or_update_namespaces_for_project(project) - project_namespaces = project.kubernetes_namespaces - - # Create all namespaces that are missing for each cluster - project.all_clusters.missing_kubernetes_namespace(project_namespaces).each do |cluster| + def self.create_or_update_namespaces_for_project(project) + clusters_with_missing_kubernetes_namespaces_for_project(project).each do |cluster| create_or_update_namespace(cluster, project) end end - private + def self.projects_with_missing_kubernetes_namespaces_for_cluster(cluster) + cluster.all_projects.missing_kubernetes_namespace(cluster.kubernetes_namespaces) + end + + private_class_method :projects_with_missing_kubernetes_namespaces_for_cluster - def create_or_update_namespace(cluster, project) + def self.clusters_with_missing_kubernetes_namespaces_for_project(project) + project.all_clusters.missing_kubernetes_namespace(project.kubernetes_namespaces) + end + + private_class_method :clusters_with_missing_kubernetes_namespaces_for_project + + def self.create_or_update_namespace(cluster, project) kubernetes_namespace = cluster.find_or_initialize_kubernetes_namespace_for_project(project) ::Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService.new( @@ -30,5 +34,7 @@ module Clusters kubernetes_namespace: kubernetes_namespace ).execute end + + private_class_method :create_or_update_namespace end end diff --git a/app/workers/cluster_platform_configure_worker.rb b/app/workers/cluster_platform_configure_worker.rb index 7f14f8efa2f..aa7570caa79 100644 --- a/app/workers/cluster_platform_configure_worker.rb +++ b/app/workers/cluster_platform_configure_worker.rb @@ -6,7 +6,7 @@ class ClusterPlatformConfigureWorker def perform(cluster_id) Clusters::Cluster.find_by_id(cluster_id).try do |cluster| - Clusters::RefreshService.new.create_or_update_namespaces_for_cluster(cluster) + Clusters::RefreshService.create_or_update_namespaces_for_cluster(cluster) end end end diff --git a/app/workers/cluster_project_configure_worker.rb b/app/workers/cluster_project_configure_worker.rb index 1271b26e945..497e57c0d0b 100644 --- a/app/workers/cluster_project_configure_worker.rb +++ b/app/workers/cluster_project_configure_worker.rb @@ -7,6 +7,6 @@ class ClusterProjectConfigureWorker def perform(project_id) project = Project.find(project_id) - ::Clusters::RefreshService.new.create_or_update_namespaces_for_project(project) + ::Clusters::RefreshService.create_or_update_namespaces_for_project(project) end end diff --git a/spec/services/clusters/refresh_service_spec.rb b/spec/services/clusters/refresh_service_spec.rb index 470639524b8..58ab3c3cf73 100644 --- a/spec/services/clusters/refresh_service_spec.rb +++ b/spec/services/clusters/refresh_service_spec.rb @@ -29,11 +29,11 @@ describe Clusters::RefreshService do end end - describe '#create_or_update_namespaces_for_cluster' do + describe '.create_or_update_namespaces_for_cluster' do let(:cluster) { create(:cluster, :provided_by_user, :project) } let(:project) { cluster.project } - subject { described_class.new.create_or_update_namespaces_for_cluster(cluster) } + subject { described_class.create_or_update_namespaces_for_cluster(cluster) } context 'cluster is project level' do include_examples 'creates a kubernetes namespace' @@ -64,10 +64,10 @@ describe Clusters::RefreshService do end end - describe '#create_or_update_namespaces_for_project' do + describe '.create_or_update_namespaces_for_project' do let(:project) { create(:project) } - subject { described_class.new.create_or_update_namespaces_for_project(project) } + subject { described_class.create_or_update_namespaces_for_project(project) } it 'creates no kubernetes namespaces' do expect { subject }.not_to change(project.kubernetes_namespaces, :count) |