Skip to content

Commit

Permalink
support shard_key, username, password
Browse files Browse the repository at this point in the history
Added descs to all config param

Signed-off-by: Yuta Iwama <[email protected]>
  • Loading branch information
ganmacs committed Mar 16, 2020
1 parent e7d82e0 commit 93ca09e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
14 changes: 13 additions & 1 deletion lib/fluent/plugin/sd_srv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ class SrvServiceDiscovery < ServiceDiscovery

helpers :timer

desc 'Service without underscore in RFC2782'
config_param :service, :string
desc 'Proto without underscore in RFC2782'
config_param :proto, :string, default: 'tcp'
desc 'Name without underscore in RFC2782'
config_param :hostname, :string
desc 'hostname of DNS server to request the SRV record'
config_param :dns_server_host, :string, default: nil
desc 'interval of requesting to DNS server'
config_param :interval, :integer, default: 60
desc "resolve hostname to IP addr of SRV's Target"
config_param :dns_lookup, :bool, default: true
desc 'The shared key per server'
config_param :shared_key, :string, default: nil, secret: true
desc 'The username for authentication'
config_param :username, :string, default: ''
desc 'The password for authentication'
config_param :password, :string, default: '', secret: true

def initialize
super
Expand Down Expand Up @@ -107,7 +119,7 @@ def fetch_srv_record
host = @dns_lookup ? dns_lookup!(addr.target) : addr.target
services << [
addr.priority,
Service.new(:srv, host.to_s, addr.port.to_i, addr.target.to_s, addr.weight, false, nil, nil, nil)
Service.new(:srv, host.to_s, addr.port.to_i, addr.target.to_s, addr.weight, false, @username, @password, @shared_key)
]
end

Expand Down
24 changes: 12 additions & 12 deletions test/plugin/test_sd_srv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ class SrvServiceDiscoveryTest < ::Test::Unit::TestCase
mock(Resolv::DNS).new { flexmock('dns_resolver', getresources: [SRV_RECORD2, SRV_RECORD1], getaddress: '127.0.0.1') }

sdf.configure(config_element('service_discovery', '', { 'service' => 'service1', 'hostname' => 'example.com' }))
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, nil, nil, nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, nil, nil, nil), sdf.services[1]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, '', '', nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, '', '', nil), sdf.services[1]
end

test 'reuturn host name without revolving name when dns_lookup is flase' do
sdf = Fluent::Plugin::SrvServiceDiscovery.new
mock(Resolv::DNS).new { flexmock('dns_resolver', getresources: [SRV_RECORD1, SRV_RECORD2], getaddress: '127.0.0.1') }

sdf.configure(config_element('service_discovery', '', { 'service' => 'service1', 'hostname' => 'example.com', 'dns_lookup' => false }))
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, 'service1.example.com', 8081, 'service1.example.com', 10, false, nil, nil, nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, 'service2.example.com', 8082, 'service2.example.com', 20, false, nil, nil, nil), sdf.services[1]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, 'service1.example.com', 8081, 'service1.example.com', 10, false, '', '', nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, 'service2.example.com', 8082, 'service2.example.com', 20, false, '', '', nil), sdf.services[1]
end

test 'pass a value as :nameserver to Resolve::DNS when dns_server_host is given' do
sdf = Fluent::Plugin::SrvServiceDiscovery.new
mock(Resolv::DNS).new(nameserver: '8.8.8.8') { flexmock('dns_resolver', getresources: [SRV_RECORD1, SRV_RECORD2], getaddress: '127.0.0.1') }

sdf.configure(config_element('service_discovery', '', { 'service' => 'service1', 'hostname' => 'example.com', 'dns_server_host' => '8.8.8.8' }))
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, nil, nil, nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, nil, nil, nil), sdf.services[1]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, '', '', nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, '', '', nil), sdf.services[1]
end

test 'pass a value as :nameserver_port to Resolve::DNS when dns_server_host has port' do
sdf = Fluent::Plugin::SrvServiceDiscovery.new
mock(Resolv::DNS).new(nameserver_port: [['8.8.8.8', 8080]]) { flexmock('dns_resolver', getresources: [SRV_RECORD1, SRV_RECORD2], getaddress: '127.0.0.1') }

sdf.configure(config_element('service_discovery', '', { 'service' => 'service1', 'hostname' => 'example.com', 'dns_server_host' => '8.8.8.8:8080' }))
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, nil, nil, nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, nil, nil, nil), sdf.services[1]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, '', '', nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, '', '', nil), sdf.services[1]
end

test 'target follows RFC2782' do
Expand All @@ -54,8 +54,8 @@ class SrvServiceDiscoveryTest < ::Test::Unit::TestCase

mock(Resolv::DNS).new { mock }
sdf.configure(config_element('service_discovery', '', { 'service' => 'service1', 'hostname' => 'example.com' }))
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, nil, nil, nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, nil, nil, nil), sdf.services[1]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, '', '', nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, '', '', nil), sdf.services[1]
end

test 'can change protocol' do
Expand All @@ -67,8 +67,8 @@ class SrvServiceDiscoveryTest < ::Test::Unit::TestCase

mock(Resolv::DNS).new { mock }
sdf.configure(config_element('service_discovery', '', { 'service' => 'service1', 'hostname' => 'example.com', 'proto' => 'udp' }))
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, nil, nil, nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, nil, nil, nil), sdf.services[1]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8081, 'service1.example.com', 10, false, '', '', nil), sdf.services[0]
assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:srv, '127.0.0.1', 8082, 'service2.example.com', 20, false, '', '', nil), sdf.services[1]
end
end

Expand Down

0 comments on commit 93ca09e

Please sign in to comment.