Skip to content

Commit

Permalink
Set backend pool name defaults in the webhook
Browse files Browse the repository at this point in the history
Currently we define defaults directly in the code, but it's better
to do so using the webhook. This patch does it and updates the tests.
  • Loading branch information
Fedosin committed Oct 13, 2022
1 parent 52e2375 commit ee4362e
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 19 deletions.
28 changes: 28 additions & 0 deletions api/v1beta1/azurecluster_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ func (c *AzureCluster) setAPIServerLBDefaults() {
}
}
}

if lb.BackendPool.Name == nil {
lb.BackendPool.Name = pointer.StringPtr(generateBackendAddressPoolName(lb.Name))
}
}

// SetNodeOutboundLBDefaults sets the default values for the NodeOutboundLB.
Expand Down Expand Up @@ -232,6 +236,13 @@ func (c *AzureCluster) SetNodeOutboundLBDefaults() {
}

c.setOutboundLBFrontendIPs(lb, generateNodeOutboundIPName)

if lb.BackendPool.Name == nil {
if lb.Name == "" {
lb.BackendPool.Name = pointer.StringPtr("")
}
lb.BackendPool.Name = pointer.StringPtr(generateOutboundBackendAddressPoolName(lb.Name))
}
}

// SetControlPlaneOutboundLBDefaults sets the default values for the control plane's outbound LB.
Expand All @@ -250,6 +261,13 @@ func (c *AzureCluster) SetControlPlaneOutboundLBDefaults() {
lb.FrontendIPsCount = pointer.Int32Ptr(1)
}
c.setOutboundLBFrontendIPs(lb, generateControlPlaneOutboundIPName)

if lb.BackendPool.Name == nil {
if lb.Name == "" {
lb.BackendPool.Name = pointer.StringPtr("")
}
lb.BackendPool.Name = pointer.StringPtr(generateOutboundBackendAddressPoolName(generateControlPlaneOutboundLBName(c.ObjectMeta.Name)))
}
}

