Skip to content

Commit

Permalink
better error messages (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Jul 25, 2024
1 parent 4918210 commit e141dbc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
17 changes: 11 additions & 6 deletions pkg/service/ipam-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,23 @@ func (i *IPAMService) AcquireChildPrefix(ctx context.Context, req *connect.Reque
if req.Msg.GetNamespace() != "" {
ctx = goipam.NewContextWithNamespace(ctx, req.Msg.GetNamespace())
}
var resp *goipam.Prefix
var err error
var (
resp *goipam.Prefix
err error
parentCidr = req.Msg.GetCidr()
childCidr = req.Msg.GetChildCidr()
length = req.Msg.GetLength()
)
if req.Msg.GetChildCidr() != "" {
resp, err = i.ipamer.AcquireSpecificChildPrefix(ctx, req.Msg.GetCidr(), req.Msg.GetChildCidr())
resp, err = i.ipamer.AcquireSpecificChildPrefix(ctx, parentCidr, childCidr)
if err != nil {
i.log.Error("acquirechildprefix", "error", err)
i.log.Error("acquirechildprefix", "parent cidr", parentCidr, "child cidr", childCidr, "length", length, "error", err)
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
} else {
resp, err = i.ipamer.AcquireChildPrefix(ctx, req.Msg.GetCidr(), uint8(req.Msg.GetLength()))
resp, err = i.ipamer.AcquireChildPrefix(ctx, parentCidr, uint8(length))
if err != nil {
i.log.Error("acquirechildprefix", "error", err)
i.log.Error("acquirechildprefix", "parent cidr", parentCidr, "length", length, "error", err)
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
}
Expand Down
9 changes: 7 additions & 2 deletions prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ func (i *ipamer) acquireChildPrefixInternal(ctx context.Context, namespace, pare
}
ipprefix, err := netip.ParsePrefix(parent.Cidr)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to parse parent.cidr:%s of parentCidr:%s %w", parent.Cidr, parentCidr, err)
}
if specificChildRequest {
childprefix, err = netip.ParsePrefix(childCidr)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to parse childCidr:%s %w", childCidr, err)
}
length = childprefix.Bits()
}
Expand Down Expand Up @@ -259,6 +259,11 @@ func (i *ipamer) acquireChildPrefixInternal(ctx context.Context, namespace, pare
cp = childprefix
}

// Ensure acquired child prefix is valid
if !cp.IsValid() {
return nil, fmt.Errorf("acquired child prefix:%s is not valid", cp.String())
}

child := &Prefix{
Cidr: cp.String(),
ParentCidr: parentCidr,
Expand Down

0 comments on commit e141dbc

Please sign in to comment.