Skip to content

Commit

Permalink
fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek010 committed Dec 13, 2024
1 parent 1037c75 commit c6d6583
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 199 deletions.
2 changes: 1 addition & 1 deletion internal/address/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (a *awsAssigner) tryAssignAddress(ctx context.Context, address *types.Addre

func (a *awsAssigner) getNetworkInterfaceID(instance *types.Instance) (string, error) {
// get network interface ID
if instance.NetworkInterfaces == nil || len(instance.NetworkInterfaces) == 0 {
if len(instance.NetworkInterfaces) == 0 {
return "", errors.Errorf("no network interfaces found for instance %s", *instance.InstanceId)
}
// get primary network interface ID with public IP address (DeviceIndex == 0)
Expand Down
85 changes: 47 additions & 38 deletions internal/address/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a *ociAssigner) Assign(ctx context.Context, instanceOCID, _ string, _ []st
a.logger.WithField("primaryVnicOCID", *vnic.Id).Debugf("got primary VNIC of the instance %s", instanceOCID)

// Handle already assigned public IP case
alreadyAssigned, err := a.handlePublicIpAlreadyAssignedCase(ctx, vnic)
alreadyAssigned, err := a.handlePublicIPAlreadyAssignedCase(ctx, vnic)
if err != nil {
return "", errors.Wrap(err, "failed to check if public ip is already assigned or not")
}
Expand All @@ -85,25 +85,25 @@ func (a *ociAssigner) Assign(ctx context.Context, instanceOCID, _ string, _ []st
}

// Get primary VNIC private IP
privateIP, err := a.networkSvc.GetPrimaryPrivateIpOfVnic(ctx, *vnic.Id)
privateIP, err := a.networkSvc.GetPrimaryPrivateIPOfVnic(ctx, *vnic.Id)
if err != nil {
return "", errors.Wrap(err, "failed to get primary VNIC private IP")
}
a.logger.WithField("privateIPOCID", *privateIP.Id).Debugf("got primary VNIC private IP of the instance %s", instanceOCID)

// Fetch all available reserved Public IPs that will be used for assignment
reservedPublicIpList, err := a.fetchPublicIps(ctx, true, false)
reservedPublicIPList, err := a.fetchPublicIps(ctx, true, false)
if err != nil {
return "", errors.Wrap(err, "failed to get list of reserved public IPs")
}
if reservedPublicIpList == nil || len(reservedPublicIpList) == 0 {
if len(reservedPublicIPList) == 0 {
return "", errors.New("no reserved public IPs available")
}
a.logger.WithField("reservedPublicIpList", reservedPublicIpList).Debug("got list of available reserved public IPs")
a.logger.WithField("reservedPublicIpList", reservedPublicIPList).Debug("got list of available reserved public IPs")

// Try to assign an IP from the reserved public IP list
for _, publicIP := range reservedPublicIpList {
if err := a.tryAssignAddress(ctx, *privateIP.Id, *publicIP.Id); err == nil {
for _, publicIP := range reservedPublicIPList {
if err = a.tryAssignAddress(ctx, *privateIP.Id, *publicIP.Id); err == nil {
a.logger.WithField("assignedIP", *publicIP.IpAddress).Infof("assigned IP %s to instance %s", *publicIP.IpAddress, instanceOCID)
return *publicIP.IpAddress, nil
}
Expand All @@ -130,20 +130,20 @@ func (a *ociAssigner) Unassign(ctx context.Context, instanceOCID, _ string) erro
a.logger.Infof("no public ip assigned to the instance %s", instanceOCID)
return ErrNoPublicIPAssigned
}
publicIp := vnic.PublicIp
publicIP := vnic.PublicIp

// Fetch assigned public IPs
reservedPublicIpList, err := a.fetchPublicIps(ctx, true, true)
reservedPublicIPList, err := a.fetchPublicIps(ctx, true, true)
if err != nil {
return errors.Wrap(err, "failed to get list of reserved public IPs")
}
a.logger.WithField("reservedPublicIpList", reservedPublicIpList).Debug("got list of reserved public IPs")
a.logger.WithField("reservedPublicIPList", reservedPublicIPList).Debug("got list of reserved public IPs")

// Check if assigned public ip is from the reserved public IP list
for _, ip := range reservedPublicIpList {
if *ip.IpAddress == *publicIp && ip.LifecycleState == core.PublicIpLifecycleStateAssigned {
for _, ip := range reservedPublicIPList {
if *ip.IpAddress == *publicIP && ip.LifecycleState == core.PublicIpLifecycleStateAssigned {
// Unassign the public IP
if err := a.networkSvc.UpdatePublicIp(ctx, *ip.Id, ""); err != nil {
if err := a.networkSvc.UpdatePublicIP(ctx, *ip.Id, ""); err != nil {
return errors.Wrap(err, "failed to unassign public IP assigned to private IP")
}
return nil
Expand All @@ -162,31 +162,38 @@ func (a *ociAssigner) getPrimaryVnicOfInstance(ctx context.Context, instanceOCID
}

// Get the primary VNIC
return a.networkSvc.GetPrimaryVnic(ctx, vnicAttachment)
vnic, err := a.networkSvc.GetPrimaryVnic(ctx, vnicAttachment)
if err != nil {
return nil, errors.Wrap(err, "failed to get primary VNIC")
}

return vnic, nil
}

// handlePublicIpAlreadyAssignedCase handles the case when the public IP is already assigned to the instance.
// handlePublicIPAlreadyAssignedCase handles the case when the public IP is already assigned to the instance.
// It returns true if the public IP is already assigned to the instance from the reserved IP list. In this case, do nothing.
// It returns false in all other cases with error(if any). In this case, if err is nil, try to assign a new public IP.
// Following are the cases and actions for each case:
// - Case1: Public IP is already assigned to the instance from the reserved IP list: Do nothing
// - Case2: Public IP is assigned to the instance but not from the reserved IP list: Unassign the public IP
// - Case3: Public IP is assigned to the instance, but it is ephemeral: Delete the ephemeral public IP
// - Case4: Unhandled case: Return error
func (a *ociAssigner) handlePublicIpAlreadyAssignedCase(ctx context.Context, vnic *core.Vnic) (bool, error) {
//
//nolint:gocognit
func (a *ociAssigner) handlePublicIPAlreadyAssignedCase(ctx context.Context, vnic *core.Vnic) (bool, error) {
if vnic == nil {
return false, nil
}
publicIp := vnic.PublicIp
if publicIp != nil {
publicIP := vnic.PublicIp
if publicIP != nil {
// Case1
// Fetch all reserved public IPs that are assigned to the private IPs
list, err := a.fetchPublicIps(ctx, true, true)
if err != nil {
return false, errors.Wrap(err, "failed to list reserved public IPs assigned to private IP")
}
for _, ip := range list {
if *ip.IpAddress == *publicIp {
if *ip.IpAddress == *publicIP {
return true, nil
}
}
Expand All @@ -198,9 +205,9 @@ func (a *ociAssigner) handlePublicIpAlreadyAssignedCase(ctx context.Context, vni
return false, errors.Wrap(err, "failed to list public IPs assigned to private IP")
}
for _, ip := range list {
if *ip.IpAddress == *publicIp {
if *ip.IpAddress == *publicIP {
// Unassign the public IP
if err := a.networkSvc.UpdatePublicIp(ctx, *ip.Id, ""); err != nil {
if err = a.networkSvc.UpdatePublicIP(ctx, *ip.Id, ""); err != nil {
return false, errors.Wrap(err, "failed to unassign public IP assigned to private IP")
}
return false, nil
Expand All @@ -212,14 +219,14 @@ func (a *ociAssigner) handlePublicIpAlreadyAssignedCase(ctx context.Context, vni
if vnic.AvailabilityDomain == nil {
return false, errors.New("availability domain not found")
}
list, err = a.fetchEphemeralPublicIps(ctx, *vnic.AvailabilityDomain)
list, err = a.fetchEphemeralPublicIPs(ctx, *vnic.AvailabilityDomain)
if err != nil {
return false, errors.Wrap(err, "failed to list ephemeral public IPs assigned to private IP")
}
for _, ip := range list {
if *ip.IpAddress == *publicIp {
if *ip.IpAddress == *publicIP {
// Delete the ephemeral public IP
if err := a.networkSvc.DeletePublicIp(ctx, *ip.Id); err != nil {
if err := a.networkSvc.DeletePublicIP(ctx, *ip.Id); err != nil {
return false, errors.Wrap(err, "failed to delete ephemeral public IP assigned to private IP")
}
return false, nil
Expand All @@ -238,18 +245,18 @@ func (a *ociAssigner) handlePublicIpAlreadyAssignedCase(ctx context.Context, vni
// If useFilter is set to true, it applies the filters.
// It returns only available public IPs if inUse is set to false.
// It returns only assigned public IPs if inUse is set to true.
func (a *ociAssigner) fetchPublicIps(ctx context.Context, useFilter bool, inUse bool) ([]core.PublicIp, error) {
func (a *ociAssigner) fetchPublicIps(ctx context.Context, useFilter, inUse bool) ([]core.PublicIp, error) {
filters := a.filters
// If useFilter is set to false, do not apply the filters
if !useFilter {
filters = nil
}
list, err := a.networkSvc.ListPublicIps(ctx, core.ListPublicIpsRequest{
list, err := a.networkSvc.ListPublicIps(ctx, &core.ListPublicIpsRequest{
Scope: core.ListPublicIpsScopeRegion,
CompartmentId: common.String(a.compartmentOCID),
}, filters)
if err != nil {
return []core.PublicIp{}, err
return nil, errors.Wrap(err, "failed to list public IPs")
}

lifecycleState := core.PublicIpLifecycleStateAvailable
Expand All @@ -266,23 +273,27 @@ func (a *ociAssigner) fetchPublicIps(ctx context.Context, useFilter bool, inUse
}
}
return updatedList, nil

}

// fetchEphemeralPublicIps returns the list of ephemeral public IPs assigned to the private IPs in the availability domain.
func (a *ociAssigner) fetchEphemeralPublicIps(ctx context.Context, availabilityDomain string) ([]core.PublicIp, error) {
return a.networkSvc.ListPublicIps(ctx, core.ListPublicIpsRequest{
// fetchEphemeralPublicIPs returns the list of ephemeral public IPs assigned to the private IPs in the availability domain.
func (a *ociAssigner) fetchEphemeralPublicIPs(ctx context.Context, availabilityDomain string) ([]core.PublicIp, error) {
list, err := a.networkSvc.ListPublicIps(ctx, &core.ListPublicIpsRequest{
Scope: core.ListPublicIpsScopeAvailabilityDomain,
AvailabilityDomain: common.String(availabilityDomain),
CompartmentId: common.String(a.compartmentOCID),
}, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to list ephemeral public IPs")
}

return list, nil
}

// tryAssignAddress tries to assign the public IP to the private IP.
// If the public IP is not available, it returns an error.
func (a *ociAssigner) tryAssignAddress(ctx context.Context, privateIPOCID, publicIPOCID string) error {
// Fetch public IP details to check if it is available
publicIP, err := a.networkSvc.GetPublicIp(ctx, publicIPOCID)
publicIP, err := a.networkSvc.GetPublicIP(ctx, publicIPOCID)
if err != nil {
return errors.Wrap(err, "failed to get public IP details")
}
Expand All @@ -296,7 +307,7 @@ func (a *ociAssigner) tryAssignAddress(ctx context.Context, privateIPOCID, publi
}

// Assign the public IP to the private IP
if err := a.networkSvc.UpdatePublicIp(ctx, *publicIP.Id, privateIPOCID); err != nil {
if err := a.networkSvc.UpdatePublicIP(ctx, *publicIP.Id, privateIPOCID); err != nil {
return errors.Wrap(err, "failed to assign public IP")
}

Expand All @@ -310,8 +321,8 @@ func (a *ociAssigner) tryAssignAddress(ctx context.Context, privateIPOCID, publi
// - "freeformTags.key1=value1"
// - "definedTags.Namespace.key1=value1"
func parseOCIFilters(cfg *config.Config) (*types.OCIFilters, error) {
if cfg == nil || cfg.Filter == nil || len(cfg.Filter) == 0 {
return nil, nil
if cfg == nil {
return nil, errors.New("config is nil")
}

freeformTags := make(map[string]string)
Expand All @@ -320,11 +331,9 @@ func parseOCIFilters(cfg *config.Config) (*types.OCIFilters, error) {
if strings.HasPrefix(filter, "freeformTags.") {
key, value, err := types.ParseFreeformTagFilter(filter)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to parse freeform tag filter")
}
freeformTags[key] = value
} else if strings.HasPrefix(filter, "definedTags.") {
// TODO: Implement defined tags
} else {
return nil, errors.New("invalid filter format for OCI, should be in format freeformTags.key=value or definedTags.Namespace.key=value, found: " + filter)
}
Expand Down
Loading

0 comments on commit c6d6583

Please sign in to comment.