From 396586fde2a416c930dce00c9a5b2dee639c04d5 Mon Sep 17 00:00:00 2001 From: Quan Tian Date: Thu, 18 Jan 2024 23:47:07 +0800 Subject: [PATCH] Add ipFamily validation Setting ipFamily to invalid value didn't return any error and kind just created an ipv4 cluster silently. It could be a bit confusing when it is set to some plausible but invalid values like IPv4, IPv6, Dual, ds. Signed-off-by: Quan Tian --- pkg/internal/apis/config/validate.go | 5 +++++ pkg/internal/apis/config/validate_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/internal/apis/config/validate.go b/pkg/internal/apis/config/validate.go index 2eabaac67e..1730252cd5 100644 --- a/pkg/internal/apis/config/validate.go +++ b/pkg/internal/apis/config/validate.go @@ -52,6 +52,11 @@ func (c *Cluster) Validate() error { } } + // ipFamily should be ipv4, ipv6, or dual + if c.Networking.IPFamily != IPv4Family && c.Networking.IPFamily != IPv6Family && c.Networking.IPFamily != DualStackFamily { + errs = append(errs, errors.Errorf("invalid ipFamily: %s", c.Networking.IPFamily)) + } + // podSubnet should be a valid CIDR if err := validateSubnets(c.Networking.PodSubnet, c.Networking.IPFamily); err != nil { errs = append(errs, errors.Errorf("invalid pod subnet %v", err)) diff --git a/pkg/internal/apis/config/validate_test.go b/pkg/internal/apis/config/validate_test.go index 3641e4f934..cce46c16ac 100644 --- a/pkg/internal/apis/config/validate_test.go +++ b/pkg/internal/apis/config/validate_test.go @@ -97,6 +97,16 @@ func TestClusterValidate(t *testing.T) { }(), ExpectErrors: 1, }, + { + Name: "bogus ipFamily", + Cluster: func() Cluster { + c := Cluster{} + SetDefaultsCluster(&c) + c.Networking.IPFamily = "ds" + return c + }(), + ExpectErrors: 1, + }, { Name: "bogus serviceSubnet", Cluster: func() Cluster {