From 935c04a29e6dc87a7ffe6d173cad65a31eb75b6f Mon Sep 17 00:00:00 2001 From: Gabriel Mougard Date: Mon, 10 Feb 2025 11:06:27 +0100 Subject: [PATCH] cmd/microcloud: refactor `validateSystems` to adapt checks whether we are in preseed or interactive mode * If we are not in preseed, the call the `detectCollisions` that output advanced network info (net interface collisions, subnet collisions) * Else, we use the former approach: check if the OVN underlay and Ceph cluster network is a valid CIDR subnet notation and output a warning if the subnets are shared. Signed-off-by: Gabriel Mougard --- cmd/microcloud/main_init.go | 43 +++++++++++++++++++------------- cmd/microcloud/main_init_test.go | 8 +++--- cmd/microcloud/preseed.go | 2 +- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/cmd/microcloud/main_init.go b/cmd/microcloud/main_init.go index 3dfd6a7c4..23c2f5a0b 100644 --- a/cmd/microcloud/main_init.go +++ b/cmd/microcloud/main_init.go @@ -309,7 +309,7 @@ func (c *initConfig) RunInteractive(cmd *cobra.Command, args []string) error { return err } - err = c.validateSystems(s) + err = c.validateSystems(s, false) if err != nil { return err } @@ -677,26 +677,35 @@ func detectCollisions(systems map[string]InitSystem) []string { return warnings } -func (c *initConfig) validateSystems(s *service.Handler) (err error) { - for _, sys := range c.systems { - if sys.MicroCephInternalNetworkSubnet == "" || sys.OVNGeneveAddr == "" { - continue - } - - _, subnet, err := net.ParseCIDR(sys.MicroCephInternalNetworkSubnet) - if err != nil { - return fmt.Errorf("Failed to parse available network interface CIDR address: %q: %w", subnet, err) +func (c *initConfig) validateSystems(s *service.Handler, preseed bool) (err error) { + if !preseed { + fmt.Println("Checking network configuration ...") + warnings := detectCollisions(c.systems) + if len(warnings) > 0 { + for _, warning := range warnings { + fmt.Println(tui.SummarizeResult(warning)) + } } + } else { + for _, sys := range c.systems { + if sys.MicroCephInternalNetwork == nil || sys.OVNGeneveNetwork == nil { + continue + } - underlayIP := net.ParseIP(sys.OVNGeneveAddr) - if underlayIP == nil { - return fmt.Errorf("OVN underlay IP %q is invalid", sys.OVNGeneveAddr) - } + _, subnet, err := net.ParseCIDR(sys.MicroCephInternalNetwork.Subnet.String()) + if err != nil { + return fmt.Errorf("Failed to parse available network interface CIDR address: %q: %w", subnet, err) + } - if subnet.Contains(underlayIP) { - tui.PrintWarning(fmt.Sprintf("OVN underlay IP (%s) is shared with the Ceph cluster network (%s)\n", underlayIP.String(), subnet.String())) + underlayIP := net.ParseIP(sys.OVNGeneveNetwork.IP.String()) + if underlayIP == nil { + return fmt.Errorf("OVN underlay IP %q is invalid", sys.OVNGeneveNetwork.IP.String()) + } - break + if sys.MicroCephInternalNetwork.Subnet.Contains(sys.OVNGeneveNetwork.IP) { + tui.PrintWarning(fmt.Sprintf("OVN underlay IP (%s) is shared with the Ceph cluster network (%s)\n", sys.OVNGeneveNetwork.IP.String(), sys.MicroCephInternalNetwork.Subnet.String())) + break + } } } diff --git a/cmd/microcloud/main_init_test.go b/cmd/microcloud/main_init_test.go index 744d6c4c0..99a79f840 100644 --- a/cmd/microcloud/main_init_test.go +++ b/cmd/microcloud/main_init_test.go @@ -55,7 +55,7 @@ func ensureValidateSystemsPasses(handler *service.Handler, testSystems map[strin systems := newTestSystemsMap(system) cfg := initConfig{systems: systems, bootstrap: true} - err := cfg.validateSystems(handler) + err := cfg.validateSystems(handler, false) if err != nil { t.Fatalf("Valid system %q failed validate: %s", testName, err) } @@ -67,7 +67,7 @@ func ensureValidateSystemsFails(handler *service.Handler, testSystems map[string systems := newTestSystemsMap(system) cfg := initConfig{systems: systems, bootstrap: true} - err := cfg.validateSystems(handler) + err := cfg.validateSystems(handler, false) if err == nil { t.Fatalf("Invalid system %q passed validation", testName) } @@ -202,7 +202,7 @@ func TestValidateSystemsMultiSystem(t *testing.T) { systems := newTestSystemsMap(sys1, sys2) cfg := initConfig{systems: systems, bootstrap: true} - err := cfg.validateSystems(handler) + err := cfg.validateSystems(handler, false) if err == nil { t.Fatalf("sys2 with conflicting management IP and ipv4.ovn.ranges passed validation") } @@ -223,7 +223,7 @@ func TestValidateSystemsMultiSystem(t *testing.T) { systems = newTestSystemsMap(sys3, sys4) cfg = initConfig{systems: systems, bootstrap: true} - err = cfg.validateSystems(handler) + err = cfg.validateSystems(handler, false) if err == nil { t.Fatalf("sys4 with conflicting management IP and ipv6.ovn.ranges passed validation") } diff --git a/cmd/microcloud/preseed.go b/cmd/microcloud/preseed.go index f22dfcc2b..6de299833 100644 --- a/cmd/microcloud/preseed.go +++ b/cmd/microcloud/preseed.go @@ -276,7 +276,7 @@ func (c *initConfig) RunPreseed(cmd *cobra.Command) error { } } - err = c.validateSystems(s) + err = c.validateSystems(s, true) if err != nil { return err }