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 14, 2023
1 parent b3cc1db commit 0d160cf
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions pkg/neg/syncers/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,6 @@ func (s *transactionSyncer) syncInternal() error {
}

func (s *transactionSyncer) syncInternalImpl() error {
// 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 err
Expand All @@ -233,7 +227,6 @@ func (s *transactionSyncer) syncInternalImpl() error {

var targetMap map[string]negtypes.NetworkEndpointSet
var endpointPodMap negtypes.EndpointPodMap
var dupCount int

if s.enableEndpointSlices {
slices, err := s.endpointSliceLister.ByIndex(endpointslices.EndpointSlicesByServiceIndex, endpointslices.FormatEndpointSlicesServiceKey(s.Namespace, s.Name))
Expand All @@ -248,13 +241,13 @@ func (s *transactionSyncer) syncInternalImpl() error {
for i, slice := range slices {
endpointSlices[i] = slice.(*discovery.EndpointSlice)
}
endpointsData := negtypes.EndpointsDataFromEndpointSlices(endpointSlices)
targetMap, endpointPodMap, dupCount, err = s.endpointsCalculator.CalculateEndpoints(endpointsData, currentMap)
if !s.isValidEPField(err) || !s.isValidEndpointInfo(endpointsData, endpointPodMap, dupCount) {
s.setErrorState()
}
if err != nil {
return fmt.Errorf("endpoints calculation error in mode %q, err: %w", s.endpointsCalculator.Mode(), err)
if s.inErrorState() {
s.resetErrorState()
} else {
targetMap, endpointPodMap, err = s.computeTargetMap(endpointSlices)
if err != nil {
return err
}
}
} else {
ep, exists, err := s.endpointLister.Get(
Expand Down Expand Up @@ -308,6 +301,24 @@ func (s *transactionSyncer) syncInternalImpl() error {
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, error) {
var targetMap map[string]negtypes.NetworkEndpointSet
var endpointPodMap negtypes.EndpointPodMap
var dupCount int
var err error

endpointsData := negtypes.EndpointsDataFromEndpointSlices(endpointSlices)
targetMap, endpointPodMap, dupCount, err = s.endpointsCalculator.CalculateEndpoints(endpointsData, nil)
if !s.isValidEPField(err) || !s.isValidEndpointInfo(endpointsData, endpointPodMap, dupCount) {
s.setErrorState()
}
if err != nil {
return nil, nil, fmt.Errorf("endpoints calculation error in mode %q, err: %w", s.endpointsCalculator.Mode(), err)
}
return targetMap, endpointPodMap, err
}

// syncLock must already be acquired before execution
func (s *transactionSyncer) inErrorState() bool {
return s.inError
Expand Down

0 comments on commit 0d160cf

Please sign in to comment.