Skip to content

Commit

Permalink
remove util.UniqString() and util.ContainsString() (#3732)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian committed Feb 19, 2024
1 parent 9abe921 commit cc935bd
Show file tree
Hide file tree
Showing 48 changed files with 129 additions and 180 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/external_gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"slices"
"strings"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -234,7 +235,7 @@ func (c *Controller) getGatewayChassis(config map[string]string) ([]string, erro
if name == "" {
continue
}
if !util.ContainsString(gwNodes, name) {
if !slices.Contains(gwNodes, name) {
gwNodes = append(gwNodes, name)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/external_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"slices"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (c *Controller) getRouterStatus() (logicalRouters map[string]util.LogicalRo
}
lsp := peerPorts[0]
switches, err := c.OVNNbClient.ListLogicalSwitch(false, func(ls *ovnnb.LogicalSwitch) bool {
return util.ContainsString(ls.Ports, lsp.UUID)
return slices.Contains(ls.Ports, lsp.UUID)
})
if err != nil || len(switches) > 1 {
klog.Errorf("failed to get logical switch of LSP %s: %v", lsp.Name, err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"fmt"
"slices"
"strings"
"unicode"

Expand Down Expand Up @@ -102,7 +103,7 @@ func (c *Controller) gcVpcNatGateway() error {
return err
}
for _, sts := range stss.Items {
if !util.ContainsString(gwStsNames, sts.Name) {
if !slices.Contains(gwStsNames, sts.Name) {
err = c.config.KubeClient.AppsV1().StatefulSets(c.config.PodNamespace).Delete(context.Background(), sts.Name, metav1.DeleteOptions{})
if err != nil {
klog.Errorf("failed to delete vpc nat gateway statefulset, %v", err)
Expand Down Expand Up @@ -199,7 +200,7 @@ func (c *Controller) gcCustomLogicalRouter() error {
if lr.Name == c.config.ClusterRouter {
continue
}
if !util.ContainsString(vpcNames, lr.Name) {
if !slices.Contains(vpcNames, lr.Name) {
klog.Infof("gc router %s", lr.Name)
if err := c.deleteVpcRouter(lr.Name); err != nil {
klog.Errorf("failed to delete router %s, %v", lr.Name, err)
Expand Down Expand Up @@ -233,7 +234,7 @@ func (c *Controller) gcNode() error {
}
}
for _, no := range ipNodeNames {
if !util.ContainsString(nodeNames, no) {
if !slices.Contains(nodeNames, no) {
klog.Infof("gc node %s", no)
if err := c.handleDeleteNode(no); err != nil {
klog.Errorf("failed to gc node %s, %v", no, err)
Expand Down Expand Up @@ -586,7 +587,7 @@ func (c *Controller) gcLoadBalancer() error {
// delete lbs
if err = c.OVNNbClient.DeleteLoadBalancers(
func(lb *ovnnb.LoadBalancer) bool {
return !util.ContainsString(vpcLbs, lb.Name)
return !slices.Contains(vpcLbs, lb.Name)
},
); err != nil {
klog.Errorf("delete load balancers: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -577,7 +578,7 @@ func (c *Controller) syncIPCR() error {

for _, ipCR := range ips {
ip := ipCR.DeepCopy()
if ip.DeletionTimestamp != nil && util.ContainsString(ip.Finalizers, util.ControllerName) {
if ip.DeletionTimestamp != nil && slices.Contains(ip.Finalizers, util.ControllerName) {
klog.Infof("enqueue update for deleting ip %s", ip.Name)
c.updateIPQueue.Add(ip.Name)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"reflect"
"slices"
"strings"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -342,7 +343,7 @@ func (c *Controller) handleDelIP(ip *kubeovnv1.IP) error {

func (c *Controller) handleAddIPFinalizer(cachedIP *kubeovnv1.IP, finalizer string) error {
if cachedIP.DeletionTimestamp.IsZero() {
if util.ContainsString(cachedIP.Finalizers, finalizer) {
if slices.Contains(cachedIP.Finalizers, finalizer) {
return nil
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"slices"
"strings"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -143,7 +144,7 @@ func (c *Controller) handleAddNamespace(key string) error {
}

for _, p := range ippools {
if util.ContainsString(p.Spec.Namespaces, key) {
if slices.Contains(p.Spec.Namespaces, key) {
ippool = p.Name
break
}
Expand All @@ -162,7 +163,7 @@ func (c *Controller) handleAddNamespace(key string) error {
return err
}
for _, v := range vpcs {
if util.ContainsString(v.Spec.Namespaces, key) {
if slices.Contains(v.Spec.Namespaces, key) {
vpc = v
break
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"fmt"
"reflect"
"slices"
"strconv"
"strings"
"unicode"
Expand Down Expand Up @@ -558,7 +559,7 @@ func (c *Controller) fetchSelectedPorts(namespace string, selector *metav1.Label
}
}
}
subnets = util.UniqString(subnets)
subnets = slices.Compact(subnets)
return ports, subnets, nil
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -393,7 +394,7 @@ func (c *Controller) handleNodeAnnotationsForProviderNetworks(node *v1.Node) err
interfaceAnno := fmt.Sprintf(util.ProviderNetworkInterfaceTemplate, pn.Name)

var newPn *kubeovnv1.ProviderNetwork
excluded := util.ContainsString(pn.Spec.ExcludeNodes, node.Name)
excluded := slices.Contains(pn.Spec.ExcludeNodes, node.Name)
if !excluded && len(node.Annotations) != 0 && node.Annotations[excludeAnno] == "true" {
newPn = pn.DeepCopy()
newPn.Spec.ExcludeNodes = append(newPn.Spec.ExcludeNodes, node.Name)
Expand All @@ -402,7 +403,7 @@ func (c *Controller) handleNodeAnnotationsForProviderNetworks(node *v1.Node) err

var customInterface string
for _, v := range pn.Spec.CustomInterfaces {
if util.ContainsString(v.Nodes, node.Name) {
if slices.Contains(v.Nodes, node.Name) {
customInterface = v.Interface
break
}
Expand Down Expand Up @@ -547,7 +548,7 @@ func (c *Controller) updateProviderNetworkForNodeDeletion(pn *kubeovnv1.Provider
// update provider network status
var needUpdate bool
newPn := pn.DeepCopy()
if util.ContainsString(newPn.Status.ReadyNodes, node) {
if slices.Contains(newPn.Status.ReadyNodes, node) {
newPn.Status.ReadyNodes = util.RemoveString(newPn.Status.ReadyNodes, node)
needUpdate = true
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/ovn_dnat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net"
"slices"

"github.com/ovn-org/libovsdb/ovsdb"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -661,7 +662,7 @@ func (c *Controller) DelDnatRule(vpcName, dnatName, externalIP, externalPort str

func (c *Controller) handleAddOvnDnatFinalizer(cachedDnat *kubeovnv1.OvnDnatRule, finalizer string) error {
if cachedDnat.DeletionTimestamp.IsZero() {
if util.ContainsString(cachedDnat.Finalizers, finalizer) {
if slices.Contains(cachedDnat.Finalizers, finalizer) {
return nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/ovn_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -585,7 +586,7 @@ func (c *Controller) natLabelAndAnnoOvnEip(eipName, natName, vpcName string) err

func (c *Controller) handleAddOvnEipFinalizer(cachedEip *kubeovnv1.OvnEip, finalizer string) error {
if cachedEip.DeletionTimestamp.IsZero() {
if util.ContainsString(cachedEip.Finalizers, finalizer) {
if slices.Contains(cachedEip.Finalizers, finalizer) {
return nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/ovn_fip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"strconv"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -600,7 +601,7 @@ func (c *Controller) GetOvnEip(eipName string) (*kubeovnv1.OvnEip, error) {

func (c *Controller) handleAddOvnFipFinalizer(cachedFip *kubeovnv1.OvnFip, finalizer string) error {
if cachedFip.DeletionTimestamp.IsZero() {
if util.ContainsString(cachedFip.Finalizers, finalizer) {
if slices.Contains(cachedFip.Finalizers, finalizer) {
return nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/ovn_snat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"slices"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -543,7 +544,7 @@ func (c *Controller) ovnSnatChangeEip(snat *kubeovnv1.OvnSnatRule, eip *kubeovnv

func (c *Controller) handleAddOvnSnatFinalizer(cachedSnat *kubeovnv1.OvnSnatRule, finalizer string) error {
if cachedSnat.DeletionTimestamp.IsZero() {
if util.ContainsString(cachedSnat.Finalizers, finalizer) {
if slices.Contains(cachedSnat.Finalizers, finalizer) {
return nil
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ func appendCheckPodToDel(c *Controller, pod *v1.Pod, ownerRefName, ownerRefKind
if !ownerRefSubnetExist {
nsSubnetNames := podNs.Annotations[util.LogicalSwitchAnnotation]
// check if pod use the subnet of its ns
if nsSubnetNames != "" && podSwitch != "" && !util.ContainsString(strings.Split(nsSubnetNames, ","), podSwitch) {
if nsSubnetNames != "" && podSwitch != "" && !slices.Contains(strings.Split(nsSubnetNames, ","), podSwitch) {
klog.Infof("ns %s annotation subnet is %s, which is inconstant with subnet for pod %s, delete pod", pod.Namespace, nsSubnetNames, pod.Name)
return true, nil
}
Expand Down Expand Up @@ -2022,7 +2022,7 @@ func (c *Controller) getVirtualIPs(pod *v1.Pod, podNets []*kubeovnNet) map[strin
vipsListMap := make(map[string][]string)
var vipNamesList []string
for _, vipName := range strings.Split(strings.TrimSpace(pod.Annotations[util.AAPsAnnotation]), ",") {
if !util.ContainsString(vipNamesList, vipName) {
if !slices.Contains(vipNamesList, vipName) {
vipNamesList = append(vipNamesList, vipName)
}
}
Expand Down Expand Up @@ -2072,7 +2072,7 @@ func (c *Controller) getVirtualIPs(pod *v1.Pod, podNets []*kubeovnNet) map[strin
}

for _, vip := range strings.Split(vipStr, ",") {
if util.IsValidIP(vip) && !util.ContainsString(vipsList, vip) {
if util.IsValidIP(vip) && !slices.Contains(vipsList, vip) {
vipsList = append(vipsList, vip)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/provider_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"fmt"
"slices"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (c *Controller) resyncProviderNetworkStatus() {

var conditionsUpdated bool
for _, node := range nodes {
if util.ContainsString(pn.Spec.ExcludeNodes, node.Name) {
if slices.Contains(pn.Spec.ExcludeNodes, node.Name) {
if pn.Status.RemoveNodeConditions(node.Name) {
conditionsUpdated = true
}
Expand Down Expand Up @@ -79,7 +80,7 @@ func (c *Controller) resyncProviderNetworkStatus() {

expectNodes = append(readyNodes, notReadyNodes...)
for _, c := range pn.Status.Conditions {
if !util.ContainsString(expectNodes, c.Node) {
if !slices.Contains(expectNodes, c.Node) {
if pn.Status.RemoveNodeConditions(c.Node) {
conditionsUpdated = true
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/qos_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"reflect"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -520,7 +521,7 @@ func (c *Controller) handleAddQoSPolicyFinalizer(key string) error {
return err
}
if cachedQoSPolicy.DeletionTimestamp.IsZero() {
if util.ContainsString(cachedQoSPolicy.Finalizers, util.ControllerName) {
if slices.Contains(cachedQoSPolicy.Finalizers, util.ControllerName) {
return nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"reflect"
"slices"
"strings"

"github.com/cnf/structhash"
Expand Down Expand Up @@ -498,7 +499,7 @@ func (c *Controller) reconcilePortSg(portName, securityGroups string) error {
continue
}
needAssociated := "false"
if util.ContainsString(newSgList, sgName) {
if slices.Contains(newSgList, sgName) {
needAssociated = "true"
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -111,7 +112,7 @@ func (c *Controller) enqueueUpdateService(oldObj, newObj interface{}) {
newClusterIps := getVipIps(newSvc)
var ipsToDel []string
for _, oldClusterIP := range oldClusterIps {
if !util.ContainsString(newClusterIps, oldClusterIP) {
if !slices.Contains(newClusterIps, oldClusterIP) {
ipsToDel = append(ipsToDel, oldClusterIP)
}
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func (c *Controller) handleDeleteService(service *vpcService) error {
ip = parseVipAddr(vip)

for _, svc := range svcs {
if util.ContainsString(util.ServiceClusterIPs(*svc), ip) {
if slices.Contains(util.ServiceClusterIPs(*svc), ip) {
found = true
break
}
Expand Down Expand Up @@ -389,7 +390,7 @@ func (c *Controller) handleUpdateService(key string) error {
}
}
for vip := range lb.Vips {
if ip := parseVipAddr(vip); (util.ContainsString(ips, ip) && !util.ContainsString(svcVips, vip)) || util.ContainsString(ipsToDel, ip) {
if ip := parseVipAddr(vip); (slices.Contains(ips, ip) && !slices.Contains(svcVips, vip)) || slices.Contains(ipsToDel, ip) {
klog.Infof("remove stale vip %s from LB %s", vip, lbName)
if err := c.OVNNbClient.LoadBalancerDeleteVip(lbName, vip, ignoreHealthCheck); err != nil {
klog.Errorf("failed to delete vip %s from LB %s: %v", vip, lbName, err)
Expand All @@ -409,7 +410,7 @@ func (c *Controller) handleUpdateService(key string) error {
}
klog.V(3).Infof("existing vips of LB %s: %v", oLbName, lb.Vips)
for vip := range oLb.Vips {
if ip := parseVipAddr(vip); util.ContainsString(ips, ip) || util.ContainsString(ipsToDel, ip) {
if ip := parseVipAddr(vip); slices.Contains(ips, ip) || slices.Contains(ipsToDel, ip) {
klog.Infof("remove stale vip %s from LB %s", vip, oLbName)
if err = c.OVNNbClient.LoadBalancerDeleteVip(oLbName, vip, ignoreHealthCheck); err != nil {
klog.Errorf("failed to delete vip %s from LB %s: %v", vip, oLbName, err)
Expand Down
Loading

0 comments on commit cc935bd

Please sign in to comment.