Welcome to mirror list, hosted at ThFree Co, Russian Federation.

elasticsearch_client.rb « concerns « clusters « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b0b6bdae0240510f5126671139e3567b2da5dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

module Clusters
  module Concerns
    module ElasticsearchClient
      include ::Gitlab::Utils::StrongMemoize

      ELASTICSEARCH_PORT = 9200
      ELASTICSEARCH_NAMESPACE = 'gitlab-managed-apps'

      def elasticsearch_client(timeout: nil)
        strong_memoize(:elasticsearch_client) do
          kube_client = cluster&.kubeclient&.core_client
          next unless kube_client

          proxy_url = kube_client.proxy_url('service', service_name, ELASTICSEARCH_PORT, ELASTICSEARCH_NAMESPACE)

          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]
            faraday.options.timeout = timeout unless timeout.nil?
          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)

          nil
        end
      end
    end
  end
end