Skip to content

Commit

Permalink
fix: wrong node matching when detaching dangling node
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Jan 8, 2025
1 parent 67c45d2 commit 85f9496
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/azuredisk/azure_controller_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (c *controllerCommon) AttachDisk(ctx context.Context, diskName, diskURI str
if err != nil {
return -1, err
}
if strings.EqualFold(string(nodeName), string(attachedNode)) {
if isSameNode(string(nodeName), string(attachedNode)) {
klog.Warningf("volume %s is actually attached to current node %s, invalidate vm cache and return error", diskURI, nodeName)
// update VM(invalidate vm cache)
if errUpdate := c.UpdateVM(ctx, nodeName); errUpdate != nil {
Expand Down
35 changes: 35 additions & 0 deletions pkg/azuredisk/azuredisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,38 @@ func checkAllocatable(ctx context.Context, clientset kubernetes.Interface, nodeN

return fmt.Errorf("isAllocatableSet: driver not found on node %s", nodeName)
}

// isSameNode checks whether node1 and node2 are the same node
func isSameNode(node1, node2 string) bool {
if strings.EqualFold(node1, node2) {
return true
}
// check whether node1 and node2 are VMSS instance name
if len(node1) < 6 || len(node2) < 6 {
return false
}

// machineName is composed of computerNamePrefix and 36-based instanceID.
// And instanceID part if in fixed length of 6 characters.
// Refer https://msftstack.wordpress.com/2017/05/10/figuring-out-azure-vm-scale-set-machine-names/.
if strings.EqualFold(node1[:len(node1)-6], node2[:len(node2)-6]) {
// check whether node1 is 36-based instanceID and node2 is 10-based instanceID
id1, err := strconv.ParseUint(node1[len(node1)-6:], 36, 64)
if err == nil {
id2, err := strconv.ParseUint(node2[len(node2)-6:], 10, 64)
if err == nil && id1 == id2 {
return true
}
}

// check whether node1 is 10-based instanceID and node2 is 36-based instanceID
id1, err = strconv.ParseUint(node1[len(node1)-6:], 10, 64)
if err == nil {
id2, err := strconv.ParseUint(node2[len(node2)-6:], 36, 64)
if err == nil && id1 == id2 {
return true
}
}
}
return false
}
52 changes: 52 additions & 0 deletions pkg/azuredisk/azuredisk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,55 @@ func TestGetUsedLunsFromNode(t *testing.T) {
}
}
}

func TestIsSameNode(t *testing.T) {
tests := []struct {
name string
nodeName string
nodeName2 string
expectedValue bool
}{
{
name: "same node",
nodeName: "test-node",
nodeName2: "test-node",
expectedValue: true,
},
{
name: "different node with shorter name",
nodeName: "node1",
nodeName2: "node2",
expectedValue: false,
},
{
name: "different node",
nodeName: "test-node",
nodeName2: "test-node2",
expectedValue: false,
},
{
name: "empty node",
nodeName: "",
nodeName2: "test-node",
expectedValue: false,
},
{
name: "node1 is 10 based instance name, node2 is 36 based instance name",
nodeName: "aks-agentpool-20657377-vmss000012",
nodeName2: "aks-agentpool-20657377-vmss00000c",
expectedValue: true,
},
{
name: "node1 is 36 based instance name, node2 is 10 based instance name",
nodeName: "aks-agentpool-20657377-vmss00000c",
nodeName2: "aks-agentpool-20657377-vmss000012",
expectedValue: true,
},
}
for _, test := range tests {
result := isSameNode(test.nodeName, test.nodeName2)
if result != test.expectedValue {
t.Errorf("test(%s): result(%v) != expected result(%v)", test.name, result, test.expectedValue)
}
}
}

0 comments on commit 85f9496

Please sign in to comment.