Skip to content
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

Merged

Conversation

alexjfisher
Copy link
Member

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 at
the expense of several extra lines of template.

@alexjfisher alexjfisher force-pushed the restore_servers_and_pools_behaviour branch from bb6a43b to c2adfde Compare January 4, 2021 15:19
@alexjfisher
Copy link
Member Author

This should also get rid of the issue with possible unwanted trailing spaces as described in #94

Copy link
Member

@ekohl ekohl left a 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 Show resolved Hide resolved
<% } -%>
<% } -%>
<% if $chrony::pools.is_a(Hash) { -%>
<% $chrony::pools.keys.sort.each |$pool| { -%>
Copy link
Member

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):

Suggested change
<% $chrony::pools.keys.sort.each |$pool| { -%>
<% $chrony::pools.sort.each |$pool, $pool_options| { -%>

Copy link
Member Author

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)

manifests/init.pp Outdated Show resolved Hide resolved
@alexjfisher alexjfisher force-pushed the restore_servers_and_pools_behaviour branch from c2adfde to 2b78b11 Compare January 4, 2021 15:45
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.
@alexjfisher alexjfisher force-pushed the restore_servers_and_pools_behaviour branch from 2b78b11 to 58b35e7 Compare January 4, 2021 15:52
@alexjfisher
Copy link
Member Author

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?

I've made that change, and I think it's worked out ok.

@alexjfisher alexjfisher force-pushed the restore_servers_and_pools_behaviour branch from b4539e8 to 9e64c25 Compare January 4, 2021 16:32
if $servers.is_a(Hash) {
$servers
} else {
$servers.reduce({}) |$memo, $server| { # lint:ignore:manifest_whitespace_opening_brace_before
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexjfisher alexjfisher requested a review from ekohl January 4, 2021 16:48
manifests/init.pp Outdated Show resolved Hide resolved
Comment on lines 48 to 50
servers => $servers.chrony::server_array_to_hash(['iburst']),
pools => $pools.chrony::server_array_to_hash(['iburst']),
peers => $peers.chrony::server_array_to_hash,
Copy link
Member

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?

Suggested change
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),

Copy link
Member Author

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)
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

<% $chrony::peers.each |$peer| { -%>
peer <%= $peer.flatten.join(' ') %>
<% $peers.keys.sort.each |$peer| { -%>
<% if $peers[$peer] and !$peers[$peer].empty { -%>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same?

Suggested change
<% 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 {}.

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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: []

Copy link
Member Author

@alexjfisher alexjfisher Jan 4, 2021

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.
@alexjfisher alexjfisher force-pushed the restore_servers_and_pools_behaviour branch from 9e64c25 to 9fff7a6 Compare January 4, 2021 17:53
Used by `servers`, `pools`, and `peers`, it allows either an array of
hosts, or a hash of hosts with their respective options.
@alexjfisher alexjfisher requested a review from ekohl January 4, 2021 18:50
Copy link
Member

@ekohl ekohl left a 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
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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.
Copy link
Member

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.

Copy link
Member Author

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 { -%>
Copy link
Member

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:

Suggested change
<% if $servers[$server].empty { -%>
<% if $servers[$server].empty() { -%>

Copy link
Member Author

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[
Copy link
Member

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?

Copy link
Member Author

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"...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexjfisher alexjfisher merged commit 21f0250 into voxpupuli:master Jan 5, 2021
@alexjfisher alexjfisher added the bug Something isn't working label Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants