fluent-plugin-kubernetes_metadata_filter 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Fluentd Kubernetes Metadata Filter Plugin - Enrich Fluentd events with
5
+ # Kubernetes metadata
6
+ #
7
+ # Copyright 2015 Red Hat, Inc.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+ class KubernetesMetadataCacheStatsTest < Test::Unit::TestCase
22
+ include KubernetesMetadata::Util
23
+
24
+ def setup
25
+ @time_fields = ['time']
26
+ @internal_time = Time.now
27
+ end
28
+
29
+ test '#create_time_from_record when time is empty' do
30
+ record = { 'time' => ' ' }
31
+ assert_equal(@internal_time.to_i, create_time_from_record(record, @internal_time).to_i)
32
+ end
33
+ test '#create_time_from_record when time is nil' do
34
+ record = {}
35
+ assert_equal(@internal_time.to_i, create_time_from_record(record, @internal_time).to_i)
36
+ end
37
+
38
+ test '#create_time_from_record when time is an integer' do
39
+ exp_time = Time.now
40
+ record = { 'time' => exp_time.to_i }
41
+ assert_equal(exp_time.to_i, create_time_from_record(record, @internal_time).to_i)
42
+ end
43
+
44
+ test '#create_time_from_record when time is a string' do
45
+ exp_time = Time.now
46
+ record = { 'time' => exp_time.to_s }
47
+ assert_equal(exp_time.to_i, create_time_from_record(record, @internal_time).to_i)
48
+ end
49
+
50
+ test '#create_time_from_record when timefields include journal time fields' do
51
+ @time_fields = ['_SOURCE_REALTIME_TIMESTAMP']
52
+ exp_time = Time.now
53
+ record = { '_SOURCE_REALTIME_TIMESTAMP' => exp_time.to_i.to_s }
54
+ assert_equal(Time.at(exp_time.to_i / 1_000_000, exp_time.to_i % 1_000_000).to_i, create_time_from_record(record, @internal_time).to_i)
55
+ end
56
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Fluentd Kubernetes Metadata Filter Plugin - Enrich Fluentd events with
3
5
  # Kubernetes metadata
@@ -20,225 +22,224 @@ require_relative '../helper'
20
22
  require_relative 'watch_test'
21
23
 
22
24
  class WatchNamespacesTestTest < WatchTest
25
+ include KubernetesMetadata::WatchNamespaces
26
+
27
+ setup do
28
+ @initial = {
29
+ kind: 'NamespaceList',
30
+ metadata: { resourceVersion: '123' },
31
+ items: [
32
+ {
33
+ metadata: {
34
+ name: 'initial',
35
+ uid: 'initial_uid'
36
+ }
37
+ },
38
+ {
39
+ metadata: {
40
+ name: 'modified',
41
+ uid: 'modified_uid'
42
+ }
43
+ }
44
+ ]
45
+ }
46
+
47
+ @created = {
48
+ type: 'CREATED',
49
+ object: {
50
+ metadata: {
51
+ name: 'created',
52
+ uid: 'created_uid'
53
+ }
54
+ }
55
+ }
56
+ @modified = {
57
+ type: 'MODIFIED',
58
+ object: {
59
+ metadata: {
60
+ name: 'foo',
61
+ uid: 'modified_uid'
62
+ }
63
+ }
64
+ }
65
+ @deleted = {
66
+ type: 'DELETED',
67
+ object: {
68
+ metadata: {
69
+ name: 'deleteme',
70
+ uid: 'deleted_uid'
71
+ }
72
+ }
73
+ }
74
+ @error = {
75
+ type: 'ERROR',
76
+ object: {
77
+ message: 'some error message'
78
+ }
79
+ }
80
+ @gone = {
81
+ type: 'ERROR',
82
+ object: {
83
+ code: 410,
84
+ kind: 'Status',
85
+ message: 'too old resource version: 123 (391079)',
86
+ metadata: {
87
+ name: 'gone',
88
+ namespace: 'gone',
89
+ uid: 'gone_uid'
90
+ },
91
+ reason: 'Gone'
92
+ }
93
+ }
94
+ end
95
+
96
+ test 'namespace list caches namespaces' do
97
+ @client.stub :get_namespaces, @initial do
98
+ process_namespace_watcher_notices(start_namespace_watch)
99
+ assert_equal(true, @namespace_cache.key?('initial_uid'))
100
+ assert_equal(true, @namespace_cache.key?('modified_uid'))
101
+ assert_equal(2, @stats[:namespace_cache_host_updates])
102
+ end
103
+ end
23
104
 
