From 9f5e3a25e409737cf37ae6dbbe650e28b9d8b3e9 Mon Sep 17 00:00:00 2001 From: Anthony Weston Date: Tue, 17 Jun 2025 15:46:54 -0700 Subject: [PATCH] Remove unnecessary disk service and delete step --- cloud/services/disks/disks.go | 100 ------------------ cloud/services/disks/service.go | 48 --------- .../azurestackhcivirtualmachine_reconciler.go | 26 ----- 3 files changed, 174 deletions(-) delete mode 100644 cloud/services/disks/disks.go delete mode 100644 cloud/services/disks/service.go diff --git a/cloud/services/disks/disks.go b/cloud/services/disks/disks.go deleted file mode 100644 index 199256e2..00000000 --- a/cloud/services/disks/disks.go +++ /dev/null @@ -1,100 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. -Portions Copyright © Microsoft Corporation. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package disks - -import ( - "context" - - azurestackhci "github.com/microsoft/cluster-api-provider-azurestackhci/cloud" - "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/telemetry" - "github.com/microsoft/moc-sdk-for-go/services/storage" - "github.com/pkg/errors" -) - -// Spec specification for disk -type Spec struct { - Name string - Source string -} - -// Get provides information about a disk. -func (s *Service) Get(ctx context.Context, spec interface{}) (interface{}, error) { - diskSpec, ok := spec.(*Spec) - if !ok { - return storage.VirtualHardDisk{}, errors.New("Invalid Disk Specification") - } - disk, err := s.Client.Get(ctx, s.Scope.GetResourceGroup(), "", diskSpec.Name) - if err != nil { - return nil, err - } - return (*disk)[0], nil -} - -// Reconcile gets/creates/updates a disk. -func (s *Service) Reconcile(ctx context.Context, spec interface{}) error { - telemetry.WriteMocInfoLog(ctx, s.Scope) - diskSpec, ok := spec.(*Spec) - if !ok { - return errors.New("Invalid Disk Specification") - } - - if _, err := s.Get(ctx, diskSpec); err == nil { - // disk already exists, cannot update since its immutable - return nil - } - - logger := s.Scope.GetLogger() - logger.Info("creating disk", "name", diskSpec.Name) - _, err := s.Client.CreateOrUpdate(ctx, s.Scope.GetResourceGroup(), "", diskSpec.Name, - &storage.VirtualHardDisk{ - Name: &diskSpec.Name, - VirtualHardDiskProperties: &storage.VirtualHardDiskProperties{}, - }) - telemetry.WriteMocOperationLog(logger, telemetry.CreateOrUpdate, s.Scope.GetCustomResourceTypeWithName(), telemetry.Disk, - telemetry.GenerateMocResourceName(s.Scope.GetResourceGroup(), diskSpec.Name), nil, err) - if err != nil { - return err - } - - logger.Info("successfully created disk", "name", diskSpec.Name) - return err -} - -// Delete deletes the disk associated with a VM. -func (s *Service) Delete(ctx context.Context, spec interface{}) error { - telemetry.WriteMocInfoLog(ctx, s.Scope) - diskSpec, ok := spec.(*Spec) - if !ok { - return errors.New("Invalid disk specification") - } - logger := s.Scope.GetLogger() - logger.Info("deleting disk", "name", diskSpec.Name) - err := s.Client.Delete(ctx, s.Scope.GetResourceGroup(), "", diskSpec.Name) - telemetry.WriteMocOperationLog(logger, telemetry.Delete, s.Scope.GetCustomResourceTypeWithName(), telemetry.Disk, - telemetry.GenerateMocResourceName(s.Scope.GetResourceGroup(), diskSpec.Name), nil, err) - if err != nil && azurestackhci.ResourceNotFound(err) { - // already deleted - return nil - } - if err != nil { - return errors.Wrapf(err, "failed to delete disk %s in resource group %s", diskSpec.Name, s.Scope.GetResourceGroup()) - } - - logger.Info("successfully deleted disk", "name", diskSpec.Name) - return err -} diff --git a/cloud/services/disks/service.go b/cloud/services/disks/service.go deleted file mode 100644 index 592599d4..00000000 --- a/cloud/services/disks/service.go +++ /dev/null @@ -1,48 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. -Portions Copyright © Microsoft Corporation. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package disks - -import ( - //"github.com/Azure/go-autorest/autorest" - azurestackhci "github.com/microsoft/cluster-api-provider-azurestackhci/cloud" - "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/scope" - "github.com/microsoft/moc-sdk-for-go/services/storage/virtualharddisk" - "github.com/microsoft/moc/pkg/auth" -) - -var _ azurestackhci.Service = (*Service)(nil) - -// Service provides operations on resource groups -type Service struct { - Client virtualharddisk.VirtualHardDiskClient - Scope scope.ScopeInterface -} - -// getDisksClient creates a new disks client. -func getDisksClient(cloudAgentFqdn string, authorizer auth.Authorizer) virtualharddisk.VirtualHardDiskClient { - disksClient, _ := virtualharddisk.NewVirtualHardDiskClient(cloudAgentFqdn, authorizer) - return *disksClient -} - -// NewService creates a new disks service. -func NewService(scope scope.ScopeInterface) *Service { - return &Service{ - Client: getDisksClient(scope.GetCloudAgentFqdn(), scope.GetAuthorizer()), - Scope: scope, - } -} diff --git a/controllers/azurestackhcivirtualmachine_reconciler.go b/controllers/azurestackhcivirtualmachine_reconciler.go index 1c574556..19d5197b 100644 --- a/controllers/azurestackhcivirtualmachine_reconciler.go +++ b/controllers/azurestackhcivirtualmachine_reconciler.go @@ -24,7 +24,6 @@ import ( infrav1 "github.com/microsoft/cluster-api-provider-azurestackhci/api/v1beta1" azurestackhci "github.com/microsoft/cluster-api-provider-azurestackhci/cloud" "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/scope" - "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/services/disks" "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/services/networkinterfaces" "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/services/virtualmachines" infrav1util "github.com/microsoft/cluster-api-provider-azurestackhci/pkg/util" @@ -45,7 +44,6 @@ type azureStackHCIVirtualMachineService struct { vmScope *scope.VirtualMachineScope networkInterfacesSvc azurestackhci.Service virtualMachinesSvc azurestackhci.GetterService - disksSvc azurestackhci.GetterService } // newAzureStackHCIMachineService populates all the services based on input scope @@ -54,7 +52,6 @@ func newAzureStackHCIVirtualMachineService(vmScope *scope.VirtualMachineScope) * vmScope: vmScope, networkInterfacesSvc: networkinterfaces.NewService(vmScope), virtualMachinesSvc: virtualmachines.NewService(vmScope), - disksSvc: disks.NewService(vmScope), } } @@ -137,15 +134,6 @@ func (s *azureStackHCIVirtualMachineService) Delete() error { return errors.Wrapf(err, "Unable to delete network interface") } - diskSpec := &disks.Spec{ - Name: azurestackhci.GenerateOSDiskName(s.vmScope.Name()), - } - - err = s.disksSvc.Delete(s.vmScope.Context, diskSpec) - if err != nil { - return errors.Wrapf(err, "Unable to delete os disk of machine %s", s.vmScope.Name()) - } - return nil } @@ -214,20 +202,6 @@ func (s *azureStackHCIVirtualMachineService) getVirtualMachineZone() (string, er return "", nil } -func (s *azureStackHCIVirtualMachineService) reconcileDisk(disk infrav1.OSDisk) error { - diskSpec := &disks.Spec{ - Name: azurestackhci.GenerateOSDiskName(s.vmScope.Name()), //disk.Name, - Source: disk.Source, - } - - err := s.disksSvc.Reconcile(s.vmScope.Context, diskSpec) - if err != nil { - return errors.Wrap(err, "unable to create VM OS disk") - } - - return err -} - func (s *azureStackHCIVirtualMachineService) reconcileNetworkInterface(nicName string, ipconfigs networkinterfaces.IPConfigurations) error { networkInterfaceSpec := &networkinterfaces.Spec{ Name: nicName,