Skip to content

Commit

Permalink
Add support for GRPC check
Browse files Browse the repository at this point in the history
Signed-off-by: Wilfried Roset <wilfriedroset@users.noreply.github.com>
  • Loading branch information
wilfriedroset committed Nov 28, 2023
1 parent db25ec4 commit 4d7a002
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/puppet/functions/consul/validate_checks.rb
Expand Up @@ -14,20 +14,22 @@ def validate_checks(obj)
validate_checks(c)
end
when Hash
raise Puppet::ParseError, 'interval must be defined for tcp, http, and script checks' if (obj.key?('http') || (obj.key?('script') || obj.key?('args')) || obj.key?('tcp')) && !obj.key?('interval')
raise Puppet::ParseError, 'interval must be defined for tcp, http, grpc and script checks' if (obj.key?('http') || (obj.key?('script') || obj.key?('args')) || obj.key?('tcp') || obj.key?('grpc')) && !obj.key?('interval')

if obj.key?('ttl')
raise Puppet::ParseError, 'script, http, tcp, and interval must not be defined for ttl checks' if obj.key?('http') || (obj.key?('args') || obj.key?('script')) || obj.key?('tcp') || obj.key?('interval')
raise Puppet::ParseError, 'script, http, tcp, grpc and interval must not be defined for ttl checks' if obj.key?('http') || (obj.key?('args') || obj.key?('script')) || obj.key?('tcp') || obj.key?('grpc') && obj.key?('interval')
elsif obj.key?('http')
raise Puppet::ParseError, 'script and tcp must not be defined for http checks' if (obj.key?('args') || obj.key?('script')) || obj.key?('tcp')
raise Puppet::ParseError, 'script, tcp and grpc must not be defined for http checks' if (obj.key?('args') || obj.key?('script')) || obj.key?('tcp') || obj.key?('grpc')
elsif obj.key?('grpc')
raise Puppet::ParseError, 'script, tcp and http must not be defined for grpc checks' if (obj.key?('args') || obj.key?('script')) || obj.key?('tcp') || obj.key?('http')
elsif obj.key?('tcp')
raise Puppet::ParseError, 'script and http must not be defined for tcp checks' if obj.key?('http') || (obj.key?('args') || obj.key?('script'))
raise Puppet::ParseError, 'script, http and grpc must not be defined for tcp checks' if obj.key?('http') || (obj.key?('args') || obj.key?('script')) || obj.key?('grpc')
elsif obj.key?('args') || obj.key?('script')
raise Puppet::ParseError, 'http and tcp must not be defined for script checks' if obj.key?('http') || obj.key?('tcp')
raise Puppet::ParseError, 'http, grpc and tcp must not be defined for script checks' if obj.key?('http') || obj.key?('tcp') || obj.key?('grpc')
elsif obj.key?('alias_service')
raise Puppet::ParseError, 'alias_service must not define http, tcp, args, script, or interval' if obj.key?('http') || obj.key?('tcp') || obj.key?('args') || obj.key?('script') || obj.key?('interval')
raise Puppet::ParseError, 'alias_service must not define http, tcp, grpc, args, script, or interval' if obj.key?('http') || obj.key?('tcp') || obj.key?('args') || obj.key?('script') || obj.key?('interval') || obj.key?('grpc')
else
raise Puppet::ParseError, 'One of ttl, script, tcp, or http must be defined.'
raise Puppet::ParseError, 'One of ttl, script, tcp, grpc or http must be defined.'
end
else
raise Puppet::ParseError, 'Unable to handle object of type <%s>' % obj.class.to_s
Expand Down
2 changes: 2 additions & 0 deletions manifests/check.pp
Expand Up @@ -32,6 +32,7 @@
$service_id = undef,
$status = undef,
$tcp = undef,
$grpc = undef,
$timeout = undef,
$token = undef,
$ttl = undef,
Expand All @@ -48,6 +49,7 @@
'script' => $script,
'args' => $args,
'tcp' => $tcp,
'grpc' => $grpc,
'interval' => $interval,
'timeout' => $timeout,
'service_id' => $service_id,
Expand Down
40 changes: 40 additions & 0 deletions spec/defines/consul_check_spec.rb
Expand Up @@ -414,6 +414,46 @@
with_content(%r{"id" *: *"aa/bb/cc"})
}
end
describe 'with grpc' do
let(:params) {{
'grpc' => 'localhost:4244'
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/) \
.with_content(/"name" *: *"my_check"/) \
.with_content(/"check" *: *\{/) \
.with_content(/"grpc" *: *"localhost:4244"/)
}
end
describe 'with grpc with tls' do
let(:params) {{
'grpc' => 'localhost:4244',
'grpc_use_tls' => true
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/) \
.with_content(/"name" *: *"my_check"/) \
.with_content(/"check" *: *\{/) \
.with_content(/"grpc" *: *"localhost:4244"/) \
.with_content(/"grpc_use_tls" *: *"true"/)
}
end
describe 'with grpc with interval' do
let(:params) {{
'grpc' => 'localhost:4244',
'interval' => '10s'
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/) \
.with_content(/"name" *: *"my_check"/) \
.with_content(/"check" *: *\{/) \
.with_content(/"grpc" *: *"localhost:4244"/) \
.with_content(/"interval" *: *"10s"/)
}
end
end
end
end

0 comments on commit 4d7a002

Please sign in to comment.