24
- include KubernetesMetadata::WatchNamespaces
25
-
26
- setup do
27
- @initial = {
28
- kind: 'NamespaceList',
29
- metadata: {resourceVersion: '123'},
30
- items: [
31
- {
32
- metadata: {
33
- name: 'initial',
34
- uid: 'initial_uid'
35
- }
36
- },
37
- {
38
- metadata: {
39
- name: 'modified',
40
- uid: 'modified_uid'
41
- }
42
- }
43
- ]
44
- }
45
-
46
- @created = {
47
- type: 'CREATED',
48
- object: {
49
- metadata: {
50
- name: 'created',
51
- uid: 'created_uid'
52
- }
53
- }
54
- }
55
- @modified = {
56
- type: 'MODIFIED',
57
- object: {
58
- metadata: {
59
- name: 'foo',
60
- uid: 'modified_uid'
61
- }
62
- }
63
- }
64
- @deleted = {
65
- type: 'DELETED',
66
- object: {
67
- metadata: {
68
- name: 'deleteme',
69
- uid: 'deleted_uid'
70
- }
71
- }
72
- }
73
- @error = {
74
- type: 'ERROR',
75
- object: {
76
- message: 'some error message'
77
- }
78
- }
79
- @gone = {
80
- type: 'ERROR',
81
- object: {
82
- code: 410,
83
- kind: 'Status',
84
- message: 'too old resource version: 123 (391079)',
85
- metadata: {
86
- name: 'gone',
87
- namespace: 'gone',
88
- uid: 'gone_uid'
89
- },
90
- reason: 'Gone'
91
- }
92
- }
93
- end
94
-
95
- test 'namespace list caches namespaces' do
96
- @client.stub :get_namespaces, @initial do
105
+ test 'namespace list caches namespaces and watch updates' do
106
+ orig_env_val = ENV['K8S_NODE_NAME']
107
+ ENV['K8S_NODE_NAME'] = 'aNodeName'
108
+ @client.stub :get_namespaces, @initial do
109
+ @client.stub :watch_namespaces, [@modified] do
97
110
  process_namespace_watcher_notices(start_namespace_watch)
98
- assert_equal(true, @namespace_cache.key?('initial_uid'))
99
- assert_equal(true, @namespace_cache.key?('modified_uid'))
100
111
  assert_equal(2, @stats[:namespace_cache_host_updates])
112
+ assert_equal(1, @stats[:namespace_cache_watch_updates])
101
113
  end
102
114
  end
103
-
104
- test 'namespace list caches namespaces and watch updates' do
105
- orig_env_val = ENV['K8S_NODE_NAME']
106
- ENV['K8S_NODE_NAME'] = 'aNodeName'
107
- @client.stub :get_namespaces, @initial do
108
- @client.stub :watch_namespaces, [@modified] do
109
- process_namespace_watcher_notices(start_namespace_watch)
110
- assert_equal(2, @stats[:namespace_cache_host_updates])
111
- assert_equal(1, @stats[:namespace_cache_watch_updates])
112
- end
113
- end
114
- ENV['K8S_NODE_NAME'] = orig_env_val
115
- end
116
-
117
- test 'namespace watch ignores CREATED' do
118
- @client.stub :watch_namespaces, [@created] do
119
- process_namespace_watcher_notices(start_namespace_watch)
120
- assert_equal(false, @namespace_cache.key?('created_uid'))
121
- assert_equal(1, @stats[:namespace_cache_watch_ignored])
122
- end
115
+ ENV['K8S_NODE_NAME'] = orig_env_val
116
+ end
117
+
118
+ test 'namespace watch ignores CREATED' do
119
+ @client.stub :watch_namespaces, [@created] do
120
+ process_namespace_watcher_notices(start_namespace_watch)
121
+ assert_equal(false, @namespace_cache.key?('created_uid'))
122
+ assert_equal(1, @stats[:namespace_cache_watch_ignored])
123
123
  end
