Skip to content

Commit

Permalink
nit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
freehan committed Jun 21, 2018
1 parent 83a6b89 commit 778f29b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
20 changes: 11 additions & 9 deletions pkg/neg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func (c *Controller) Run(stopCh <-chan struct{}) {
func (c *Controller) IsHealthy() error {
// check if last seen service and endpoint processing is more than an hour ago
if c.syncTracker.Get().Before(time.Now().Add(-time.Hour)) {
msg := fmt.Sprintf("NEG controller has not proccessed any service and endpoint updates for more than an hour. Something went wrong. Last sync was on %v", c.syncTracker.Get())
msg := fmt.Sprintf("NEG controller has not processed any service "+
"and endpoint updates for more than an hour. Something went wrong. "+
"Last sync was on %v", c.syncTracker.Get())
glog.Error(msg)
return fmt.Errorf(msg)
}
Expand Down Expand Up @@ -240,7 +242,7 @@ func (c *Controller) processService(key string) error {
if !enabled {
c.manager.StopSyncer(namespace, name)
// delete the annotation
return c.syncNegAnnotation(namespace, name, service, make(PortNameMap))
return c.syncNegStatusAnnotation(namespace, name, service, make(PortNameMap))
}

glog.V(2).Infof("Syncing service %q", key)
Expand Down Expand Up @@ -272,19 +274,20 @@ func (c *Controller) processService(key string) error {
svcPortMap = svcPortMap.Union(negSvcPorts)
}

err = c.syncNegAnnotation(namespace, name, service, svcPortMap)
err = c.syncNegStatusAnnotation(namespace, name, service, svcPortMap)
if err != nil {
return err
}
return c.manager.EnsureSyncers(namespace, name, svcPortMap)
}

func (c *Controller) syncNegAnnotation(namespace, name string, service *apiv1.Service, portMap PortNameMap) error {
func (c *Controller) syncNegStatusAnnotation(namespace, name string, service *apiv1.Service, portMap PortNameMap) error {
zones, err := c.zoneGetter.ListZones()
if err != nil {
return err
}

// Remove NEG Status Annotation when no NEG is needed
if len(portMap) == 0 {
if _, ok := service.Annotations[annotations.NEGStatusKey]; ok {
// TODO: use PATCH to remove annotation
Expand All @@ -297,24 +300,23 @@ func (c *Controller) syncNegAnnotation(namespace, name string, service *apiv1.Se
}

portToNegs := make(PortNameMap)
for svcPort, _ := range portMap {
for svcPort := range portMap {
portToNegs[svcPort] = c.namer.NEG(namespace, name, svcPort)
}
negSvcState := GetNegStatus(zones, portToNegs)
formattedAnnotation, err := json.Marshal(negSvcState)
bytes, err := json.Marshal(negSvcState)
if err != nil {
return err
}

annotation := string(formattedAnnotation)

annotation := string(bytes)
existingAnnotation, ok := service.Annotations[annotations.NEGStatusKey]
if ok && existingAnnotation == annotation {
return nil
}

service.Annotations[annotations.NEGStatusKey] = annotation
glog.V(2).Infof("Updating NEG visibility annotation %q on service %s/%s.", annotation, namespace, name)
// TODO: use PATCH to Update Annotation
return c.serviceLister.Update(service)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/neg/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,20 @@ func TestSyncNegAnnotation(t *testing.T) {
t.Fatalf("Service was not retrieved successfully, err: %v", err)
}

controller.syncNegAnnotation(testServiceNamespace, testServiceName, svc.(*apiv1.Service), tc.previousPortMap)
controller.syncNegStatusAnnotation(testServiceNamespace, testServiceName, svc.(*apiv1.Service), tc.previousPortMap)
svc, _, _ = controller.serviceLister.GetByKey(svcKey)

var oldSvcPorts []int32
for port, _ := range tc.previousPortMap {
for port := range tc.previousPortMap {
oldSvcPorts = append(oldSvcPorts, port)
}
validateServiceStateAnnotation(t, svc.(*apiv1.Service), oldSvcPorts)

controller.syncNegAnnotation(testServiceNamespace, testServiceName, svc.(*apiv1.Service), tc.portMap)
controller.syncNegStatusAnnotation(testServiceNamespace, testServiceName, svc.(*apiv1.Service), tc.portMap)
svc, _, _ = controller.serviceLister.GetByKey(svcKey)

var svcPorts []int32
for port, _ := range tc.portMap {
for port := range tc.portMap {
svcPorts = append(svcPorts, port)
}
validateServiceStateAnnotation(t, svc.(*apiv1.Service), svcPorts)
Expand Down

0 comments on commit 778f29b

Please sign in to comment.