2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'openssl'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Clusters
|
|
|
|
module Applications
|
2019-07-07 11:18:12 +05:30
|
|
|
class Helm < ApplicationRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
self.table_name = 'clusters_applications_helm'
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
attr_encrypted :ca_key,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
key: Settings.attr_encrypted_db_key_base_truncated,
|
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
include ::Clusters::Concerns::ApplicationCore
|
|
|
|
include ::Clusters::Concerns::ApplicationStatus
|
2019-10-12 21:52:04 +05:30
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
default_value_for :version, Gitlab::Kubernetes::Helm::HELM_VERSION
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
before_create :create_keys_and_certs
|
|
|
|
|
|
|
|
def issue_client_cert
|
|
|
|
ca_cert_obj.issue
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def set_initial_status
|
|
|
|
return unless not_installable?
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
self.status = status_states[:installable] if cluster&.platform_kubernetes_active?
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
# It can only be uninstalled if there are no other applications installed
|
|
|
|
# or with intermitent installation statuses in the database.
|
2019-07-31 22:56:46 +05:30
|
|
|
def allowed_to_uninstall?
|
2019-10-12 21:52:04 +05:30
|
|
|
strong_memoize(:allowed_to_uninstall) do
|
|
|
|
applications = nil
|
|
|
|
|
|
|
|
Clusters::Cluster::APPLICATIONS.each do |application_name, klass|
|
|
|
|
next if application_name == 'helm'
|
|
|
|
|
|
|
|
extra_apps = Clusters::Applications::Helm.where('EXISTS (?)', klass.select(1).where(cluster_id: cluster_id))
|
|
|
|
|
|
|
|
applications = applications ? applications.or(extra_apps) : extra_apps
|
|
|
|
end
|
|
|
|
|
|
|
|
!applications.exists?
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def install_command
|
2018-11-18 11:00:15 +05:30
|
|
|
Gitlab::Kubernetes::Helm::InitCommand.new(
|
|
|
|
name: name,
|
2018-11-20 20:47:30 +05:30
|
|
|
files: files,
|
2020-06-23 00:09:42 +05:30
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
|
|
|
local_tiller_enabled: cluster.local_tiller_enabled?
|
2018-11-18 11:00:15 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def uninstall_command
|
|
|
|
Gitlab::Kubernetes::Helm::ResetCommand.new(
|
|
|
|
name: name,
|
|
|
|
files: files,
|
2020-06-23 00:09:42 +05:30
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
|
|
|
local_tiller_enabled: cluster.local_tiller_enabled?
|
2019-10-12 21:52:04 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def has_ssl?
|
|
|
|
ca_key.present? && ca_cert.present?
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def post_uninstall
|
|
|
|
cluster.kubeclient.delete_namespace(Gitlab::Kubernetes::Helm::NAMESPACE)
|
|
|
|
rescue Kubeclient::ResourceNotFoundError
|
|
|
|
# we actually don't care if the namespace is not present
|
|
|
|
# since we want to delete it anyway.
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def files
|
|
|
|
{
|
|
|
|
'ca.pem': ca_cert,
|
|
|
|
'cert.pem': tiller_cert.cert_string,
|
|
|
|
'key.pem': tiller_cert.key_string
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_keys_and_certs
|
|
|
|
ca_cert = Gitlab::Kubernetes::Helm::Certificate.generate_root
|
|
|
|
self.ca_key = ca_cert.key_string
|
|
|
|
self.ca_cert = ca_cert.cert_string
|
|
|
|
end
|
|
|
|
|
|
|
|
def tiller_cert
|
|
|
|
@tiller_cert ||= ca_cert_obj.issue(expires_in: Gitlab::Kubernetes::Helm::Certificate::INFINITE_EXPIRY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ca_cert_obj
|
|
|
|
return unless has_ssl?
|
|
|
|
|
|
|
|
Gitlab::Kubernetes::Helm::Certificate
|
|
|
|
.from_strings(ca_key, ca_cert)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|