// setOutboundLBFrontendIPs sets the frontend ips for the given load balancer.
Expand Down Expand Up @@ -433,3 +451,13 @@ func generateNatGatewayIPName(clusterName, subnetName string) string {
func withIndex(name string, n int) string {
return fmt.Sprintf("%s-%d", name, n)
}

// generateBackendAddressPoolName generates a load balancer backend address pool name.
func generateBackendAddressPoolName(lbName string) string {
return fmt.Sprintf("%s-%s", lbName, "backendPool")
}

// generateOutboundBackendAddressPoolName generates a load balancer outbound backend address pool name.
func generateOutboundBackendAddressPoolName(lbName string) string {
return fmt.Sprintf("%s-%s", lbName, "outboundBackendPool")
}
130 changes: 129 additions & 1 deletion api/v1beta1/azurecluster_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/Azure/go-autorest/autorest/to"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

func TestResourceGroupDefault(t *testing.T) {
Expand Down Expand Up @@ -853,6 +854,9 @@ func TestAPIServerLBDefaults(t *testing.T) {
},
},
},
BackendPool: BackendPool{
Name: pointer.String("cluster-test-public-lb-backendPool"),
},
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Type: Public,
Expand Down Expand Up @@ -894,6 +898,57 @@ func TestAPIServerLBDefaults(t *testing.T) {
},
},
},
BackendPool: BackendPool{
Name: pointer.String("cluster-test-internal-lb-backendPool"),
},
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Type: Internal,
IdleTimeoutInMinutes: to.Int32Ptr(DefaultOutboundRuleIdleTimeoutInMinutes),
},
Name: "cluster-test-internal-lb",
},
},
},
},
},
{
name: "with custom backend pool name",
cluster: &AzureCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{
LoadBalancerClassSpec: LoadBalancerClassSpec{
Type: Internal,
},
BackendPool: BackendPool{
Name: pointer.String("custom-backend-pool"),
},
},
},
},
},
output: &AzureCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{
FrontendIPs: []FrontendIP{
{
Name: "cluster-test-internal-lb-frontEnd",
FrontendIPClass: FrontendIPClass{
PrivateIPAddress: DefaultInternalLBIPAddress,
},
},
},
BackendPool: BackendPool{
Name: pointer.String("custom-backend-pool"),
},
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Type: Internal,
Expand Down Expand Up @@ -1077,6 +1132,9 @@ func TestNodeOutboundLBDefaults(t *testing.T) {
Name: "pip-cluster-test-node-outbound",
},
}},
BackendPool: BackendPool{
Name: pointer.String("cluster-test-outboundBackendPool"),
},
FrontendIPsCount: to.Int32Ptr(1),
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Expand Down Expand Up @@ -1255,6 +1313,9 @@ func TestNodeOutboundLBDefaults(t *testing.T) {
Name: "pip-cluster-test-node-outbound",
},
}},
BackendPool: BackendPool{
Name: pointer.String("cluster-test-outboundBackendPool"),
},
FrontendIPsCount: to.Int32Ptr(1),
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Expand Down Expand Up @@ -1365,6 +1426,9 @@ func TestNodeOutboundLBDefaults(t *testing.T) {
},
}},
FrontendIPsCount: to.Int32Ptr(1),
BackendPool: BackendPool{
Name: pointer.String("cluster-test-outboundBackendPool"),
},
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Type: Public,
Expand Down Expand Up @@ -1535,7 +1599,7 @@ func TestNodeOutboundLBDefaults(t *testing.T) {
},
},
{
name: "NodeOutboundLB declared as input with non-default IdleTimeoutInMinutes and FrontendIPsCount values",
name: "NodeOutboundLB declared as input with non-default IdleTimeoutInMinutes, FrontendIPsCount, BackendPool values",
cluster: &AzureCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-test",
Expand All @@ -1545,6 +1609,9 @@ func TestNodeOutboundLBDefaults(t *testing.T) {
APIServerLB: LoadBalancerSpec{LoadBalancerClassSpec: LoadBalancerClassSpec{Type: Public}},
NodeOutboundLB: &LoadBalancerSpec{
FrontendIPsCount: to.Int32Ptr(2),
BackendPool: BackendPool{
Name: pointer.String("custom-backend-pool"),
},
LoadBalancerClassSpec: LoadBalancerClassSpec{
IdleTimeoutInMinutes: to.Int32Ptr(15),
},
Expand Down Expand Up @@ -1578,6 +1645,9 @@ func TestNodeOutboundLBDefaults(t *testing.T) {
},
},
},
BackendPool: BackendPool{
Name: pointer.String("custom-backend-pool"),
},
FrontendIPsCount: to.Int32Ptr(2), // we expect the original value to be respected here
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Expand Down Expand Up @@ -1793,6 +1863,9 @@ func TestControlPlaneOutboundLBDefaults(t *testing.T) {
},
ControlPlaneOutboundLB: &LoadBalancerSpec{
Name: "cluster-test-outbound-lb",
BackendPool: BackendPool{
Name: pointer.String("cluster-test-outbound-lb-outboundBackendPool"),
},
FrontendIPs: []FrontendIP{
{
Name: "cluster-test-outbound-lb-frontEnd-1",
Expand All @@ -1818,6 +1891,61 @@ func TestControlPlaneOutboundLBDefaults(t *testing.T) {
},
},
},
{
name: "custom outbound lb backend pool",
cluster: &AzureCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{LoadBalancerClassSpec: LoadBalancerClassSpec{Type: Internal}},
ControlPlaneOutboundLB: &LoadBalancerSpec{
BackendPool: BackendPool{
Name: pointer.String("custom-outbound-lb"),
},
LoadBalancerClassSpec: LoadBalancerClassSpec{
IdleTimeoutInMinutes: to.Int32Ptr(15),
},
},
},
},
},
output: &AzureCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{
LoadBalancerClassSpec: LoadBalancerClassSpec{
Type: Internal,
},
},
ControlPlaneOutboundLB: &LoadBalancerSpec{
Name: "cluster-test-outbound-lb",
BackendPool: BackendPool{
Name: pointer.String("custom-outbound-lb"),
},
FrontendIPs: []FrontendIP{
{
Name: "cluster-test-outbound-lb-frontEnd",
PublicIP: &PublicIPSpec{
Name: "pip-cluster-test-controlplane-outbound",
},
},
},
FrontendIPsCount: to.Int32Ptr(1),
LoadBalancerClassSpec: LoadBalancerClassSpec{
SKU: SKUStandard,
Type: Public,
IdleTimeoutInMinutes: to.Int32Ptr(15),
},
},
},
},
},
},
}

