sigs.k8s.io/cluster-api-provider-azure@v1.17.0/exp/controllers/azuremachinepool_reconciler.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package controllers
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    24  	"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
    25  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
    26  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
    27  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/scalesets"
    28  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
    29  	"sigs.k8s.io/cluster-api-provider-azure/util/tele"
    30  )
    31  
    32  // azureMachinePoolService is the group of services called by the AzureMachinePool controller.
    33  type azureMachinePoolService struct {
    34  	scope    *scope.MachinePoolScope
    35  	skuCache *resourceskus.Cache
    36  	services []azure.ServiceReconciler
    37  }
    38  
    39  // newAzureMachinePoolService populates all the services based on input scope.
    40  func newAzureMachinePoolService(machinePoolScope *scope.MachinePoolScope) (*azureMachinePoolService, error) {
    41  	cache, err := resourceskus.GetCache(machinePoolScope, machinePoolScope.Location())
    42  	if err != nil {
    43  		return nil, errors.Wrap(err, "failed to create a NewCache")
    44  	}
    45  	roleAssignmentsSvc, err := roleassignments.New(machinePoolScope)
    46  	if err != nil {
    47  		return nil, errors.Wrap(err, "failed to create a roleassignments service")
    48  	}
    49  	scaleSetsSvc, err := scalesets.New(machinePoolScope, cache)
    50  	if err != nil {
    51  		return nil, errors.Wrap(err, "failed to create a scalesets service")
    52  	}
    53  	tagsSvc, err := tags.New(machinePoolScope)
    54  	if err != nil {
    55  		return nil, errors.Wrap(err, "failed creating tags service")
    56  	}
    57  
    58  	return &azureMachinePoolService{
    59  		scope: machinePoolScope,
    60  		services: []azure.ServiceReconciler{
    61  			scaleSetsSvc,
    62  			roleAssignmentsSvc,
    63  			tagsSvc,
    64  		},
    65  		skuCache: cache,
    66  	}, nil
    67  }
    68  
    69  // Reconcile reconciles all the services in pre determined order.
    70  func (s *azureMachinePoolService) Reconcile(ctx context.Context) error {
    71  	ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureMachinePoolService.Reconcile")
    72  	defer done()
    73  
    74  	// Ensure that the deprecated networking field values have been migrated to the new NetworkInterfaces field.
    75  	s.scope.AzureMachinePool.SetNetworkInterfacesDefaults()
    76  
    77  	if err := s.scope.SetSubnetName(); err != nil {
    78  		return errors.Wrap(err, "failed defaulting subnet name")
    79  	}
    80  
    81  	for _, service := range s.services {
    82  		if err := service.Reconcile(ctx); err != nil {
    83  			return errors.Wrapf(err, "failed to reconcile AzureMachinePool service %s", service.Name())
    84  		}
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  // Pause pauses all the services.
    91  func (s *azureMachinePoolService) Pause(ctx context.Context) error {
    92  	ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureMachinePoolService.Pause")
    93  	defer done()
    94  
    95  	for _, service := range s.services {
    96  		pauser, ok := service.(azure.Pauser)
    97  		if !ok {
    98  			continue
    99  		}
   100  		if err := pauser.Pause(ctx); err != nil {
   101  			return errors.Wrapf(err, "failed to pause AzureMachinePool service %s", service.Name())
   102  		}
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  // Delete reconciles all the services in pre determined order.
   109  func (s *azureMachinePoolService) Delete(ctx context.Context) error {
   110  	ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureMachinePoolService.Delete")
   111  	defer done()
   112  
   113  	// Delete services in reverse order of creation.
   114  	for i := len(s.services) - 1; i >= 0; i-- {
   115  		if err := s.services[i].Delete(ctx); err != nil {
   116  			return errors.Wrapf(err, "failed to delete AzureMachinePool service %s", s.services[i].Name())
   117  		}
   118  	}
   119  
   120  	return nil
   121  }