diff --git a/pkg/neg/syncers/transaction.go b/pkg/neg/syncers/transaction.go index 028aea3ffd..8fe9311e41 100644 --- a/pkg/neg/syncers/transaction.go +++ b/pkg/neg/syncers/transaction.go @@ -204,12 +204,6 @@ func (s *transactionSyncer) syncInternal() error { } func (s *transactionSyncer) syncInternalImpl() *negtypes.NegSyncResult { - // TODO(cheungdavid): for now we reset the boolean so it is a no-op, but - // in the future, it will be used to trigger degraded mode if the syncer is in error state. - if s.inErrorState() { - s.resetErrorState() - } - if s.needInit || s.isZoneChange() { if err := s.ensureNetworkEndpointGroups(); err != nil { return negtypes.NewNegSyncResult(err, negtypes.ResultNegNotFound) @@ -236,7 +230,7 @@ func (s *transactionSyncer) syncInternalImpl() *negtypes.NegSyncResult { var targetMap map[string]negtypes.NetworkEndpointSet var endpointPodMap negtypes.EndpointPodMap - var dupCount int + var result *negtypes.NegSyncResult if s.enableEndpointSlices { slices, err := s.endpointSliceLister.ByIndex(endpointslices.EndpointSlicesByServiceIndex, endpointslices.FormatEndpointSlicesServiceKey(s.Namespace, s.Name)) @@ -268,18 +262,13 @@ func (s *transactionSyncer) syncInternalImpl() *negtypes.NegSyncResult { s.logger.V(3).Info("Endpoint slice syncs", "Namespace", endpointslice.Namespace, "Name", endpointslice.Name, "staleness", epsStaleness) } - endpointsData := negtypes.EndpointsDataFromEndpointSlices(endpointSlices) - targetMap, endpointPodMap, dupCount, err = s.endpointsCalculator.CalculateEndpoints(endpointsData, currentMap) - if valid, result := s.CheckEPField(err); !valid { - s.setErrorState(result.Result) - return result - } - if valid, result := s.CheckEndpointInfo(endpointsData, endpointPodMap, dupCount); !valid { - s.setErrorState(result.Result) - return result - } - if err != nil { - return negtypes.NewNegSyncResult(fmt.Errorf("endpoints calculation error in mode %q, err: %w", s.endpointsCalculator.Mode(), err), negtypes.ResultOtherError) + if s.inErrorState() { + s.resetErrorState() + } else { + targetMap, endpointPodMap, result = s.computeTargetMap(endpointSlices) + if result.Error != nil { + return result + } } } else { ep, exists, err := s.endpointLister.Get( @@ -333,6 +322,24 @@ func (s *transactionSyncer) syncInternalImpl() *negtypes.NegSyncResult { return s.syncNetworkEndpoints(addEndpoints, removeEndpoints) } +// computeTargetMap retrieve endpoint slices and compute the desire map +func (s *transactionSyncer) computeTargetMap(endpointSlices []*discovery.EndpointSlice) (map[string]negtypes.NetworkEndpointSet, negtypes.EndpointPodMap, *negtypes.NegSyncResult) { + endpointsData := negtypes.EndpointsDataFromEndpointSlices(endpointSlices) + targetMap, endpointPodMap, dupCount, err := s.endpointsCalculator.CalculateEndpoints(endpointsData, nil) + var valid bool + var result *negtypes.NegSyncResult + if valid, result = s.CheckEPField(err); !valid { + s.setErrorState(result.Result) + } + if valid, result = s.CheckEndpointInfo(endpointsData, endpointPodMap, dupCount); !valid { + s.setErrorState(result.Result) + } + if result.Error != nil { + return nil, nil, result + } + return targetMap, endpointPodMap, negtypes.NewNegSyncResult(nil, negtypes.ResultSuccess) +} + // syncLock must already be acquired before execution func (s *transactionSyncer) inErrorState() bool { return s.errorState != ""