From a32a6217672bed4e3654e52c910b271eb39bfa8e Mon Sep 17 00:00:00 2001 From: budimanjojo Date: Thu, 6 Apr 2023 12:28:42 +0700 Subject: [PATCH] fix: better testing of NetworkInterfaces so test doesn't fail ref: https://github.com/budimanjojo/talhelper/issues/105 --- pkg/config/validate/config.go | 9 +++++---- pkg/config/validate/validate_test.go | 2 +- pkg/config/validate/validator.go | 11 +++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/config/validate/config.go b/pkg/config/validate/config.go index 049a12fb..0607ee90 100644 --- a/pkg/config/validate/config.go +++ b/pkg/config/validate/config.go @@ -37,7 +37,7 @@ type Node struct { DisableSearchDomain string `validate:"isBool"` Nameservers []string `validate:"isIPList"` ConfigPatches []map[string]interface{} `validate:"isRFC6902List"` - NetworkInterfaces []*NetworkInterface + NetworkInterfaces []*NetworkInterface `validate:"isValidNetworkInterfaces"` InstallDiskSelector *InstallDiskSelector KernelModules []*KernelModule NodeLabels map[string]string @@ -60,9 +60,9 @@ type InstallDiskSelector struct { } type NetworkInterface struct { - Interface string `validate:"requiredWithout:Nodes.[].NetworkInterfaces.DeviceSelector"` - DeviceSelector *NetworkDeviceSelector `validate:"requiredWithout:Nodes.[].NetworkInterfaces.Interface"` - Addresses []string `validate:"isCIDRList"` + Interface string + DeviceSelector *NetworkDeviceSelector + Addresses []string `validate:"isCIDRList"` Routes []Route Bond *Bond Vlans []*Vlan @@ -174,5 +174,6 @@ func (c Config) Messages() map[string]string { "isInt": "{field} is not a valid integer", "isUint": "{field} is not a valid unsigned integer", "isDomainOrIP": "{field} is not a valid domain or IP address", + "isValidNetworkInterfaces": "{field} requires one of `interface` and `deviceSelector` to be set", } } diff --git a/pkg/config/validate/validate_test.go b/pkg/config/validate/validate_test.go index ad393d5e..81688996 100644 --- a/pkg/config/validate/validate_test.go +++ b/pkg/config/validate/validate_test.go @@ -48,7 +48,7 @@ nodes: "Nodes.0.InstallDisk": false, "Nodes.0.DisableSearchDomain": true, "Nodes.0.Nameservers": false, - "Nodes.0.NetworkInterfaces.0.Interface": true, + "Nodes.0.NetworkInterfaces": true, "Nodes.0.NetworkInterfaces.0.Addresses": true, "Nodes.0.NetworkInterfaces.0.Mtu": true, "Nodes.0.NetworkInterfaces.0.Routes.0.Gateway": true, diff --git a/pkg/config/validate/validator.go b/pkg/config/validate/validator.go index 2756bc7c..96d47f1b 100644 --- a/pkg/config/validate/validator.go +++ b/pkg/config/validate/validator.go @@ -93,3 +93,14 @@ func (c Config) IsDomainOrIP(domainIP string) bool { return false } + +func (c Config) IsValidNetworkInterfaces(ifaces []*NetworkInterface) bool { + for _, iface := range ifaces { + if iface.Interface == "" && iface.DeviceSelector == nil { + return false + } else if iface.Interface != "" && iface.DeviceSelector != nil { + return false + } + } + return true +}