Skip to content

Commit

Permalink
Update service to use refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Dec 1, 2021
1 parent 12f270d commit 494e77d
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 490 deletions.
9 changes: 9 additions & 0 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ func (s *ClusterScope) SetSubnet(subnetSpec infrav1.SubnetSpec) {
}
}

func (s *ClusterScope) SetNatGatewayIDInSubnets(name string, id string) {
for _, subnet := range s.Subnets() {
if subnet.NatGateway.Name == name {
subnet.NatGateway.ID = id
s.SetSubnet(subnet)
}
}
}

// ControlPlaneRouteTable returns the cluster controlplane routetable.
func (s *ClusterScope) ControlPlaneRouteTable() infrav1.RouteTable {
subnet, _ := s.AzureCluster.Spec.NetworkSpec.GetControlPlaneSubnet()
Expand Down
39 changes: 5 additions & 34 deletions azure/services/natgateways/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,11 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)

// client wraps go-sdk.
type client interface {
Get(context.Context, string, string) (network.NatGateway, error)
CreateOrUpdateAsync(context.Context, azure.ResourceSpecGetter) (interface{}, azureautorest.FutureAPI, error)
DeleteAsync(context.Context, azure.ResourceSpecGetter) (azureautorest.FutureAPI, error)
IsDone(context.Context, azureautorest.FutureAPI) (bool, error)
Result(context.Context, azureautorest.FutureAPI, string) (interface{}, error)
}

// azureClient contains the Azure go-sdk Client.
type azureClient struct {
natgateways network.NatGatewaysClient
}

var _ client = (*azureClient)(nil)

// newClient creates a new VM client from subscription ID.
func newClient(auth azure.Authorizer) *azureClient {
c := netNatGatewaysClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
Expand All @@ -61,40 +50,23 @@ func netNatGatewaysClient(subscriptionID string, baseURI string, authorizer auto
}

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

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

// 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) {
func (ac *azureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter, parameters interface{}) (interface{}, azureautorest.FutureAPI, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "natgateways.Service.CreateOrUpdateAsync")
defer done()

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())
} 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())
}

natGateway, ok := params.(network.NatGateway)
natGateway, ok := parameters.(network.NatGateway)
if !ok {
if params == nil {
// nothing to do here.
return existingNatGateway, nil, nil
}
return nil, nil, errors.Errorf("%T is not a network.NatGateway", params)
return nil, nil, errors.Errorf("%T is not a network.NatGateway", parameters)
}

future, err := ac.natgateways.CreateOrUpdate(ctx, spec.ResourceGroupName(), spec.ResourceName(), natGateway)
Expand All @@ -113,7 +85,6 @@ func (ac *azureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.Resou
}

result, err := future.Result(ac.natgateways)

// if the operation completed, return a nil future
return result, nil, err
}
Expand Down
130 changes: 0 additions & 130 deletions azure/services/natgateways/mock_natgateways/client_mock.go

This file was deleted.

2 changes: 0 additions & 2 deletions azure/services/natgateways/mock_natgateways/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ limitations under the License.
*/

// Run go generate to regenerate this mock.
//go:generate ../../../../hack/tools/bin/mockgen -destination client_mock.go -package mock_natgateways -source ../client.go Client
//go:generate ../../../../hack/tools/bin/mockgen -destination natgateways_mock.go -package mock_natgateways -source ../natgateways.go NatGatewayScope
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt client_mock.go > _client_mock.go && mv _client_mock.go client_mock.go"
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt natgateways_mock.go > _natgateways_mock.go && mv _natgateways_mock.go natgateways_mock.go"
package mock_natgateways //nolint
12 changes: 12 additions & 0 deletions azure/services/natgateways/mock_natgateways/natgateways_mock.go

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

55 changes: 31 additions & 24 deletions azure/services/natgateways/natgateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ type NatGatewayScope interface {
logr.Logger
azure.ClusterScoper
azure.AsyncStatusUpdater
SetNatGatewayIDInSubnets(natGatewayName string, natGatewayID string)
NatGatewaySpecs() []azure.ResourceSpecGetter
}

// Service provides operations on azure resources.
type Service struct {
Scope NatGatewayScope
client
async.Reconciler
}

// New creates a new service.
func New(scope NatGatewayScope) *Service {
client := newClient(scope)
return &Service{
Scope: scope,
client: newClient(scope),
Scope: scope,
Reconciler: async.New(scope, client, client),
}
}

Expand All @@ -63,26 +65,33 @@ func (s *Service) Reconcile(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultAzureServiceReconcileTimeout)
defer cancel()

if !s.Scope.Vnet().IsManaged(s.Scope.ClusterName()) {
s.Scope.V(4).Info("Skipping nat gateways reconcile in custom vnet mode")

s.Scope.UpdatePutStatus(infrav1.NATGatewaysReadyCondition, serviceName, nil)
return nil
}

// We go through the list of NatGatewaySpecs to reconcile each one, independently of the resultingErr of the previous one.
// If multiple errors occur, we return the most pressing one
// order of precedence is: error creating -> creating in progress -> created (no error)
var resultingErr error
for _, natGatewaySpec := range s.Scope.NatGatewaySpecs() {
natGateway, err := async.CreateResource(ctx, s.Scope, s.client, natGatewaySpec, serviceName)
result, err := s.CreateResource(ctx, natGatewaySpec, serviceName)
if err != nil {
if !azure.IsOperationNotDoneError(err) || resultingErr == nil {
resultingErr = err
}
}
if err == nil && resultingErr != nil {
networkNatGateway, ok := natGateway.(network.NatGateway)
if err == nil {
natGateway, ok := result.(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)
return errors.Errorf("created resource %T is not a network.NatGateway", result)
}

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

Expand All @@ -98,27 +107,25 @@ func (s *Service) Delete(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultAzureServiceReconcileTimeout)
defer cancel()

var result error
if !s.Scope.Vnet().IsManaged(s.Scope.ClusterName()) {
s.Scope.V(4).Info("Skipping nat gateway deletion in custom vnet mode")

s.Scope.UpdateDeleteStatus(infrav1.NATGatewaysReadyCondition, serviceName, nil)
return nil
}

var resultingErr error

// We go through the list of NatGatewaySpecs to delete each one, independently of the result of the previous one.
// We go through the list of NatGatewaySpecs to delete each one, independently of the resultingErr of the previous one.
// If multiple errors occur, we return the most pressing one
// order of precedence is: error deleting -> deleting in progress -> deleted (no error)
for _, natGatewaySpec := range s.Scope.NatGatewaySpecs() {
if err := async.DeleteResource(ctx, s.Scope, s.client, natGatewaySpec, serviceName); err != nil {
if !azure.IsOperationNotDoneError(err) || result == nil {
result = err
if err := s.DeleteResource(ctx, natGatewaySpec, serviceName); err != nil {
if !azure.IsOperationNotDoneError(err) || resultingErr == nil {
resultingErr = err
}
}
}
s.Scope.UpdateDeleteStatus(infrav1.NATGatewaysReadyCondition, serviceName, result)
return result
}

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.Scope.UpdateDeleteStatus(infrav1.NATGatewaysReadyCondition, serviceName, resultingErr)
return resultingErr
}
Loading

0 comments on commit 494e77d

Please sign in to comment.