Skip to content

Commit

Permalink
Make inbound NAT rules reconcile/delete async
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Jan 12, 2022
1 parent 42cbf86 commit a0fb7d3
Show file tree
Hide file tree
Showing 18 changed files with 816 additions and 559 deletions.
1 change: 1 addition & 0 deletions azure/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type NetworkDescriber interface {
SetSubnet(infrav1.SubnetSpec)
IsIPv6Enabled() bool
ControlPlaneRouteTable() infrav1.RouteTable
APIServerLB() *infrav1.LoadBalancerSpec
APIServerLBName() string
APIServerLBPoolName(string) string
IsAPIServerPrivate() bool
Expand Down
28 changes: 28 additions & 0 deletions azure/mock_azure/azure_mock.go

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

24 changes: 17 additions & 7 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/availabilitysets"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/disks"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/inboundnatrules"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachines"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
Expand Down Expand Up @@ -193,16 +194,25 @@ func (m *MachineScope) PublicIPSpecs() []azure.PublicIPSpec {
}

// InboundNatSpecs returns the inbound NAT specs.
func (m *MachineScope) InboundNatSpecs() []azure.InboundNatSpec {
func (m *MachineScope) InboundNatSpecs(portsInUse map[int32]struct{}) []azure.ResourceSpecGetter {
// The existing inbound NAT rules are needed in order to find an available SSH port for each new inbound NAT rule.
if m.Role() == infrav1.ControlPlane {
return []azure.InboundNatSpec{
{
Name: m.Name(),
LoadBalancerName: m.APIServerLBName(),
},
spec := &inboundnatrules.InboundNatSpec{
Name: m.Name(),
ResourceGroup: m.ResourceGroup(),
LoadBalancerName: m.APIServerLBName(),
FrontendIPConfigurationID: nil,
PortsInUse: portsInUse,
}
if frontEndIPs := m.APIServerLB().FrontendIPs; len(frontEndIPs) > 0 {
ipConfig := frontEndIPs[0].Name
id := azure.FrontendIPConfigID(m.SubscriptionID(), m.ResourceGroup(), m.APIServerLBName(), ipConfig)
spec.FrontendIPConfigurationID = to.StringPtr(id)
}

return []azure.ResourceSpecGetter{spec}
}
return []azure.InboundNatSpec{}
return []azure.ResourceSpecGetter{}
}

// NICSpecs returns the network interface specs.
Expand Down
47 changes: 39 additions & 8 deletions azure/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package scope

import (
"context"
"fmt"
"reflect"
"testing"

Expand All @@ -32,6 +33,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/disks"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/inboundnatrules"
)

func TestMachineScope_Name(t *testing.T) {
Expand Down Expand Up @@ -283,7 +285,7 @@ func TestMachineScope_InboundNatSpecs(t *testing.T) {
tests := []struct {
name string
machineScope MachineScope
want []azure.InboundNatSpec
want []azure.ResourceSpecGetter
}{
{
name: "returns empty when infra is not control plane",
Expand All @@ -295,7 +297,7 @@ func TestMachineScope_InboundNatSpecs(t *testing.T) {
},
},
},
want: []azure.InboundNatSpec{},
want: []azure.ResourceSpecGetter{},
},
{
name: "returns InboundNatSpec when infra is control plane",
Expand All @@ -313,29 +315,58 @@ func TestMachineScope_InboundNatSpecs(t *testing.T) {
},
},
ClusterScoper: &ClusterScope{
AzureClients: AzureClients{
EnvironmentSettings: auth.EnvironmentSettings{
Values: map[string]string{
auth.SubscriptionID: "123",
},
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
SubscriptionID: "123",
NetworkSpec: infrav1.NetworkSpec{
APIServerLB: infrav1.LoadBalancerSpec{
Name: "foo-loadbalancer",
FrontendIPs: []infrav1.FrontendIP{
{
Name: "foo-frontend-ip",
},
},
},
},
},
},
},
},
want: []azure.InboundNatSpec{
{
Name: "machine-name",
LoadBalancerName: "foo-loadbalancer",
want: []azure.ResourceSpecGetter{
&inboundnatrules.InboundNatSpec{
Name: "machine-name",
LoadBalancerName: "foo-loadbalancer",
ResourceGroup: "my-rg",
FrontendIPConfigurationID: to.StringPtr(azure.FrontendIPConfigID("123", "my-rg", "foo-loadbalancer", "foo-frontend-ip")),
PortsInUse: make(map[int32]struct{}),
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := tt.machineScope.InboundNatSpecs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("InboundNatSpecs() = %v, want %v", got, tt.want)
t.Parallel()
if got := tt.machineScope.InboundNatSpecs(make(map[int32]struct{})); !reflect.DeepEqual(got, tt.want) {
gotArray := "[ "
for _, spec := range got {
gotArray += fmt.Sprintf("%+v ", spec)
}
gotArray += "]"
wantArray := "[ "
for _, spec := range tt.want {
wantArray += fmt.Sprintf("%+v ", spec)
}
wantArray += "]"
t.Errorf("InboundNatSpecs([]converters.ExistingInboundNatSpec{}) = %s, want %s", gotArray, wantArray)
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ func (s *ManagedControlPlaneScope) IsVnetManaged() bool {
return true
}

// APIServerLBName returns the API Server LB spec.
func (s *ManagedControlPlaneScope) APIServerLB() *infrav1.LoadBalancerSpec {
return nil // does not apply for AKS
}

// APIServerLBName returns the API Server LB name.
func (s *ManagedControlPlaneScope) APIServerLBName() string {
return "" // does not apply for AKS
Expand Down

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

Loading

0 comments on commit a0fb7d3

Please sign in to comment.