Skip to content

Commit

Permalink
Refactor procedure to compute endpoint target map
Browse files Browse the repository at this point in the history
Refactor the code that compute endpoint target map into a separate
function computeTargetMap.
  • Loading branch information
sawsa307 committed Feb 27, 2023
1 parent 88f329d commit 9e0f1ae
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions pkg/neg/syncers/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 != ""
Expand Down

0 comments on commit 9e0f1ae

Please sign in to comment.