Class: Fluent::Plugin::Uuid2podnameFilter

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

Constant Summary collapse

K8_POD_CA_CERT =
'ca.crt'
K8_POD_TOKEN =
'token'

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



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

def configure(conf)
  super

  def log.trace?
    level == Fluent::Log::LEVEL_TRACE
  end
  require 'kubeclient'

  log.debug "Uuid2podname Filter configure"

  # Use Kubernetes default service account if we're in a pod.
  if @kubernetes_url.nil?
    log.debug "Kubernetes URL is not set - inspecting environment"

    env_host = ENV['KUBERNETES_SERVICE_HOST']
    env_port = ENV['KUBERNETES_SERVICE_PORT']
    if !env_host.nil? && !env_port.nil?
      if insecure
        @kubernetes_url = "http://#{env_host}:#{env_port}/api"
      else
        @kubernetes_url = "https://#{env_host}:#{env_port}/api"
      end
      log.debug "Kubernetes URL is now '#{@kubernetes_url}'"
    end
  end

  # Use SSL certificate and bearer token from Kubernetes service account.
  if Dir.exist?(@secret_dir)
    log.debug "Found directory with secrets: #{@secret_dir}"
    ca_cert = File.join(@secret_dir, K8_POD_CA_CERT)
    pod_token = File.join(@secret_dir, K8_POD_TOKEN)

    if @ca_file.nil? and File.exist?(ca_cert)
      log.debug "Found CA certificate: #{ca_cert}"
      @ca_file = ca_cert
    end

    if @bearer_token_file.nil? and File.exist?(pod_token)
      log.debug "Found pod token: #{pod_token}"
      @bearer_token_file = pod_token
    end
  end

  if !@kubernetes_url.nil?
    if insecure
      log.debug "Creating insecure K8S client"
      @client = Kubeclient::Client.new @kubernetes_url, @apiVersion
    else 
      ssl_options = {
          client_cert: !@client_cert.nil? ? OpenSSL::X509::Certificate.new(File.read(@client_cert)) : nil,
          client_key:  !@client_key.nil? ? 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
      }

      auth_options = {}

      if !@bearer_token_file.nil?
        bearer_token = File.read(@bearer_token_file)
        auth_options[:bearer_token] = bearer_token
      end

      log.debug "Creating secure K8S client"
      @client = Kubeclient::Client.new @kubernetes_url, @apiVersion,
                                      ssl_options: ssl_options,
                                      auth_options: auth_options
    end

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

#fetch_podsObject



128
129
130
131
132
133
134
135
136
# File 'lib/fluent/plugin/filter_uuid2podname.rb', line 128

def fetch_pods()
  log.debug "Uuid2podname Filter fetch_pods"
  newPodsHash = {}
  @client.get_pods.each do |pod_object|
     = (pod_object)
    newPodsHash[['kubernetes']['pod_id']] = 
  end
  @podsHash = newPodsHash.merge @podsHash
end

#filter_stream(tag, es) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fluent/plugin/filter_uuid2podname.rb', line 104

def filter_stream(tag, es)
  log.debug "Uuid2podname Filter filter"
  return es if (es.respond_to?(:empty?) && es.empty?) || !es.is_a?(Fluent::EventStream)
  new_es = Fluent::MultiEventStream.new
  es.each do |time, record|
     = nil
    if @podsHash.has_key?(record['uuid'])
       = @podsHash[record['uuid']]
      log.debug "find pod for uuid: #{record['uuid']}"
    else
      fetch_pods
      if @podsHash.has_key?(record['uuid'])
         = @podsHash[record['uuid']]
      else
        log.error "pod does not exist: #{record['uuid']}"
      end 
    end
    record = record.merge() if 
    new_es.add(time, record)
  end

  new_es
end

#parse_pod_metadata(pod_object) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fluent/plugin/filter_uuid2podname.rb', line 138

def (pod_object)
  labels = syms_to_strs(pod_object['metadata']['labels'].to_h)
  if @de_dot
    self.de_dot!(labels)
    self.de_dot!(annotations)
  end
  
  # collect container informations
  container_meta = {}
  begin
    pod_object['status']['containerStatuses'].each do|container_status|
      # get plain container id (eg. docker://hash -> hash)
      container_id = container_status['containerID'].sub /^[-_a-zA-Z0-9]+:\/\//, ''
      container_meta[container_id] = {
          'name' => container_status['name'],
          'image' => container_status['image'],
          'image_id' => container_status['imageID']
      }
    end
  rescue
    log.debug("parsing container meta information failed for: #{pod_object['metadata']['namespace']}/#{pod_object['metadata']['name']} ")
  end
  
   = {
      'namespace_name' => pod_object['metadata']['namespace'],
      'pod_id'         => pod_object['metadata']['uid'],
      'pod_name'       => pod_object['metadata']['name'],
      'containers'     => syms_to_strs(container_meta),
      'labels'         => labels,
      'host'           => pod_object['spec']['nodeName'],
      'master_url'     => @kubernetes_url
  }
   = {
    'kubernetes' => 
  }
  return 
end

#syms_to_strs(hsh) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fluent/plugin/filter_uuid2podname.rb', line 176

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