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

Fix bug where IPs were assigned from affine blocks of disabled pools. #806

Merged

Conversation

ozdanborne
Copy link
Member

@ozdanborne ozdanborne commented Mar 5, 2018

Description

Fixes #689 .

Of course, this change will need to be pulled into projectcalico/cni-plugin before we see any results.

Todos

  • Tests
  • Documentation
  • Release note

Release Note

Fixes a bug where IPs could be assigned from disabled IP pools.

lib/ipam/ipam.go Outdated
// Get all the configured pools.
enabledPools, err := c.pools.GetEnabledPools(version.Number)
if err != nil {
log.WithError(err).Errorf("Error reading configured pools")
Copy link
Member

Choose a reason for hiding this comment

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

We create a context logger just a little bit below - I think you should move that above and then make sure all the logs are using logCtx

lib/ipam/ipam.go Outdated

// Start by trying to assign from one of the host-affine blocks. We
// Get all the configured pools.
Copy link
Member

Choose a reason for hiding this comment

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

Probably worth encapsulating the pool determination logic into its own function for easier testing and to clean up the autoAssign code a bit.

Copy link
Member

Choose a reason for hiding this comment

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

This code then becomes:

pools, err := c.determinePools(requestedPools)
if err != nil { ... }

lib/ipam/ipam.go Outdated
// If no pools were requested, default to all enabled pools.
pools = enabledPools
}
log.Debugf("Finding an unclaimed block from pools: %v", pools)
Copy link
Member

Choose a reason for hiding this comment

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

This log is wrong now - we're not finding an unclaimed block yet (we might use an already claimed one)

Copy link
Member

@caseydavenport caseydavenport left a comment

Choose a reason for hiding this comment

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

Just a few comments.

lib/ipam/ipam.go Outdated
for _, rp := range requestedPools {
if _, ok := pm[rp.String()]; !ok {
// The requested pool doesn't exist.
return nil, fmt.Errorf("the given pool (%s) does not exist, or is not enabled", rp.IPNet.String())
Copy link
Member Author

Choose a reason for hiding this comment

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

@caseydavenport I'm wondering if erroring here is a bit harsh. We could just drop any pools that do not exist, just like we drop any pools that aren't enabled.

Copy link
Member Author

@ozdanborne ozdanborne Mar 5, 2018

Choose a reason for hiding this comment

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

Actually, I'm thinking this code is duplicate of the above comparison. Let me look into it further...

@ozdanborne ozdanborne force-pushed the fix-disabled-pools-rebased branch 4 times, most recently from d7bda94 to 1b54a08 Compare March 5, 2018 23:58
lib/ipam/ipam.go Outdated
pools = append(pools, rp)
}
}
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Just a personal preference, but I'd drop the "else" branch here and do something like this:

if len(requestedPools) > 0 {
  // Build a map
  ...

  // Make sure each pool exists
  ...

  // Return the requested pools.
  return requestedPools
}

return enabledPools

I think it makes the code a bit easier to follow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Neat. Way cleaner. Fixed

@ozdanborne ozdanborne force-pushed the fix-disabled-pools-rebased branch from 1b54a08 to 8089f94 Compare March 6, 2018 01:20
lib/ipam/ipam.go Outdated
}
}

return pools, nil
Copy link
Member

Choose a reason for hiding this comment

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

One more nit here - it's bad to mix named and unnamed return values.

I'd suggest removing the named return value from above, and simply returning requestedPools if we don't hit an error in the loop above.

That's nice because ultimately this function will always return either the requested pools, or all enabled pools and it becomes more obvious with that change.

Copy link
Member

Choose a reason for hiding this comment

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

e.g. something like this:

for _, rp := range requestedPools{
  if !ok {
    ERROR!
  }
}

// Requested pools are valid
return requestedPools, nil

lib/ipam/ipam.go Outdated
if _, ok := pm[rp.String()]; !ok {
// The requested pool doesn't exist.
return nil, fmt.Errorf("the given pool (%s) does not exist, or is not enabled", rp.IPNet.String())
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: don't need the else here
(not reviewing but I was flying by and saw this)

@ozdanborne ozdanborne force-pushed the fix-disabled-pools-rebased branch from eae77f0 to b7657b4 Compare March 6, 2018 17:55
@caseydavenport caseydavenport added this to the Calico v3.1.0 milestone Mar 6, 2018
@caseydavenport caseydavenport merged commit 3be72c2 into projectcalico:master Mar 7, 2018
@ozdanborne ozdanborne deleted the fix-disabled-pools-rebased branch March 8, 2018 18:30
@caseydavenport caseydavenport mentioned this pull request Mar 13, 2018
3 tasks
caseydavenport added a commit that referenced this pull request Mar 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Addresses are allocated from disabled ipPools
3 participants