Module: KubernetesMetadata::Common

Included in:
Fluent::Plugin::KubernetesMetadataFilter, WatchNamespaces, WatchPods
Defined in:
lib/fluent/plugin/kubernetes_metadata_common.rb

Defined Under Namespace

Classes: GoneError

Instance Method Summary collapse

Instance Method Details

#match_annotations(annotations) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/fluent/plugin/kubernetes_metadata_common.rb', line 30

def match_annotations(annotations)
  result = {}
  @annotations_regexps.each do |regexp|
    annotations.each do |key, value|
      result[key] = value if ::Fluent::StringUtil.match_regexp(regexp, key.to_s)
    end
  end
  result
end

#parse_namespace_metadata(namespace_object) ⇒ Object

rubocop:disable Metrics/AbcSize



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fluent/plugin/kubernetes_metadata_common.rb', line 40

def (namespace_object) # rubocop:disable Metrics/AbcSize
  labels = ''
  labels = syms_to_strs(namespace_object[:metadata][:labels].to_h) unless @skip_labels || @skip_namespace_labels
  annotations = match_annotations(syms_to_strs(namespace_object[:metadata][:annotations].to_h))

   = {
    'namespace_id' => namespace_object[:metadata][:uid],
    'creation_timestamp' => namespace_object[:metadata][:creationTimestamp]
  }
  ['namespace_labels'] = labels unless labels.empty?
  ['namespace_annotations'] = annotations unless annotations.empty?
  
end

#parse_pod_metadata(pod_object) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/plugin/kubernetes_metadata_common.rb', line 54

def (pod_object) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  labels = ''
  labels = syms_to_strs(pod_object[:metadata][:labels].to_h) unless @skip_labels || @skip_pod_labels
  annotations = match_annotations(syms_to_strs(pod_object[:metadata][:annotations].to_h))

  # collect container information
  container_meta = {}
  begin
    if pod_object[:status] && pod_object[:status][:containerStatuses]
      pod_object[:status][:containerStatuses].each do |container_status|
        container_id = (container_status[:containerID] || '').sub(%r{^[-_a-zA-Z0-9]+://}, '')
        key = container_status[:name]
        container_meta[key] = if @skip_container_metadata
                                {
                                  'name' => container_status[:name]
                                }
                              else
                                {
                                  'name' => container_status[:name],
                                  'image' => container_status[:image],
                                  'image_id' => container_status[:imageID],
                                  :containerID => container_id
                                }
                              end
      end
    end
  rescue StandardError => e
    log.warn("parsing container meta information failed for: #{pod_object[:metadata][:namespace]}/#{pod_object[:metadata][:name]}: #{e}") # rubocop:disable Layout/LineLength
  end

  ownerrefs_meta = []
  if @include_ownerrefs_metadata && pod_object[:metadata][:ownerReferences]
    begin
      pod_object[:metadata][:ownerReferences].each do |owner_reference|
        ownerrefs_meta.append({ 'kind' => owner_reference[:kind], 'name' => owner_reference[:name] })
      end
    rescue StandardError => e
      log.warn(
        "parsing ownerrefs meta information failed for: #{pod_object[:metadata][:namespace]}/#{pod_object[:metadata][:name]}: #{e}" # rubocop:disable Layout/LineLength
      )
    end
  end

   = {
    'namespace_name' => pod_object[:metadata][:namespace],
    'pod_id' => pod_object[:metadata][:uid],
    'pod_name' => pod_object[:metadata][:name],
    'pod_ip' => pod_object[:status][:podIP],
    'containers' => syms_to_strs(container_meta),
    'host' => pod_object[:spec][:nodeName],
    'ownerrefs' => (ownerrefs_meta if @include_ownerrefs_metadata)
  }.compact
  ['labels'] = labels unless labels.empty?
  ['annotations'] = annotations unless annotations.empty?
  ['master_url'] = @kubernetes_url unless @skip_master_url
  
end

#syms_to_strs(hsh) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fluent/plugin/kubernetes_metadata_common.rb', line 112

def syms_to_strs(hsh)
  newhsh = {}
  hsh.each_pair do |kk, vv|
    vv = syms_to_strs(vv) if vv.is_a?(Hash)
    if kk.is_a?(Symbol)
      newhsh[kk.to_s] = vv
    else
      newhsh[kk] = vv
    end
  end
  newhsh
end