for _, c := range cases {
Expand Down
21 changes: 3 additions & 18 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,6 @@ func (s *ClusterScope) PublicIPSpecs() []azure.ResourceSpecGetter {

// LBSpecs returns the load balancer specs.
func (s *ClusterScope) LBSpecs() []azure.ResourceSpecGetter {
apiBackendPoolName := s.APIServerLBPoolName(s.APIServerLB().Name)
if s.APIServerLB().BackendPool.Name != nil {
apiBackendPoolName = *s.APIServerLB().BackendPool.Name
}

specs := []azure.ResourceSpecGetter{
&loadbalancers.LBSpec{
// API Server LB
Expand All @@ -249,19 +244,14 @@ func (s *ClusterScope) LBSpecs() []azure.ResourceSpecGetter {
Type: s.APIServerLB().Type,
SKU: infrav1.SKUStandard,
Role: infrav1.APIServerRole,
BackendPoolName: apiBackendPoolName,
BackendPoolName: *s.APIServerLB().BackendPool.Name,
IdleTimeoutInMinutes: s.APIServerLB().IdleTimeoutInMinutes,
AdditionalTags: s.AdditionalTags(),
},
}

// Node outbound LB
if s.NodeOutboundLB() != nil {
nodeBackendPoolName := s.OutboundPoolName(s.NodeOutboundLB().Name)
if s.NodeOutboundLB().BackendPool.Name != nil {
nodeBackendPoolName = *s.NodeOutboundLB().BackendPool.Name
}

specs = append(specs, &loadbalancers.LBSpec{
Name: s.NodeOutboundLB().Name,
ResourceGroup: s.ResourceGroup(),
Expand All @@ -273,7 +263,7 @@ func (s *ClusterScope) LBSpecs() []azure.ResourceSpecGetter {
FrontendIPConfigs: s.NodeOutboundLB().FrontendIPs,
Type: s.NodeOutboundLB().Type,
SKU: s.NodeOutboundLB().SKU,
BackendPoolName: nodeBackendPoolName,
BackendPoolName: *s.NodeOutboundLB().BackendPool.Name,
IdleTimeoutInMinutes: s.NodeOutboundLB().IdleTimeoutInMinutes,
Role: infrav1.NodeOutboundRole,
AdditionalTags: s.AdditionalTags(),
Expand All @@ -282,11 +272,6 @@ func (s *ClusterScope) LBSpecs() []azure.ResourceSpecGetter {

// Control Plane Outbound LB
if s.ControlPlaneOutboundLB() != nil {
controlPlaneBackendPoolName := s.OutboundPoolName(azure.GenerateControlPlaneOutboundLBName(s.ClusterName()))
if s.ControlPlaneOutboundLB().BackendPool.Name != nil {
controlPlaneBackendPoolName = *s.ControlPlaneOutboundLB().BackendPool.Name
}

specs = append(specs, &loadbalancers.LBSpec{
Name: s.ControlPlaneOutboundLB().Name,
ResourceGroup: s.ResourceGroup(),
Expand All @@ -298,7 +283,7 @@ func (s *ClusterScope) LBSpecs() []azure.ResourceSpecGetter {
FrontendIPConfigs: s.ControlPlaneOutboundLB().FrontendIPs,
Type: s.ControlPlaneOutboundLB().Type,
SKU: s.ControlPlaneOutboundLB().SKU,
BackendPoolName: controlPlaneBackendPoolName,
BackendPoolName: *s.ControlPlaneOutboundLB().BackendPool.Name,
IdleTimeoutInMinutes: s.NodeOutboundLB().IdleTimeoutInMinutes,
Role: infrav1.ControlPlaneOutboundRole,
AdditionalTags: s.AdditionalTags(),
Expand Down

0 comments on commit ee4362e

Please sign in to comment.