Skip to content

Commit

Permalink
add separate loadbalancer spec for node outbound lb
Browse files Browse the repository at this point in the history
  • Loading branch information
shysank committed Mar 18, 2021
1 parent a6f3ad5 commit f296b3d
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 145 deletions.
8 changes: 7 additions & 1 deletion api/v1alpha3/azurecluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (src *AzureCluster) ConvertTo(dstRaw conversion.Hub) error { // nolint
return err
}

dst.Spec.NetworkSpec.LoadBalancerNodeOutboundIPs = restored.Spec.NetworkSpec.LoadBalancerNodeOutboundIPs
dst.Spec.NetworkSpec.APIServerLB.FrontendIPsCount = restored.Spec.NetworkSpec.APIServerLB.FrontendIPsCount
dst.Spec.NetworkSpec.NodeOutboundLB = restored.Spec.NetworkSpec.NodeOutboundLB

return nil
}
Expand Down Expand Up @@ -240,3 +241,8 @@ func Convert_v1alpha3_APIEndpoint_To_v1alpha4_APIEndpoint(in *apiv1alpha3.APIEnd
func Convert_v1alpha4_APIEndpoint_To_v1alpha3_APIEndpoint(in *apiv1alpha4.APIEndpoint, out *apiv1alpha3.APIEndpoint, s apiconversion.Scope) error {
return apiv1alpha3.Convert_v1alpha4_APIEndpoint_To_v1alpha3_APIEndpoint(in, out, s)
}

// Convert_v1alpha4_LoadBalancerSpec_To_v1alpha3_LoadBalancerSpec is an autogenerated conversion function.
func Convert_v1alpha4_LoadBalancerSpec_To_v1alpha3_LoadBalancerSpec(in *v1alpha4.LoadBalancerSpec, out *LoadBalancerSpec, s apiconversion.Scope) error {
return autoConvert_v1alpha4_LoadBalancerSpec_To_v1alpha3_LoadBalancerSpec(in, out, s)
}
18 changes: 7 additions & 11 deletions api/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions api/v1alpha4/azurecluster_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package v1alpha4

import (
"fmt"

"k8s.io/utils/pointer"
)

const (
Expand All @@ -43,6 +45,7 @@ func (c *AzureCluster) setNetworkSpecDefaults() {
c.setVnetDefaults()
c.setSubnetDefaults()
c.setAPIServerLBDefaults()
c.setNodeOutboundLBDefaults()
}

func (c *AzureCluster) setResourceGroupDefault() {
Expand Down Expand Up @@ -148,6 +151,58 @@ func (c *AzureCluster) setAPIServerLBDefaults() {
}
}

func (c *AzureCluster) setNodeOutboundLBDefaults() {
if c.Spec.NetworkSpec.NodeOutboundLB == nil {
if c.Spec.NetworkSpec.APIServerLB.Type == Internal {
return
}
c.Spec.NetworkSpec.NodeOutboundLB = &LoadBalancerSpec{}
}

lb := c.Spec.NetworkSpec.NodeOutboundLB
if lb.Type == "" {
lb.Type = Public
}
if lb.SKU == "" {
lb.SKU = SKUStandard
}

if lb.Name == "" {
lb.Name = c.ObjectMeta.Name
}
if len(lb.FrontendIPs) == 0 {

if lb.FrontendIPsCount == nil {
lb.FrontendIPsCount = pointer.Int32Ptr(1)
}

switch *lb.FrontendIPsCount {
case 0: // do nothing
case 1:
lb.FrontendIPs = []FrontendIP{
{
Name: generateFrontendIPConfigName(lb.Name),
PublicIP: &PublicIPSpec{
Name: generateNodeOutboundIPName(c.ObjectMeta.Name),
},
},
}
default:
for i := 0; i < int(*lb.FrontendIPsCount); i++ {
lb.FrontendIPs = append(lb.FrontendIPs, FrontendIP{
Name: withIndex(generateFrontendIPConfigName(lb.Name), i+1),
PublicIP: &PublicIPSpec{
Name: withIndex(generateNodeOutboundIPName(c.ObjectMeta.Name), i+1),
},
})
}

}
} else {
lb.FrontendIPsCount = pointer.Int32Ptr(int32(len(lb.FrontendIPs)))
}
}

// generateVnetName generates a virtual network name, based on the cluster name.
func generateVnetName(clusterName string) string {
return fmt.Sprintf("%s-%s", clusterName, "vnet")
Expand Down Expand Up @@ -197,3 +252,13 @@ func generatePublicIPName(clusterName string) string {
func generateFrontendIPConfigName(lbName string) string {
return fmt.Sprintf("%s-%s", lbName, "frontEnd")
}

// generateNodeOutboundIPName generates a public IP name, based on the cluster name.
func generateNodeOutboundIPName(clusterName string) string {
return fmt.Sprintf("pip-%s-node-outbound", clusterName)
}

// withIndex appends the index as suffix to a generated name
func withIndex(name string, n int) string {
return fmt.Sprintf("%s-%d", name, n)
}
188 changes: 188 additions & 0 deletions api/v1alpha4/azurecluster_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha4

import (
"encoding/json"
"k8s.io/utils/pointer"
"reflect"
"testing"

Expand Down Expand Up @@ -701,3 +702,190 @@ func TestAzureEnviromentDefault(t *testing.T) {
})
}
}

func TestNodeOutboundLBDefaults(t *testing.T) {
cases := []struct {
name string
cluster *AzureCluster
output *AzureCluster
}{
{
name: "default lb for public clusters",
cluster: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{Type: Public},
},
},
},
output: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{
Type: Public,
},
NodeOutboundLB: &LoadBalancerSpec{
Name: "cluster-test",
SKU: SKUStandard,
FrontendIPs: []FrontendIP{{
Name: "cluster-test-frontEnd",
PublicIP: &PublicIPSpec{
Name: "pip-cluster-test-node-outbound",
},
}},
Type: Public,
FrontendIPsCount: pointer.Int32Ptr(1),
},
},
},
},
},
{
name: "no lb for private clusters",
cluster: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{Type: Internal},
},
},
},
output: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{
Type: Internal,
},
},
},
},
},
{
name: "frontendIPsCount > 1",
cluster: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{Type: Public},
NodeOutboundLB: &LoadBalancerSpec{FrontendIPsCount: pointer.Int32Ptr(2)},
},
},
},
output: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{
Type: Public,
},
NodeOutboundLB: &LoadBalancerSpec{
Name: "cluster-test",
SKU: SKUStandard,
FrontendIPs: []FrontendIP{
{
Name: "cluster-test-frontEnd-1",
PublicIP: &PublicIPSpec{
Name: "pip-cluster-test-node-outbound-1",
},
},
{
Name: "cluster-test-frontEnd-2",
PublicIP: &PublicIPSpec{
Name: "pip-cluster-test-node-outbound-2",
},
},
},
Type: Public,
FrontendIPsCount: pointer.Int32Ptr(2),
},
},
},
},
},
{
name: "when frontend ips are configured",
cluster: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{Type: Public},
NodeOutboundLB: &LoadBalancerSpec{FrontendIPs: []FrontendIP{
{
Name: "fip-1",
PublicIP: &PublicIPSpec{
Name: "pip-1",
},
},
{
Name: "fip-2",
PublicIP: &PublicIPSpec{
Name: "pip-2",
},
},
}},
},
},
},
output: &AzureCluster{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-test",
},
Spec: AzureClusterSpec{
NetworkSpec: NetworkSpec{
APIServerLB: LoadBalancerSpec{Type: Public},
NodeOutboundLB: &LoadBalancerSpec{
Name: "cluster-test",
Type: Public,
SKU: SKUStandard,
FrontendIPs: []FrontendIP{
{
Name: "fip-1",
PublicIP: &PublicIPSpec{
Name: "pip-1",
},
},
{
Name: "fip-2",
PublicIP: &PublicIPSpec{
Name: "pip-2",
},
},
},
FrontendIPsCount: pointer.Int32Ptr(2),
},
},
},
},
},
}

for _, c := range cases {
tc := c
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tc.cluster.setNodeOutboundLBDefaults()
if !reflect.DeepEqual(tc.cluster, tc.output) {
expected, _ := json.MarshalIndent(tc.output, "", "\t")
actual, _ := json.MarshalIndent(tc.cluster, "", "\t")
t.Errorf("Expected %s, got %s", string(expected), string(actual))
}
})
}
}
Loading

0 comments on commit f296b3d

Please sign in to comment.