diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2018-05-29 12:02:09 +0200 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2018-05-29 12:02:25 +0200 |
commit | dc0861e70986e4c82bb8296614455eb276872cd4 (patch) | |
tree | 23033d1ad9fba6ba5160e88f191bd820cabff25f | |
parent | 65bfec654a1ff2894e7829573d266f8d63c846f2 (diff) | |
download | gitlab-ce-remove-kubernetes-integration-page.tar.gz |
Remove Kubernetes Integration pageremove-kubernetes-integration-page
-rw-r--r-- | app/controllers/projects/clusters_controller.rb | 5 | ||||
-rw-r--r-- | app/models/concerns/deployment_platform.rb | 46 | ||||
-rw-r--r-- | app/models/project.rb | 2 | ||||
-rw-r--r-- | app/models/project_services/kubernetes_service.rb | 160 |
4 files changed, 41 insertions, 172 deletions
diff --git a/app/controllers/projects/clusters_controller.rb b/app/controllers/projects/clusters_controller.rb index aeaba3a0acf..dab23d99801 100644 --- a/app/controllers/projects/clusters_controller.rb +++ b/app/controllers/projects/clusters_controller.rb @@ -1,5 +1,6 @@ class Projects::ClustersController < Projects::ApplicationController before_action :cluster, except: [:index, :new] + before_action :ensure_kubernetes_cluster_template, only: [:index] before_action :authorize_read_cluster! before_action :authorize_create_cluster!, only: [:new] before_action :authorize_update_cluster!, only: [:update] @@ -71,6 +72,10 @@ class Projects::ClustersController < Projects::ApplicationController .present(current_user: current_user) end + def ensure_kubernetes_cluster_template + project.ensure_kubernetes_cluster_template + end + def create_params params.require(:cluster).permit( :enabled, diff --git a/app/models/concerns/deployment_platform.rb b/app/models/concerns/deployment_platform.rb index 52851b3d0b2..5e733629b19 100644 --- a/app/models/concerns/deployment_platform.rb +++ b/app/models/concerns/deployment_platform.rb @@ -2,17 +2,27 @@ module DeploymentPlatform # EE would override this and utilize environment argument # rubocop:disable Gitlab/ModuleWithInstanceVariables def deployment_platform(environment: nil) + ensure_kubernetes_cluster_template + @deployment_platform ||= {} @deployment_platform[environment] ||= find_deployment_platform(environment) end + # We create KubernetesService object to indicate that it was imported from active template + # Saved KubernetesService object ensures that corresponding Cluster is created + # KubernetesService itself is shallow holder of parameters only + def ensure_kubernetes_cluster_template + return if kubernetes_service + return unless kubernetes_service_template + + Service.build_from_template(id, kubernetes_service_template).save! + end + private def find_deployment_platform(environment) - find_cluster_platform_kubernetes(environment: environment) || - find_kubernetes_service_integration || - build_cluster_and_deployment_platform + find_cluster_platform_kubernetes(environment: environment) end # EE would override this and utilize environment argument @@ -21,37 +31,7 @@ module DeploymentPlatform .last&.platform_kubernetes end - def find_kubernetes_service_integration - services.deployment.reorder(nil).find_by(active: true) - end - - def build_cluster_and_deployment_platform - return unless kubernetes_service_template - - cluster = ::Clusters::Cluster.create(cluster_attributes_from_service_template) - cluster.platform_kubernetes if cluster.persisted? - end - def kubernetes_service_template @kubernetes_service_template ||= KubernetesService.active.find_by_template end - - def cluster_attributes_from_service_template - { - name: 'kubernetes-template', - projects: [self], - provider_type: :user, - platform_type: :kubernetes, - platform_kubernetes_attributes: platform_kubernetes_attributes_from_service_template - } - end - - def platform_kubernetes_attributes_from_service_template - { - api_url: kubernetes_service_template.api_url, - ca_pem: kubernetes_service_template.ca_pem, - token: kubernetes_service_template.token, - namespace: kubernetes_service_template.namespace - } - end end diff --git a/app/models/project.rb b/app/models/project.rb index 0fe9f8880b4..1b038241897 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1028,7 +1028,7 @@ class Project < ActiveRecord::Base end def disabled_services - [] + ["kubernetes"] end def find_or_initialize_service(name) diff --git a/app/models/project_services/kubernetes_service.rb b/app/models/project_services/kubernetes_service.rb index 20fed432e55..3a3c33f5d36 100644 --- a/app/models/project_services/kubernetes_service.rb +++ b/app/models/project_services/kubernetes_service.rb @@ -1,14 +1,8 @@ ## -# NOTE: -# We'll move this class to Clusters::Platforms::Kubernetes, which contains exactly the same logic. -# After we've migrated data, we'll remove KubernetesService. This would happen in a few months. -# If you're modyfiyng this class, please note that you should update the same change in Clusters::Platforms::Kubernetes. +# KubernetesService is shallow parameters holder +# On first create in context of project it creates Cluster::Cluster +# with all parameters class KubernetesService < DeploymentService - include Gitlab::Kubernetes - include ReactiveCaching - - 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 # is an invalid or inappropriate name prop_accessor :namespace @@ -30,7 +24,6 @@ class KubernetesService < DeploymentService before_validation :enforce_namespace_to_lower_case - validate :deprecation_validation, unless: :template? validates :namespace, allow_blank: true, length: 1..63, @@ -40,7 +33,7 @@ class KubernetesService < DeploymentService message: Gitlab::Regex.kubernetes_namespace_regex_message } - after_save :clear_reactive_cache! + after_create :create_project_kubernetes_cluster, unless: :template? def initialize_properties self.properties = {} if properties.nil? @@ -102,71 +95,33 @@ class KubernetesService < DeploymentService { success: false, result: err } end - def predefined_variables - config = YAML.dump(kubeconfig) - - Gitlab::Ci::Variables::Collection.new.tap do |variables| - variables - .append(key: 'KUBE_URL', value: api_url) - .append(key: 'KUBE_TOKEN', value: token, public: false) - .append(key: 'KUBE_NAMESPACE', value: actual_namespace) - .append(key: 'KUBECONFIG', value: config, public: false, file: true) - - if ca_pem.present? - variables - .append(key: 'KUBE_CA_PEM', value: ca_pem) - .append(key: 'KUBE_CA_PEM_FILE', value: ca_pem, file: true) - end - end - end - - # Constructs a list of terminals from the reactive cache - # - # Returns nil if the cache is empty, in which case you should try again a - # short time later - def terminals(environment) - with_reactive_cache do |data| - pods = filter_by_label(data[:pods], app: environment.slug) - terminals = pods.flat_map { |pod| terminals_for_pod(api_url, actual_namespace, pod) } - terminals.each { |terminal| add_terminal_auth(terminal, terminal_auth) } - end - end - - # Caches resources in the namespace so other calls don't need to block on - # network access - def calculate_reactive_cache - return unless active? && project && !project.pending_delete? + TEMPLATE_PLACEHOLDER = 'Kubernetes namespace'.freeze - # We may want to cache extra things in the future - { pods: read_pods } - end + private - def kubeclient - @kubeclient ||= build_kubeclient! - end + def create_project_kubernetes_cluster + return unless active? - def deprecated? - !active + ::Clusters::Cluster.create(cluster_attributes_from_service_template) end - def deprecation_message - content = _("Kubernetes service integration has been deprecated. %{deprecated_message_content} your Kubernetes clusters using the new <a href=\"%{url}\"/>Kubernetes Clusters</a> page") % { - deprecated_message_content: deprecated_message_content, - url: Gitlab::Routing.url_helpers.project_clusters_path(project) + def cluster_attributes_from_service_template + { + name: 'kubernetes-template', + projects: [project], + provider_type: :user, + platform_type: :kubernetes, + platform_kubernetes_attributes: platform_kubernetes_attributes_from_service_template } - content.html_safe end - TEMPLATE_PLACEHOLDER = 'Kubernetes namespace'.freeze - - private - - def kubeconfig - to_kubeconfig( - url: api_url, - namespace: actual_namespace, + def platform_kubernetes_attributes_from_service_template + { + api_url: api_url, + ca_pem: ca_pem, token: token, - ca_pem: ca_pem) + namespace: namespace + } end def namespace_placeholder @@ -180,78 +135,7 @@ class KubernetesService < DeploymentService slug.gsub(/[^-a-z0-9]/, '-').gsub(/^-+/, '') end - def build_kubeclient!(api_path: 'api', api_version: 'v1') - raise "Incomplete settings" unless api_url && actual_namespace && token - - ::Kubeclient::Client.new( - join_api_url(api_path), - api_version, - auth_options: kubeclient_auth_options, - ssl_options: kubeclient_ssl_options, - http_proxy_uri: ENV['http_proxy'] - ) - end - - # Returns a hash of all pods in the namespace - def read_pods - kubeclient = build_kubeclient! - - kubeclient.get_pods(namespace: actual_namespace).as_json - rescue Kubeclient::HttpError => err - raise err unless err.error_code == 404 - - [] - end - - def kubeclient_ssl_options - opts = { verify_ssl: OpenSSL::SSL::VERIFY_PEER } - - if ca_pem.present? - opts[:cert_store] = OpenSSL::X509::Store.new - opts[:cert_store].add_cert(OpenSSL::X509::Certificate.new(ca_pem)) - end - - opts - end - - def kubeclient_auth_options - { bearer_token: token } - end - - def join_api_url(api_path) - url = URI.parse(api_url) - prefix = url.path.sub(%r{/+\z}, '') - - url.path = [prefix, api_path].join("/") - - url.to_s - end - - def terminal_auth - { - token: token, - ca_pem: ca_pem, - max_session_time: Gitlab::CurrentSettings.terminal_max_session_time - } - end - def enforce_namespace_to_lower_case self.namespace = self.namespace&.downcase end - - def deprecation_validation - return if active_changed?(from: true, to: false) - - if deprecated? - errors[:base] << deprecation_message - end - end - - def deprecated_message_content - if active? - _("Your Kubernetes cluster information on this page is still editable, but you are advised to disable and reconfigure") - else - _("Fields on this page are now uneditable, you can configure") - end - end end |