From e6ea78db06c21fde2fb905fecef3195d17c20677 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Tue, 14 Feb 2023 22:16:34 +0000 Subject: [PATCH] Create skeleton for degraded mode procedure Create skeleton for degraded mode procedure --- pkg/neg/syncers/transaction.go | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/pkg/neg/syncers/transaction.go b/pkg/neg/syncers/transaction.go index d2e2eab1c3..70b2635bc6 100644 --- a/pkg/neg/syncers/transaction.go +++ b/pkg/neg/syncers/transaction.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "net/http" + "strconv" "strings" "sync" "time" @@ -259,6 +260,10 @@ func (s *transactionSyncer) syncInternalImpl() error { } if s.inErrorState() { + targetMap, endpointPodMap, err = s.computeTargetMapDegradedMode(endpointSlices, s.NegSyncerKey.PortTuple.Name, s.NegSyncerKey.NegType) + if err != nil { + return err + } s.resetErrorState() } else { targetMap, endpointPodMap, err = s.computeTargetMap(endpointSlices) @@ -334,6 +339,44 @@ func (s *transactionSyncer) computeTargetMap(endpointSlices []*discovery.Endpoin return targetMap, endpointPodMap, err } +// computeTargetMapDegradedMode does endpoint calculation differently +// to fix the error in NEG controller and return the desire map +func (s *transactionSyncer) computeTargetMapDegradedMode(slices []*discovery.EndpointSlice, servicePortName string, endpointType negtypes.NetworkEndpointType) (map[string]negtypes.NetworkEndpointSet, negtypes.EndpointPodMap, error) { + var targetMap map[string]negtypes.NetworkEndpointSet + var endpointPodMap negtypes.EndpointPodMap + var err error + + for _, slice := range slices { + // if it is a custom endpoint slice, don't include it + // so it won't block the neg api call from succeeding + if val, ok := slice.ObjectMeta.Labels[discovery.LabelManagedBy]; !ok || val != "endpointslice-controller.k8s.io" { + continue + } + matchPort := "" + for _, port := range slice.Ports { + if *port.Name == servicePortName { + matchPort = strconv.Itoa(int(*port.Port)) + break + } + } + if len(matchPort) == 0 { + continue + } + + for _, ep := range slice.Endpoints { + if ep.TargetRef == nil { + continue + } + s.validateAndAddEndpoints(ep, matchPort, endpointType, targetMap, endpointPodMap) + } + } + return targetMap, endpointPodMap, err +} + +// validateAndAddEndpoints fills in missing information and creates network endpoint for each endpoint addresss +func (s *transactionSyncer) validateAndAddEndpoints(ep discovery.Endpoint, matchPort string, endpointType negtypes.NetworkEndpointType, targetMap map[string]negtypes.NetworkEndpointSet, endpointPodMap negtypes.EndpointPodMap) { +} + // syncLock must already be acquired before execution func (s *transactionSyncer) inErrorState() bool { return s.errorState != ""