From c762eaf2e5a3e56393d611ef65070723e62c1ae9 Mon Sep 17 00:00:00 2001 From: Aaron U'Ren Date: Fri, 26 Apr 2024 13:39:04 -0500 Subject: [PATCH] feat(ipset): add more name utilities Naming ipsets with the advent of IPv6 gets tricky because IPv6 ipsets have to be prefixed with inet6:. This commit adds additional utilities that help users find the correct name of ipsets. --- pkg/controllers/netpol/utils.go | 8 ++++++++ pkg/utils/ipset.go | 9 +++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/netpol/utils.go b/pkg/controllers/netpol/utils.go index 328ffae1a..ebdc39473 100644 --- a/pkg/controllers/netpol/utils.go +++ b/pkg/controllers/netpol/utils.go @@ -166,3 +166,11 @@ func getPodIPForFamily(pod podInfo, ipFamily api.IPFamily) (string, error) { return "", fmt.Errorf("did not recognize IP Family for pod: %s:%s family: %s", pod.namespace, pod.name, ipFamily) } + +func ipSetName(setName string, ipFamily api.IPFamily) string { + if ipFamily == api.IPv4Protocol { + return utils.IPSetName(setName, false) + } else { + return utils.IPSetName(setName, true) + } +} diff --git a/pkg/utils/ipset.go b/pkg/utils/ipset.go index 9cb69c8c2..0b8ee33f9 100644 --- a/pkg/utils/ipset.go +++ b/pkg/utils/ipset.go @@ -427,13 +427,18 @@ func (set *Set) IsActive() (bool, error) { return true, nil } -func (ipset *IPSet) Name(setName string) string { - if ipset.isIpv6 && !strings.HasPrefix(setName, IPv6SetPrefix+":") { +// IPSetName returns the proper set name for this component based upon whether or not it is an IPv6 set +func IPSetName(setName string, isIPv6 bool) string { + if isIPv6 && !strings.HasPrefix(setName, IPv6SetPrefix+":") { return fmt.Sprintf("%s:%s", IPv6SetPrefix, setName) } return setName } +func (ipset *IPSet) Name(setName string) string { + return IPSetName(setName, ipset.isIpv6) +} + func (set *Set) name() string { return set.Parent.Name(set.Name) }