Skip to content

Commit

Permalink
Merge pull request #1720 from jrha/nmstate_schema
Browse files Browse the repository at this point in the history
ncm-network: Move nmstate options to new schema
  • Loading branch information
jrha authored Nov 6, 2024
2 parents e16692d + dd2b18f commit 6a63bb9
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 57 deletions.
18 changes: 4 additions & 14 deletions ncm-network/src/main/pan/components/network/core-schema-legacy.pan
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ type structure_route = {
@{route add command options to use (cannot be combined with other options)}
"command" ? string with !match(SELF, '[;]')
} with {
if (exists(SELF['command'])) {
module = value('/software/components/network/ncm-module', '');
if (module == 'nmstate') error("Command routes are not supported by the nmstate backend");
if (length(SELF) != 1) error("Cannot use command and any of the other attributes as route");
} else {
if (!exists(SELF['address']))
error("Address is mandatory for route (in absence of command)");
if (exists(SELF['prefix']) && exists(SELF['netmask']))
error("Use either prefix or netmask as route");
};
if (!exists(SELF['address']))
error("Address is mandatory for route (in absence of command)");
if (exists(SELF['prefix']) && exists(SELF['netmask']))
error("Use either prefix or netmask as route");

if (exists(SELF['prefix'])) {
pref = SELF['prefix'];
Expand Down Expand Up @@ -439,10 +433,6 @@ type structure_network = {
"set_hwaddr" ? boolean
"nmcontrolled" ? boolean
"allow_nm" ? boolean
@{let NetworkManager manage the dns (only for nmstate)}
"nm_manage_dns" : boolean = false
@{let ncm-network cleanup inactive connections (only for nmstate)}
"nm_clean_inactive_conn" : boolean = true
"primary_ip" ? string
"routers" ? structure_router{}
"ipv6" ? structure_ipv6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declaration template components/network/core-schema;

final variable QUATTOR_TYPES_NETWORK_LEGACY ?= true;
final variable QUATTOR_TYPES_NETWORK_LEGACY ?= false;

include if (QUATTOR_TYPES_NETWORK_LEGACY) 'components/network/core-schema-legacy'
else 'components/network/types/network';
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type network_ipv6 = {
when using ncm-network (unless specified otherwise).
}
type structure_network = {
include structure_network_backend_specific
"domainname" : type_fqdn
"hostname" : type_shorthostname
"realhostname" ? string with is_shorthostname(SELF) || is_fqdn(SELF)
Expand All @@ -63,10 +64,6 @@ type structure_network = {
"set_hwaddr" ? boolean
"nmcontrolled" ? boolean
"allow_nm" ? boolean
@{let NetworkManager manage the dns (only for nmstate)}
"nm_manage_dns" : boolean = false
@{let ncm-network cleanup inactive connections (only for nmstate)}
"nm_clean_inactive_conn" : boolean = true
"primary_ip" ? string
"routers" ? network_router{}
"ipv6" ? network_ipv6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
declaration template components/network/types/network/backend/initscripts;

@{implement types specific for initscripts / network.pm}

type structure_network_backend_specific = {
};

function network_valid_route = {
if (exists(SELF['prefix']) && exists(SELF['netmask'])) {
error("Use either prefix or netmask as route");
};

if (exists(SELF['prefix'])) {
network_valid_prefix(SELF);
};

true;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
declaration template components/network/types/network/backend/nmstate;

@{implement types specific for nmstate / nmstate.pm}

type structure_network_backend_specific = {
@{let NetworkManager manage the dns}
"manage_dns" : boolean = false
@{let ncm-network cleanup inactive connections}
"clean_inactive_conn" : boolean = true
};

function network_valid_route = {
if (exists(SELF['command'])) {
if (length(SELF) != 1) error("Cannot use command and any of the other attributes as route");
} else {
if (!exists(SELF['address']))
error("Address is mandatory for route (in absence of command)");
if (exists(SELF['prefix']) && exists(SELF['netmask']))
error("Use either prefix or netmask as route");
};

if (exists(SELF['prefix'])) {
network_valid_prefix(SELF);
};

true;
};
52 changes: 20 additions & 32 deletions ncm-network/src/main/pan/components/network/types/network/route.pan
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ declaration template components/network/types/network/route;

type network_valid_routing_table = string with exists("/system/network/routing_table/" + SELF);

function network_valid_prefix = {
pref = ARGV[0]['prefix'];
ipv6 = false;
foreach (k; v; ARGV[0]) {
if (match(to_string(v), ':')) {
ipv6 = true;
};
};
if (ipv6) {
if (!is_ipv6_prefix_length(pref)) {
error("Prefix %s is not a valid IPv6 prefix", pref);
};
} else {
if (!is_ipv4_prefix_length(pref)) {
error("Prefix %s is not a valid IPv4 prefix", pref);
};
};
};

@documentation{
Add route (IPv4 of IPv6)
Presence of ':' in any of the values indicates this is IPv6 related.
Expand All @@ -21,35 +40,4 @@ type network_route = {
"onlink" ? boolean
@{route add command options to use (cannot be combined with other options)}
"command" ? string with !match(SELF, '[;]')
} with {
if (exists(SELF['command'])) {
network_exclude_backend('nmstate', 'command routes');
if (length(SELF) != 1) error("Cannot use command and any of the other attributes as route");
} else {
if (!exists(SELF['address']))
error("Address is mandatory for route (in absence of command)");
if (exists(SELF['prefix']) && exists(SELF['netmask']))
error("Use either prefix or netmask as route");
};

if (exists(SELF['prefix'])) {
pref = SELF['prefix'];
ipv6 = false;
foreach (k; v; SELF) {
if (match(to_string(v), ':')) {
ipv6 = true;
};
};
if (ipv6) {
if (!is_ipv6_prefix_length(pref)) {
error("Prefix %s is not a valid IPv6 prefix", pref);
};
} else {
if (!is_ipv4_prefix_length(pref)) {
error("Prefix %s is not a valid IPv4 prefix", pref);
};
};
};

true;
};
} with network_valid_route(SELF);
12 changes: 6 additions & 6 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use Readonly;

Readonly my $NMSTATECTL => '/usr/bin/nmstatectl';
Readonly my $NMCLI_CMD => '/usr/bin/nmcli';
# pick a config name for nmstate yml to configure dns-resolver: settings. if nm_manage_dns=true
# pick a config name for nmstate yml to configure dns-resolver: settings. if manage_dns=true
Readonly my $NM_RESOLV_YML => "/etc/nmstate/resolv.yml";
Readonly my $NM_DROPIN_CFG_FILE => "/etc/NetworkManager/conf.d/90-quattor.conf";

Expand Down Expand Up @@ -84,7 +84,7 @@ sub is_valid_interface

# By default, NetworkManager on Red Hat Enterprise Linux (RHEL) 8+ dynamically updates the /etc/resolv.conf
# file with the DNS settings from active NetworkManager connection profiles. we manage this using ncm-resolver.
# so disable this unless nm_manage_dns = true. resolver details can be set using nmstate but not doing this now.
# so disable this unless manage_dns = true. resolver details can be set using nmstate but not doing this now.
sub disable_nm_manage_dns
{
my ($self, $manage_dns, $nwsrv) = @_;
Expand Down Expand Up @@ -608,7 +608,7 @@ sub generate_nmstate_config
};

# Generate hash of dns-resolver config for nmstate.
# only used if nm_manage_dns = true.
# only used if manage_dns = true.
sub generate_nm_resolver_config
{
my ($self, $net, $manage) = @_;
Expand Down Expand Up @@ -769,7 +769,7 @@ sub nmstate_apply
$action = 0;
}
# apply resolver config if exists.
# this will exist at this stage if nm_manage_dns is set to true.
# this will exist at this stage if manage_dns is set to true.
my $resolv_state = $exifiles->{$NM_RESOLV_YML} || 0;
if ($self->file_exists($NM_RESOLV_YML))
{
Expand Down Expand Up @@ -840,7 +840,7 @@ sub Configure
my $nwtree = $config->getTree($NETWORK_PATH);

my $hostname = $nwtree->{realhostname} || "$nwtree->{hostname}.$nwtree->{domainname}";
my $manage_dns = $nwtree->{nm_manage_dns} || 0;
my $manage_dns = $nwtree->{manage_dns} || 0;
my $dgw = $nwtree->{default_gateway};
if (!$dgw) {
$self->warn ("No default gateway configured");
Expand Down Expand Up @@ -983,7 +983,7 @@ sub Configure

# cleanup dangling inactive connections after ncm network changes are applied.
# defaults to cleanup
my $clean_inactive_conn = $net->{nm_clean_inactive_conn};
my $clean_inactive_conn = $net->{clean_inactive_conn};
if ($clean_inactive_conn and $stopstart) {
# look to cleanup connections only when something is changed.
$self->clear_inactive_nm_connections;
Expand Down
1 change: 1 addition & 0 deletions ncm-network/src/test/perl/nmstate_simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ ok(command_history_ok([
'/usr/bin/nmcli connection',
'service NetworkManager reload',
'systemctl disable nmstate',
'systemctl stop nmstate',
'/usr/bin/nmstatectl apply /etc/nmstate/eth0.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/resolv.yml',
'service NetworkManager reload',
Expand Down
2 changes: 2 additions & 0 deletions ncm-network/src/test/resources/nmstate_simple.pan
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
object template nmstate_simple;

variable QUATTOR_TYPES_NETWORK_BACKEND = 'nmstate';

include 'simple_base_profile';
"/hardware/cards/nic/eth0/hwaddr" = "6e:a5:1b:55:77:0a";
# the next include is mainly to the profile, it is not used in the tests
Expand Down

0 comments on commit 6a63bb9

Please sign in to comment.