Skip to content

Commit

Permalink
Create skeleton for degraded mode procedure
Browse files Browse the repository at this point in the history
Create skeleton for degraded mode procedure. It will be enabled with
enableDegradedMode flag, and they will be created in a different PR when
the implementation is done.
  • Loading branch information
sawsa307 committed Mar 7, 2023
1 parent 6778265 commit e92dc66
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
34 changes: 32 additions & 2 deletions pkg/neg/syncers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,35 @@ func toZoneNetworkEndpointMap(eds []negtypes.EndpointsData, zoneGetter negtypes.
return zoneNetworkEndpointMap, networkEndpointPodMap, dupCount, nil
}

func toZoneNetworkEndpointMapDegradedMode(eds []negtypes.EndpointsData, zoneGetter negtypes.ZoneGetter, podLister cache.Indexer, servicePortName string, networkEndpointType negtypes.NetworkEndpointType) (map[string]negtypes.NetworkEndpointSet, negtypes.EndpointPodMap, int, error) {
targetMap := map[string]negtypes.NetworkEndpointSet{}
endpointPodMap := negtypes.EndpointPodMap{}
var dupCount int
for _, ed := range eds {
matchPort := ""
for _, port := range ed.Ports {
if port.Name == servicePortName {
matchPort = strconv.Itoa(int(port.Port))
break
}
}
if len(matchPort) == 0 {
continue
}
for _, endpointAddress := range ed.Addresses {
if endpointAddress.TargetRef == nil {
continue
}
dupCount += ValidateAndAddEndpoints(endpointAddress, zoneGetter, podLister, matchPort, networkEndpointType, targetMap, endpointPodMap)
}
}
return targetMap, endpointPodMap, dupCount, nil
}

