Skip to content

Commit

Permalink
Merge pull request #648 from wilfriedroset/grpc
Browse files Browse the repository at this point in the history
Add support for GRPC check
  • Loading branch information
rwaffen committed Dec 13, 2023
2 parents db25ec4 + 216d2a7 commit 562d009
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
9 changes: 9 additions & 0 deletions REFERENCE.md
Expand Up @@ -615,6 +615,7 @@ The following parameters are available in the `consul::check` defined type:
* [`service_id`](#-consul--check--service_id)
* [`status`](#-consul--check--status)
* [`tcp`](#-consul--check--tcp)
* [`grpc`](#-consul--check--grpc)
* [`timeout`](#-consul--check--timeout)
* [`token`](#-consul--check--token)
* [`ttl`](#-consul--check--ttl)
Expand Down Expand Up @@ -703,6 +704,14 @@ The IP/hostname and port for the service healthcheck. Should be in 'hostname:por

Default value: `undef`

##### <a name="-consul--check--grpc"></a>`grpc`

Data type: `Any`

GRPC endpoint for the service healthcheck

Default value: `undef`

##### <a name="-consul--check--timeout"></a>`timeout`

Data type: `Any`
Expand Down
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
3 changes: 3 additions & 0 deletions manifests/check.pp
Expand Up @@ -15,6 +15,7 @@
# @param service_id An optional service_id to match this check against
# @param status The default state of the check when it is registered against a consul agent. Should be either "critical" or "passing"
# @param tcp The IP/hostname and port for the service healthcheck. Should be in 'hostname:port' format.
# @param grpc GRPC endpoint for the service healthcheck
# @param timeout A timeout value for HTTP request only
# @param token ACL token for interacting with the catalog (must be 'management' type)
# @param ttl Value in seconds before the http endpoint considers a failing healthcheck to be "HARD" down.
Expand All @@ -32,6 +33,7 @@
$service_id = undef,
$status = undef,
$tcp = undef,
$grpc = undef,
$timeout = undef,
$token = undef,
$ttl = undef,
Expand All @@ -48,6 +50,7 @@
'script' => $script,
'args' => $args,
'tcp' => $tcp,
'grpc' => $grpc,
'interval' => $interval,
'timeout' => $timeout,
'service_id' => $service_id,
Expand Down
51 changes: 43 additions & 8 deletions spec/defines/consul_check_spec.rb
Expand Up @@ -240,6 +240,41 @@
}
end

describe 'with grpc' do
let(:params) do
{
'grpc' => 'localhost:80',
'interval' => '30s',
}
end

it {
is_expected.to contain_file('/etc/consul/check_my_check.json'). \
with_content(%r{"id" *: *"my_check"}). \
with_content(%r{"name" *: *"my_check"}). \
with_content(%r{"check" *: *\{}). \
with_content(%r{"grpc" *: *"localhost:80"})
}
end

describe 'with grpc with interval' do
let(:params) do
{
'grpc' => 'localhost:80',
'interval' => '30s',
}
end

it {
is_expected.to contain_file('/etc/consul/check_my_check.json'). \
with_content(%r{"id" *: *"my_check"}). \
with_content(%r{"name" *: *"my_check"}). \
with_content(%r{"check" *: *\{}). \
with_content(%r{"grpc" *: *"localhost:80"}). \
with_content(%r{"interval" *: *"30s"})
}
end

describe 'with script and service_id' do
let(:params) do
{
Expand Down Expand Up @@ -289,7 +324,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, and interval must not be defined for ttl checks})
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, grpc and interval must not be defined for ttl checks})
}
end

Expand All @@ -303,7 +338,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, and interval must not be defined for ttl checks})
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, grpc and interval must not be defined for ttl checks})
}
end

Expand All @@ -317,7 +352,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, and interval must not be defined for ttl checks})
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, grpc and interval must not be defined for ttl checks})
}
end

Expand All @@ -331,7 +366,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, and interval must not be defined for ttl checks})
is_expected.to raise_error(Puppet::Error, %r{script, http, tcp, grpc and interval must not be defined for ttl checks})
}
end

Expand All @@ -345,7 +380,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{script and tcp must not be defined for http checks})
is_expected.to raise_error(Puppet::Error, %r{script, tcp and grpc must not be defined for http checks})
}
end

Expand All @@ -357,7 +392,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{interval must be defined for tcp, http, and script checks})
is_expected.to raise_error(Puppet::Error, %r{interval must be defined for tcp, http, grpc and script checks})
}
end

Expand All @@ -369,7 +404,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{interval must be defined for tcp, http, and script checks})
is_expected.to raise_error(Puppet::Error, %r{interval must be defined for tcp, http, grpc and script checks})
}
end

Expand All @@ -381,7 +416,7 @@
end

it {
is_expected.to raise_error(Puppet::Error, %r{interval must be defined for tcp, http, and script checks})
is_expected.to raise_error(Puppet::Error, %r{interval must be defined for tcp, http, grpc and script checks})
}
end

Expand Down

0 comments on commit 562d009

Please sign in to comment.