Skip to content

Conversation

@rjdenney
Copy link
Contributor

Reason for Change:

A bug was found when using older versions of CNI with versions of CNS 1.5 and newer where it would panic when there aren;'t any NCs on the NNC. When CNI requested IPs in this state CNS would think it requires "0" IPs to return since it is currently based on the number of NCs. When trying to index a slice of 0 we would then see the panic.

This fix should lead to CNS returning an error in a few locations when we see that there are 0 NCs since this also implies that there aren't any IPs in the pool.

Issue Fixed:

Requirements:

Notes:

@rjdenney rjdenney requested a review from a team as a code owner July 18, 2023 16:18
@rjdenney rjdenney requested a review from ashvindeodhar July 18, 2023 16:18
podIPInfo := make([]cns.PodIpInfo, numDesiredIPAddresses)
// if there are no NCs on the NNC there will be no IPs in the pool so return error
if numDesiredIPAddresses == 0 {
//nolint:goerr113 // return error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This linter bypass is inappropriate here. The error returned below should be defined like:

var errNoPoolIPs = errors.New("no NCs found on the NNC so no IPs are in the pool")

and consequently used like:

return nil, errNoPoolIPs

Note also the nil, instead of podIPInfo. In cases where an error is returned, it should be a nil or zero value being returned first.

This also applies to all of the other instances of //nolint:goerr113 // return error for this func.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a ErrNoNCs error variable and check for it in the UTs as well.

Comment on lines 678 to 683
// Sets the number of desired IPs equal to the number of NCs so that we can get one IP per NC
numDesiredIPAddresses := len(desiredIPAddresses)
// Creates a slice of PodIpInfo with the size as number of NCs to hold the result for assigned IP configs
podIPInfo := make([]cns.PodIpInfo, numDesiredIPAddresses)
// if there are no NCs on the NNC there will be no IPs in the pool so return error
if numDesiredIPAddresses == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where you basically want to map over desiredIPAddresses, it's usually more idiomatic to let the caller pass in a []cns.PodIpInfo that is partially complete (with just the IP addresses). This lets the caller control the allocation, rather than having these intermediate allocations in here.

That also forces the caller of this method to handle the request rather than spreading that responsibility among many other funcs.

If you don't pursue that, this conditional should be at the beginning of the method (so we don't allocate a slice).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The majority of these lines were already present, so I don't think a major refactor is needed here, but agree with moving the input validation to be the first thing in the method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have emphasized the first part as non-blocking, but moving the conditional as blocking feedback. I still like to provide feedback on the code overall in case someone wants to refactor it... It's unlikely we will ever schedule refactoring this, so PRs provide the only opportunity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the check higher but we won't refactor for this PR since we are trying to get it in fairly quickly. It is a good idea though.

Comment on lines 788 to 789
//nolint:goerr113 // return error
return podIPInfo, fmt.Errorf("No NCs found on the NNC so no IPs are in the pool")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly why the error should be reified in a var--so it can be reused here (and checked by callers).

@rjdenney rjdenney force-pushed the IPConfigHandlerFix branch from dfef9e1 to 3650c4f Compare July 19, 2023 17:44
@rbtr rbtr self-requested a review July 19, 2023 20:45
Copy link
Collaborator

@rbtr rbtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me

@rbtr rbtr merged commit b1a63a9 into master Jul 20, 2023
@rbtr rbtr deleted the IPConfigHandlerFix branch July 20, 2023 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants