Skip to content

Commit

Permalink
update natgateways to use aso framework
Browse files Browse the repository at this point in the history
  • Loading branch information
nawazkh committed Oct 1, 2023
1 parent 7eb1782 commit cee8b8d
Show file tree
Hide file tree
Showing 14 changed files with 703 additions and 1,183 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ WEBHOOK_ROOT ?= $(MANIFEST_ROOT)/webhook
RBAC_ROOT ?= $(MANIFEST_ROOT)/rbac
ASO_CRDS_PATH := $(MANIFEST_ROOT)/aso/crds.yaml
ASO_VERSION := v2.3.0
ASO_CRDS := resourcegroups.resources.azure.com
ASO_CRDS := resourcegroups.resources.azure.com natgateways.network.azure.com

# Allow overriding the imagePullPolicy
PULL_POLICY ?= Always
Expand Down
5 changes: 3 additions & 2 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701"
"hash/fnv"
"sort"
"strconv"
Expand Down Expand Up @@ -326,9 +327,9 @@ func (s *ClusterScope) RouteTableSpecs() []azure.ResourceSpecGetter {
}

// NatGatewaySpecs returns the node NAT gateway.
func (s *ClusterScope) NatGatewaySpecs() []azure.ResourceSpecGetter {
func (s *ClusterScope) NatGatewaySpecs() []azure.ASOResourceSpecGetter[*asonetworkv1.NatGateway] {
natGatewaySet := make(map[string]struct{})
var natGateways []azure.ResourceSpecGetter
var natGateways []azure.ASOResourceSpecGetter[*asonetworkv1.NatGateway]

// We ignore the control plane NAT gateway, as we will always use a LB to enable egress on the control plane.
for _, subnet := range s.NodeSubnets() {
Expand Down
103 changes: 98 additions & 5 deletions azure/scope/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package scope
import (
"context"
"fmt"
asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -63,6 +65,25 @@ func specArrayToString(specs []azure.ResourceSpecGetter) string {
return sb.String()
}

func specToStringForAsoResource[T genruntime.MetaObject](spec azure.ASOResourceSpecGetter[T]) string {
var sb strings.Builder
sb.WriteString("{ ")
sb.WriteString(fmt.Sprintf("%+v ", spec))
sb.WriteString("}")
return sb.String()
}

func specArrayToStringForAsoResource[T genruntime.MetaObject](specs []azure.ASOResourceSpecGetter[T]) string {
var sb strings.Builder
sb.WriteString("[\n")
for _, spec := range specs {
sb.WriteString(fmt.Sprintf("\t%+v\n", specToStringForAsoResource(spec)))
}
sb.WriteString("]")

return sb.String()
}

func TestAPIServerHost(t *testing.T) {
fakeSubscriptionID := "123"

Expand Down Expand Up @@ -867,7 +888,7 @@ func TestNatGatewaySpecs(t *testing.T) {
tests := []struct {
name string
clusterScope ClusterScope
want []azure.ResourceSpecGetter
want []azure.ASOResourceSpecGetter[*asonetworkv1.NatGateway]
}{
{
name: "returns nil if no subnets are specified",
Expand Down Expand Up @@ -929,7 +950,7 @@ func TestNatGatewaySpecs(t *testing.T) {
},
cache: &ClusterCache{},
},
want: []azure.ResourceSpecGetter{
want: []azure.ASOResourceSpecGetter[*asonetworkv1.NatGateway]{
&natgateways.NatGatewaySpec{
Name: "fake-nat-gateway-1",
ResourceGroup: "my-rg",
Expand Down Expand Up @@ -1007,7 +1028,7 @@ func TestNatGatewaySpecs(t *testing.T) {
},
cache: &ClusterCache{},
},
want: []azure.ResourceSpecGetter{
want: []azure.ASOResourceSpecGetter[*asonetworkv1.NatGateway]{
&natgateways.NatGatewaySpec{
Name: "fake-nat-gateway-1",
ResourceGroup: "my-rg",
Expand Down Expand Up @@ -1084,7 +1105,7 @@ func TestNatGatewaySpecs(t *testing.T) {
},
cache: &ClusterCache{},
},
want: []azure.ResourceSpecGetter{
want: []azure.ASOResourceSpecGetter[*asonetworkv1.NatGateway]{
&natgateways.NatGatewaySpec{
Name: "fake-nat-gateway-1",
ResourceGroup: "my-rg",
Expand All @@ -1105,7 +1126,79 @@ func TestNatGatewaySpecs(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := tt.clusterScope.NatGatewaySpecs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NatGatewaySpecs() = %s, want %s", specArrayToString(got), specArrayToString(tt.want))
t.Errorf("NatGatewaySpecs() = %s, want %s", specArrayToStringForAsoResource(got), specArrayToStringForAsoResource(tt.want))
}
})
}
}

func TestSetNatGatewayIDInSubnets(t *testing.T) {
tests := []struct {
name string
clusterScope ClusterScope
asoNatgateway *asonetworkv1.NatGateway
}{
{
name: "sets nat gateway id in the matching subnet",
clusterScope: ClusterScope{
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
NetworkSpec: infrav1.NetworkSpec{
Subnets: infrav1.Subnets{
{
SubnetClassSpec: infrav1.SubnetClassSpec{
Name: "fake-subnet-1",
},
NatGateway: infrav1.NatGateway{
NatGatewayClassSpec: infrav1.NatGatewayClassSpec{
Name: "fake-nat-gateway-1",
},
},
},
{
SubnetClassSpec: infrav1.SubnetClassSpec{
Name: "fake-subnet-2",
},
NatGateway: infrav1.NatGateway{
NatGatewayClassSpec: infrav1.NatGatewayClassSpec{
Name: "fake-nat-gateway-2",
},
},
},
},
},
},
},
cache: &ClusterCache{},
},
asoNatgateway: &asonetworkv1.NatGateway{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-nat-gateway-1",
},
Status: asonetworkv1.NatGateway_STATUS{
Id: ptr.To("dummy-id-1"),
},
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
t.Parallel()
tt.clusterScope.SetNatGatewayIDInSubnets(tt.asoNatgateway.Name, *tt.asoNatgateway.Status.Id)
for _, subnet := range tt.clusterScope.AzureCluster.Spec.NetworkSpec.Subnets {
if subnet.NatGateway.Name == tt.asoNatgateway.Name {
g.Expect(subnet.NatGateway.ID).To(Equal(*tt.asoNatgateway.Status.Id))
} else {
g.Expect(subnet.NatGateway.ID).To(Equal(""))
}
}
})
}
Expand Down
122 changes: 0 additions & 122 deletions azure/services/natgateways/client.go

This file was deleted.

21 changes: 0 additions & 21 deletions azure/services/natgateways/mock_natgateways/doc.go

This file was deleted.

Loading

0 comments on commit cee8b8d

Please sign in to comment.