Skip to content

Commit

Permalink
use cache.MetaObjectToName() to get namespaced name
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian committed Dec 17, 2024
1 parent 2cb14d0 commit 9095432
Show file tree
Hide file tree
Showing 32 changed files with 137 additions and 584 deletions.
10 changes: 2 additions & 8 deletions pkg/controller/admin_network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
v1alpha1 "sigs.k8s.io/network-policy-api/apis/v1alpha1"
Expand Down Expand Up @@ -45,19 +44,14 @@ type AdminNetworkPolicyChangedDelta struct {
}

func (c *Controller) enqueueAddAnp(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}
key := cache.MetaObjectToName(obj.(*v1alpha1.AdminNetworkPolicy)).String()
klog.V(3).Infof("enqueue add anp %s", key)
c.addAnpQueue.Add(key)
}

func (c *Controller) enqueueDeleteAnp(obj interface{}) {
anp := obj.(*v1alpha1.AdminNetworkPolicy)
klog.V(3).Infof("enqueue delete anp %s", anp.Name)
klog.V(3).Infof("enqueue delete banp %s", cache.MetaObjectToName(anp).String())
c.deleteAnpQueue.Add(anp)
}

Expand Down
10 changes: 2 additions & 8 deletions pkg/controller/baseline_admin_network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/scylladb/go-set/strset"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
v1alpha1 "sigs.k8s.io/network-policy-api/apis/v1alpha1"
Expand All @@ -18,19 +17,14 @@ import (
)

func (c *Controller) enqueueAddBanp(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}
key := cache.MetaObjectToName(obj.(*v1alpha1.BaselineAdminNetworkPolicy)).String()
klog.V(3).Infof("enqueue add banp %s", key)
c.addBanpQueue.Add(key)
}

