sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/disks/disks.go (about)

     1  /*
     2  Copyright 2019 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 disks
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
    23  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    24  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    25  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/async"
    26  	"sigs.k8s.io/cluster-api-provider-azure/util/tele"
    27  )
    28  
    29  const serviceName = "disks"
    30  
    31  // DiskScope defines the scope interface for a disk service.
    32  type DiskScope interface {
    33  	azure.ClusterDescriber
    34  	azure.AsyncStatusUpdater
    35  	DiskSpecs() []azure.ResourceSpecGetter
    36  }
    37  
    38  // Service provides operations on Azure resources.
    39  type Service struct {
    40  	Scope DiskScope
    41  	async.Reconciler
    42  }
    43  
    44  // New creates a disks service.
    45  func New(scope DiskScope) (*Service, error) {
    46  	client, err := newClient(scope, scope.DefaultedAzureCallTimeout())
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return &Service{
    51  		Scope: scope,
    52  		Reconciler: async.New[armcompute.DisksClientCreateOrUpdateResponse,
    53  			armcompute.DisksClientDeleteResponse](scope, nil, client),
    54  	}, nil
    55  }
    56  
    57  // Name returns the service name.
    58  func (s *Service) Name() string {
    59  	return serviceName
    60  }
    61  
    62  // Reconcile on disk is currently no-op. OS disks should only be deleted and will create with the VM automatically.
    63  func (s *Service) Reconcile(ctx context.Context) error {
    64  	_, _, done := tele.StartSpanWithLogger(ctx, "disks.Service.Reconcile")
    65  	defer done()
    66  
    67  	// DisksReadyCondition is set in the VM service.
    68  	return nil
    69  }
    70  
    71  // Delete deletes the disk associated with a VM.
    72  func (s *Service) Delete(ctx context.Context) error {
    73  	ctx, _, done := tele.StartSpanWithLogger(ctx, "disks.Service.Delete")
    74  	defer done()
    75  
    76  	ctx, cancel := context.WithTimeout(ctx, s.Scope.DefaultedAzureServiceReconcileTimeout())
    77  	defer cancel()
    78  
    79  	specs := s.Scope.DiskSpecs()
    80  	if len(specs) == 0 {
    81  		return nil
    82  	}
    83  
    84  	// We go through the list of DiskSpecs to delete each one, independently of the result of the previous one.
    85  	// If multiple errors occur, we return the most pressing one.
    86  	//  Order of precedence (highest -> lowest) is: error that is not an operationNotDoneError (i.e. error creating) -> operationNotDoneError (i.e. creating in progress) -> no error (i.e. created)
    87  	var result error
    88  	for _, diskSpec := range specs {
    89  		if err := s.DeleteResource(ctx, diskSpec, serviceName); err != nil {
    90  			if !azure.IsOperationNotDoneError(err) || result == nil {
    91  				result = err
    92  			}
    93  		}
    94  	}
    95  	s.Scope.UpdateDeleteStatus(infrav1.DisksReadyCondition, serviceName, result)
    96  	return result
    97  }
    98  
    99  // IsManaged returns always returns true as CAPZ does not support BYO disk.
   100  func (s *Service) IsManaged(ctx context.Context) (bool, error) {
   101  	return true, nil
   102  }