Skip to content

Commit

Permalink
Remove subnet from NAT gateway spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Dec 1, 2021
1 parent c904909 commit 12f270d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
21 changes: 12 additions & 9 deletions azure/services/natgateways/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ func newClient(auth azure.Authorizer) *azureClient {
return &azureClient{c}
}

// netNatGatewaysClient creates a new NAT gateways client from subscription ID.
// netNatGatewaysClient creates a new nat gateways client from subscription ID.
func netNatGatewaysClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) network.NatGatewaysClient {
natGatewaysClient := network.NewNatGatewaysClientWithBaseURI(baseURI, subscriptionID)
azure.SetAutoRestClientDefaults(&natGatewaysClient.Client, authorizer)
return natGatewaysClient
}

// Get gets the specified NAT gateway.
// Get gets the specified nat gateway.
func (ac *azureClient) Get(ctx context.Context, resourceGroupName, natGatewayName string) (network.NatGateway, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "natgateways.azureClient.Get")
defer done()

return ac.natgateways.Get(ctx, resourceGroupName, natGatewayName, "")
}

// CreateOrUpdateAsync creates or updates a NAT gateway asynchronously.
// CreateOrUpdateAsync creates or updates a Nat Gateway asynchronously.
// It sends a PUT request to Azure and if accepted without error, the func will return a Future which can be used to track the ongoing
// progress of the operation.
func (ac *azureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter) (interface{}, azureautorest.FutureAPI, error) {
Expand All @@ -78,14 +78,14 @@ func (ac *azureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.Resou
var existingNatGateway interface{}

if existing, err := ac.Get(ctx, spec.ResourceGroupName(), spec.ResourceName()); err != nil && !azure.ResourceNotFound(err) {
return nil, nil, errors.Wrapf(err, "failed to get NAT gateway %s for %s in %s", spec.ResourceName(), spec.OwnerResourceName(), spec.ResourceGroupName())
return nil, nil, errors.Wrapf(err, "failed to get Nat Gateway %s for %s in %s", spec.ResourceName(), spec.OwnerResourceName(), spec.ResourceGroupName())
} else if err == nil {
existingNatGateway = existing
}

params, err := spec.Parameters(existingNatGateway)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to get desired parameters for NAT gateway %s", spec.ResourceName())
return nil, nil, errors.Wrapf(err, "failed to get desired parameters for Nat Gateway %s", spec.ResourceName())
}

natGateway, ok := params.(network.NatGateway)
Expand Down Expand Up @@ -118,7 +118,7 @@ func (ac *azureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.Resou
return result, nil, err
}

// DeleteAsync deletes a NAT gateway asynchronously. DeleteAsync sends a DELETE
// DeleteAsync deletes a Nat Gateway asynchronously. DeleteAsync sends a DELETE
// request to Azure and if accepted without error, the func will return a Future which can be used to track the ongoing
// progress of the operation.
func (ac *azureClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecGetter) (azureautorest.FutureAPI, error) {
Expand Down Expand Up @@ -146,19 +146,22 @@ func (ac *azureClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecG

// IsDone returns true if the long-running operation has completed.
func (ac *azureClient) IsDone(ctx context.Context, future azureautorest.FutureAPI) (bool, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "natgateways.Service.DeleteAsync")
ctx, _, done := tele.StartSpanWithLogger(ctx, "natgateways.Service.IsDone")
defer done()

done, err := future.DoneWithContext(ctx, ac.natgateways)
isDone, err := future.DoneWithContext(ctx, ac.natgateways)
if err != nil {
return false, errors.Wrap(err, "failed checking if the operation was complete")
}

return done, nil
return isDone, nil
}

// Result fetches the result of a long-running operation future.
func (ac *azureClient) Result(ctx context.Context, futureData azureautorest.FutureAPI, futureType string) (interface{}, error) {
_, _, done := tele.StartSpanWithLogger(ctx, "natgateways.Service.Result")
defer done()

if futureData == nil {
return nil, errors.Errorf("cannot get result from nil future")
}
Expand Down
9 changes: 3 additions & 6 deletions azure/services/natgateways/natgateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ func (s *Service) Reconcile(ctx context.Context) error {
}
}
if err == nil && resultingErr != nil {
// TODO: consider making OwnerResourceName() a no-op
subnetSpec := s.Scope.Subnet(natGatewaySpec.OwnerResourceName())
networkNatGateway, ok := natGateway.(network.NatGateway)
if !ok {
// Return out of loop since this would be an unexpcted fatal error
return errors.Errorf("created resource %T is not a network.NatGateway", natGateway)
}

// TODO: is it necessary to set the spec since it doesn't appear to change any behavior
subnetSpec.NatGateway.ID = *networkNatGateway.ID
s.Scope.SetSubnet(subnetSpec)
s.SetNatGatewayIDInSubnets(natGatewaySpec.ResourceName(), *networkNatGateway.ID)
}
}

Expand Down Expand Up @@ -116,12 +114,11 @@ func (s *Service) Delete(ctx context.Context) error {
return result
}

func (s *Service) SetNatGatewayID(name string, id string) {
func (s *Service) SetNatGatewayIDInSubnets(name string, id string) {
for _, subnet := range s.Scope.Subnets() {
if subnet.NatGateway.Name == name {
subnet.NatGateway.ID = id
s.Scope.SetSubnet(subnet)
// s.AzureCluster.Spec.NetworkSpec.Subnets[i] = id
}
}
}

0 comments on commit 12f270d

Please sign in to comment.