@@ -19,13 +19,15 @@ package azure
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "path"
22
23
"time"
23
24
24
25
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
25
26
26
27
"k8s.io/apimachinery/pkg/types"
27
28
kwait "k8s.io/apimachinery/pkg/util/wait"
28
29
cloudprovider "k8s.io/cloud-provider"
30
+ volerr "k8s.io/cloud-provider/volume/errors"
29
31
"k8s.io/klog"
30
32
"k8s.io/utils/keymutex"
31
33
)
@@ -93,6 +95,32 @@ func (c *controllerCommon) getNodeVMSet(nodeName types.NodeName) (VMSet, error)
93
95
// AttachDisk attaches a vhd to vm. The vhd must exist, can be identified by diskName, diskURI.
94
96
// return (lun, error)
95
97
func (c * controllerCommon ) AttachDisk (isManagedDisk bool , diskName , diskURI string , nodeName types.NodeName , cachingMode compute.CachingTypes ) (int32 , error ) {
98
+ if isManagedDisk {
99
+ diskName := path .Base (diskURI )
100
+ resourceGroup , err := getResourceGroupFromDiskURI (diskURI )
101
+ if err != nil {
102
+ return - 1 , err
103
+ }
104
+
105
+ ctx , cancel := getContextWithCancel ()
106
+ defer cancel ()
107
+
108
+ disk , err := c .cloud .DisksClient .Get (ctx , resourceGroup , diskName )
109
+ if err != nil {
110
+ return - 1 , err
111
+ }
112
+
113
+ if disk .ManagedBy != nil {
114
+ attachErr := fmt .Sprintf (
115
+ "disk(%s) already attached to node(%s), could not be attached to node(%s)" ,
116
+ diskURI , * disk .ManagedBy , nodeName )
117
+ attachedNode := path .Base (* disk .ManagedBy )
118
+ klog .V (2 ).Infof ("found dangling volume %s attached to node %s" , diskURI , attachedNode )
119
+ danglingErr := volerr .NewDanglingError (attachErr , types .NodeName (attachedNode ), "" )
120
+ return - 1 , danglingErr
121
+ }
122
+ }
123
+
96
124
vmset , err := c .getNodeVMSet (nodeName )
97
125
if err != nil {
98
126
return - 1 , err
0 commit comments