func (c *Controller) enqueueDeleteBanp(obj interface{}) {
banp := obj.(*v1alpha1.BaselineAdminNetworkPolicy)
klog.V(3).Infof("enqueue delete banp %s", banp.Name)
klog.V(3).Infof("enqueue delete banp %s", cache.MetaObjectToName(banp).String())
c.deleteBanpQueue.Add(banp)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ func getWorkItemKey(obj any) string {
case string:
return v
case *vpcService:
return fmt.Sprintf("%s/%s", v.Svc.Namespace, v.Svc.Name)
return cache.MetaObjectToName(obj.(*vpcService).Svc).String()
case *AdminNetworkPolicyChangedDelta:
return v.key
case *SlrInfo:
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/deployment.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package controller

import (
"fmt"
"reflect"

appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"

kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
Expand Down Expand Up @@ -33,7 +33,7 @@ func (c *Controller) enqueueAddDeployment(obj interface{}) {
deploy := obj.(*appsv1.Deployment)
for _, ref := range deploy.OwnerReferences {
if ref.APIVersion == vpcEgressGatewayGroupVersion && ref.Kind == vpcEgressGatewayKind {
key := fmt.Sprintf("%s/%s", deploy.Namespace, ref.Name)
key := types.NamespacedName{Namespace: deploy.Namespace, Name: ref.Name}.String()
klog.V(3).Infof("enqueue update vpc-egress-gateway %s", key)
c.addOrUpdateVpcEgressGatewayQueue.Add(key)
return
Expand Down
20 changes: 2 additions & 18 deletions pkg/controller/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ import (
)

func (c *Controller) enqueueAddEndpoint(obj interface{}) {
var (
key string
err error
)

if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}
key := cache.MetaObjectToName(obj.(*v1.Endpoints)).String()
klog.V(3).Infof("enqueue add endpoint %s", key)
c.addOrUpdateEndpointQueue.Add(key)
}
Expand All @@ -42,15 +34,7 @@ func (c *Controller) enqueueUpdateEndpoint(oldObj, newObj interface{}) {
return
}

var (
key string
err error
)

if key, err = cache.MetaNamespaceKeyFunc(newObj); err != nil {
utilruntime.HandleError(err)
return
}
key := cache.MetaObjectToName(newEp).String()
klog.V(3).Infof("enqueue update endpoint %s", key)
c.addOrUpdateEndpointQueue.Add(key)
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/controller/inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

"github.com/kubeovn/kube-ovn/pkg/ovs"
Expand All @@ -33,6 +34,7 @@ func (c *Controller) inspectPod() error {
}

podName := c.getNameByPod(pod)
key := cache.MetaObjectToName(pod).String()
podNets, err := c.getPodKubeovnNets(pod)
if err != nil {
klog.Errorf("failed to list pod subnets, %v", err)
Expand All @@ -57,16 +59,17 @@ func (c *Controller) inspectPod() error {
}
if _, err := c.config.KubeClient.CoreV1().Pods(pod.Namespace).Patch(context.Background(), pod.Name,
types.StrategicMergePatchType, patch, metav1.PatchOptions{}, ""); err != nil {
klog.Errorf("patch pod %s/%s failed %v during inspection", pod.Name, pod.Namespace, err)
klog.Errorf("patch pod %s failed %v during inspection", key, err)
return err
}
klog.V(5).Infof("finish remove annotation for %s", portName)
c.addOrUpdatePodQueue.Add(fmt.Sprintf("%s/%s", pod.Namespace, pod.Name))
klog.V(5).Infof("enqueue update pod %s", key)
c.addOrUpdatePodQueue.Add(key)
break
} else if pod.Annotations[fmt.Sprintf(util.AllocatedAnnotationTemplate, podNet.ProviderName)] == "true" && pod.Spec.NodeName != "" &&
pod.Annotations[fmt.Sprintf(util.RoutedAnnotationTemplate, podNet.ProviderName)] != "true" {
klog.V(5).Infof("enqueue update pod %s/%s", pod.Namespace, pod.Name)
c.addOrUpdatePodQueue.Add(fmt.Sprintf("%s/%s", pod.Namespace, pod.Name))
klog.V(5).Infof("enqueue update pod %s", key)
c.addOrUpdatePodQueue.Add(key)
break
}
}
Expand Down
24 changes: 5 additions & 19 deletions pkg/controller/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
Expand All @@ -35,23 +34,13 @@ func (c *Controller) enqueueAddIP(obj interface{}) {
klog.V(3).Infof("enqueue update attach status for subnet %s", as)
c.updateSubnetStatusQueue.Add(as)
}
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}

key := cache.MetaObjectToName(ipObj).String()
klog.V(3).Infof("enqueue add ip %s", key)
c.addIPQueue.Add(key)
}

func (c *Controller) enqueueUpdateIP(oldObj, newObj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(newObj); err != nil {
utilruntime.HandleError(err)
return
}
oldIP := oldObj.(*kubeovnv1.IP)
newIP := newObj.(*kubeovnv1.IP)
// ip can not change these specs below
Expand Down Expand Up @@ -84,6 +73,7 @@ func (c *Controller) enqueueUpdateIP(oldObj, newObj interface{}) {
}
}
if !newIP.DeletionTimestamp.IsZero() {
key := cache.MetaObjectToName(newIP).String()
klog.V(3).Infof("enqueue update ip %s", key)
c.updateIPQueue.Add(key)
return
Expand All @@ -98,16 +88,12 @@ func (c *Controller) enqueueUpdateIP(oldObj, newObj interface{}) {
}

func (c *Controller) enqueueDelIP(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}
ipObj := obj.(*kubeovnv1.IP)
if strings.HasPrefix(ipObj.Name, util.U2OInterconnName[0:19]) {
return
}

key := cache.MetaObjectToName(ipObj).String()
klog.V(3).Infof("enqueue del ip %s", key)
c.delIPQueue.Add(ipObj)
}
Expand Down
16 changes: 3 additions & 13 deletions pkg/controller/ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

Expand All @@ -18,32 +17,23 @@ import (
)

func (c *Controller) enqueueAddIPPool(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
utilruntime.HandleError(err)
return
}
key := cache.MetaObjectToName(obj.(*kubeovnv1.IPPool)).String()
klog.V(3).Infof("enqueue add ippool %s", key)
c.addOrUpdateIPPoolQueue.Add(key)
}

func (c *Controller) enqueueDeleteIPPool(obj interface{}) {
ippool := obj.(*kubeovnv1.IPPool)
klog.V(3).Infof("enqueue delete ippool %s", ippool.Name)
klog.V(3).Infof("enqueue delete ippool %s", cache.MetaObjectToName(ippool).String())
c.deleteIPPoolQueue.Add(ippool)
}

func (c *Controller) enqueueUpdateIPPool(oldObj, newObj interface{}) {
oldIPPool := oldObj.(*kubeovnv1.IPPool)
newIPPool := newObj.(*kubeovnv1.IPPool)
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err != nil {
utilruntime.HandleError(err)
return
}

if !reflect.DeepEqual(oldIPPool.Spec.Namespaces, newIPPool.Spec.Namespaces) ||
!reflect.DeepEqual(oldIPPool.Spec.IPs, newIPPool.Spec.IPs) {
key := cache.MetaObjectToName(newIPPool).String()
klog.V(3).Infof("enqueue update ippool %s", key)
c.addOrUpdateIPPoolQueue.Add(key)
}
Expand Down
19 changes: 3 additions & 16 deletions pkg/controller/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@ import (
)

func (c *Controller) enqueueAddVMIMigration(obj interface{}) {
var (
key string
err error
)

if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}

klog.Infof("enqueue add VMI migration %s ", key)
key := cache.MetaObjectToName(obj.(*kubevirtv1.VirtualMachineInstanceMigration)).String()
klog.Infof("enqueue add VMI migration %s", key)
c.addOrUpdateVMIMigrationQueue.Add(key)
}

Expand All @@ -38,11 +29,7 @@ func (c *Controller) enqueueUpdateVMIMigration(oldObj, newObj interface{}) {

if !newVmi.DeletionTimestamp.IsZero() ||
!reflect.DeepEqual(oldVmi.Status.Phase, newVmi.Status.Phase) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err != nil {
utilruntime.HandleError(err)
return
}
key := cache.MetaObjectToName(newVmi).String()
klog.Infof("enqueue update VMI migration %s", key)
c.addOrUpdateVMIMigrationQueue.Add(key)
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/controller/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

Expand All @@ -25,12 +24,8 @@ func (c *Controller) enqueueAddNamespace(obj interface{}) {
c.updateNpQueue.Add(np)
}
}
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}

key := cache.MetaObjectToName(obj.(*v1.Namespace)).String()
c.addNamespaceQueue.Add(key)
}

Expand Down
Loading

0 comments on commit 9095432

Please sign in to comment.