Skip to content

Commit 5e6d9de

Browse files
committed
test secondary vnic
1 parent a95563b commit 5e6d9de

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

pkg/cloudprovider/providers/oci/instances.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ func (cp *CloudProvider) extractNodeAddresses(ctx context.Context, instanceID st
9797
addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()})
9898
}
9999

100+
secondaryVnic, err := cp.client.Compute().GetSecondaryVNICForInstance(ctx, compartmentID, instanceID)
101+
if err != nil {
102+
return nil, errors.Wrap(err, "GetSecondaryVNICForInstance")
103+
}
104+
105+
if secondaryVnic == nil {
106+
return addresses, nil
107+
}
108+
109+
if (secondaryVnic.IsPrimary == nil || !*secondaryVnic.IsPrimary) && secondaryVnic.PrivateIp != nil && *secondaryVnic.PrivateIp != "" {
110+
ip := net.ParseIP(*secondaryVnic.PrivateIp)
111+
if ip == nil {
112+
return nil, fmt.Errorf("instance has invalid private address: %q", *secondaryVnic.PrivateIp)
113+
}
114+
addresses = append(addresses, api.NodeAddress{Type: api.NodeInternalIP, Address: ip.String()})
115+
}
116+
117+
if (secondaryVnic.IsPrimary == nil || !*secondaryVnic.IsPrimary) && secondaryVnic.PublicIp != nil && *secondaryVnic.PublicIp != "" {
118+
ip := net.ParseIP(*secondaryVnic.PublicIp)
119+
if ip == nil {
120+
return nil, errors.Errorf("instance has invalid public address: %q", *secondaryVnic.PublicIp)
121+
}
122+
addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()})
123+
}
100124
// Changing this can have wide reaching impact.
101125
//
102126
// if vnic.HostnameLabel != nil && *vnic.HostnameLabel != "" {

pkg/cloudprovider/providers/oci/instances_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ func (MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compartm
336336
return instanceVnics[instanceID], nil
337337
}
338338

339+
func (c *MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
340+
return instanceVnics[instanceID], nil
341+
}
342+
339343
func (MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
340344
return nil, nil
341345
}

pkg/csi/driver/bv_controller_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ func (c *MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compa
288288
return nil, nil
289289
}
290290

291+
func (c *MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
292+
return nil, nil
293+
}
294+
291295
func (c *MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
292296
return nil, nil
293297
}

pkg/oci/client/compute.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package client
1616

1717
import (
1818
"context"
19+
"log"
1920
"strings"
2021

2122
"github.com/oracle/oci-go-sdk/v65/core"
@@ -33,6 +34,8 @@ type ComputeInterface interface {
3334

3435
GetPrimaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error)
3536

37+
GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error)
38+
3639
VolumeAttachmentInterface
3740
}
3841

@@ -151,6 +154,55 @@ func (c *client) GetPrimaryVNICForInstance(ctx context.Context, compartmentID, i
151154
return nil, errors.WithStack(errNotFound)
152155
}
153156

157+
func (c *client) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
158+
logger := c.logger.With("instanceID", instanceID, "compartmentID", compartmentID)
159+
160+
var page *string
161+
for {
162+
resp, err := c.listVNICAttachments(ctx, core.ListVnicAttachmentsRequest{
163+
InstanceId: &instanceID,
164+
CompartmentId: &compartmentID,
165+
Page: page,
166+
RequestMetadata: c.requestMetadata,
167+
})
168+
169+
if err != nil {
170+
return nil, err
171+
}
172+
173+
for _, attachment := range resp.Items {
174+
if attachment.LifecycleState != core.VnicAttachmentLifecycleStateAttached {
175+
logger.With("vnicAttachmentID", *attachment.Id).Info("VNIC attachment is not in attached state")
176+
log.Println(logger)
177+
continue
178+
}
179+
180+
if attachment.VnicId == nil {
181+
// Should never happen but lets be extra cautious as field is non-mandatory in OCI API.
182+
logger.With("vnicAttachmentID", *attachment.Id).Error("VNIC attachment is attached but has no VNIC ID")
183+
log.Println(logger)
184+
continue
185+
}
186+
187+
// TODO(apryde): Cache map[instanceID]primaryVNICID.
188+
vnic, err := c.GetVNIC(ctx, *attachment.VnicId)
189+
if err != nil {
190+
return nil, err
191+
}
192+
193+
if !*vnic.IsPrimary {
194+
return vnic, nil
195+
}
196+
}
197+
198+
if page = resp.OpcNextPage; resp.OpcNextPage == nil {
199+
break
200+
}
201+
}
202+
203+
return nil, errors.WithStack(errNotFound)
204+
}
205+
154206
func (c *client) GetInstanceByNodeName(ctx context.Context, compartmentID, vcnID, nodeName string) (*core.Instance, error) {
155207
// First try lookup by display name.
156208
instance, err := c.getInstanceByDisplayName(ctx, compartmentID, nodeName)

pkg/volume/provisioner/block/block_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ func (c *MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compa
210210
return nil, nil
211211
}
212212

213+
func (c *MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
214+
return nil, nil
215+
}
216+
213217
func (c *MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
214218
return nil, nil
215219
}

pkg/volume/provisioner/fss/fss_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ func (c *MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compa
209209
return nil, nil
210210
}
211211

212+
func (c *MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
213+
return nil, nil
214+
}
215+
212216
func (c *MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
213217
return nil, nil
214218
}

0 commit comments

Comments
 (0)