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

pods.rb « logs « elasticsearch « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 66499ae956afe7a95c363bd20426fd920078f2d5 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

module Gitlab
  module Elasticsearch
    module Logs
      class Pods
        # How many items to fetch in a query
        PODS_LIMIT = 500
        CONTAINERS_LIMIT = 500

        def initialize(client)
          @client = client
        end

        def pods(namespace)
          body = build_body(namespace)
          response = @client.search body: body

          format_response(response)
        end

        private

        def build_body(namespace)
          {
            aggs: {
              pods: {
                aggs: {
                  containers: {
                    terms: {
                      field: 'kubernetes.container.name',
                      size: ::Gitlab::Elasticsearch::Logs::Pods::CONTAINERS_LIMIT
                    }
                  }
                },
                terms: {
                  field: 'kubernetes.pod.name',
                  size: ::Gitlab::Elasticsearch::Logs::Pods::PODS_LIMIT
                }
              }
            },
            query: {
              bool: {
                must: {
                  match_phrase: {
                    "kubernetes.namespace": namespace
                  }
                }
              }
            },
            # don't populate hits, only the aggregation is needed
            size: 0
          }
        end

        def format_response(response)
          results = response.dig("aggregations", "pods", "buckets") || []
          results.map do |bucket|
            {
              name: bucket["key"],
              container_names: (bucket.dig("containers", "buckets") || []).map do |cbucket|
                cbucket["key"]
              end
            }
          end
        end
      end
    end
  end
end