124
+ end
124
125
 
125
- test 'namespace watch ignores MODIFIED when info not in cache' do
126
- @client.stub :watch_namespaces, [@modified] do
127
- process_namespace_watcher_notices(start_namespace_watch)
128
- assert_equal(false, @namespace_cache.key?('modified_uid'))
129
- assert_equal(1, @stats[:namespace_cache_watch_misses])
130
- end
126
+ test 'namespace watch ignores MODIFIED when info not in cache' do
127
+ @client.stub :watch_namespaces, [@modified] do
128
+ process_namespace_watcher_notices(start_namespace_watch)
129
+ assert_equal(false, @namespace_cache.key?('modified_uid'))
130
+ assert_equal(1, @stats[:namespace_cache_watch_misses])
131
131
  end
132
-
133
- test 'namespace watch updates cache when MODIFIED is received and info is cached' do
134
- @namespace_cache['modified_uid'] = {}
135
- @client.stub :watch_namespaces, [@modified] do
136
- process_namespace_watcher_notices(start_namespace_watch)
137
- assert_equal(true, @namespace_cache.key?('modified_uid'))
138
- assert_equal(1, @stats[:namespace_cache_watch_updates])
139
- end
132
+ end
133
+
134
+ test 'namespace watch updates cache when MODIFIED is received and info is cached' do
135
+ @namespace_cache['modified_uid'] = {}
136
+ @client.stub :watch_namespaces, [@modified] do
137
+ process_namespace_watcher_notices(start_namespace_watch)
138
+ assert_equal(true, @namespace_cache.key?('modified_uid'))
139
+ assert_equal(1, @stats[:namespace_cache_watch_updates])
140
140
  end
141
-
142
- test 'namespace watch ignores DELETED' do
143
- @namespace_cache['deleted_uid'] = {}
144
- @client.stub :watch_namespaces, [@deleted] do
145
- process_namespace_watcher_notices(start_namespace_watch)
146
- assert_equal(true, @namespace_cache.key?('deleted_uid'))
147
- assert_equal(1, @stats[:namespace_cache_watch_deletes_ignored])
148
- end
141
+ end
142
+
143
+ test 'namespace watch ignores DELETED' do
144
+ @namespace_cache['deleted_uid'] = {}
145
+ @client.stub :watch_namespaces, [@deleted] do
146
+ process_namespace_watcher_notices(start_namespace_watch)
147
+ assert_equal(true, @namespace_cache.key?('deleted_uid'))
148
+ assert_equal(1, @stats[:namespace_cache_watch_deletes_ignored])
149
149
  end
150
-
151
- test 'namespace watch raises Fluent::UnrecoverableError when cannot re-establish connection to k8s API server' do
152
- # Stub start_namespace_watch to simulate initial successful connection to API server
153
- stub(self).start_namespace_watch
154
- # Stub watch_namespaces to simluate not being able to set up watch connection to API server
155
- stub(@client).watch_namespaces { raise }
156
- @client.stub :get_namespaces, @initial do
157
- assert_raise Fluent::UnrecoverableError do
158
- set_up_namespace_thread
159
- end
150
+ end
151
+
152
+ test 'namespace watch raises Fluent::UnrecoverableError when cannot re-establish connection to k8s API server' do
153
+ # Stub start_namespace_watch to simulate initial successful connection to API server
154
+ stub(self).start_namespace_watch
155
+ # Stub watch_namespaces to simluate not being able to set up watch connection to API server
156
+ stub(@client).watch_namespaces { raise }
157
+ @client.stub :get_namespaces, @initial do
158
+ assert_raise Fluent::UnrecoverableError do
159
+ set_up_namespace_thread
160
160
  end
