From e141dbc1d1a63fa9f9a073dbf83773281fd60be1 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Thu, 25 Jul 2024 09:52:09 +0200 Subject: [PATCH] better error messages (#159) --- pkg/service/ipam-service.go | 17 +++++++++++------ prefix.go | 9 +++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pkg/service/ipam-service.go b/pkg/service/ipam-service.go index 0b4702f..1434ae8 100644 --- a/pkg/service/ipam-service.go +++ b/pkg/service/ipam-service.go @@ -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) } } diff --git a/prefix.go b/prefix.go index f752934..6ae2fd9 100644 --- a/prefix.go +++ b/prefix.go @@ -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() } @@ -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,