2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-07 08:39:38 -04:00
|
|
|
require 'openssl'
|
|
|
|
|
2017-11-02 06:14:10 -04:00
|
|
|
module Clusters
|
2017-11-02 10:10:46 -04:00
|
|
|
module Applications
|
2019-03-28 09:17:42 -04:00
|
|
|
class Helm < ApplicationRecord
|
2017-11-02 10:10:46 -04:00
|
|
|
self.table_name = 'clusters_applications_helm'
|
|
|
|
|
2018-08-07 08:39:38 -04:00
|
|
|
attr_encrypted :ca_key,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
key: Settings.attr_encrypted_db_key_base_truncated,
|
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
2017-12-22 12:23:43 -05:00
|
|
|
include ::Clusters::Concerns::ApplicationCore
|
2017-11-02 12:57:50 -04:00
|
|
|
include ::Clusters::Concerns::ApplicationStatus
|
2019-08-02 15:02:57 -04:00
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
2017-11-02 06:14:10 -04:00
|
|
|
|
2017-11-02 11:57:18 -04:00
|
|
|
default_value_for :version, Gitlab::Kubernetes::Helm::HELM_VERSION
|
2017-11-02 06:14:10 -04:00
|
|
|
|
2018-08-07 08:39:38 -04:00
|
|
|
before_create :create_keys_and_certs
|
|
|
|
|
|
|
|
def issue_client_cert
|
|
|
|
ca_cert_obj.issue
|
|
|
|
end
|
|
|
|
|
2017-11-07 08:26:14 -05:00
|
|
|
def set_initial_status
|
2017-11-07 08:52:11 -05:00
|
|
|
return unless not_installable?
|
|
|
|
|
2019-09-22 20:06:29 -04:00
|
|
|
self.status = status_states[:installable] if cluster&.platform_kubernetes_active?
|
2017-11-06 09:48:44 -05:00
|
|
|
end
|
|
|
|
|
2019-08-02 15:02:57 -04:00
|
|
|
# It can only be uninstalled if there are no other applications installed
|
|
|
|
# or with intermitent installation statuses in the database.
|
2019-04-12 01:42:48 -04:00
|
|
|
def allowed_to_uninstall?
|
2019-08-02 15:02:57 -04:00
|
|
|
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))
|
|
|
|
|
2019-08-13 01:18:59 -04:00
|
|
|
applications = applications ? applications.or(extra_apps) : extra_apps
|
2019-08-02 15:02:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
!applications.exists?
|
|
|
|
end
|
2019-04-12 01:42:48 -04:00
|
|
|
end
|
|
|
|
|
2017-11-07 09:26:14 -05:00
|
|
|
def install_command
|
2018-08-07 08:39:38 -04:00
|
|
|
Gitlab::Kubernetes::Helm::InitCommand.new(
|
|
|
|
name: name,
|
2018-09-06 06:03:38 -04:00
|
|
|
files: files,
|
2020-08-09 23:09:44 -04:00
|
|
|
rbac: cluster.platform_kubernetes_rbac?
|
2018-08-07 08:39:38 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-08-02 15:02:57 -04:00
|
|
|
def uninstall_command
|
|
|
|
Gitlab::Kubernetes::Helm::ResetCommand.new(
|
|
|
|
name: name,
|
|
|
|
files: files,
|
2020-08-09 23:09:44 -04:00
|
|
|
rbac: cluster.platform_kubernetes_rbac?
|
2019-08-02 15:02:57 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-08-07 08:39:38 -04:00
|
|
|
def has_ssl?
|
|
|
|
ca_key.present? && ca_cert.present?
|
|
|
|
end
|
|
|
|
|
2019-09-23 20:06:02 -04:00
|
|
|
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-08-07 08:39:38 -04:00
|
|
|
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-07-25 11:36:58 -04:00
|
|
|
end
|
2017-11-02 06:14:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|