Skip to content

Commit

Permalink
add error log When ipam allocate nodecidr failure
Browse files Browse the repository at this point in the history
Signed-off-by: hui.kong <[email protected]>
  • Loading branch information
konghui authored and aanm committed Oct 20, 2020
1 parent 7fae985 commit ef6ecbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/ipam/allocator/podcidr/podcidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package podcidr

import (
"bytes"
"context"
"fmt"
"net"
Expand Down Expand Up @@ -171,6 +172,7 @@ type NodesPodCIDRManager struct {
}

type CIDRAllocator interface {
fmt.Stringer
Occupy(cidr *net.IPNet) error
AllocateNext() (*net.IPNet, error)
Release(cidr *net.IPNet) error
Expand Down Expand Up @@ -849,12 +851,37 @@ func (n *NodesPodCIDRManager) allocateNext(nodeName string) (*nodeCIDRs, bool, e
nCIDRs.v6PodCIDRs = []*net.IPNet{v6CIDR}
}

if nCIDRs == nil {
err = fmt.Errorf("Unable to allocate node CIDR for node %s. IPAMInfo: {IPv4: %s, IPv6: %s}. Please check that your configuration is correct",
nodeName,
getCIDRAllocatorsInfo(n.v4CIDRAllocators, v4AllocatorType),
getCIDRAllocatorsInfo(n.v6CIDRAllocators, v6AllocatorType))
return nil, false, err
}

n.nodes[nodeName] = nCIDRs

return nCIDRs, true, nil

}

func getCIDRAllocatorsInfo(cidrAllocators []CIDRAllocator, netTypes string) string {
var cidrAllocatorsInfo bytes.Buffer
cidrAllocatorsLength := len(cidrAllocators)
if cidrAllocatorsLength == 0 {
return "[]"
}

for index, cidrAllocator := range cidrAllocators {
cidrAllocatorsInfo.WriteString(fmt.Sprintf("%s", cidrAllocator.String()))
if index < cidrAllocatorsLength-1 {
cidrAllocatorsInfo.WriteString(fmt.Sprintf(", "))
}
}

return fmt.Sprintf("[%s]", cidrAllocatorsInfo.String())
}

// allocateFirstFreeCIDR allocates the first CIDR available from the slice of
// cidrAllocators.
func allocateFirstFreeCIDR(cidrAllocators []CIDRAllocator) (revertFunc revert.RevertFunc, cidr *net.IPNet, err error) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/ipam/allocator/podcidr/podcidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type mockCIDRAllocator struct {
OnInRange func(cidr *net.IPNet) bool
}

func (d *mockCIDRAllocator) String() string {
return "clusterCIDR: 10.0.0.0/24, nodeMask: 24"
}

func (d *mockCIDRAllocator) Occupy(cidr *net.IPNet) error {
if d.OnOccupy != nil {
return d.OnOccupy(cidr)
Expand Down

0 comments on commit ef6ecbd

Please sign in to comment.