161
- assert_equal(3, @stats[:namespace_watch_failures])
162
- assert_equal(2, Thread.current[:namespace_watch_retry_count])
163
- assert_equal(4, Thread.current[:namespace_watch_retry_backoff_interval])
164
- assert_nil(@stats[:namespace_watch_error_type_notices])
165
161
  end
166
-
167
- test 'namespace watch resets watch retry count when exceptions are encountered and connection to k8s API server is re-established' do
168
- @client.stub :get_namespaces, @initial do
169
- @client.stub :watch_namespaces, [[@created, @exception_raised]] do
170
- # Force the infinite watch loop to exit after 3 seconds. Verifies that
171
- # no unrecoverable error was thrown during this period of time.
172
- assert_raise Timeout::Error.new('execution expired') do
173
- Timeout.timeout(3) do
174
- set_up_namespace_thread
175
- end
162
+ assert_equal(3, @stats[:namespace_watch_failures])
163
+ assert_equal(2, Thread.current[:namespace_watch_retry_count])
164
+ assert_equal(4, Thread.current[:namespace_watch_retry_backoff_interval])
165
+ assert_nil(@stats[:namespace_watch_error_type_notices])
166
+ end
167
+
168
+ test 'namespace watch resets watch retry count when exceptions are encountered and connection to k8s API server is re-established' do
169
+ @client.stub :get_namespaces, @initial do
170
+ @client.stub :watch_namespaces, [[@created, @exception_raised]] do
171
+ # Force the infinite watch loop to exit after 3 seconds. Verifies that
172
+ # no unrecoverable error was thrown during this period of time.
173
+ assert_raise Timeout::Error.new('execution expired') do
174
+ Timeout.timeout(3) do
175
+ set_up_namespace_thread
176
176
  end
177
- assert_operator(@stats[:namespace_watch_failures], :>=, 3)
178
- assert_operator(Thread.current[:namespace_watch_retry_count], :<=, 1)
179
- assert_operator(Thread.current[:namespace_watch_retry_backoff_interval], :<=, 1)
180
177
  end
178
+ assert_operator(@stats[:namespace_watch_failures], :>=, 3)
179
+ assert_operator(Thread.current[:namespace_watch_retry_count], :<=, 1)
180
+ assert_operator(Thread.current[:namespace_watch_retry_backoff_interval], :<=, 1)
181
181
  end
182
182
  end
183
-
184
- test 'namespace watch resets watch retry count when error is received and connection to k8s API server is re-established' do
185
- @client.stub :get_namespaces, @initial do
186
- @client.stub :watch_namespaces, [@error] do
187
- # Force the infinite watch loop to exit after 3 seconds. Verifies that
188
- # no unrecoverable error was thrown during this period of time.
189
- assert_raise Timeout::Error.new('execution expired') do
190
- Timeout.timeout(3) do
191
- set_up_namespace_thread
192
- end
183
+ end
184
+
185
+ test 'namespace watch resets watch retry count when error is received and connection to k8s API server is re-established' do
186
+ @client.stub :get_namespaces, @initial do
187
+ @client.stub :watch_namespaces, [@error] do
188
+ # Force the infinite watch loop to exit after 3 seconds. Verifies that
189
+ # no unrecoverable error was thrown during this period of time.
190
+ assert_raise Timeout::Error.new('execution expired') do
191
+ Timeout.timeout(3) do
192
+ set_up_namespace_thread
193
193
  end
194
- assert_operator(@stats[:namespace_watch_failures], :>=, 3)
195
- assert_operator(Thread.current[:namespace_watch_retry_count], :<=, 1)
196
- assert_operator(Thread.current[:namespace_watch_retry_backoff_interval], :<=, 1)
197
194
  end
195
+ assert_operator(@stats[:namespace_watch_failures], :>=, 3)
196
+ assert_operator(Thread.current[:namespace_watch_retry_count], :<=, 1)
197
+ assert_operator(Thread.current[:namespace_watch_retry_backoff_interval], :<=, 1)
198
198
  end
199
199
  end
