Skip to content

Commit

Permalink
fact(ipset): simplify cleanup code by reducing family complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
aauren committed May 14, 2024
1 parent 28585f6 commit d086841
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
24 changes: 6 additions & 18 deletions pkg/controllers/netpol/network_policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,28 +684,16 @@ func (npc *NetworkPolicyController) cleanupStaleIPSets(activePolicyIPSets map[st
}()
}

for ipFamily, ipsets := range npc.ipSetHandlers {
for _, ipsets := range npc.ipSetHandlers {
cleanupPolicyIPSets := make([]*utils.Set, 0)

if err := ipsets.Save(); err != nil {
klog.Fatalf("failed to initialize ipsets command executor due to %s", err.Error())
}
if ipFamily == v1core.IPv6Protocol {
for _, set := range ipsets.Sets() {
if strings.HasPrefix(set.Name, fmt.Sprintf("%s:%s", utils.FamillyInet6, kubeSourceIPSetPrefix)) ||
strings.HasPrefix(set.Name, fmt.Sprintf("%s:%s", utils.FamillyInet6, kubeDestinationIPSetPrefix)) {
if _, ok := activePolicyIPSets[set.Name]; !ok {
cleanupPolicyIPSets = append(cleanupPolicyIPSets, set)
}
}
}
} else {
for _, set := range ipsets.Sets() {
if strings.HasPrefix(set.Name, kubeSourceIPSetPrefix) ||
strings.HasPrefix(set.Name, kubeDestinationIPSetPrefix) {
if _, ok := activePolicyIPSets[set.Name]; !ok {
cleanupPolicyIPSets = append(cleanupPolicyIPSets, set)
}
for _, set := range ipsets.Sets() {
if set.HasPrefix(kubeSourceIPSetPrefix) ||
set.HasPrefix(kubeDestinationIPSetPrefix) {
if _, ok := activePolicyIPSets[set.Name]; !ok {
cleanupPolicyIPSets = append(cleanupPolicyIPSets, set)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/utils/ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ func (ipset *IPSet) Name(setName string) string {
return IPSetName(setName, ipset.isIpv6)
}

func (set *Set) HasPrefix(prefix string) bool {
fullPrefix := IPSetName(prefix, set.Parent.isIpv6)
return strings.HasPrefix(set.name(), fullPrefix)
}

func (set *Set) name() string {
return set.Parent.Name(set.Name)
}
Expand Down

0 comments on commit d086841

Please sign in to comment.