Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract secondary vnic #462

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", *secondaryVnic.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
18 changes: 18 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 Expand Up @@ -762,6 +766,8 @@ func TestExtractNodeAddresses(t *testing.T) {
name: "basic-complete",
in: "basic-complete",
out: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
// v1.NodeAddress{Type: v1.NodeHostName, Address: "basic-complete.subnetwithdnslabel.vcnwithdnslabel.oraclevcn.com"},
Expand All @@ -773,6 +779,7 @@ func TestExtractNodeAddresses(t *testing.T) {
name: "no-external-ip",
in: "no-external-ip",
out: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
// v1.NodeAddress{Type: v1.NodeHostName, Address: "no-external-ip.subnetwithdnslabel.vcnwithdnslabel.oraclevcn.com"},
// v1.NodeAddress{Type: v1.NodeInternalDNS, Address: "no-external-ip.subnetwithdnslabel.vcnwithdnslabel.oraclevcn.com"},
Expand All @@ -783,6 +790,7 @@ func TestExtractNodeAddresses(t *testing.T) {
name: "no-internal-ip",
in: "no-internal-ip",
out: []v1.NodeAddress{
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
// v1.NodeAddress{Type: v1.NodeHostName, Address: "no-internal-ip.subnetwithdnslabel.vcnwithdnslabel.oraclevcn.com"},
// v1.NodeAddress{Type: v1.NodeInternalDNS, Address: "no-internal-ip.subnetwithdnslabel.vcnwithdnslabel.oraclevcn.com"},
Expand All @@ -807,6 +815,8 @@ func TestExtractNodeAddresses(t *testing.T) {
out: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
},
err: nil,
},
Expand All @@ -816,6 +826,8 @@ func TestExtractNodeAddresses(t *testing.T) {
out: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
},
err: nil,
},
Expand All @@ -825,6 +837,8 @@ func TestExtractNodeAddresses(t *testing.T) {
out: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
},
err: nil,
},
Expand Down Expand Up @@ -995,6 +1009,8 @@ func TestNodeAddressesByProviderID(t *testing.T) {
out: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
},
err: nil,
},
Expand All @@ -1004,6 +1020,8 @@ func TestNodeAddressesByProviderID(t *testing.T) {
out: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeExternalIP, Address: "0.0.0.1"},
},
err: 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
Loading