Module: KubernetesMetadata::WatchNamespaces
- Includes:
- Common
- Included in:
- Fluent::Plugin::KubernetesMetadataFilter
- Defined in:
- lib/fluent/plugin/kubernetes_metadata_watch_namespaces.rb
Instance Method Summary collapse
-
#get_namespaces_and_start_watcher ⇒ Object
List all namespaces, record the resourceVersion and return a watcher starting from that resourceVersion.
-
#process_namespace_watcher_notices(watcher) ⇒ Object
Process a watcher notice and potentially raise an exception.
-
#reset_namespace_watch_retry_stats ⇒ Object
Reset namespace watch retry count and backoff interval as there is a successful watch notice.
- #set_up_namespace_thread ⇒ Object
- #start_namespace_watch ⇒ Object
Methods included from Common
#match_annotations, #parse_namespace_metadata, #parse_pod_metadata, #syms_to_strs
Instance Method Details
#get_namespaces_and_start_watcher ⇒ Object
List all namespaces, record the resourceVersion and return a watcher starting from that resourceVersion.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/fluent/plugin/kubernetes_metadata_watch_namespaces.rb', line 91 def get_namespaces_and_start_watcher = { resource_version: '0' # Fetch from API server cache instead of etcd quorum read } namespaces = @client.get_namespaces() namespaces[:items].each do |namespace| cache_key = namespace[:metadata][:uid] @namespace_cache[cache_key] = (namespace) @stats.bump(:namespace_cache_host_updates) end # continue watching from most recent resourceVersion [:resource_version] = namespaces[:metadata][:resourceVersion] watcher = @client.watch_namespaces() reset_namespace_watch_retry_stats watcher end |
#process_namespace_watcher_notices(watcher) ⇒ Object
Process a watcher notice and potentially raise an exception.
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 |
# File 'lib/fluent/plugin/kubernetes_metadata_watch_namespaces.rb', line 118 def process_namespace_watcher_notices(watcher) watcher.each do |notice| case notice[:type] when 'MODIFIED' reset_namespace_watch_retry_stats cache_key = notice[:object][:metadata][:uid] cached = @namespace_cache[cache_key] if cached @namespace_cache[cache_key] = (notice[:object]) @stats.bump(:namespace_cache_watch_updates) else @stats.bump(:namespace_cache_watch_misses) end when 'DELETED' reset_namespace_watch_retry_stats # ignore and let age out for cases where # deleted but still processing logs @stats.bump(:namespace_cache_watch_deletes_ignored) when 'ERROR' if notice[:object] && notice[:object][:code] == 410 @stats.bump(:namespace_watch_gone_notices) raise GoneError else @stats.bump(:namespace_watch_error_type_notices) = notice[:object][:message] if notice[:object] && notice[:object][:message] raise "Error while watching namespaces: #{}" end else reset_namespace_watch_retry_stats # Don't pay attention to creations, since the created namespace may not # be used by any namespace on this node. @stats.bump(:namespace_cache_watch_ignored) end end end |
#reset_namespace_watch_retry_stats ⇒ Object
Reset namespace watch retry count and backoff interval as there is a successful watch notice.
112 113 114 115 |
# File 'lib/fluent/plugin/kubernetes_metadata_watch_namespaces.rb', line 112 def reset_namespace_watch_retry_stats Thread.current[:namespace_watch_retry_count] = 0 Thread.current[:namespace_watch_retry_backoff_interval] = @watch_retry_interval end |
#set_up_namespace_thread ⇒ Object
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 |
# File 'lib/fluent/plugin/kubernetes_metadata_watch_namespaces.rb', line 28 def set_up_namespace_thread # Any failures / exceptions in the initial setup should raise # Fluent:ConfigError, so that users can inspect potential errors in # the configuration. namespace_watcher = start_namespace_watch Thread.current[:namespace_watch_retry_backoff_interval] = @watch_retry_interval Thread.current[:namespace_watch_retry_count] = 0 # Any failures / exceptions in the followup watcher notice # processing will be swallowed and retried. These failures / # exceptions could be caused by Kubernetes API being temporarily # down. We assume the configuration is correct at this point. loop do namespace_watcher ||= get_namespaces_and_start_watcher process_namespace_watcher_notices(namespace_watcher) rescue GoneError => e # Expected error. Quietly go back through the loop in order to # start watching from the latest resource versions @stats.bump(:namespace_watch_gone_errors) log.info('410 Gone encountered. Restarting namespace watch to reset resource versions.', e) namespace_watcher = nil rescue StandardError => e @stats.bump(:namespace_watch_failures) if Thread.current[:namespace_watch_retry_count] < @watch_retry_max_times # Instead of raising exceptions and crashing Fluentd, swallow # the exception and reset the watcher. log.info( 'Exception encountered parsing namespace watch event. ' \ 'The connection might have been closed. Sleeping for ' \ "#{Thread.current[:namespace_watch_retry_backoff_interval]} " \ 'seconds and resetting the namespace watcher.', e ) sleep(Thread.current[:namespace_watch_retry_backoff_interval]) Thread.current[:namespace_watch_retry_count] += 1 Thread.current[:namespace_watch_retry_backoff_interval] *= @watch_retry_exponential_backoff_base namespace_watcher = nil else # Since retries failed for many times, log as errors instead # of info and raise exceptions and trigger Fluentd to restart. = 'Exception encountered parsing namespace watch event. The ' \ 'connection might have been closed. Retried ' \ "#{@watch_retry_max_times} times yet still failing. Restarting." log.error(, e) raise Fluent::UnrecoverableError, end end end |
#start_namespace_watch ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fluent/plugin/kubernetes_metadata_watch_namespaces.rb', line 77 def start_namespace_watch get_namespaces_and_start_watcher rescue StandardError => e = 'start_namespace_watch: Exception encountered setting up ' \ "namespace watch from Kubernetes API #{@apiVersion} endpoint " \ "#{@kubernetes_url}: #{e.}" += " (#{e.response})" if e.respond_to?(:response) log.debug() raise Fluent::ConfigError, end |