From ae3aec053c626228310dbb3e3893d6b80889010b Mon Sep 17 00:00:00 2001 From: Gaurav Ghildiyal Date: Tue, 31 Jan 2023 10:19:51 -0800 Subject: [PATCH] Remove Services.Get() from syncNegStatusAnnotation() --- pkg/neg/controller.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/neg/controller.go b/pkg/neg/controller.go index 5f4668613d..a22901b9ac 100644 --- a/pkg/neg/controller.go +++ b/pkg/neg/controller.go @@ -655,11 +655,18 @@ func (c *Controller) syncNegStatusAnnotation(namespace, name string, portMap neg if err != nil { return err } - coreClient := c.client.CoreV1() - service, err := coreClient.Services(namespace).Get(context.TODO(), name, metav1.GetOptions{}) + obj, exists, err := c.serviceLister.GetByKey(getServiceKey(namespace, name).Key()) if err != nil { return err } + if !exists { + // Service no longer exists so doesn't require any update. + return nil + } + service, ok := obj.(*apiv1.Service) + if !ok { + return fmt.Errorf("cannot convert obj to Service; obj=%T", obj) + } // Remove NEG Status Annotation when no NEG is needed if len(portMap) == 0 { @@ -667,7 +674,7 @@ func (c *Controller) syncNegStatusAnnotation(namespace, name string, portMap neg newSvcObjectMeta := service.ObjectMeta.DeepCopy() delete(newSvcObjectMeta.Annotations, annotations.NEGStatusKey) c.logger.V(2).Info("Removing NEG status annotation from service", "service", klog.KRef(namespace, name)) - return patch.PatchServiceObjectMetadata(coreClient, service, *newSvcObjectMeta) + return patch.PatchServiceObjectMetadata(c.client.CoreV1(), service, *newSvcObjectMeta) } // service doesn't have the expose NEG annotation and doesn't need update return nil @@ -689,7 +696,7 @@ func (c *Controller) syncNegStatusAnnotation(namespace, name string, portMap neg } newSvcObjectMeta.Annotations[annotations.NEGStatusKey] = annotation c.logger.V(2).Info("Updating NEG visibility annotation on service", "annotation", annotation, "service", klog.KRef(namespace, name)) - return patch.PatchServiceObjectMetadata(coreClient, service, *newSvcObjectMeta) + return patch.PatchServiceObjectMetadata(c.client.CoreV1(), service, *newSvcObjectMeta) } func (c *Controller) handleErr(err error, key interface{}) {