@@ -163,9 +163,15 @@ func (c *client) WaitForVolumeAttached(volumeAttachmentId string) (core.VolumeAt
163
163
// ATTACHING or ATTACHED and returns the first volume attachment found.
164
164
func (c * client ) FindVolumeAttachment (volumeId string ) (core.VolumeAttachment , error ) {
165
165
var page * string
166
+
167
+ vcnCompartment , err := c .getVCNCompartment ()
168
+ if err != nil {
169
+ return nil , err
170
+ }
171
+
166
172
for {
167
173
request := core.ListVolumeAttachmentsRequest {
168
- CompartmentId : & c . config . Auth . CompartmentOCID ,
174
+ CompartmentId : vcnCompartment ,
169
175
Page : page ,
170
176
VolumeId : & volumeId ,
171
177
}
@@ -195,12 +201,25 @@ func (c *client) FindVolumeAttachment(volumeId string) (core.VolumeAttachment, e
195
201
return nil , fmt .Errorf ("failed to find volume attachment for %q" , volumeId )
196
202
}
197
203
198
- func (c * client ) getAllSubnetsForVNC () (* []core.Subnet , error ) {
204
+ func (c * client ) getVCNCompartment () (* string , error ) {
205
+ ctx , cancel := context .WithTimeout (c .ctx , time .Minute )
206
+ defer cancel ()
207
+
208
+ vcn , err := c .network .GetVcn (ctx , core.GetVcnRequest {VcnId : & c .config .Auth .VcnOCID })
209
+ if err != nil {
210
+ return nil , err
211
+ }
212
+
213
+ return vcn .CompartmentId , nil
214
+ }
215
+
216
+ func (c * client ) getAllSubnetsForVCN (vcnCompartment * string ) (* []core.Subnet , error ) {
199
217
var page * string
200
218
subnetList := []core.Subnet {}
219
+
201
220
for {
202
221
request := core.ListSubnetsRequest {
203
- CompartmentId : & c . config . Auth . CompartmentOCID ,
222
+ CompartmentId : vcnCompartment ,
204
223
VcnId : & c .config .Auth .VcnOCID ,
205
224
Page : page ,
206
225
}
@@ -231,16 +250,16 @@ func (c *client) isVnicAttachmentInSubnets(vnicAttachment *core.VnicAttachment,
231
250
return false
232
251
}
233
252
234
- // findInstanceByNodeNameIsVnic try to find the BM Instance
235
- // // it makes the assumption that he nodename has to be resolvable
253
+ // findInstanceByNodeNameIsVNIC tries to find an OCI Instance to attach a volume to.
254
+ // It makes the assumption that the nodename has to be resolvable.
236
255
// https://kubernetes.io/docs/concepts/architecture/nodes/#management
237
256
// So if the displayname doesn't match the nodename then
238
257
// 1) get the IP of the node name doing a reverse lookup and see if we can find it.
239
258
// I'm leaving the DNS lookup till later as the options below fix the OKE issue
240
259
// 2) see if the nodename is equal to the hostname label
241
- // 3) see if the nodename is an ip
242
- func (c * client ) findInstanceByNodeNameIsVnic (cache * cache.OCICache , nodeName string ) (* core.Instance , error ) {
243
- subnets , err := c .getAllSubnetsForVNC ( )
260
+ // 3) see if the nodename is an IP
261
+ func (c * client ) findInstanceByNodeNameIsVNIC (cache * cache.OCICache , nodeName string , compartment * string ) (* core.Instance , error ) {
262
+ subnets , err := c .getAllSubnetsForVCN ( compartment )
244
263
if err != nil {
245
264
log .Printf ("Error getting subnets for VCN: %s" , c .config .Auth .VcnOCID )
246
265
return nil , err
@@ -253,7 +272,7 @@ func (c *client) findInstanceByNodeNameIsVnic(cache *cache.OCICache, nodeName st
253
272
var page * string
254
273
for {
255
274
vnicAttachmentsRequest := core.ListVnicAttachmentsRequest {
256
- CompartmentId : & c . config . Auth . CompartmentOCID ,
275
+ CompartmentId : compartment ,
257
276
Page : page ,
258
277
}
259
278
vnicAttachments , err := func () (core.ListVnicAttachmentsResponse , error ) {
@@ -318,12 +337,14 @@ func (c *client) findInstanceByNodeNameIsVnic(cache *cache.OCICache, nodeName st
318
337
return & running [0 ], nil
319
338
}
320
339
321
- func (c * client ) findInstanceByNodeNameIsDisplayName (nodeName string ) (* core.Instance , error ) {
340
+ // findInstanceByNodeNameIsDisplayName returns the first running instance where the display name and node name match.
341
+ // If no instance is found we return an error.
342
+ func (c * client ) findInstanceByNodeNameIsDisplayName (nodeName string , compartment * string ) (* core.Instance , error ) {
322
343
var running []core.Instance
323
344
var page * string
324
345
for {
325
346
listInstancesRequest := core.ListInstancesRequest {
326
- CompartmentId : & c . config . Auth . CompartmentOCID ,
347
+ CompartmentId : compartment ,
327
348
DisplayName : & nodeName ,
328
349
Page : page ,
329
350
}
@@ -373,18 +394,23 @@ func getCacheDirectory() string {
373
394
// GetInstanceByNodeName retrieves the corresponding core.Instance or a
374
395
// SearchError if no instance matching the node name is found.
375
396
func (c * client ) GetInstanceByNodeName (nodeName string ) (* core.Instance , error ) {
376
- log .Printf ("GetInstanceByNodeName:%s" , nodeName )
397
+ log .Printf ("GetInstanceByNodeName: %s" , nodeName )
377
398
ociCache , err := cache .Open (fmt .Sprintf ("%s/%s" , getCacheDirectory (), "nodenamecache.json" ))
378
399
if err != nil {
379
400
return nil , err
380
401
}
381
402
defer ociCache .Close ()
382
403
404
+ vcnCompartment , err := c .getVCNCompartment ()
405
+ if err != nil {
406
+ return nil , err
407
+ }
408
+
383
409
// Cache lookup failed so time to refill the cache
384
- instance , err := c .findInstanceByNodeNameIsDisplayName (nodeName )
410
+ instance , err := c .findInstanceByNodeNameIsDisplayName (nodeName , vcnCompartment )
385
411
if err != nil {
386
412
log .Printf ("Unable to find OCI instance by displayname trying hostname/public ip" )
387
- instance , err = c .findInstanceByNodeNameIsVnic (ociCache , nodeName )
413
+ instance , err = c .findInstanceByNodeNameIsVNIC (ociCache , nodeName , vcnCompartment )
388
414
if err != nil {
389
415
log .Printf ("Unable to find OCI instance by hostname/displayname" )
390
416
}
0 commit comments