2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
module Applications
|
|
|
|
class ElasticStack < ApplicationRecord
|
2020-05-24 23:13:21 +05:30
|
|
|
VERSION = '3.0.0'
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
ELASTICSEARCH_PORT = 9200
|
|
|
|
|
|
|
|
self.table_name = 'clusters_applications_elastic_stacks'
|
|
|
|
|
|
|
|
include ::Clusters::Concerns::ApplicationCore
|
|
|
|
include ::Clusters::Concerns::ApplicationStatus
|
|
|
|
include ::Clusters::Concerns::ApplicationVersion
|
|
|
|
include ::Clusters::Concerns::ApplicationData
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
default_value_for :version, VERSION
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
def chart
|
2020-05-24 23:13:21 +05:30
|
|
|
'elastic-stack/elastic-stack'
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
'https://charts.gitlab.io'
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def install_command
|
|
|
|
Gitlab::Kubernetes::Helm::InstallCommand.new(
|
|
|
|
name: 'elastic-stack',
|
|
|
|
version: VERSION,
|
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
|
|
|
chart: chart,
|
2020-05-24 23:13:21 +05:30
|
|
|
repository: repository,
|
2020-03-13 15:44:24 +05:30
|
|
|
files: files,
|
2020-05-24 23:13:21 +05:30
|
|
|
preinstall: migrate_to_3_script,
|
2020-06-23 00:09:42 +05:30
|
|
|
postinstall: post_install_script,
|
|
|
|
local_tiller_enabled: cluster.local_tiller_enabled?
|
2019-12-26 22:10:19 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def uninstall_command
|
|
|
|
Gitlab::Kubernetes::Helm::DeleteCommand.new(
|
|
|
|
name: 'elastic-stack',
|
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
|
|
|
files: files,
|
2020-06-23 00:09:42 +05:30
|
|
|
postdelete: post_delete_script,
|
|
|
|
local_tiller_enabled: cluster.local_tiller_enabled?
|
2019-12-26 22:10:19 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def files
|
|
|
|
super.merge('wait-for-elasticsearch.sh': File.read("#{Rails.root}/vendor/elastic_stack/wait-for-elasticsearch.sh"))
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def elasticsearch_client(timeout: nil)
|
2019-12-26 22:10:19 +05:30
|
|
|
strong_memoize(:elasticsearch_client) do
|
|
|
|
next unless kube_client
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
proxy_url = kube_client.proxy_url('service', service_name, ::Clusters::Applications::ElasticStack::ELASTICSEARCH_PORT, Gitlab::Kubernetes::Helm::NAMESPACE)
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
Elasticsearch::Client.new(url: proxy_url) do |faraday|
|
|
|
|
# ensures headers containing auth data are appended to original client options
|
|
|
|
faraday.headers.merge!(kube_client.headers)
|
|
|
|
# ensure TLS certs are properly verified
|
|
|
|
faraday.ssl[:verify] = kube_client.ssl_options[:verify_ssl]
|
|
|
|
faraday.ssl[:cert_store] = kube_client.ssl_options[:cert_store]
|
2020-06-23 00:09:42 +05:30
|
|
|
faraday.options.timeout = timeout unless timeout.nil?
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
rescue Kubeclient::HttpError => error
|
|
|
|
# If users have mistakenly set parameters or removed the depended clusters,
|
|
|
|
# `proxy_url` could raise an exception because gitlab can not communicate with the cluster.
|
|
|
|
# We check for a nil client in downstream use and behaviour is equivalent to an empty state
|
|
|
|
log_exception(error, :failed_to_create_elasticsearch_client)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
nil
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def chart_above_v2?
|
|
|
|
Gem::Version.new(version) >= Gem::Version.new('2.0.0')
|
|
|
|
end
|
|
|
|
|
|
|
|
def chart_above_v3?
|
|
|
|
Gem::Version.new(version) >= Gem::Version.new('3.0.0')
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
private
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def service_name
|
|
|
|
chart_above_v3? ? 'elastic-stack-elasticsearch-master' : 'elastic-stack-elasticsearch-client'
|
|
|
|
end
|
|
|
|
|
|
|
|
def pvc_selector
|
|
|
|
chart_above_v3? ? "app=elastic-stack-elasticsearch-master" : "release=elastic-stack"
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def post_install_script
|
|
|
|
[
|
2020-05-24 23:13:21 +05:30
|
|
|
"timeout -t60 sh /data/helm/elastic-stack/config/wait-for-elasticsearch.sh http://elastic-stack-elasticsearch-master:9200"
|
2020-03-13 15:44:24 +05:30
|
|
|
]
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def post_delete_script
|
|
|
|
[
|
2020-05-24 23:13:21 +05:30
|
|
|
Gitlab::Kubernetes::KubectlCmd.delete("pvc", "--selector", pvc_selector, "--namespace", Gitlab::Kubernetes::Helm::NAMESPACE)
|
2020-03-13 15:44:24 +05:30
|
|
|
]
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def kube_client
|
|
|
|
cluster&.kubeclient&.core_client
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
def migrate_to_3_script
|
|
|
|
return [] if !updating? || chart_above_v3?
|
|
|
|
|
|
|
|
# Chart version 3.0.0 moves to our own chart at https://gitlab.com/gitlab-org/charts/elastic-stack
|
|
|
|
# and is not compatible with pre-existing resources. We first remove them.
|
|
|
|
[
|
|
|
|
Gitlab::Kubernetes::Helm::DeleteCommand.new(
|
|
|
|
name: 'elastic-stack',
|
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
2020-06-23 00:09:42 +05:30
|
|
|
files: files,
|
|
|
|
local_tiller_enabled: cluster.local_tiller_enabled?
|
2020-05-24 23:13:21 +05:30
|
|
|
).delete_command,
|
|
|
|
Gitlab::Kubernetes::KubectlCmd.delete("pvc", "--selector", "release=elastic-stack", "--namespace", Gitlab::Kubernetes::Helm::NAMESPACE)
|
|
|
|
]
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|