Class: Fluent::KubernetesMetadataFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_kubernetes_metadata.rb

Instance Method Summary collapse

Constructor Details

#initializeKubernetesMetadataFilter

Returns a new instance of KubernetesMetadataFilter.



53
54
55
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 53

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



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
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 57

def configure(conf)
  super

  require 'kubeclient'
  require 'active_support/core_ext/object/blank'
  require 'lru_redux'

  @client = Kubeclient::Client.new @kubernetes_url, @apiVersion

  @client.ssl_options(
    client_cert: @client_cert.present? ? OpenSSL::X509::Certificate.new(File.read(@client_cert)) : nil,
    client_key: @client_key.present? ? OpenSSL::PKey::RSA.new(File.read(@client_key)) : nil,
    ca_file: @ca_file,
    verify_ssl: @verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
  )

  if @bearer_token_file.present?
    bearer_token = File.read(@bearer_token_file)
    RestClient.add_before_execution_proc do |req, params|
      req['authorization'] ||= "Bearer #{bearer_token}"
    end
  end

  begin
    @client.api_valid?
  rescue KubeException => kube_error
    raise Fluent::ConfigError, "Invalid Kubernetes API endpoint: #{kube_error.message}"
  end

  @cache = LruRedux::ThreadSafeCache.new(@cache_size)
  @container_name_to_kubernetes_name_regexp_compiled = Regexp.compile(@container_name_to_kubernetes_name_regexp)
end

#filter_stream(tag, es) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 90

def filter_stream(tag, es)
  new_es = MultiEventStream.new

  es.each {|time, record|
    if record.has_key?(:docker) && record[:docker].has_key?(:name)
      this = self
       = @cache.getset(record[:docker][:name]){
        match_data = record[:docker][:name].match(@container_name_to_kubernetes_name_regexp_compiled)
        if match_data
          this.(
            match_data[:pod_name],
            match_data[:pod_container_name],
            match_data[:namespace]
          )
        end
      }

      record[:kubernetes] =  if 
    end

    new_es.add(time, record)
  }

  new_es
end

#get_metadata(pod_name, container_name, namespace) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 35

def (pod_name, container_name, namespace)
  begin
     = @client.get_pod(pod_name, namespace)
    if 
      return {
        :uid => ['metadata']['uid'],
        :namespace => ['metadata']['namespace'],
        :pod_name => ['metadata']['name'],
        :container_name => container_name,
        :labels => ['metadata']['labels'].to_h,
        :host => ['spec']['host']
      }
    end
  rescue KubeException
    nil
  end
end