sigs.k8s.io/cluster-api-provider-azure@v1.14.3/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/util/tele"
    29  )
    30  
    31  // azureMachinePoolService is the group of services called by the AzureMachinePool controller.
    32  type azureMachinePoolService struct {
    33  	scope    *scope.MachinePoolScope
    34  	skuCache *resourceskus.Cache
    35  	services []azure.ServiceReconciler
    36  }
    37  
    38  // newAzureMachinePoolService populates all the services based on input scope.
    39  func newAzureMachinePoolService(machinePoolScope *scope.MachinePoolScope) (*azureMachinePoolService, error) {
    40  	cache, err := resourceskus.GetCache(machinePoolScope, machinePoolScope.Location())
    41  	if err != nil {
    42  		return nil, errors.Wrap(err, "failed to create a NewCache")
    43  	}
    44  	roleAssignmentsSvc, err := roleassignments.New(machinePoolScope)
    45  	if err != nil {
    46  		return nil, errors.Wrap(err, "failed to create a roleassignments service")
    47  	}
    48  	scaleSetsSvc, err := scalesets.New(machinePoolScope, cache)
    49  	if err != nil {
    50  		return nil, errors.Wrap(err, "failed to create a scalesets service")
    51  	}
    52  
    53  	return &azureMachinePoolService{
    54  		scope: machinePoolScope,
    55  		services: []azure.ServiceReconciler{
    56  			scaleSetsSvc,
    57  			roleAssignmentsSvc,
    58  		},
    59  		skuCache: cache,
    60  	}, nil
    61  }
    62  
    63  // Reconcile reconciles all the services in pre determined order.
    64  func (s *azureMachinePoolService) Reconcile(ctx context.Context) error {
    65  	ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureMachinePoolService.Reconcile")
    66  	defer done()
    67  
    68  	// Ensure that the deprecated networking field values have been migrated to the new NetworkInterfaces field.
    69  	s.scope.AzureMachinePool.SetNetworkInterfacesDefaults()
    70  
    71  	if err := s.scope.SetSubnetName(); err != nil {
    72  		return errors.Wrap(err, "failed defaulting subnet name")
    73  	}
    74  
    75  	for _, service := range s.services {
    76  		if err := service.Reconcile(ctx); err != nil {
    77  			return errors.Wrapf(err, "failed to reconcile AzureMachinePool service %s", service.Name())
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  // Pause pauses all the services.
    85  func (s *azureMachinePoolService) Pause(ctx context.Context) error {
    86  	ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureMachinePoolService.Pause")
    87  	defer done()
    88  
    89  	for _, service := range s.services {
    90  		pauser, ok := service.(azure.Pauser)
    91  		if !ok {
    92  			continue
    93  		}
    94  		if err := pauser.Pause(ctx); err != nil {
    95  			return errors.Wrapf(err, "failed to pause AzureMachinePool service %s", service.Name())
    96  		}
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  // Delete reconciles all the services in pre determined order.
   103  func (s *azureMachinePoolService) Delete(ctx context.Context) error {
   104  	ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureMachinePoolService.Delete")
   105  	defer done()
   106  
   107  	// Delete services in reverse order of creation.
   108  	for i := len(s.services) - 1; i >= 0; i-- {
   109  		if err := s.services[i].Delete(ctx); err != nil {
   110  			return errors.Wrapf(err, "failed to delete AzureMachinePool service %s", s.services[i].Name())
   111  		}
   112  	}
   113  
   114  	return nil
   115  }