Skip to content
This repository was archived by the owner on Jun 23, 2020. It is now read-only.

Commit 71cd111

Browse files
Merge pull request #138 from oracle/ol/fix-137
Lookup subnets in VCN compartment
2 parents cebec7b + f7d63c8 commit 71cd111

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

pkg/oci/client/config.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ type AuthConfig struct {
4242
Region string `yaml:"region"`
4343
RegionKey string `yaml:"regionKey"`
4444
TenancyOCID string `yaml:"tenancy"`
45-
CompartmentOCID string `yaml:"compartment"`
45+
CompartmentOCID string `yaml:"compartment"` // DEPRECATED (we no longer directly use this)
4646
UserOCID string `yaml:"user"`
4747
PrivateKey string `yaml:"key"`
4848
Passphrase string `yaml:"passphrase"`
49-
PrivateKeyPassphrase string `yaml:"key_passphase"` // DEPRECIATED
49+
PrivateKeyPassphrase string `yaml:"key_passphase"` // DEPRECATED
5050
Fingerprint string `yaml:"fingerprint"`
5151
VcnOCID string `yaml:"vcn"`
5252
}
@@ -187,9 +187,6 @@ func validateAuthConfig(c *Config, fldPath *field.Path) field.ErrorList {
187187
if c.Auth.TenancyOCID == "" {
188188
errList = append(errList, field.Required(fldPath.Child("tenancy"), ""))
189189
}
190-
if c.Auth.CompartmentOCID == "" {
191-
errList = append(errList, field.Required(fldPath.Child("compartment"), ""))
192-
}
193190
if c.Auth.UserOCID == "" {
194191
errList = append(errList, field.Required(fldPath.Child("user"), ""))
195192
}

pkg/oci/client/config_test.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,6 @@ func TestValidateConfig(t *testing.T) {
168168
errs: field.ErrorList{
169169
&field.Error{Type: field.ErrorTypeRequired, Field: "auth.tenancy", BadValue: ""},
170170
},
171-
}, {
172-
name: "missing_compartment_ocid",
173-
in: &Config{
174-
Auth: AuthConfig{
175-
Region: "us-phoenix-1",
176-
RegionKey: "phx",
177-
TenancyOCID: "ocid1.tennancy.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
178-
UserOCID: "ocid1.user.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
179-
PrivateKey: "-----BEGIN RSA PRIVATE KEY----- (etc)",
180-
Fingerprint: "d4:1d:8c:d9:8f:00:b2:04:e9:80:09:98:ec:f8:42:7e",
181-
VcnOCID: "ocid1.user.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
182-
},
183-
},
184-
errs: field.ErrorList{
185-
&field.Error{Type: field.ErrorTypeRequired, Field: "auth.compartment", BadValue: ""},
186-
},
187171
}, {
188172
name: "missing_user_ocid",
189173
in: &Config{
@@ -217,7 +201,7 @@ func TestValidateConfig(t *testing.T) {
217201
&field.Error{Type: field.ErrorTypeRequired, Field: "auth.key", BadValue: ""},
218202
},
219203
}, {
220-
name: "missing_figerprint",
204+
name: "missing_fingerprint",
221205
in: &Config{
222206
Auth: AuthConfig{
223207
Region: "us-phoenix-1",

pkg/oci/client/oci.go

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,15 @@ func (c *client) WaitForVolumeAttached(volumeAttachmentId string) (core.VolumeAt
163163
// ATTACHING or ATTACHED and returns the first volume attachment found.
164164
func (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.
375396
func (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

Comments
 (0)