Class: Fluent::KubernetesMetadataFilter
- Inherits:
-
Filter
- Object
- Filter
- Fluent::KubernetesMetadataFilter
- Includes:
- KubernetesMetadata::CacheStrategy, KubernetesMetadata::Common, KubernetesMetadata::WatchNamespaces, KubernetesMetadata::WatchPods
- Defined in:
- lib/fluent/plugin/filter_kubernetes_metadata.rb
Constant Summary collapse
- K8_POD_CA_CERT =
'ca.crt'- K8_POD_TOKEN =
'token'
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #create_time_from_record(record) ⇒ Object
- #de_dot!(h) ⇒ Object
- #dump_stats ⇒ Object
- #fetch_namespace_metadata(namespace_name) ⇒ Object
- #fetch_pod_metadata(namespace_name, pod_name) ⇒ Object
- #filter_stream(tag, es) ⇒ Object
- #filter_stream_from_files(tag, es) ⇒ Object
- #filter_stream_from_journal(tag, es) ⇒ Object
- #get_metadata_for_record(match_data, cache_key, create_time, batch_miss_cache) ⇒ Object
-
#initialize ⇒ KubernetesMetadataFilter
constructor
A new instance of KubernetesMetadataFilter.
Methods included from KubernetesMetadata::WatchPods
Methods included from KubernetesMetadata::Common
#match_annotations, #parse_namespace_metadata, #parse_pod_metadata, #syms_to_strs
Methods included from KubernetesMetadata::WatchNamespaces
Methods included from KubernetesMetadata::CacheStrategy
Constructor Details
#initialize ⇒ KubernetesMetadataFilter
Returns a new instance of KubernetesMetadataFilter.
143 144 145 146 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 143 def initialize super @prev_time = Time.now end |
Instance Method Details
#configure(conf) ⇒ Object
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 148 def configure(conf) super def log.trace? level == Fluent::Log::LEVEL_TRACE end require 'kubeclient' require 'active_support/core_ext/object/blank' require 'lru_redux' @stats = KubernetesMetadata::Stats.new if @de_dot && (@de_dot_separator =~ /\./).present? raise Fluent::ConfigError, "Invalid de_dot_separator: cannot be or contain '.'" end if @cache_ttl < 0 log.info "Setting the cache TTL to :none because it was <= 0" @cache_ttl = :none end # Caches pod/namespace UID tuples for a given container UID. @id_cache = LruRedux::TTL::ThreadSafeCache.new(@cache_size, @cache_ttl) # Use the container UID as the key to fetch a hash containing pod metadata @cache = LruRedux::TTL::ThreadSafeCache.new(@cache_size, @cache_ttl) # Use the namespace UID as the key to fetch a hash containing namespace metadata @namespace_cache = LruRedux::TTL::ThreadSafeCache.new(@cache_size, @cache_ttl) @tag_to_kubernetes_name_regexp_compiled = Regexp.compile(@tag_to_kubernetes_name_regexp) @container_name_to_kubernetes_regexp_compiled = Regexp.compile(@container_name_to_kubernetes_regexp) # Use Kubernetes default service account if we're in a pod. if @kubernetes_url.nil? env_host = ENV['KUBERNETES_SERVICE_HOST'] env_port = ENV['KUBERNETES_SERVICE_PORT'] if env_host.present? && env_port.present? @kubernetes_url = "https://#{env_host}:#{env_port}/api" end end # Use SSL certificate and bearer token from Kubernetes service account. if Dir.exist?(@secret_dir) ca_cert = File.join(@secret_dir, K8_POD_CA_CERT) pod_token = File.join(@secret_dir, K8_POD_TOKEN) if !@ca_file.present? and File.exist?(ca_cert) @ca_file = ca_cert end if !@bearer_token_file.present? and File.exist?(pod_token) @bearer_token_file = pod_token end end if @kubernetes_url.present? = { 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) [:bearer_token] = bearer_token end @client = Kubeclient::Client.new @kubernetes_url, @apiVersion, ssl_options: , auth_options: begin @client.api_valid? rescue KubeException => kube_error raise Fluent::ConfigError, "Invalid Kubernetes API #{@apiVersion} endpoint #{@kubernetes_url}: #{kube_error.}" end if @watch thread = Thread.new(self) { |this| this.start_pod_watch } thread.abort_on_exception = true namespace_thread = Thread.new(self) { |this| this.start_namespace_watch } namespace_thread.abort_on_exception = true end end if @use_journal log.debug "Will stream from the journal" self.class.class_eval { alias_method :filter_stream, :filter_stream_from_journal } else log.debug "Will stream from the files" self.class.class_eval { alias_method :filter_stream, :filter_stream_from_files } end @annotations_regexps = [] @annotation_match.each do |regexp| begin @annotations_regexps << Regexp.compile(regexp) rescue RegexpError => e log.error "Error: invalid regular expression in annotation_match: #{e}" end end end |
#create_time_from_record(record) ⇒ Object
271 272 273 274 275 276 277 278 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 271 def create_time_from_record(record) time = if @use_journal record['_SOURCE_REALTIME_TIMESTAMP'].nil? ? record['_SOURCE_REALTIME_TIMESTAMP'] : record['__REALTIME_TIMESTAMP'] else record['time'] end (time.nil? || time.chop.empty?) ? Time.now : Time.parse(time) end |
#de_dot!(h) ⇒ Object
344 345 346 347 348 349 350 351 352 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 344 def de_dot!(h) h.keys.each do |ref| if h[ref] && ref =~ /\./ v = h.delete(ref) newref = ref.to_s.gsub('.', @de_dot_separator) h[newref] = v end end end |
#dump_stats ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 101 def dump_stats @curr_time = Time.now return if @curr_time.to_i - @prev_time.to_i < @stats_interval @prev_time = @curr_time @stats.set(:pod_cache_size, @cache.count) @stats.set(:namespace_cache_size, @namespace_cache.count) if @namespace_cache log.info(@stats) if log.level == Fluent::Log::LEVEL_TRACE log.trace(" id cache: #{@id_cache.to_a}") log.trace(" pod cache: #{@cache.to_a}") log.trace("namespace cache: #{@namespace_cache.to_a}") end end |
#fetch_namespace_metadata(namespace_name) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 115 def (namespace_name) log.trace("fetching namespace metadata: #{namespace_name}") if log.trace? begin = @client.get_namespace(namespace_name) unless log.trace("no metadata returned for: #{namespace_name}") if log.trace? @stats.bump(:namespace_cache_api_nil_not_found) else begin log.trace("raw metadata for #{namespace_name}: #{}") if log.trace? = () @stats.bump(:namespace_cache_api_updates) log.trace("parsed metadata for #{namespace_name}: #{}") if log.trace? @namespace_cache[['namespace_id']] = return rescue Exception => e log.debug(e) @stats.bump(:namespace_cache_api_nil_bad_resp_payload) log.trace("returning empty metadata for #{namespace_name} due to error '#{e}'") if log.trace? end end rescue Exception => kube_error @stats.bump(:namespace_cache_api_nil_error) log.debug "Exception '#{kube_error}' encountered fetching namespace metadata from Kubernetes API #{@apiVersion} endpoint #{@kubernetes_url}" end {} end |
#fetch_pod_metadata(namespace_name, pod_name) ⇒ Object
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 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 73 def (namespace_name, pod_name) log.trace("fetching pod metadata: #{namespace_name}/#{pod_name}") if log.trace? begin = @client.get_pod(pod_name, namespace_name) unless log.trace("no metadata returned for: #{namespace_name}/#{pod_name}") if log.trace? @stats.bump(:pod_cache_api_nil_not_found) else begin log.trace("raw metadata for #{namespace_name}/#{pod_name}: #{}") if log.trace? = () @stats.bump(:pod_cache_api_updates) log.trace("parsed metadata for #{namespace_name}/#{pod_name}: #{}") if log.trace? @cache[['pod_id']] = return rescue Exception=>e log.debug(e) @stats.bump(:pod_cache_api_nil_bad_resp_payload) log.trace("returning empty metadata for #{namespace_name}/#{pod_name} due to error '#{e}'") if log.trace? end end rescue Exception=>e @stats.bump(:pod_cache_api_nil_error) log.debug "Exception '#{e}' encountered fetching pod metadata from Kubernetes API #{@apiVersion} endpoint #{@kubernetes_url}" end {} end |
#filter_stream(tag, es) ⇒ Object
280 281 282 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 280 def filter_stream(tag, es) es end |
#filter_stream_from_files(tag, es) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 284 def filter_stream_from_files(tag, es) return es if (es.respond_to?(:empty?) && es.empty?) || !es.is_a?(Fluent::EventStream) new_es = MultiEventStream.new match_data = tag.match(@tag_to_kubernetes_name_regexp_compiled) batch_miss_cache = {} if match_data container_id = match_data['docker_id'] = { 'docker' => { 'container_id' => container_id }, 'kubernetes' => (match_data, container_id, create_time_from_record(es.first[1]), batch_miss_cache) } end es.each do |time, record| record = record.merge(Marshal.load(Marshal.dump())) if new_es.add(time, record) end dump_stats new_es end |
#filter_stream_from_journal(tag, es) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 308 def filter_stream_from_journal(tag, es) return es if (es.respond_to?(:empty?) && es.empty?) || !es.is_a?(Fluent::EventStream) new_es = MultiEventStream.new batch_miss_cache = {} es.each do |time, record| = nil if record.has_key?('CONTAINER_NAME') && record.has_key?('CONTAINER_ID_FULL') = record['CONTAINER_NAME'].match(@container_name_to_kubernetes_regexp_compiled) do |match_data| container_id = record['CONTAINER_ID_FULL'] = { 'docker' => { 'container_id' => container_id }, 'kubernetes' => (match_data, container_id, create_time_from_record(record), batch_miss_cache) } end unless log.debug "Error: could not match CONTAINER_NAME from record #{record}" @stats.bump(:container_name_match_failed) end elsif record.has_key?('CONTAINER_NAME') && record['CONTAINER_NAME'].start_with?('k8s_') log.debug "Error: no container name and id in record #{record}" @stats.bump(:container_name_id_missing) end record = record.merge() if new_es.add(time, record) end dump_stats new_es end |
#get_metadata_for_record(match_data, cache_key, create_time, batch_miss_cache) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 256 def (match_data, cache_key, create_time, batch_miss_cache) namespace_name = match_data['namespace'] pod_name = match_data['pod_name'] = { 'container_name' => match_data['container_name'], 'namespace_name' => namespace_name, 'pod_name' => pod_name } if @kubernetes_url.present? = (cache_key, namespace_name, pod_name, create_time, batch_miss_cache) .merge!() if end end |