From 31c256c154e7a8727de9e91b1353b9740f380dd8 Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Thu, 2 Nov 2017 16:57:18 +0100 Subject: [PATCH] General cleanup --- app/models/clusters/applications/helm.rb | 2 +- app/services/clusters/base_helm_service.rb | 2 +- app/services/clusters/install_app_service.rb | 1 - .../clusters/install_tiller_service.rb | 24 ---------------- ...10_create_clusters_kubernetes_helm_apps.rb | 4 +++ lib/gitlab/{clusters => kubernetes}/helm.rb | 28 +++++++++++-------- 6 files changed, 22 insertions(+), 39 deletions(-) delete mode 100644 app/services/clusters/install_tiller_service.rb rename lib/gitlab/{clusters => kubernetes}/helm.rb (84%) diff --git a/app/models/clusters/applications/helm.rb b/app/models/clusters/applications/helm.rb index a18a3f87bc4..c35db143205 100644 --- a/app/models/clusters/applications/helm.rb +++ b/app/models/clusters/applications/helm.rb @@ -9,7 +9,7 @@ module Clusters belongs_to :cluster, class_name: 'Clusters::Cluster', foreign_key: :cluster_id - default_value_for :version, Gitlab::Clusters::Helm::HELM_VERSION + default_value_for :version, Gitlab::Kubernetes::Helm::HELM_VERSION def name NAME diff --git a/app/services/clusters/base_helm_service.rb b/app/services/clusters/base_helm_service.rb index c7f7e2d0877..dd717a0bb58 100644 --- a/app/services/clusters/base_helm_service.rb +++ b/app/services/clusters/base_helm_service.rb @@ -17,7 +17,7 @@ module Clusters end def helm_api - @helm ||= Gitlab::Clusters::Helm.new(kubeclient) + @helm ||= Gitlab::Kubernetes::Helm.new(kubeclient) end end end diff --git a/app/services/clusters/install_app_service.rb b/app/services/clusters/install_app_service.rb index 496af2495fd..a72cfa0a17f 100644 --- a/app/services/clusters/install_app_service.rb +++ b/app/services/clusters/install_app_service.rb @@ -15,7 +15,6 @@ module Clusters rescue KubeException => ke app.make_errored!("Kubernetes error: #{ke.message}") rescue StandardError => e - Rails.logger.warn(e.message) app.make_errored!("Can't start installation process") end end diff --git a/app/services/clusters/install_tiller_service.rb b/app/services/clusters/install_tiller_service.rb deleted file mode 100644 index ac77a7ea3c2..00000000000 --- a/app/services/clusters/install_tiller_service.rb +++ /dev/null @@ -1,24 +0,0 @@ -module Clusters - class InstallTillerService < BaseService - def execute - ensure_namespace - install - end - - private - - def kubernetes_service - return @kubernetes_service if defined?(@kubernetes_service) - - @kubernetes_service = project&.kubernetes_service - end - - def ensure_namespace - kubernetes_service&.ensure_namespace! - end - - def install - kubernetes_service&.helm_client&.init! - end - end -end diff --git a/db/migrate/20171031100710_create_clusters_kubernetes_helm_apps.rb b/db/migrate/20171031100710_create_clusters_kubernetes_helm_apps.rb index 1035adfe2b5..ef2e7e07a41 100644 --- a/db/migrate/20171031100710_create_clusters_kubernetes_helm_apps.rb +++ b/db/migrate/20171031100710_create_clusters_kubernetes_helm_apps.rb @@ -1,4 +1,8 @@ class CreateClustersKubernetesHelmApps < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + def change create_table :clusters_applications_helm do |t| t.references :cluster, null: false, unique: true, foreign_key: { on_delete: :cascade } diff --git a/lib/gitlab/clusters/helm.rb b/lib/gitlab/kubernetes/helm.rb similarity index 84% rename from lib/gitlab/clusters/helm.rb rename to lib/gitlab/kubernetes/helm.rb index 9c75fe2be96..76bb14a0609 100644 --- a/lib/gitlab/clusters/helm.rb +++ b/lib/gitlab/kubernetes/helm.rb @@ -1,7 +1,6 @@ module Gitlab - module Clusters + module Kubernetes class Helm - Error = Class.new(StandardError) HELM_VERSION = '2.7.0'.freeze NAMESPACE = 'gitlab-managed-apps'.freeze COMMAND_SCRIPT = <<-EOS.freeze @@ -18,12 +17,11 @@ module Gitlab end def init! - ensure_namespace! - @kubeclient.create_pod(pod_resource(OpenStruct.new(name: 'helm'))) + install(OpenStruct.new(name: 'helm')) end def install(app) - ensure_namespace! + create_namespace! unless has_namespace? @kubeclient.create_pod(pod_resource(app)) end @@ -86,19 +84,25 @@ module Gitlab "install #{app.chart} --name #{app.name} --namespace #{NAMESPACE}" end - def ensure_namespace! + def has_namespace? + return @has_namespace if defined?(@has_namespace) + begin @kubeclient.get_namespace(NAMESPACE) + @has_namespace = true rescue KubeException => ke raise ke unless ke.error_code == 404 - - namespace_resource = ::Kubeclient::Resource.new - namespace_resource.metadata = {} - namespace_resource.metadata.name = NAMESPACE - - @kubeclient.create_namespace(namespace_resource) + false end end + + def create_namespace! + namespace_resource = ::Kubeclient::Resource.new + namespace_resource.metadata = {} + namespace_resource.metadata.name = NAMESPACE + + @kubeclient.create_namespace(namespace_resource) + end end end end