200
-
201
- test 'namespace watch continues after retries succeed' do
202
- @client.stub :get_namespaces, @initial do
203
- @client.stub :watch_namespaces, [@modified, @error, @modified] do
204
- # Force the infinite watch loop to exit after 3 seconds. Verifies that
205
- # no unrecoverable error was thrown during this period of time.
206
- assert_raise Timeout::Error.new('execution expired') do
207
- Timeout.timeout(3) do
208
- set_up_namespace_thread
209
- end
200
+ end
201
+
202
+ test 'namespace watch continues after retries succeed' do
203
+ @client.stub :get_namespaces, @initial do
204
+ @client.stub :watch_namespaces, [@modified, @error, @modified] do
205
+ # Force the infinite watch loop to exit after 3 seconds. Verifies that
206
+ # no unrecoverable error was thrown during this period of time.
207
+ assert_raise Timeout::Error.new('execution expired') do
208
+ Timeout.timeout(3) do
209
+ set_up_namespace_thread
210
210
  end
211
- assert_operator(@stats[:namespace_watch_failures], :>=, 3)
212
- assert_operator(Thread.current[:namespace_watch_retry_count], :<=, 1)
213
- assert_operator(Thread.current[:namespace_watch_retry_backoff_interval], :<=, 1)
214
- assert_operator(@stats[:namespace_watch_error_type_notices], :>=, 3)
215
211
  end
212
+ assert_operator(@stats[:namespace_watch_failures], :>=, 3)
213
+ assert_operator(Thread.current[:namespace_watch_retry_count], :<=, 1)
214
+ assert_operator(Thread.current[:namespace_watch_retry_backoff_interval], :<=, 1)
215
+ assert_operator(@stats[:namespace_watch_error_type_notices], :>=, 3)
216
216
  end
217
217
  end
218
+ end
218
219
 
219
- test 'namespace watch raises a GoneError when a 410 Gone error is received' do
220
- @cache['gone_uid'] = {}
221
- @client.stub :watch_namespaces, [@gone] do
222
- assert_raise KubernetesMetadata::Common::GoneError do
223
- process_namespace_watcher_notices(start_namespace_watch)
224
- end
225
- assert_equal(1, @stats[:namespace_watch_gone_notices])
220
+ test 'namespace watch raises a GoneError when a 410 Gone error is received' do
221
+ @cache['gone_uid'] = {}
222
+ @client.stub :watch_namespaces, [@gone] do
223
+ assert_raise KubernetesMetadata::Common::GoneError do
224
+ process_namespace_watcher_notices(start_namespace_watch)
226
225
  end
226
+ assert_equal(1, @stats[:namespace_watch_gone_notices])
227
227
  end
228
-
229
- test 'namespace watch retries when 410 Gone errors are encountered' do
230
- @client.stub :get_namespaces, @initial do
231
- @client.stub :watch_namespaces, [@created, @gone, @modified] do
232
- # Force the infinite watch loop to exit after 3 seconds. Verifies that
233
- # no unrecoverable error was thrown during this period of time.
234
- assert_raise Timeout::Error.new('execution expired') do
235
- Timeout.timeout(3) do
236
- set_up_namespace_thread
237
- end
228
+ end
229
+
230
+ test 'namespace watch retries when 410 Gone errors are encountered' do
231
+ @client.stub :get_namespaces, @initial do
232
+ @client.stub :watch_namespaces, [@created, @gone, @modified] do
233
+ # Force the infinite watch loop to exit after 3 seconds. Verifies that
234
+ # no unrecoverable error was thrown during this period of time.
235
+ assert_raise Timeout::Error.new('execution expired') do
236
+ Timeout.timeout(3) do
237
+ set_up_namespace_thread
238
238
  end
239
- assert_operator(@stats[:namespace_watch_gone_errors], :>=, 3)
240
- assert_operator(@stats[:namespace_watch_gone_notices], :>=, 3)
241
239
  end
240
+ assert_operator(@stats[:namespace_watch_gone_errors], :>=, 3)
241
+ assert_operator(@stats[:namespace_watch_gone_notices], :>=, 3)
242
242
  end
243
243
  end
244
+ end
244
245
  end