-
-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore behaviour of servers
and pools
parameters
#103
Restore behaviour of servers
and pools
parameters
#103
Conversation
bb6a43b
to
c2adfde
Compare
This should also get rid of the issue with possible unwanted trailing spaces as described in #94 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to normalize the array to a hash in a Puppet function and pass that to the template instead to keep it simpler?
templates/chrony.conf.epp
Outdated
<% } -%> | ||
<% } -%> | ||
<% if $chrony::pools.is_a(Hash) { -%> | ||
<% $chrony::pools.keys.sort.each |$pool| { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you use this (untested):
<% $chrony::pools.keys.sort.each |$pool| { -%> | |
<% $chrony::pools.sort.each |$pool, $pool_options| { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not.
Signature 1
sort(String $string_value, Optional[Callable[2,2]] &$block)
Signature 2
sort(Array $array_value, Optional[Callable[2,2]] &$block)
c2adfde
to
2b78b11
Compare
Before the conversion to epp in voxpupuli#79, an array of servers/pools would get the `iburst` option set for each server/pool. This restores the old behaviour and gets rid of the `flatten` magic at the expense of several extra lines of template.
2b78b11
to
58b35e7
Compare
I've made that change, and I think it's worked out ok. |
b4539e8
to
9e64c25
Compare
if $servers.is_a(Hash) { | ||
$servers | ||
} else { | ||
$servers.reduce({}) |$memo, $server| { # lint:ignore:manifest_whitespace_opening_brace_before |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a comment to voxpupuli/puppet-lint-manifest_whitespace-check#8
manifests/config.pp
Outdated
servers => $servers.chrony::server_array_to_hash(['iburst']), | ||
pools => $pools.chrony::server_array_to_hash(['iburst']), | ||
peers => $peers.chrony::server_array_to_hash, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this syntax odd. Doesn't this also work?
servers => $servers.chrony::server_array_to_hash(['iburst']), | |
pools => $pools.chrony::server_array_to_hash(['iburst']), | |
peers => $peers.chrony::server_array_to_hash, | |
servers => chrony::server_array_to_hash($servers, ['iburst']), | |
pools => chrony::server_array_to_hash($pools, ['iburst']), | |
peers => chrony::server_array_to_hash($peers), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess for some functions it reads better (eg. reduce
with an optional start value), but here, I think you're right. I've updated it.
'server 2.pool.ntp.org iburst', | ||
'server 3.pool.ntp.org iburst' | ||
] | ||
expect(config_file_contents.split("\n") & expected_lines).to eq(expected_lines) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this also make sense?
it do
is_expected.to contain_file(config_file).
with_content(/server 0.pool.ntp.org iburst/).
with_content(/server 1.pool.ntp.org iburst/).
with_content(/server 2.pool.ntp.org iburst/).
with_content(/server 3.pool.ntp.org iburst/)
end
I think it does essentially the same, but feels more native.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm specifically testing the lines have been sorted and appear in the correct order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've had some issues with & expected_lines
in the past. For example, when I wanted to match something that showed up multiple times (like }
in a block). https://github.com/rspec/rspec-expectations#collection-membership isn't very satisfying either.
Another is to have a large text block:
let(:expected_content) do
<<~CONTENT
server ...
CONTENT
end
it { is_expected.to contain_file(config_file).with_content(Regex.new(Regex.escape(expected_content))) }
No idea if it works, but I'm wondering what a good solution is here with voxpupuli/voxpupuli-test#14 in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left this as is for now. I think the shared matchers will be really useful for getting a consistent approach in the future though.
templates/chrony.conf.epp
Outdated
<% $chrony::peers.each |$peer| { -%> | ||
peer <%= $peer.flatten.join(' ') %> | ||
<% $peers.keys.sort.each |$peer| { -%> | ||
<% if $peers[$peer] and !$peers[$peer].empty { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the same?
<% if $peers[$peer] and !$peers[$peer].empty { -%> | |
<% if !empty($peers[$peer]) { -%> |
If so, I would probably reverse the conditions so it becomes if empty() {} else {}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you can call empty on undef
though? I'll double check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://puppet.com/docs/puppet/6.4/function.html#empty states:
For backwards compatibility with the stdlib function with the same name the following data types are also accepted by the function instead of raising an error. Using these is deprecated and will raise a warning:
Numeric - false is returned for all Numeric values.
Undef - true is returned for all Undef values.
So yes, you can call it but you probably shouldn't is the answer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with a warning (and I've already made the change!). I don't think many users will encounter this and TBH, supporting undef
is a corner case that's easily avoided by the user. eg the hiera in #94 (comment)
could be changed to
chrony::servers:
ntp1:
- minpoll 7
ntp2: []
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
puppetlabs/puppet#6776 (comment) ;)
The deprecation is removed in this PR.
Simpify the epp template by using a private helper function to normalise arrays into hashes for the `servers`, `pools`, and `peers` parameters.
9e64c25
to
9fff7a6
Compare
Used by `servers`, `pools`, and `peers`, it allows either an array of hosts, or a hash of hosts with their respective options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some questions inline, but overall 👍 and wouldn't mind merging it as is.
@@ -17,6 +17,19 @@ | |||
* `chrony::params`: chrony class parameters | |||
* `chrony::service`: Manages the chrony service | |||
|
|||
### Functions | |||
|
|||
#### Public Functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps worth a bug report to puppet-strings that it shouldn't output this header if there are no public functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened puppetlabs/puppet-strings#267
|
||
This selects the servers to use for NTP servers. It can be an array of servers | ||
or a hash of servers to their respective options. | ||
or a hash of servers to their respective options. If an array is used, `iburst` will be configured for each server. | ||
If you don't want to use `iburst`, use a hash instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps an @example
is useful for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
<% $chrony::servers.each |$server| { -%> | ||
server <%= $server.flatten.join(' ') %> | ||
<% $servers.keys.sort.each |$server| { -%> | ||
<% if $servers[$server].empty { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lack of knowledge on my side, but does this work or should it be:
<% if $servers[$server].empty { -%> | |
<% if $servers[$server].empty() { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are fine. If using parenthesis, I'd have probably written if empty($servers[$server])
, but I quite like the first form.
@@ -0,0 +1,4 @@ | |||
type Chrony::Servers = Variant[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some documentation that makes sense for this type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I'll add that. Puppet-strings style-guide wrongly states "However, Strings does not generate information for type aliases or facts"...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before the conversion to epp in
#79, an array of
servers/pools would get the
iburst
option set for each server/pool.This restores the old behaviour and gets rid of the
flatten
magic atthe expense of several extra lines of template.