From 3033ae88de30564dbc784601a251e0b42d6dbd33 Mon Sep 17 00:00:00 2001 From: shysank Date: Thu, 17 Mar 2022 10:04:41 -0700 Subject: [PATCH] fix outbound lb conversion for v1alpha3 clusters --- api/v1alpha3/azurecluster_conversion.go | 41 +++++++++++++----------- api/v1beta1/azurecluster_default.go | 10 +++--- api/v1beta1/azurecluster_default_test.go | 4 +-- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/api/v1alpha3/azurecluster_conversion.go b/api/v1alpha3/azurecluster_conversion.go index 8f3f0bcc029..afb4a1af10d 100644 --- a/api/v1alpha3/azurecluster_conversion.go +++ b/api/v1alpha3/azurecluster_conversion.go @@ -42,12 +42,35 @@ func (src *AzureCluster) ConvertTo(dstRaw conversion.Hub) error { // nolint dst.Annotations = nil } } + + // set default control plane outbound lb for private v1alpha3 clusters. + if src.Spec.NetworkSpec.APIServerLB.Type == Internal { + dst.Spec.NetworkSpec.ControlPlaneOutboundLB = &infrav1beta1.LoadBalancerSpec{ + FrontendIPsCount: pointer.Int32Ptr(1), + } + // We also need to set the defaults here because "get" won't set defaults, and hence there is no mismatch when a client + // gets a v1alpha3 cluster. + dst.SetControlPlaneOutboundLBDefaults() + } + + // set default node plane outbound lb for all v1alpha3 clusters. + dst.Spec.NetworkSpec.NodeOutboundLB = &infrav1beta1.LoadBalancerSpec{ + FrontendIPsCount: pointer.Int32Ptr(1), + } + // We also need to set the defaults here because "get" won't set defaults, and hence there is no mismatch when a client + // gets a v1alpha3 cluster. + dst.SetNodeOutboundLBDefaults() + // Manually restore data. restored := &infrav1beta1.AzureCluster{} if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok { return err } + // override outbound lb if it's present in restored. + dst.Spec.NetworkSpec.ControlPlaneOutboundLB = restored.Spec.NetworkSpec.ControlPlaneOutboundLB + dst.Spec.NetworkSpec.NodeOutboundLB = restored.Spec.NetworkSpec.NodeOutboundLB + dst.Spec.NetworkSpec.PrivateDNSZoneName = restored.Spec.NetworkSpec.PrivateDNSZoneName dst.Spec.NetworkSpec.APIServerLB.FrontendIPsCount = restored.Spec.NetworkSpec.APIServerLB.FrontendIPsCount @@ -55,24 +78,6 @@ func (src *AzureCluster) ConvertTo(dstRaw conversion.Hub) error { // nolint dst.Spec.CloudProviderConfigOverrides = restored.Spec.CloudProviderConfigOverrides dst.Spec.BastionSpec = restored.Spec.BastionSpec - // set default control plane outbound lb for private v1alpha3 clusters - if src.Spec.NetworkSpec.APIServerLB.Type == Internal && restored.Spec.NetworkSpec.ControlPlaneOutboundLB == nil { - dst.Spec.NetworkSpec.ControlPlaneOutboundLB = &infrav1beta1.LoadBalancerSpec{ - FrontendIPsCount: pointer.Int32Ptr(1), - } - } else { - dst.Spec.NetworkSpec.ControlPlaneOutboundLB = restored.Spec.NetworkSpec.ControlPlaneOutboundLB - } - - // set default node plane outbound lb for all v1alpha3 clusters - if restored.Spec.NetworkSpec.NodeOutboundLB == nil { - dst.Spec.NetworkSpec.NodeOutboundLB = &infrav1beta1.LoadBalancerSpec{ - FrontendIPsCount: pointer.Int32Ptr(1), - } - } else { - dst.Spec.NetworkSpec.NodeOutboundLB = restored.Spec.NetworkSpec.NodeOutboundLB - } - // Here we manually restore outbound security rules. Since v1alpha3 only supports ingress ("Inbound") rules, all v1alpha4/v1beta1 outbound rules are dropped when an AzureCluster // is converted to v1alpha3. We loop through all security group rules. For all previously existing outbound rules we restore the full rule. for _, restoredSubnet := range restored.Spec.NetworkSpec.Subnets { diff --git a/api/v1beta1/azurecluster_default.go b/api/v1beta1/azurecluster_default.go index f4374e3329d..35073f2a0c5 100644 --- a/api/v1beta1/azurecluster_default.go +++ b/api/v1beta1/azurecluster_default.go @@ -55,8 +55,8 @@ func (c *AzureCluster) setNetworkSpecDefaults() { c.setSubnetDefaults() c.setVnetPeeringDefaults() c.setAPIServerLBDefaults() - c.setNodeOutboundLBDefaults() - c.setControlPlaneOutboundLBDefaults() + c.SetNodeOutboundLBDefaults() + c.SetControlPlaneOutboundLBDefaults() } func (c *AzureCluster) setResourceGroupDefault() { @@ -205,7 +205,8 @@ func (c *AzureCluster) setAPIServerLBDefaults() { } } -func (c *AzureCluster) setNodeOutboundLBDefaults() { +// SetNodeOutboundLBDefaults sets the node outbound lb defaults. +func (c *AzureCluster) SetNodeOutboundLBDefaults() { if c.Spec.NetworkSpec.NodeOutboundLB == nil { if c.Spec.NetworkSpec.APIServerLB.Type == Internal { return @@ -245,7 +246,8 @@ func (c *AzureCluster) setNodeOutboundLBDefaults() { c.setOutboundLBFrontendIPs(lb, generateNodeOutboundIPName) } -func (c *AzureCluster) setControlPlaneOutboundLBDefaults() { +// SetControlPlaneOutboundLBDefaults sets the control plane outbound lb defaults. +func (c *AzureCluster) SetControlPlaneOutboundLBDefaults() { // public clusters don't need control plane outbound lb if c.Spec.NetworkSpec.APIServerLB.Type == Public { return diff --git a/api/v1beta1/azurecluster_default_test.go b/api/v1beta1/azurecluster_default_test.go index 4e5c03749e1..e5cd2de331e 100644 --- a/api/v1beta1/azurecluster_default_test.go +++ b/api/v1beta1/azurecluster_default_test.go @@ -1386,7 +1386,7 @@ func TestNodeOutboundLBDefaults(t *testing.T) { tc := c t.Run(tc.name, func(t *testing.T) { t.Parallel() - tc.cluster.setNodeOutboundLBDefaults() + tc.cluster.SetNodeOutboundLBDefaults() if !reflect.DeepEqual(tc.cluster, tc.output) { expected, _ := json.MarshalIndent(tc.output, "", "\t") actual, _ := json.MarshalIndent(tc.cluster, "", "\t") @@ -1508,7 +1508,7 @@ func TestControlPlaneOutboundLBDefaults(t *testing.T) { tc := c t.Run(tc.name, func(t *testing.T) { t.Parallel() - tc.cluster.setControlPlaneOutboundLBDefaults() + tc.cluster.SetControlPlaneOutboundLBDefaults() if !reflect.DeepEqual(tc.cluster, tc.output) { expected, _ := json.MarshalIndent(tc.output, "", "\t") actual, _ := json.MarshalIndent(tc.cluster, "", "\t")