Skip to content

Commit 1f1ff9a

Browse files
committed
fix: don't create outbound LB if using NatGateway
1 parent eced64a commit 1f1ff9a

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

api/v1alpha4/azurecluster_default.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,18 @@ func (c *AzureCluster) setNodeOutboundLBDefaults() {
202202
return
203203
}
204204

205-
var oneSubnetWithoutNatGateway bool
205+
var needsOutboundLB bool
206206
for _, subnet := range c.Spec.NetworkSpec.Subnets {
207207
if subnet.Role == SubnetNode && !subnet.IsNatGatewayEnabled() {
208-
oneSubnetWithoutNatGateway = true
208+
needsOutboundLB = true
209209
break
210210
}
211211
}
212212

213213
// If we don't default the outbound LB when there are some subnets with nat gateway,
214214
// and some without, those without wouldn't have outbound traffic. So taking the
215215
// safer route, we configure the outbound LB in that scenario.
216-
if len(c.Spec.NetworkSpec.Subnets) > 0 && !oneSubnetWithoutNatGateway {
216+
if !needsOutboundLB {
217217
return
218218
}
219219

azure/services/natgateways/natgateways.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package natgateways
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
24+
autorest "github.com/Azure/go-autorest/autorest/azure"
2325
"github.com/Azure/go-autorest/autorest/to"
2426
"github.com/go-logr/logr"
2527
"github.com/pkg/errors"
@@ -78,6 +80,8 @@ func (s *Service) Reconcile(ctx context.Context) error {
7880
natGatewaySpec.Subnet.NatGateway = *existingNatGateway
7981
s.Scope.SetSubnet(natGatewaySpec.Subnet)
8082
continue
83+
} else {
84+
s.Scope.Error(errors.New("unexpected NatGateway name mismatch"), "NatGateway spec Name did not match actual NatGateway resource name", "actual NatGatewayIP.Name", existingNatGateway.NatGatewayIP.Name, "spec NatGatewayIP.Name", natGatewaySpec.NatGatewayIP.Name)
8185
}
8286
default:
8387
// nat gateway doesn't exist but its name was specified in the subnet, let's create it
@@ -118,12 +122,26 @@ func (s *Service) getExisting(ctx context.Context, spec azure.NatGatewaySpec) (*
118122
if err != nil {
119123
return nil, err
120124
}
125+
// We must have a non-nil PublicIPAddresses, and the underlying SubResource slice type must have a length of 1
126+
// TODO do we want to eventually handle NatGateway resources w/ more than one public IP address?
127+
if !(existingNatGateway.PublicIPAddresses != nil && len(*existingNatGateway.PublicIPAddresses) == 1) {
128+
return nil, errors.Wrap(err, "failed to parse PublicIPAddresses")
129+
}
130+
publicIPAddressID := to.String((*existingNatGateway.PublicIPAddresses)[0].ID)
131+
resource, err := autorest.ParseResourceID(publicIPAddressID)
132+
if err != nil {
133+
return nil, errors.Wrap(err, "failed to parse Resource ID from PublicIPAddresses ID")
134+
}
135+
// We depend upon a non-empty ResourceName string
136+
if resource.ResourceName == "" {
137+
return nil, errors.Wrap(err, fmt.Sprintf("got unexpected ResourceName value from NatGateway PublicIpAddress, ResourceName=%s", resource.ResourceName))
138+
}
121139

122140
return &infrav1.NatGateway{
123141
ID: to.String(existingNatGateway.ID),
124142
Name: to.String(existingNatGateway.Name),
125143
NatGatewayIP: infrav1.PublicIPSpec{
126-
Name: to.String((*existingNatGateway.PublicIPAddresses)[0].ID),
144+
Name: resource.ResourceName,
127145
},
128146
}, nil
129147
}

azure/services/subnets/subnets.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,8 @@ func (s *Service) Reconcile(ctx context.Context) error {
6363
return errors.Wrapf(err, "failed to get subnet %s", subnetSpec.Name)
6464
case err == nil:
6565
// subnet already exists, update the spec and skip creation
66-
var subnet infrav1.SubnetSpec
67-
subnet.ID = existingSubnet.ID
68-
subnet.Name = existingSubnet.Name
69-
subnet.Role = existingSubnet.Role
70-
subnet.CIDRBlocks = existingSubnet.CIDRBlocks
71-
72-
s.Scope.SetSubnet(subnet)
66+
s.Scope.SetSubnet(*existingSubnet)
67+
continue
7368

7469
case !s.Scope.IsVnetManaged():
7570
return fmt.Errorf("vnet was provided but subnet %s is missing", subnetSpec.Name)
@@ -167,12 +162,9 @@ func (s *Service) getExisting(ctx context.Context, rgName string, spec azure.Sub
167162
addresses = to.StringSlice(subnet.SubnetPropertiesFormat.AddressPrefixes)
168163
}
169164

170-
subnetSpec := &infrav1.SubnetSpec{
171-
Role: spec.Role,
172-
Name: to.String(subnet.Name),
173-
ID: to.String(subnet.ID),
174-
CIDRBlocks: addresses,
175-
}
165+
subnetSpec := s.Scope.Subnet(spec.Name)
166+
subnetSpec.ID = to.String(subnet.ID)
167+
subnetSpec.CIDRBlocks = addresses
176168

177-
return subnetSpec, nil
169+
return &subnetSpec, nil
178170
}

0 commit comments

Comments
 (0)