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

add possibility to manage network interfaces with a template #497

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

trefzer
Copy link
Contributor

@trefzer trefzer commented Nov 12, 2024

This gives the possibility to manage all network/netdev/link settings from hashes.

I'm aware of pull request #340 from @rwaffen which started an equal solution last year.
Since the development there does not seem to go on, I started this solution which currrently
gives us the opportunitiy to create any configuration stated in the systemd documentation.

All type defintions are taken out of systemd documentation (not documented, not in our type defintion).
I'm aware, that some types could defined more accurate, but I tried to get a compromise inbetween accurate type and
workload ;).

@trefzer trefzer force-pushed the dev_network branch 2 times, most recently from 979abd5 to 16dd60c Compare November 12, 2024 15:36
Comment on lines +85 to +87
fname => "${_filename}.link",
config => deep_merge(pick($link_profiles[$interface_name], {}), $interface['link']),
}),
Copy link
Member

Choose a reason for hiding this comment

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

Better to put the closing braces on separate lines to avoid the need for double indentation:

Suggested change
fname => "${_filename}.link",
config => deep_merge(pick($link_profiles[$interface_name], {}), $interface['link']),
}),
fname => "${_filename}.link",
config => deep_merge(pick($link_profiles[$interface_name], {}), $interface['link']),
},
),

Copy link
Contributor 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 that this makes the code better readable. I will not change that for now.

Copy link
Member

Choose a reason for hiding this comment

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

It does. The double indentation that you have to do currently is really just a bug in puppet-lint, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no the double intentation is correct since there are to brackets to intent.
to clarify:
my solution:

epp('template, {
    var => val,
})

your solution:

epp('template,
 {
    var => val,
 }
)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, IMO it is easier to follow the indentation when there is only at most one level of change between each line (your example has var => val, indented one level too far in my solution), easier to match up the opening and closing braces, and reduces diffs when adding and removing items to the comma-separated lists by already having separate lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok validatio now fails, the it needs 12 spaces, no way to lower that.
I will remove the last commit tomorrow. Think we can resolv this.

Copy link
Member

Choose a reason for hiding this comment

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

Right, it's clearly not correct to have the closing brace on the same line as the content inside of the brace. You'd have to also move the opening brace and fix the indentation.

Copy link
Member

Choose a reason for hiding this comment

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

my suggestion:

      $data = { fname  => "${_filename}.link", config => deep_merge(pick($link_profiles[$interface_name], {}), $interface['link'])}
      systemd::network { "${_filename}.link":
        path    => $path,
        content => epp('systemd/network.epp', $data),
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bastelfreak I am not convinced that this is a better solution. Adding additional variables does not seem to be necessary as the line for config is not too long.

In my opinion, my suggestion is still the most readable, although the double indentation created by the lint logic is kind of strange. But in the end it makes sense (the lint logic).

Copy link
Member

Choose a reason for hiding this comment

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

Personally I prefer to keep each brace in a separate line.
Yes, more lines of code, but better understanding, why indentation is needed.

$interfaces.each | String[1] $interface_name, Systemd::Interface $interface | {
  $_filename=pick($interface['filename'], $interface_name)
  if 'link' in $interface.keys() {
    systemd::network { "${_filename}.link":
      path    => $path,
      content => epp('systemd/network.epp',
        {
          fname  => "${_filename}.link",
          config => deep_merge(pick($link_profiles[$interface_name], {}), $interface['link']),
        }
      ),
    }
  }
}

manifests/networkd.pp Outdated Show resolved Hide resolved
manifests/networkd.pp Outdated Show resolved Hide resolved
manifests/networkd.pp Outdated Show resolved Hide resolved
manifests/networkd.pp Outdated Show resolved Hide resolved
manifests/networkd.pp Show resolved Hide resolved
manifests/networkd.pp Outdated Show resolved Hide resolved
@@ -0,0 +1,26 @@
<%- | String[1] $fname = {},
Copy link
Contributor

@traylenator traylenator Jan 19, 2025

Choose a reason for hiding this comment

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

I've the same comment as the last one - #340 (comment)

Why a new template here.

The existing template just needs a another two lines to support each of the [Match] and [Link] sections
and new types can be added Systemd::Unit::Match and Systemd::Unit::Link

Once that is done it may make sense to create a systemd::network that uses these but if not done this way you loose all
the basics of managing a say a drop in and creating a validated file, .link files would become some special unit file and they are not.

I would say just do an equivalent MR as #502 was done for .swap units.

A .link file is just another unit file.

Copy link
Contributor

@traylenator traylenator Jan 19, 2025

Choose a reason for hiding this comment

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

Something like this - #513

which is a skeleton and the great work on the types here could be moved over to that structure to be consistent with the other units.

@@ -0,0 +1,84 @@
# interface definition
type Systemd::Interface::Link::Link = Struct[{
'Description' => Optional[String[1]],
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'Description' => Optional[String[1]],
Optional['Description'] => String[1],

Does this stop a massive hash of unset values being added to the catalogue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh interesting did not realize this difference yet. Do you have more information about the difference ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have a reference.

One says the key does not have to be present in the hash at all.

The other adds the key with a default value or undef.
Would have to check the catalogue to see if there is a difference.

Copy link
Contributor Author

@trefzer trefzer Jan 19, 2025

Choose a reason for hiding this comment

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

Theres no undef in the catalog either way.
Adding the following type:

type Test::Test = Struct[{
    'always'        => String[1],
    'one'           => Optional[String[1]],
    Optional['two'] => String[1],
}]

Using it in a class:
Test::Test $te = { 'always' => '2' },

Renders in the catalog to:

- type: Class
  ....
  parameters:
    te:
      always: '2'

Neither one nor two key of the hash is in the catalog.
So I assume there is no difference in the result beside the writing in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants