fluent-plugin-sd-dns 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7ec8418e9687b279922116248916d96f8f19e6d82375bb0a960688e295c15f39
4
+ data.tar.gz: 5c989bbe0134e7f03ba1325364cec7081d1d3ca8af08ddede43d78ec53c98875
5
+ SHA512:
6
+ metadata.gz: 28fe1112d11567f1f0f44ae3b4b690a905ca4f4b29209d085d447facb660059bb7a9d2683e90330910187753dc08ce361d9957d034c124d8db06f22d3c4d1f42
7
+ data.tar.gz: b317a0c23ef25733aa9468d15700b4a5f8f325b54c944dd6f051c9eda0a32c456eaecded76c400fa1a5751792eb2c265175b885338923ead8367cb633dfd75f4
@@ -0,0 +1,30 @@
1
+ Gemfile.lock
2
+ INSTALL
3
+ NEWS
4
+ Makefile
5
+ Makefile.in
6
+ README
7
+ ac
8
+ aclocal.m4
9
+ autom4te.cache
10
+ confdefs.h
11
+ config.log
12
+ config.status
13
+ configure
14
+ deps/
15
+ fluent-cat
16
+ fluent-gem
17
+ fluentd
18
+ pkg/*
19
+ test/tmp/*
20
+ test/config/tmp/*
21
+ make_dist.sh
22
+ Gemfile.local
23
+ .ruby-version
24
+ *.swp
25
+ coverage/*
26
+ .vagrant/
27
+ cov-int/
28
+ cov-fluentd.tar.gz
29
+ .vscode
30
+ .idea/
@@ -0,0 +1,24 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.4
5
+ - 2.5
6
+ - 2.6
7
+ - 2.7
8
+ - ruby-head
9
+
10
+ gemfile:
11
+ - Gemfile
12
+
13
+ branches:
14
+ only:
15
+ - master
16
+
17
+ before_install: gem update bundler
18
+ script: bundle exec rake test
19
+
20
+ sudo: false
21
+
22
+ matrix:
23
+ allow_failures:
24
+ - rvm: ruby-head
@@ -0,0 +1,3 @@
1
+ Release 0.1.0 - 2020/09/24
2
+
3
+ * Implement sd_dns plugin
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,203 @@
1
+ # DNS based service discovery plugin for [Fluentd](https://fluentd.org)
2
+
3
+ The `dns` service discovery plugin updates target services by DNS list.
4
+
5
+ This plugin uses `Socket.getaddrinfo` internally.
6
+
7
+ ## Requirements
8
+
9
+ - Fluentd v1.8 or later
10
+
11
+ ## Installation
12
+
13
+ Use RubyGems:
14
+
15
+ fluent-gem install fluent-plugin-sd-dns --no-document
16
+
17
+ ## Configuration
18
+
19
+ Use `@type dns` in `<service_discovery>`.
20
+
21
+ ```
22
+ <match pattern>
23
+ @type forward
24
+
25
+ <service_discovery>
26
+ @type dns
27
+ <entry>
28
+ host host1.domain
29
+ port 24224
30
+ </entry>
31
+ <entry>
32
+ host host2.domain
33
+ port 24225
34
+ </entry>
35
+ </service_discovery>
36
+ </match>
37
+ ```
38
+
39
+ If `host1.domain` returns 3 addresses and `host2.domain` returns 2 addresses, `sd_dns` generates 5 address entries with given port.
40
+
41
+ e.g.
42
+
43
+ - `host1.domain` returns `1.2.3.4`, `1.2.3.5` and `1.2.3.6`
44
+ - `host2.domain` returns `2.2.3.4` and `2.2.3.5`
45
+
46
+ `sd_dns` generates `1.2.3.4:24224`, `1.2.3.5:24224`, `1.2.3.6:24224`, `2.2.3.4:24225` and `2.2.3.5:24225`.
47
+
48
+ ### <entry> section
49
+
50
+ Specify host and additional information. Each entry has the following parameters:
51
+
52
+ - host
53
+ - port
54
+ - address_family
55
+ - shared_key
56
+ - username
57
+ - password
58
+ - weight
59
+
60
+ #### host
61
+
62
+ - Type: string
63
+
64
+ Hostname for service lookup. This is *required* parameter
65
+
66
+ #### port
67
+
68
+ - Type: integer
69
+
70
+ Port of service. This is *required* parameter
71
+
72
+ #### address_family
73
+
74
+ - Type: string (`ipv4` or `ipv6`)
75
+ - Default: nil (both)
76
+
77
+ Specify the desired address family. By default, `sd_dns` gets IPv4 and IPv6 addresses. If you want to get only IPv4 or IPv6, set it.
78
+
79
+ ```
80
+ <entry>
81
+ host host1.domain
82
+ port 24224
83
+ address_family ipv4
84
+ </entry>
85
+ ```
86
+
87
+ #### shared_key
88
+
89
+ - Type: string
90
+ - Default: nil
91
+
92
+ The shared_key per entry
93
+
94
+ #### username
95
+
96
+ - Type: string
97
+ - Default: empty string
98
+
99
+ The username for authentication.
100
+
101
+ #### password
102
+
103
+ - Type: string
104
+ - Default: empty string
105
+
106
+ The password for authentication
107
+
108
+ #### weight
109
+
110
+ - Type: Integer
111
+ - Default: 60
112
+
113
+ The load balancing weight
114
+
115
+ `shared_key`, `username`, `password` and `weight` are mainly for `out_forward`.
116
+
117
+ ```
118
+ <match pattern>
119
+ @type forward
120
+
121
+ <service_discovery>
122
+ @type dns
123
+ <entry>
124
+ host host1.domain
125
+ port 24224
126
+ username user1
127
+ password pass1
128
+ shared_key key1
129
+ weight 80
130
+ </entry>
131
+ <entry>
132
+ host host2.domain
133
+ port 24225
134
+ username user2
135
+ password pass2
136
+ shared_key key2
137
+ weight 20
138
+ </entry>
139
+ </service_discovery>
140
+ </match>
141
+ ```
142
+
143
+ ### interval
144
+
145
+ - Type: time
146
+ - Default: nil (no update)
147
+
148
+ Interval for updating the service list. By default, `sd_dns` keeps to use first service list. If you want to update the service list periodically, set `interval` parameter.
149
+
150
+ ```
151
+ <match pattern>
152
+ @type forward
153
+
154
+ <service_discovery>
155
+ @type dns
156
+ interval 60s
157
+ <entry>
158
+ host host.domain
159
+ port 80
160
+ </entry>
161
+ </service_discovery>
162
+ </match>
163
+ ```
164
+
165
+ ### ignore_dns_error
166
+
167
+ - Type: bool
168
+ - Default: false
169
+
170
+ Ignore DNS related error or not. This parameter is for multiple `<entry>` case. By default, `sd_dns` will stop the update process when raises an error in DNS resolution.
171
+
172
+ If you want to contine the update process, set `ignore_dns_error true`
173
+
174
+ ```
175
+ <match pattern>
176
+ @type forward
177
+
178
+ <service_discovery>
179
+ @type dns
180
+ interval 60
181
+ ignore_dns_error true
182
+ <entry>
183
+ host host1.domain
184
+ port 24224
185
+ </entry>
186
+ <entry>
187
+ host host2.domain
188
+ port 24225
189
+ </entry>
190
+ </service_discovery>
191
+ </match>
192
+ ```
193
+
194
+ When error happens, `sd_dns` generates following warning message:
195
+
196
+ ```
197
+ failed to get service list from host1.domain
198
+ ```
199
+
200
+ ## More information
201
+
202
+ - Copyright: 2020- Fluentd Authors
203
+ - License: Apache License, Version 2.0
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.test_files = FileList['test/plugin/test_*.rb']
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => [:build]
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "fluent-plugin-sd-dns"
6
+ gem.description = "DNS based service discovery plugin for Fluentd"
7
+ gem.license = "Apache-2.0"
8
+ gem.homepage = "https://github.com/fluent/fluent-plugin-sd-dns"
9
+ gem.summary = gem.description
10
+ gem.version = "0.1.0"
11
+ gem.authors = ["Masahiro Nakagawa"]
12
+ gem.email = "repeatedly@gmail.com"
13
+ #gem.platform = Gem::Platform::RUBY
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_dependency "fluentd", ">= 1.8"
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "flexmock", "~> 2.0"
22
+ gem.add_development_dependency "rr", "~> 1.0"
23
+ gem.add_development_dependency "test-unit", "~> 3.3"
24
+ gem.add_development_dependency "test-unit-rr", "~> 1.0"
25
+ end
@@ -0,0 +1,135 @@
1
+ #
2
+ # Fluentd
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'socket'
18
+
19
+ require 'fluent/plugin_helper'
20
+ require 'fluent/plugin/service_discovery'
21
+
22
+ module Fluent
23
+ module Plugin
24
+ class DnsServiceDiscovery < ServiceDiscovery
25
+ include PluginHelper::Mixin
26
+
27
+ Plugin.register_sd('dns', self)
28
+
29
+ helpers :timer
30
+
31
+ desc 'Ignore DNS related error or not'
32
+ config_param :ignore_dns_error, :bool, default: false
33
+ desc 'Interval to refresh service list'
34
+ config_param :interval, :time, default: nil
35
+
36
+ config_section :entry, param_name: :entry_configs, required: true, multi: true do
37
+ desc 'Hostname for service lookup'
38
+ config_param :host, :string
39
+ desc 'Port of service'
40
+ config_param :port, :integer
41
+ desc 'Specify the address family for returned address'
42
+ config_param :address_family, default: nil do |val|
43
+ case val.downcase
44
+ when 'ipv4'
45
+ Socket::Constants::AF_INET
46
+ when 'ipv6'
47
+ Socket::Constants::AF_INET6
48
+ else
49
+ raise Fluent::ConfigError, "Unsupported parameter value: #{val}"
50
+ end
51
+ end
52
+ desc 'The shared_key for authentication per host'
53
+ config_param :shared_key, :string, default: nil, secret: true
54
+ desc 'The username for authentication per host'
55
+ config_param :username, :string, default: ''
56
+ desc 'The password for authentication per host'
57
+ config_param :password, :string, default: '', secret: true
58
+ desc 'The load balancing weight per host'
59
+ config_param :weight, :integer, default: 60
60
+ end
61
+
62
+ def configure(conf)
63
+ super
64
+ @services = fetch_services_from_dns
65
+ end
66
+
67
+ def start(queue)
68
+ if @interval
69
+ timer_execute(:"sd_dns_refresh", @interval) do
70
+ refresh_service_list(queue)
71
+ end
72
+ else
73
+ refresh_service_list(queue)
74
+ end
75
+
76
+ super()
77
+ end
78
+
79
+ private
80
+
81
+ def refresh_service_list(queue)
82
+ old_services = @services
83
+ new_services = begin
84
+ fetch_services_from_dns
85
+ rescue => e
86
+ @log.error("sd_dns: #{e}")
87
+ return
88
+ end
89
+
90
+ if new_services.nil? || new_services.empty?
91
+ return
92
+ end
93
+
94
+ diff = []
95
+ join = new_services - old_services
96
+ # Need service_in first to guarantee that server exist at least one all time.
97
+ join.each do |j|
98
+ diff << ServiceDiscovery.service_in_msg(j)
99
+ end
100
+
101
+ drain = old_services - new_services
102
+ drain.each do |d|
103
+ diff << ServiceDiscovery.service_out_msg(d)
104
+ end
105
+
106
+ @services = new_services
107
+
108
+ diff.each do |a|
109
+ queue.push(a)
110
+ end
111
+ end
112
+
113
+ def fetch_services_from_dns
114
+ service_list = []
115
+ @entry_configs.each { |conf|
116
+ ai_list = begin
117
+ Socket.getaddrinfo(conf.host, conf.port, conf.address_family, Socket::SOCK_STREAM)
118
+ rescue => e
119
+ if @ignore_dns_error
120
+ @log.warn "failed to get service list from #{conf.host}", error: e
121
+ next
122
+ else
123
+ raise
124
+ end
125
+ end
126
+ ai_list.each_with_index { |ai, i|
127
+ name = "#{i}-#{conf.host}"
128
+ service_list << Service.new(:dns, ai[3], ai[1], name, conf.weight, false, conf.username, conf.password, conf.shared_key)
129
+ }
130
+ }
131
+ service_list
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,231 @@
1
+ require 'rr'
2
+ require 'test/unit'
3
+ require 'test/unit/rr'
4
+ require 'fileutils'
5
+ require 'flexmock/test_unit'
6
+
7
+ require 'fluent/env'
8
+ require 'fluent/test'
9
+ require 'fluent/plugin/sd_dns'
10
+
11
+ def ipv6_enabled?
12
+ require 'socket'
13
+
14
+ begin
15
+ TCPServer.open("::1", 0)
16
+ true
17
+ rescue
18
+ false
19
+ end
20
+ end
21
+
22
+ class DnsServiceDiscoveryTest < ::Test::Unit::TestCase
23
+ def parse_config(conf)
24
+ Fluent::Config.parse(conf, "(test)", "(test_dir)", true)
25
+ end
26
+
27
+ def config(param: nil, af: 'ipv4')
28
+ %[
29
+ @type dns
30
+ <entry>
31
+ host localhost
32
+ port 80
33
+ address_family #{af}
34
+ #{param}
35
+ </entry>
36
+ ]
37
+ end
38
+
39
+ setup do
40
+ @sd_dns = Fluent::Plugin::DnsServiceDiscovery.new
41
+ end
42
+
43
+ sub_test_case '#configure with IPv4' do
44
+ test 'set services' do
45
+ @sd_dns.configure(parse_config(config))
46
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '127.0.0.1', 80, '0-localhost', 60, false, '', '', nil), @sd_dns.services[0]
47
+ end
48
+
49
+ test 'set services with auth settings' do
50
+ @sd_dns.configure(parse_config(config(param: %[
51
+ shared_key test_key
52
+ username test_user
53
+ password test_pass
54
+ ])))
55
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '127.0.0.1', 80, '0-localhost', 60, false, 'test_user', 'test_pass', 'test_key'), @sd_dns.services[0]
56
+ end
57
+
58
+ test 'set services with multiple <entry>' do
59
+ @sd_dns.configure(parse_config(%[
60
+ @type dns
61
+ <entry>
62
+ host localhost
63
+ port 80
64
+ address_family ipv4
65
+ </entry>
66
+ <entry>
67
+ host localhost
68
+ port 81
69
+ address_family ipv4
70
+ </entry>
71
+ <entry>
72
+ host localhost
73
+ port 82
74
+ address_family ipv4
75
+ </entry>
76
+ ]))
77
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '127.0.0.1', 80, '0-localhost', 60, false, '', '', nil), @sd_dns.services[0]
78
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '127.0.0.1', 81, '0-localhost', 60, false, '', '', nil), @sd_dns.services[1]
79
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '127.0.0.1', 82, '0-localhost', 60, false, '', '', nil), @sd_dns.services[2]
80
+ end
81
+
82
+ test 'raise an error for no <entry>' do
83
+ assert_raise(Fluent::ConfigError) {
84
+ @sd_dns.configure(parse_config(%[@type dns]))
85
+ }
86
+ end
87
+ end
88
+
89
+ sub_test_case '#configure with IPv6 enabled' do
90
+ test 'set services' do
91
+ omit "IPv6 is not supported on this environment" unless ipv6_enabled?
92
+
93
+ @sd_dns.configure(parse_config(config(af: 'ipv6')))
94
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '::1', 80, '0-localhost', 60, false, '', '', nil), @sd_dns.services[0]
95
+ end
96
+
97
+ test 'set services with unspecified address family' do
98
+ omit "IPv6 is not supported on this environment" unless ipv6_enabled?
99
+
100
+ @sd_dns.configure(parse_config(%[
101
+ @type dns
102
+ <entry>
103
+ host localhost
104
+ port 80
105
+ </entry>
106
+ ]))
107
+ assert_equal 2, @sd_dns.services.size
108
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '::1', 80, '0-localhost', 60, false, '', '', nil), @sd_dns.services[0]
109
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:dns, '127.0.0.1', 80, '1-localhost', 60, false, '', '', nil), @sd_dns.services[1]
110
+ end
111
+ end
112
+
113
+ sub_test_case 'failure cases' do
114
+ test 'getaddrinfo raises an error' do
115
+ stub(Socket).getaddrinfo('localhost', 80, Socket::Constants::AF_INET, Socket::SOCK_STREAM) { raise "Failed for test!" }
116
+
117
+ assert_raise(RuntimeError.new("Failed for test!")) {
118
+ @sd_dns.configure(parse_config(config))
119
+ }
120
+ assert_empty @sd_dns.services
121
+ end
122
+
123
+ test 'getaddrinfo raises an error with ignore_dns_error true' do
124
+ stub(Socket).getaddrinfo('localhost', 80, Socket::Constants::AF_INET, Socket::SOCK_STREAM) { raise "Failed for test!" }
125
+
126
+ assert_nothing_raised {
127
+ @sd_dns.configure(parse_config(%[
128
+ @type dns
129
+ ignore_dns_error true
130
+ <entry>
131
+ host localhost
132
+ port 80
133
+ address_family ipv4
134
+ </entry>
135
+ ]))
136
+ }
137
+ assert_empty @sd_dns.services
138
+ assert { @sd_dns.log.out.logs.any? { |log| log.include?('failed to get service list from localhost') }}
139
+ end
140
+ end
141
+
142
+ sub_test_case '#start' do
143
+ module TestTimerEventHelperWrapper
144
+ # easy to control statsevent
145
+ def timer_execute(_name, _interval, &block)
146
+ @test_timer_event_helper_wrapper_context = Fiber.new do
147
+ loop do
148
+ block.call
149
+
150
+ if Fiber.yield == :finish
151
+ break
152
+ end
153
+ end
154
+ end
155
+
156
+ resume
157
+ end
158
+
159
+ def resume
160
+ @test_timer_event_helper_wrapper_context.resume(:resume)
161
+ end
162
+
163
+ def shutdown
164
+ super
165
+
166
+ if @test_timer_event_helper_wrapper_context
167
+ @test_timer_event_helper_wrapper_context.resume(:finish)
168
+ end
169
+ end
170
+ end
171
+
172
+ def config_with_interval
173
+ %[
174
+ @type dns
175
+ interval 10s
176
+ <entry>
177
+ host localhost
178
+ port 80
179
+ address_family ipv4
180
+ </entry>
181
+ ]
182
+ end
183
+
184
+ teardown do
185
+ if @sd_dns
186
+ @sd_dns.stop unless @sd_dns.stopped?
187
+ @sd_dns.before_shutdown unless @sd_dns.before_shutdown?
188
+ @sd_dns.shutdown unless @sd_dns.shutdown?
189
+ @sd_dns.after_shutdown unless @sd_dns.after_shutdown?
190
+ @sd_dns.close unless @sd_dns.closed?
191
+ @sd_dns.terminate unless @sd_dns.terminated?
192
+ end
193
+ end
194
+
195
+ test 'Skip if service list are not updated' do
196
+ @sd_dns.extend(TestTimerEventHelperWrapper)
197
+ @sd_dns.configure(parse_config(config_with_interval))
198
+ queue = []
199
+
200
+ @sd_dns.start(queue)
201
+ assert_empty queue
202
+
203
+ @sd_dns.resume
204
+ assert_empty queue
205
+ end
206
+
207
+ test 'if service list is changed, service_in and service_out event happen' do
208
+ stub(Socket).getaddrinfo('localhost', 80, Socket::Constants::AF_INET, Socket::SOCK_STREAM) {
209
+ [["AF_INET", 80, "localhost", "127.0.0.1", 2, 1, 6]]
210
+ }
211
+ @sd_dns.extend(TestTimerEventHelperWrapper)
212
+ @sd_dns.configure(parse_config(config_with_interval))
213
+
214
+ stub(Socket).getaddrinfo('localhost', 80, Socket::Constants::AF_INET, Socket::SOCK_STREAM) {
215
+ [["AF_INET", 90, "localhost", "192.168.0.1", 2, 1, 6]]
216
+ }
217
+ queue = []
218
+ @sd_dns.start(queue)
219
+
220
+ join = queue.shift
221
+ assert_equal Fluent::Plugin::ServiceDiscovery::SERVICE_IN, join.type
222
+ assert_equal 90, join.service.port
223
+ assert_equal '192.168.0.1', join.service.host
224
+
225
+ drain = queue.shift
226
+ assert_equal Fluent::Plugin::ServiceDiscovery::SERVICE_OUT, drain.type
227
+ assert_equal 80, drain.service.port
228
+ assert_equal '127.0.0.1', drain.service.host
229
+ end
230
+ end
231
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-sd-dns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masahiro Nakagawa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: flexmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit-rr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ description: DNS based service discovery plugin for Fluentd
98
+ email: repeatedly@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - ".travis.yml"
105
+ - ChangeLog
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - fluent-plugin-sd-dns.gemspec
111
+ - lib/fluent/plugin/sd_dns.rb
112
+ - test/plugin/test_sd_dns.rb
113
+ homepage: https://github.com/fluent/fluent-plugin-sd-dns
114
+ licenses:
115
+ - Apache-2.0
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.0.3
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: DNS based service discovery plugin for Fluentd
136
+ test_files:
137
+ - test/plugin/test_sd_dns.rb