Skip to content

Commit

Permalink
Merge pull request #1688 from aka7/ncm_network_nmstate_alias
Browse files Browse the repository at this point in the history
ncm-network: nmstate support for alias ip addresses
  • Loading branch information
jrha authored Jun 21, 2024
2 parents 1a8f0f8 + 88f2b66 commit 3f9e67d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
28 changes: 25 additions & 3 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,23 @@ sub ip_in_network {
return NetAddr::IP->new("$check_ip")->within($subnet);
}

# construct alias ip structure.
sub generate_alias_ips {
my ($self, $alias_list) = @_;
my $all_ip = [];
foreach my $alias_name (sort keys %$alias_list) {
my $ip_list = {};
my $netmask = $alias_list->{$alias_name}->{'netmask'};
my $ip = $alias_list->{$alias_name}->{'ip'};
$ip_list->{ip} = $ip;
$ip_list->{'prefix-length'} = $self->get_masklen("$ip/$netmask");
push (@$all_ip, \%$ip_list);
}
return \@$all_ip;
}



# 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 @@ -399,16 +416,21 @@ sub generate_nmstate_config
if ($is_ip) {
# if device has manual ip assigned
my $ip_list = {};
my $all_ip = [];
if ($iface->{netmask}) {
my $ip = NetAddr::IP->new($iface->{ip}."/".$iface->{netmask});
$ip_list->{ip} = $ip->addr;
$ip_list->{'prefix-length'} = $ip->masklen;
} else {
$self->error("$name with (IPv4) ip and no netmask configured");
}

# TODO: append alias ip to ip_list as array, providing ips as array of hashref.
$ifaceconfig->{ipv4}->{address} = [$ip_list];
push @$all_ip, $ip_list if scalar $ip_list;
if ($iface->{aliases}) {
# if device has additional alias ipv4 addresses defined. add them to config
$self->verbose("alias ip (ipv4) addr defined for $name, configuring additional ips");
push @$all_ip, @{$self->generate_alias_ips($iface->{aliases})};
}
$ifaceconfig->{ipv4}->{address} = $all_ip;
$ifaceconfig->{ipv4}->{dhcp} = $YFALSE;
$ifaceconfig->{ipv4}->{enabled} = $YTRUE;
} elsif ($iface->{ipv6addr}) {
Expand Down
29 changes: 29 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,32 @@ interfaces:
type: dummy
EOF

Readonly my $ALIAS_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
interfaces:
- ipv4:
address:
- ip: 4.3.2.11
prefix-length: 24
- ip: 4.3.2.12
prefix-length: 24
dhcp: false
enabled: true
mac-address: 6e:a5:1b:55:77:0d
name: eth4
profile-name: eth4
state: up
type: ethernet
routes:
config:
- next-hop-interface: eth4
state: absent
- destination: 0.0.0.0/0
next-hop-address: 4.3.2.254
next-hop-interface: eth4
EOF

Readonly my $IB_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
Expand Down Expand Up @@ -289,4 +315,7 @@ is($ibpkyml, $IBPKEY_YML, "Exact ib1 pkey yml config");
my $dummy_yml = get_file_contents("/etc/nmstate/dummy_myvip.yml");
is($dummy_yml, $DUMMY_YML, "Exact dummy interface yml config");

my $alias_yml = get_file_contents("/etc/nmstate/eth4.yml");
is($alias_yml, $ALIAS_YML, "Exact alias interface yml config");

done_testing();
11 changes: 11 additions & 0 deletions ncm-network/src/test/resources/nmstate_advance.pan
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ prefix "/system/network/vips/myvip";
"fqdn" = "myvip.test.com";
"ip" = "4.3.2.10";
"interfaces/0" = "eth0";

# create aliases interfaces
"/hardware/cards/nic/eth4/hwaddr" = "6e:a5:1b:55:77:0d";
prefix "/system/network/interfaces/eth4";
"ip" = "4.3.2.11";
"netmask" = "255.255.255.0";
"broadcast" = "4.3.2.255";
"aliases/dba/broadcast" = "4.3.2.255";
"aliases/dba/fqdn" = "host-alias1.quattor.com";
"aliases/dba/ip" = "4.3.2.12";
"aliases/dba/netmask" = "255.255.255.0";

0 comments on commit 3f9e67d

Please sign in to comment.