Skip to content

Commit b056358

Browse files
committed
updated to return bool instead of error for checking cidr superset
1 parent 59441be commit b056358

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

cns/restserver/internalapi.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,8 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.
632632
existingReq := existingNCInfo.CreateNetworkContainerRequest
633633
if !reflect.DeepEqual(existingReq.IPConfiguration.IPSubnet, req.IPConfiguration.IPSubnet) {
634634
// check for potential overlay subnet expansion - checking if new subnet is a superset of old subnet
635-
err := validateCIDRSuperset(req.IPConfiguration.IPSubnet.IPAddress, existingReq.IPConfiguration.IPSubnet.IPAddress)
636-
if err != nil {
635+
isCIDRSuperset := validateCIDRSuperset(req.IPConfiguration.IPSubnet.IPAddress, existingReq.IPConfiguration.IPSubnet.IPAddress)
636+
if !isCIDRSuperset {
637637
logger.Errorf("[Azure CNS] Error. PrimaryCA is not same, NCId %s, old CA %s/%d, new CA %s/%d", //nolint:staticcheck // Suppress SA1019: logger.Errorf is deprecated
638638
req.NetworkContainerid,
639639
existingReq.IPConfiguration.IPSubnet.IPAddress,
@@ -729,27 +729,27 @@ func (service *HTTPRestService) GetIMDSNCs(ctx context.Context) (map[string]stri
729729
}
730730

731731
// IsCIDRSuperset returns true if newCIDR is a superset of oldCIDR (i.e., all IPs in oldCIDR are contained in newCIDR).
732-
func validateCIDRSuperset(newCIDR, oldCIDR string) error {
732+
func validateCIDRSuperset(newCIDR, oldCIDR string) bool {
733733
// Parse newCIDR and oldCIDR into netip.Prefix
734734
newPrefix, err := netip.ParsePrefix(newCIDR)
735735
if err != nil {
736-
return errors.Wrapf(err, "parsing newCIDR %q", newCIDR)
736+
return false
737737
}
738738

739739
oldPrefix, err := netip.ParsePrefix(oldCIDR)
740740
if err != nil {
741-
return errors.Wrapf(err, "parsing oldCIDR %q", oldCIDR)
741+
return false
742742
}
743743

744744
// Condition 1: Check if the new prefix length is smaller (larger range) than the old prefix length
745745
if newPrefix.Bits() >= oldPrefix.Bits() {
746-
return errors.New("newCIDR does not have a larger range than oldCIDR")
746+
return false
747747
}
748748

749749
// Condition 2: Check if the base IP of oldCIDR is contained in newCIDR
750750
if !newPrefix.Contains(oldPrefix.Addr()) {
751-
return errors.New("old subnet's base IP is not contained in new subnet")
751+
return false
752752
}
753753

754-
return nil
754+
return true
755755
}

0 commit comments

Comments
 (0)