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

ncm-network: provide backward compatibility for vlan interface config for nmstate. #1667

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,31 @@ sub generate_vip_config {
return $iface_cfg;
}

# private sub to extract vlan id from name
# i.e given eth0.123 return 123
sub _get_vlan_id {
my ($self, $name) = @_;
return $1 if ($name =~ m/\.(\d+)$/);
}

# find vlan id from either the name or device.
# i.e eth0.123 will return 123. by checking iface name and device
# returns vlan id.
sub find_vlan_id {
my ($self, $iface, $device) = @_;
# a vlan interface can defined in two ways
# interface/name.vlanid/
# or interface/name/device=device.vlanid
# replace everything up-to and including . to get vlan id of the interface.
# favors ifacename.vlanid, then device.vlanid
my $vlanid = $self->_get_vlan_id($iface);
# if vlanid is empty here, lets check if device has vlan id.
if ((! $vlanid) && ($device)) {
$vlanid = $self->_get_vlan_id($device);
}
return $vlanid;
}

# generates the hashrefs for interface in yaml file format needed by nmstate.
# bulk of the config settings needed by the nmstate yml is done here.
# to add additional options, it should be constructed here.
Expand Down Expand Up @@ -330,10 +355,11 @@ sub generate_nmstate_config
$ifaceconfig->{state} = "up";
}
} elsif ($is_vlan_eth) {
my $vlan_id = $name;
# replace everything up-to and including . to get vlan id of the interface.
# TODO: instead of this, should perhaps add valid-id in schema? but may not be backward compatible for existing host entreis, aqdb will need updating?
$vlan_id =~ s/^[^.]*.//;;
my $vlan_id = $self->find_vlan_id($name, $iface->{device});
# if vlan_id is empty, error
if (! $vlan_id) {
$self->error("Could not find vlan id for vlan device $name");
jrha marked this conversation as resolved.
Show resolved Hide resolved
}
$ifaceconfig->{type} = "vlan";
$ifaceconfig->{vlan}->{'base-iface'} = $iface->{physdev};
$ifaceconfig->{vlan}->{'id'} = $vlan_id;
Expand Down
28 changes: 28 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ routes:
next-hop-interface: eth0.123
EOF

Readonly my $VLAN0_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
interfaces:
- ipv4:
address:
- ip: 4.3.2.1
prefix-length: 24
dhcp: false
enabled: true
name: vlan0
profile-name: vlan0
state: up
type: vlan
vlan:
base-iface: eth0
id: '123'
routes:
config:
- next-hop-interface: vlan0
state: absent
- destination: 1.2.3.4/32
next-hop-interface: vlan0
EOF


Readonly my $DHCP_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
Expand Down Expand Up @@ -178,4 +204,6 @@ is($bondyml, $BOND_YML, "Exact bond0 yml config");
my $vlanyml = get_file_contents("/etc/nmstate/eth0.123.yml");
is($vlanyml, $VLAN_YML, "Exact eth0.123 vlan yml config");

my $vlanyml2 = get_file_contents("/etc/nmstate/vlan0.yml");
is($vlanyml2, $VLAN0_YML, "Exact vlan0 yml config");
done_testing();
7 changes: 7 additions & 0 deletions ncm-network/src/test/resources/nmstate_advance.pan
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ prefix "/system/network/interfaces/eth0";
prefix "/system/network/interfaces/eth0.123";
"physdev" = "eth0";
"route/0" = dict("address", "1.2.3.4");

# test vlan interface route on vlan for backward compatibily with network.pm
"/system/network/interfaces/vlan0" = create("defaultinterface");
prefix "/system/network/interfaces/vlan0";
"device" = "eth0.123";
"physdev" = "eth0";
"route/0" = dict("address", "1.2.3.4");
Loading