@@ -163,9 +163,15 @@ func (c *client) WaitForVolumeAttached(volumeAttachmentId string) (core.VolumeAt
163163// ATTACHING or ATTACHED and returns the first volume attachment found.
164164func (c * client ) FindVolumeAttachment (volumeId string ) (core.VolumeAttachment , error ) {
165165 var page * string
166+
167+ vcnCompartment , err := c .getVCNCompartment ()
168+ if err != nil {
169+ return nil , err
170+ }
171+
166172 for {
167173 request := core.ListVolumeAttachmentsRequest {
168- CompartmentId : & c . config . Auth . CompartmentOCID ,
174+ CompartmentId : vcnCompartment ,
169175 Page : page ,
170176 VolumeId : & volumeId ,
171177 }
@@ -195,12 +201,25 @@ func (c *client) FindVolumeAttachment(volumeId string) (core.VolumeAttachment, e
195201 return nil , fmt .Errorf ("failed to find volume attachment for %q" , volumeId )
196202}
197203
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 ) {
199217 var page * string
200218 subnetList := []core.Subnet {}
219+
201220 for {
202221 request := core.ListSubnetsRequest {
203- CompartmentId : & c . config . Auth . CompartmentOCID ,
222+ CompartmentId : vcnCompartment ,
204223 VcnId : & c .config .Auth .VcnOCID ,
205224 Page : page ,
206225 }
@@ -231,16 +250,16 @@ func (c *client) isVnicAttachmentInSubnets(vnicAttachment *core.VnicAttachment,
231250 return false
232251}
233252
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.
236255// https://kubernetes.io/docs/concepts/architecture/nodes/#management
237256// So if the displayname doesn't match the nodename then
238257// 1) get the IP of the node name doing a reverse lookup and see if we can find it.
239258// I'm leaving the DNS lookup till later as the options below fix the OKE issue
240259// 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 )
244263 if err != nil {
245264 log .Printf ("Error getting subnets for VCN: %s" , c .config .Auth .VcnOCID )
246265 return nil , err
@@ -253,7 +272,7 @@ func (c *client) findInstanceByNodeNameIsVnic(cache *cache.OCICache, nodeName st
253272 var page * string
254273 for {
255274 vnicAttachmentsRequest := core.ListVnicAttachmentsRequest {
256- CompartmentId : & c . config . Auth . CompartmentOCID ,
275+ CompartmentId : compartment ,
257276 Page : page ,
258277 }
259278 vnicAttachments , err := func () (core.ListVnicAttachmentsResponse , error ) {
@@ -318,12 +337,14 @@ func (c *client) findInstanceByNodeNameIsVnic(cache *cache.OCICache, nodeName st
318337 return & running [0 ], nil
319338}
320339
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 ) {
322343 var running []core.Instance
323344 var page * string
324345 for {
325346 listInstancesRequest := core.ListInstancesRequest {
326- CompartmentId : & c . config . Auth . CompartmentOCID ,
347+ CompartmentId : compartment ,
327348 DisplayName : & nodeName ,
328349 Page : page ,
329350 }
@@ -373,18 +394,23 @@ func getCacheDirectory() string {
373394// GetInstanceByNodeName retrieves the corresponding core.Instance or a
374395// SearchError if no instance matching the node name is found.
375396func (c * client ) GetInstanceByNodeName (nodeName string ) (* core.Instance , error ) {
376- log .Printf ("GetInstanceByNodeName:%s" , nodeName )
397+ log .Printf ("GetInstanceByNodeName: %s" , nodeName )
377398 ociCache , err := cache .Open (fmt .Sprintf ("%s/%s" , getCacheDirectory (), "nodenamecache.json" ))
378399 if err != nil {
379400 return nil , err
380401 }
381402 defer ociCache .Close ()
382403
404+ vcnCompartment , err := c .getVCNCompartment ()
405+ if err != nil {
406+ return nil , err
407+ }
408+
383409 // Cache lookup failed so time to refill the cache
384- instance , err := c .findInstanceByNodeNameIsDisplayName (nodeName )
410+ instance , err := c .findInstanceByNodeNameIsDisplayName (nodeName , vcnCompartment )
385411 if err != nil {
386412 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 )
388414 if err != nil {
389415 log .Printf ("Unable to find OCI instance by hostname/displayname" )
390416 }
0 commit comments