// validateAndAddEndpoints fills in missing information and creates network endpoint for each endpoint addresss
func ValidateAndAddEndpoints(ep negtypes.AddressData, zoneGetter negtypes.ZoneGetter, podLister cache.Indexer, matchPort string, endpointType negtypes.NetworkEndpointType, targetMap map[string]negtypes.NetworkEndpointSet, endpointPodMap negtypes.EndpointPodMap) {
func ValidateAndAddEndpoints(ep negtypes.AddressData, zoneGetter negtypes.ZoneGetter, podLister cache.Indexer, matchPort string, endpointType negtypes.NetworkEndpointType, targetMap map[string]negtypes.NetworkEndpointSet, endpointPodMap negtypes.EndpointPodMap) int {
var dupCount int
for _, address := range ep.Addresses {
fmt.Println(address)
key := fmt.Sprintf("%s/%s", ep.TargetRef.Namespace, ep.TargetRef.Name)
obj, exists, err := podLister.GetByKey(key)
if err != nil || !exists {
Expand Down Expand Up @@ -327,8 +352,13 @@ func ValidateAndAddEndpoints(ep negtypes.AddressData, zoneGetter negtypes.ZoneGe
continue
}
targetMap[zone].Insert(networkEndpoint)
// increment the count for duplicated endpoint
if _, contains := endpointPodMap[networkEndpoint]; contains {
dupCount += 1
}
endpointPodMap[networkEndpoint] = types.NamespacedName{Namespace: ep.TargetRef.Namespace, Name: ep.TargetRef.Name}
}
return dupCount
}

// validatePod checks if this pod is a valid pod resource
Expand Down
122 changes: 122 additions & 0 deletions pkg/neg/syncers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,128 @@ func TestNEGRecreate(t *testing.T) {
}
}

func TestToZoneNetworkEndpointMapDegradedMode(t *testing.T) {
t.Parallel()

fakeZoneGetter := negtypes.NewFakeZoneGetter()
podLister := negtypes.NewTestContext().PodInformer.GetIndexer()
addPodsToLister(podLister)

managedByController := "endpointslice-controller.k8s.io"
testNonExistPort := "non-exists"
testEmptyNamedPort := ""
testNamedPort := "named-Port"

testCases := []struct {
desc string
portName string
managedByLabel string
expectedEndpointMap map[string]negtypes.NetworkEndpointSet
expectedPodMap negtypes.EndpointPodMap
networkEndpointType negtypes.NetworkEndpointType
}{
{
desc: "non exist target port",
portName: testNonExistPort,
managedByLabel: managedByController,
expectedEndpointMap: map[string]negtypes.NetworkEndpointSet{},
expectedPodMap: negtypes.EndpointPodMap{},
networkEndpointType: negtypes.VmIpPortEndpointType,
},
{
desc: "empty named port",
portName: testEmptyNamedPort,
managedByLabel: managedByController,
expectedEndpointMap: map[string]negtypes.NetworkEndpointSet{
negtypes.TestZone1: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.1.1||instance1||80"),
networkEndpointFromEncodedEndpoint("10.100.1.2||instance1||80"),
networkEndpointFromEncodedEndpoint("10.100.2.1||instance2||80"),
networkEndpointFromEncodedEndpoint("10.100.1.3||instance1||80"),
networkEndpointFromEncodedEndpoint("10.100.1.4||instance1||80")),
negtypes.TestZone2: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.3.1||instance3||80")),
},
expectedPodMap: negtypes.EndpointPodMap{
networkEndpointFromEncodedEndpoint("10.100.1.1||instance1||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod1"},
networkEndpointFromEncodedEndpoint("10.100.1.2||instance1||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod2"},
networkEndpointFromEncodedEndpoint("10.100.2.1||instance2||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod3"},
networkEndpointFromEncodedEndpoint("10.100.3.1||instance3||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod4"},
networkEndpointFromEncodedEndpoint("10.100.1.3||instance1||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod5"},
networkEndpointFromEncodedEndpoint("10.100.1.4||instance1||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod6"},
},
networkEndpointType: negtypes.VmIpPortEndpointType,
},
{
desc: "named target port",
portName: testNamedPort,
managedByLabel: managedByController,
expectedEndpointMap: map[string]negtypes.NetworkEndpointSet{
negtypes.TestZone1: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.2.2||instance2||81")),
negtypes.TestZone2: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.4.1||instance4||81"),
networkEndpointFromEncodedEndpoint("10.100.3.2||instance3||8081"),
networkEndpointFromEncodedEndpoint("10.100.4.2||instance4||8081"),
networkEndpointFromEncodedEndpoint("10.100.4.3||instance4||81"),
networkEndpointFromEncodedEndpoint("10.100.4.4||instance4||8081")),
},
expectedPodMap: negtypes.EndpointPodMap{
networkEndpointFromEncodedEndpoint("10.100.2.2||instance2||81"): types.NamespacedName{Namespace: testNamespace, Name: "pod7"},
networkEndpointFromEncodedEndpoint("10.100.4.1||instance4||81"): types.NamespacedName{Namespace: testNamespace, Name: "pod8"},
networkEndpointFromEncodedEndpoint("10.100.4.3||instance4||81"): types.NamespacedName{Namespace: testNamespace, Name: "pod9"},
networkEndpointFromEncodedEndpoint("10.100.3.2||instance3||8081"): types.NamespacedName{Namespace: testNamespace, Name: "pod10"},
networkEndpointFromEncodedEndpoint("10.100.4.2||instance4||8081"): types.NamespacedName{Namespace: testNamespace, Name: "pod11"},
networkEndpointFromEncodedEndpoint("10.100.4.4||instance4||8081"): types.NamespacedName{Namespace: testNamespace, Name: "pod12"},
},
networkEndpointType: negtypes.VmIpPortEndpointType,
},
{
desc: "Non-GCP network endpoints",
portName: testEmptyNamedPort,
managedByLabel: managedByController,
expectedEndpointMap: map[string]negtypes.NetworkEndpointSet{
negtypes.TestZone1: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.1.1||||80"),
networkEndpointFromEncodedEndpoint("10.100.1.2||||80"),
networkEndpointFromEncodedEndpoint("10.100.2.1||||80"),
networkEndpointFromEncodedEndpoint("10.100.1.3||||80"),
networkEndpointFromEncodedEndpoint("10.100.1.4||||80")),
negtypes.TestZone2: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.3.1||||80")),
},
expectedPodMap: negtypes.EndpointPodMap{
networkEndpointFromEncodedEndpoint("10.100.1.1||||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod1"},
networkEndpointFromEncodedEndpoint("10.100.1.2||||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod2"},
networkEndpointFromEncodedEndpoint("10.100.2.1||||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod3"},
networkEndpointFromEncodedEndpoint("10.100.3.1||||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod4"},
networkEndpointFromEncodedEndpoint("10.100.1.3||||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod5"},
networkEndpointFromEncodedEndpoint("10.100.1.4||||80"): types.NamespacedName{Namespace: testNamespace, Name: "pod6"},
},
networkEndpointType: negtypes.NonGCPPrivateEndpointType,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
testEndpointSlices := getTestEndpointSlices(testService, testNamespace)

testEndpointSlices[2].ObjectMeta.Labels[discovery.LabelManagedBy] = tc.managedByLabel
// check if custom endpoint slice is filtered out
targetMap, endpointPodMap, _, err := toZoneNetworkEndpointMapDegradedMode(negtypes.EndpointsDataFromEndpointSlices(testEndpointSlices), fakeZoneGetter, podLister, tc.portName, tc.networkEndpointType)
if err != nil {
t.Errorf("expected error=nil, but got %v", err)
}

if !reflect.DeepEqual(targetMap, tc.expectedEndpointMap) {
t.Errorf("degraded mode endpoint set is not calculated correctly:\ngot %+v,\n expected %+v", targetMap, tc.expectedEndpointMap)
}
if !reflect.DeepEqual(endpointPodMap, tc.expectedPodMap) {
t.Errorf("degraded mode endpoint map is not calculated correctly:\ngot %+v,\n expected %+v", endpointPodMap, tc.expectedPodMap)
}
})
}
}

func TestValidateAndAddEndpoints(t *testing.T) {
t.Parallel()
matchPort := strconv.Itoa(int(80))
Expand Down

0 comments on commit e92dc66

Please sign in to comment.