github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/cmd/ndm_daemonset/probe/deletehandler.go (about)

     1  /*
     2  Copyright 2020 The OpenEBS 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 probe
    18  
    19  import (
    20  	apis "github.com/openebs/node-disk-manager/api/v1alpha1"
    21  	"github.com/openebs/node-disk-manager/blockdevice"
    22  
    23  	"k8s.io/klog/v2"
    24  )
    25  
    26  // removeBlockDeviceFromHierarchyCache removes a block device from the hierarchy.
    27  // returns true if the device existed in the cache, else returns false
    28  func (pe *ProbeEvent) removeBlockDeviceFromHierarchyCache(bd blockdevice.BlockDevice) bool {
    29  	_, ok := pe.Controller.BDHierarchy[bd.DevPath]
    30  	if !ok {
    31  		klog.Infof("Disk %s not in hierarchy", bd.DevPath)
    32  		// not in hierarchy continue
    33  		return false
    34  	}
    35  	// remove from the hierarchy
    36  	delete(pe.Controller.BDHierarchy, bd.DevPath)
    37  	return true
    38  }
    39  
    40  // deleteBlockDevice marks the block device resource as inactive
    41  // The following cases are handled
    42  //	1. Device using legacy UUID
    43  //	2. Device using GPT UUID
    44  //	3. Device using partition table UUID (zfs localPV)
    45  //  4. Device using the partition table / fs uuid annotation
    46  func (pe *ProbeEvent) deleteBlockDevice(bd blockdevice.BlockDevice, bdAPIList *apis.BlockDeviceList) error {
    47  
    48  	if !pe.removeBlockDeviceFromHierarchyCache(bd) {
    49  		return nil
    50  	}
    51  
    52  	// try with gpt uuid
    53  	if uuid, ok := generateUUID(bd); ok {
    54  		existingBD := pe.Controller.GetExistingBlockDeviceResource(bdAPIList, uuid)
    55  		if existingBD != nil {
    56  			pe.Controller.DeactivateBlockDevice(*existingBD)
    57  			klog.V(4).Infof("deactivated device: %s, using GPT UUID", bd.DevPath)
    58  			return nil
    59  		}
    60  		// uuid could be generated, but the disk may be using the legacy scheme
    61  	}
    62  
    63  	// try with partition table uuid - for zfs local pV
    64  	if partUUID, ok := generateUUIDFromPartitionTable(bd); ok {
    65  		existingBD := pe.Controller.GetExistingBlockDeviceResource(bdAPIList, partUUID)
    66  		if existingBD != nil {
    67  			pe.Controller.DeactivateBlockDevice(*existingBD)
    68  			klog.V(4).Infof("deactivated device: %s, using partition table UUID", bd.DevPath)
    69  			return nil
    70  		}
    71  	}
    72  
    73  	// try with FSUUID annotation
    74  	if existingBD := getExistingBDWithFsUuid(bd, bdAPIList); existingBD != nil {
    75  		pe.Controller.DeactivateBlockDevice(*existingBD)
    76  		klog.V(4).Infof("deactivated device: %s, using FS UUID annotation", bd.DevPath)
    77  		return nil
    78  	}
    79  
    80  	// try with partition uuid annotation
    81  	// if the device is a partition, the parent can get deactivated because of search by partition UUID.
    82  	// Therefore the search result is used only if the device is not a partition.
    83  	if existingBD := getExistingBDWithPartitionUUID(bd, bdAPIList); bd.DeviceAttributes.DeviceType != blockdevice.BlockDeviceTypePartition &&
    84  		existingBD != nil {
    85  		pe.Controller.DeactivateBlockDevice(*existingBD)
    86  		klog.V(4).Infof("deactivated device: %s, using Partition UUID annotation", bd.DevPath)
    87  		return nil
    88  	}
    89  
    90  	// try with legacy uuid
    91  	legacyUUID, _ := generateLegacyUUID(bd)
    92  	existingBD := pe.Controller.GetExistingBlockDeviceResource(bdAPIList, legacyUUID)
    93  	if existingBD != nil {
    94  		pe.Controller.DeactivateBlockDevice(*existingBD)
    95  		klog.V(4).Infof("deactivated device: %s, using legacy UUID", bd.DevPath)
    96  		return nil
    97  	}
    98  
    99  	return nil
   100  }