@@ -14,6 +14,7 @@ import (
1414 "github.com/avast/retry-go/v4"
1515 restfulspec "github.com/emicklei/go-restful-openapi/v2"
1616 restful "github.com/emicklei/go-restful/v3"
17+ "go4.org/netipx"
1718
1819 "github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore"
1920 "github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
@@ -835,6 +836,9 @@ func makeBGPFilterMachine(m metal.Machine, ips metal.IPsMap) (v1.BGPFilter, erro
835836 if underlay != nil && underlay .ContainsIP (i .IPAddress ) {
836837 continue
837838 }
839+
840+ // TODO machine BGPFilter must not contain firewall private network IPs
841+
838842 // Allow all other ip addresses allocated for the project.
839843 ipwithMask , err := ipWithMask (i .IPAddress )
840844 if err != nil {
@@ -843,7 +847,11 @@ func makeBGPFilterMachine(m metal.Machine, ips metal.IPsMap) (v1.BGPFilter, erro
843847 cidrs = append (cidrs , ipwithMask )
844848 }
845849
846- return v1 .NewBGPFilter (vnis , cidrs ), nil
850+ compactedCidrs , err := compactCidrs (cidrs )
851+ if err != nil {
852+ return v1.BGPFilter {}, err
853+ }
854+ return v1 .NewBGPFilter (vnis , compactedCidrs ), nil
847855}
848856
849857func ipWithMask (ip string ) (string , error ) {
@@ -854,6 +862,28 @@ func ipWithMask(ip string) (string, error) {
854862 return fmt .Sprintf ("%s/%d" , ip , parsed .BitLen ()), nil
855863}
856864
865+ func compactCidrs (cidrs []string ) ([]string , error ) {
866+ // compact all cidrs which are used to be added to the route map
867+ // to find the smallest sorted set of prefixes which covers all cidrs which need to be added.
868+ var ipsetBuilder netipx.IPSetBuilder
869+ for _ , cidr := range cidrs {
870+ parsed , err := netip .ParsePrefix (cidr )
871+ if err != nil {
872+ return nil , err
873+ }
874+ ipsetBuilder .AddPrefix (parsed )
875+ }
876+ set , err := ipsetBuilder .IPSet ()
877+ if err != nil {
878+ return nil , fmt .Errorf ("unable to create ipset:%w" , err )
879+ }
880+ var compactedCidrs []string
881+ for _ , pfx := range set .Prefixes () {
882+ compactedCidrs = append (compactedCidrs , pfx .String ())
883+ }
884+ return compactedCidrs , nil
885+ }
886+
857887func makeBGPFilter (m metal.Machine , vrf string , ips metal.IPsMap ) (v1.BGPFilter , error ) {
858888 var (
859889 filter v1.BGPFilter
0 commit comments