Class: Fluent::KubernetesMetadataFilter
- Inherits:
-
Filter
- Object
- Filter
- Fluent::KubernetesMetadataFilter
- 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
- #de_dot!(h) ⇒ Object
-
#filter_stream(tag, es) ⇒ Object
NOTE: fluentd requires that records/hashes have string keys, not symbol keys docs.fluentd.org/articles/plugin-development#record-format.
- #get_metadata(namespace_name, pod_name, container_name) ⇒ Object
-
#initialize ⇒ KubernetesMetadataFilter
constructor
A new instance of KubernetesMetadataFilter.
- #merge_json_log(record) ⇒ Object
- #start_namespace_watch ⇒ Object
- #start_watch ⇒ Object
- #syms_to_strs(hsh) ⇒ Object
Constructor Details
#initialize ⇒ KubernetesMetadataFilter
81 82 83 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 81 def initialize super end |
Instance Method Details
#configure(conf) ⇒ Object
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 111 112 113 114 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 85 def configure(conf) super require 'kubeclient' require 'active_support/core_ext/object/blank' require 'lru_redux' if @de_dot && (@de_dot_separator =~ /\./).present? raise Fluent::ConfigError, "Invalid de_dot_separator: cannot be or contain '.'" end if @cache_ttl < 0 @cache_ttl = :none end @cache = LruRedux::TTL::ThreadSafeCache.new(@cache_size, @cache_ttl) if @include_namespace_id @namespace_cache = LruRedux::TTL::ThreadSafeCache.new(@cache_size, @cache_ttl) end @tag_to_kubernetes_name_regexp_compiled = Regexp.compile(@tag_to_kubernetes_name_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 endpoint: #{kube_error.message}" end if @watch thread = Thread.new(self) { |this| this.start_watch } thread.abort_on_exception = true if @include_namespace_id namespace_thread = Thread.new(self) { |this| this.start_namespace_watch } namespace_thread.abort_on_exception = true end end end end |
#de_dot!(h) ⇒ Object
234 235 236 237 238 239 240 241 242 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 234 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 |
#filter_stream(tag, es) ⇒ Object
NOTE: fluentd requires that records/hashes have string keys, not symbol keys docs.fluentd.org/articles/plugin-development#record-format
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 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 167 def filter_stream(tag, es) new_es = MultiEventStream.new match_data = tag.match(@tag_to_kubernetes_name_regexp_compiled) if match_data = { 'docker' => { 'container_id' => match_data['docker_id'] }, 'kubernetes' => { 'namespace_name' => match_data['namespace'], 'pod_name' => match_data['pod_name'], 'container_name' => match_data['container_name'] } } if @kubernetes_url.present? cache_key = "#{metadata['kubernetes']['namespace_name']}_#{metadata['kubernetes']['pod_name']}_#{metadata['kubernetes']['container_name']}" this = self = @cache.getset(cache_key) { if = this.( ['kubernetes']['namespace_name'], ['kubernetes']['pod_name'], ['kubernetes']['container_name'] ) ['kubernetes'] = if end } if @include_namespace_id namespace_name = ['kubernetes']['namespace_name'] namespace_id = @namespace_cache.getset(namespace_name) { namespace = @client.get_namespace(namespace_name) namespace['metadata']['uid'] if namespace } ['kubernetes']['namespace_id'] = namespace_id if namespace_id end end end es.each { |time, record| record = merge_json_log(record) if @merge_json_log record = record.merge() if new_es.add(time, record) } new_es end |
#get_metadata(namespace_name, pod_name, container_name) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 60 def (namespace_name, pod_name, container_name) begin = @client.get_pod(pod_name, namespace_name) return if ! labels = syms_to_strs(['metadata']['labels'].to_h) if @de_dot self.de_dot!(labels) end return { 'namespace_name' => namespace_name, 'pod_id' => ['metadata']['uid'], 'pod_name' => pod_name, 'container_name' => container_name, 'labels' => labels, 'host' => ['spec']['nodeName'] } rescue KubeException nil end end |
#merge_json_log(record) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 221 def merge_json_log(record) if record.has_key?('log') log = record['log'].strip if log[0].eql?('{') && log[-1].eql?('}') begin record = JSON.parse(log).merge(record) rescue JSON::ParserError end end end record end |
#start_namespace_watch ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 286 def start_namespace_watch resource_version = @client.get_namespaces.resourceVersion watcher = @client.watch_namespaces(resource_version) watcher.each do |notice| puts notice case notice.type when 'DELETED' @namespace_cache.delete(notice.object['metadata']['name']) else # We only care about each namespace's name and UID, neither of which # is modifiable, so we only have to care about deletions. end end end |
#start_watch ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 244 def start_watch begin resource_version = @client.get_pods.resourceVersion watcher = @client.watch_pods(resource_version) rescue Exception => e raise Fluent::ConfigError, "Exception encountered fetching metadata from Kubernetes API endpoint: #{e.message}" end watcher.each do |notice| case notice.type when 'MODIFIED' if notice.object.status.containerStatuses pod_cache_key = "#{notice.object['metadata']['namespace']}_#{notice.object['metadata']['name']}" notice.object.status.containerStatuses.each { |container_status| cache_key = "#{pod_cache_key}_#{container_status['name']}" cached = @cache[cache_key] if cached # Only thing that can be modified is labels labels = syms_to_strs(notice.object..labels.to_h) if @de_dot self.de_dot!(labels) end cached['kubernetes']['labels'] = labels @cache[cache_key] = cached end } end when 'DELETED' if notice.object.status.containerStatuses pod_cache_key = "#{notice.object['metadata']['namespace']}_#{notice.object['metadata']['name']}" notice.object.status.containerStatuses.each { |container_status| cache_key = "#{pod_cache_key}_#{container_status['name']}" @cache.delete(cache_key) } end else # Don't pay attention to creations, since the created pod may not # end up on this node. end end end |
#syms_to_strs(hsh) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fluent/plugin/filter_kubernetes_metadata.rb', line 45 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 |