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

ensure to store valid childprefixes only and better error messages #159

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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