Skip to content

Commit

Permalink
extract secondary vnic
Browse files Browse the repository at this point in the history
  • Loading branch information
yutpeng committed Apr 8, 2024
1 parent cd3c0b6 commit 4a74925
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/cloudprovider/providers/oci/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ func (cp *CloudProvider) extractNodeAddresses(ctx context.Context, instanceID st
addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()})
}

secondaryVnic, err := cp.client.Compute().GetSecondaryVNICForInstance(ctx, compartmentID, instanceID)
if err != nil {
return nil, errors.Wrap(err, "GetSecondaryVNICForInstance")
}

if secondaryVnic.PrivateIp != nil && *secondaryVnic.PrivateIp != "" {
ip := net.ParseIP(*secondaryVnic.PrivateIp)
if ip == nil {
return nil, fmt.Errorf("instance has invalid private address: %q", *vnic.PrivateIp)
}
addresses = append(addresses, api.NodeAddress{Type: api.NodeInternalIP, Address: ip.String()})
}

if secondaryVnic.PublicIp != nil && *secondaryVnic.PublicIp != "" {
ip := net.ParseIP(*secondaryVnic.PublicIp)
if ip == nil {
return nil, errors.Errorf("instance has invalid public address: %q", *secondaryVnic.PublicIp)
}
addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()})
}

// Changing this can have wide reaching impact.
//
// if vnic.HostnameLabel != nil && *vnic.HostnameLabel != "" {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudprovider/providers/oci/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ func (MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compartm
return instanceVnics[instanceID], nil
}

func (MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
return instanceVnics[instanceID], nil
}

func (MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
return nil, nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/csi/driver/bv_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ func (c *MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compa
return nil, nil
}

func (c *MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
return nil, nil
}

func (c *MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
var page *string
var requestMetadata common.RequestMetadata
Expand Down
48 changes: 48 additions & 0 deletions pkg/oci/client/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type ComputeInterface interface {

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

GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error)

VolumeAttachmentInterface
}

Expand Down Expand Up @@ -151,6 +153,52 @@ func (c *client) GetPrimaryVNICForInstance(ctx context.Context, compartmentID, i
return nil, errors.WithStack(errNotFound)
}

func (c *client) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
logger := c.logger.With("instanceID", instanceID, "compartmentID", compartmentID)

var page *string
for {
resp, err := c.listVNICAttachments(ctx, core.ListVnicAttachmentsRequest{
InstanceId: &instanceID,
CompartmentId: &compartmentID,
Page: page,
RequestMetadata: c.requestMetadata,
})

if err != nil {
return nil, err
}

for _, attachment := range resp.Items {
if attachment.LifecycleState != core.VnicAttachmentLifecycleStateAttached {
logger.With("vnicAttachmentID", *attachment.Id).Info("VNIC attachment is not in attached state")
continue
}

if attachment.VnicId == nil {
// Should never happen but lets be extra cautious as field is non-mandatory in OCI API.
logger.With("vnicAttachmentID", *attachment.Id).Error("VNIC attachment is attached but has no VNIC ID")
continue
}

// TODO(apryde): Cache map[instanceID]primaryVNICID.
vnic, err := c.GetVNIC(ctx, *attachment.VnicId)
if err != nil {
return nil, err
}
if !*vnic.IsPrimary {
return vnic, nil
}
}

if page = resp.OpcNextPage; resp.OpcNextPage == nil {
break
}
}

return nil, errors.WithStack(errNotFound)
}

func (c *client) GetInstanceByNodeName(ctx context.Context, compartmentID, vcnID, nodeName string) (*core.Instance, error) {
// First try lookup by display name.
instance, err := c.getInstanceByDisplayName(ctx, compartmentID, nodeName)
Expand Down
4 changes: 4 additions & 0 deletions pkg/volume/provisioner/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ func (c *MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compa
return nil, nil
}

func (c *MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
return nil, nil
}

func (c *MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
return nil, nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/volume/provisioner/fss/fss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func (c *MockComputeClient) GetPrimaryVNICForInstance(ctx context.Context, compa
return nil, nil
}

func (c *MockComputeClient) GetSecondaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
return nil, nil
}

func (c *MockComputeClient) FindVolumeAttachment(ctx context.Context, compartmentID, volumeID string) (core.VolumeAttachment, error) {
return nil, nil
}
Expand Down

0 comments on commit 4a74925

Please sign in to comment.