Skip to content

Commit

Permalink
feat(ipset): add more name utilities
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aauren committed Apr 26, 2024
1 parent ada3179 commit c762eaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/controllers/netpol/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
9 changes: 7 additions & 2 deletions pkg/utils/ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit c762eaf

Please sign in to comment.