mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-21 09:57:52 +00:00
extract same code of es and esm to pkg
migrate files: endpointset.go endpointslice_tracker.go endpointslice_tracker_test.go errors.go
This commit is contained in:
13
pkg/controller/util/endpointslice/OWNERS
Normal file
13
pkg/controller/util/endpointslice/OWNERS
Normal file
@@ -0,0 +1,13 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
approvers:
|
||||
- robscott
|
||||
- freehan
|
||||
- sig-network-approvers
|
||||
reviewers:
|
||||
- robscott
|
||||
- freehan
|
||||
- sig-network-reviewers
|
||||
labels:
|
||||
- sig/network
|
||||
|
96
pkg/controller/util/endpointslice/endpointset.go
Normal file
96
pkg/controller/util/endpointslice/endpointset.go
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package endpointslice
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
discovery "k8s.io/api/discovery/v1"
|
||||
endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint"
|
||||
)
|
||||
|
||||
// endpointHash is used to uniquely identify endpoints. Only including addresses
|
||||
// and hostnames as unique identifiers allows us to do more in place updates
|
||||
// should attributes such as topology, conditions, or targetRef change.
|
||||
type endpointHash string
|
||||
type endpointHashObj struct {
|
||||
Addresses []string
|
||||
Hostname string
|
||||
}
|
||||
|
||||
func hashEndpoint(endpoint *discovery.Endpoint) endpointHash {
|
||||
sort.Strings(endpoint.Addresses)
|
||||
hashObj := endpointHashObj{Addresses: endpoint.Addresses}
|
||||
if endpoint.Hostname != nil {
|
||||
hashObj.Hostname = *endpoint.Hostname
|
||||
}
|
||||
|
||||
return endpointHash(endpointutil.DeepHashObjectToString(hashObj))
|
||||
}
|
||||
|
||||
// EndpointSet provides simple methods for comparing sets of Endpoints.
|
||||
type EndpointSet map[endpointHash]*discovery.Endpoint
|
||||
|
||||
// Insert adds items to the set.
|
||||
func (s EndpointSet) Insert(items ...*discovery.Endpoint) EndpointSet {
|
||||
for _, item := range items {
|
||||
s[hashEndpoint(item)] = item
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Delete removes all items from the set.
|
||||
func (s EndpointSet) Delete(items ...*discovery.Endpoint) EndpointSet {
|
||||
for _, item := range items {
|
||||
delete(s, hashEndpoint(item))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Has returns true if and only if item is contained in the set.
|
||||
func (s EndpointSet) Has(item *discovery.Endpoint) bool {
|
||||
_, contained := s[hashEndpoint(item)]
|
||||
return contained
|
||||
}
|
||||
|
||||
// Returns an endpoint matching the hash if contained in the set.
|
||||
func (s EndpointSet) Get(item *discovery.Endpoint) *discovery.Endpoint {
|
||||
return s[hashEndpoint(item)]
|
||||
}
|
||||
|
||||
// UnsortedList returns the slice with contents in random order.
|
||||
func (s EndpointSet) UnsortedList() []*discovery.Endpoint {
|
||||
endpoints := make([]*discovery.Endpoint, 0, len(s))
|
||||
for _, endpoint := range s {
|
||||
endpoints = append(endpoints, endpoint)
|
||||
}
|
||||
return endpoints
|
||||
}
|
||||
|
||||
// Returns a single element from the set.
|
||||
func (s EndpointSet) PopAny() (*discovery.Endpoint, bool) {
|
||||
for _, endpoint := range s {
|
||||
s.Delete(endpoint)
|
||||
return endpoint, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Len returns the size of the set.
|
||||
func (s EndpointSet) Len() int {
|
||||
return len(s)
|
||||
}
|
191
pkg/controller/util/endpointslice/endpointslice_tracker.go
Normal file
191
pkg/controller/util/endpointslice/endpointslice_tracker.go
Normal file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package endpointslice
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
discovery "k8s.io/api/discovery/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
const (
|
||||
deletionExpected = -1
|
||||
)
|
||||
|
||||
// GenerationsBySlice tracks expected EndpointSlice generations by EndpointSlice
|
||||
// uid. A value of deletionExpected (-1) may be used here to indicate that we
|
||||
// expect this EndpointSlice to be deleted.
|
||||
type GenerationsBySlice map[types.UID]int64
|
||||
|
||||
// EndpointSliceTracker tracks EndpointSlices and their associated generation to
|
||||
// help determine if a change to an EndpointSlice has been processed by the
|
||||
// EndpointSlice controller.
|
||||
type EndpointSliceTracker struct {
|
||||
// lock protects generationsByService.
|
||||
lock sync.Mutex
|
||||
// generationsByService tracks the generations of EndpointSlices for each
|
||||
// Service.
|
||||
generationsByService map[types.NamespacedName]GenerationsBySlice
|
||||
}
|
||||
|
||||
// NewEndpointSliceTracker creates and initializes a new endpointSliceTracker.
|
||||
func NewEndpointSliceTracker() *EndpointSliceTracker {
|
||||
return &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{},
|
||||
}
|
||||
}
|
||||
|
||||
// Has returns true if the endpointSliceTracker has a generation for the
|
||||
// provided EndpointSlice.
|
||||
func (est *EndpointSliceTracker) Has(endpointSlice *discovery.EndpointSlice) bool {
|
||||
est.lock.Lock()
|
||||
defer est.lock.Unlock()
|
||||
|
||||
gfs, ok := est.GenerationsForSliceUnsafe(endpointSlice)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
_, ok = gfs[endpointSlice.UID]
|
||||
return ok
|
||||
}
|
||||
|
||||
// ShouldSync returns true if this endpointSliceTracker does not have a
|
||||
// generation for the provided EndpointSlice or it is greater than the
|
||||
// generation of the tracked EndpointSlice.
|
||||
func (est *EndpointSliceTracker) ShouldSync(endpointSlice *discovery.EndpointSlice) bool {
|
||||
est.lock.Lock()
|
||||
defer est.lock.Unlock()
|
||||
|
||||
gfs, ok := est.GenerationsForSliceUnsafe(endpointSlice)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
g, ok := gfs[endpointSlice.UID]
|
||||
return !ok || endpointSlice.Generation > g
|
||||
}
|
||||
|
||||
// StaleSlices returns true if any of the following are true:
|
||||
// 1. One or more of the provided EndpointSlices have older generations than the
|
||||
// corresponding tracked ones.
|
||||
// 2. The tracker is expecting one or more of the provided EndpointSlices to be
|
||||
// deleted.
|
||||
// 3. The tracker is tracking EndpointSlices that have not been provided.
|
||||
func (est *EndpointSliceTracker) StaleSlices(service *v1.Service, endpointSlices []*discovery.EndpointSlice) bool {
|
||||
est.lock.Lock()
|
||||
defer est.lock.Unlock()
|
||||
|
||||
nn := types.NamespacedName{Name: service.Name, Namespace: service.Namespace}
|
||||
gfs, ok := est.generationsByService[nn]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
providedSlices := map[types.UID]int64{}
|
||||
for _, endpointSlice := range endpointSlices {
|
||||
providedSlices[endpointSlice.UID] = endpointSlice.Generation
|
||||
g, ok := gfs[endpointSlice.UID]
|
||||
if ok && (g == deletionExpected || g > endpointSlice.Generation) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for uid, generation := range gfs {
|
||||
if generation == deletionExpected {
|
||||
continue
|
||||
}
|
||||
_, ok := providedSlices[uid]
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Update adds or updates the generation in this endpointSliceTracker for the
|
||||
// provided EndpointSlice.
|
||||
func (est *EndpointSliceTracker) Update(endpointSlice *discovery.EndpointSlice) {
|
||||
est.lock.Lock()
|
||||
defer est.lock.Unlock()
|
||||
|
||||
gfs, ok := est.GenerationsForSliceUnsafe(endpointSlice)
|
||||
|
||||
if !ok {
|
||||
gfs = GenerationsBySlice{}
|
||||
est.generationsByService[getServiceNN(endpointSlice)] = gfs
|
||||
}
|
||||
gfs[endpointSlice.UID] = endpointSlice.Generation
|
||||
}
|
||||
|
||||
// DeleteService removes the set of generations tracked for the Service.
|
||||
func (est *EndpointSliceTracker) DeleteService(namespace, name string) {
|
||||
est.lock.Lock()
|
||||
defer est.lock.Unlock()
|
||||
|
||||
serviceNN := types.NamespacedName{Name: name, Namespace: namespace}
|
||||
delete(est.generationsByService, serviceNN)
|
||||
}
|
||||
|
||||
// ExpectDeletion sets the generation to deletionExpected in this
|
||||
// endpointSliceTracker for the provided EndpointSlice.
|
||||
func (est *EndpointSliceTracker) ExpectDeletion(endpointSlice *discovery.EndpointSlice) {
|
||||
est.lock.Lock()
|
||||
defer est.lock.Unlock()
|
||||
|
||||
gfs, ok := est.GenerationsForSliceUnsafe(endpointSlice)
|
||||
|
||||
if !ok {
|
||||
gfs = GenerationsBySlice{}
|
||||
est.generationsByService[getServiceNN(endpointSlice)] = gfs
|
||||
}
|
||||
gfs[endpointSlice.UID] = deletionExpected
|
||||
}
|
||||
|
||||
// HandleDeletion removes the generation in this endpointSliceTracker for the
|
||||
// provided EndpointSlice. This returns true if the tracker expected this
|
||||
// EndpointSlice to be deleted and false if not.
|
||||
func (est *EndpointSliceTracker) HandleDeletion(endpointSlice *discovery.EndpointSlice) bool {
|
||||
est.lock.Lock()
|
||||
defer est.lock.Unlock()
|
||||
|
||||
gfs, ok := est.GenerationsForSliceUnsafe(endpointSlice)
|
||||
|
||||
if ok {
|
||||
g, ok := gfs[endpointSlice.UID]
|
||||
delete(gfs, endpointSlice.UID)
|
||||
if ok && g != deletionExpected {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// GenerationsForSliceUnsafe returns the generations for the Service
|
||||
// corresponding to the provided EndpointSlice, and a bool to indicate if it
|
||||
// exists. A lock must be applied before calling this function.
|
||||
func (est *EndpointSliceTracker) GenerationsForSliceUnsafe(endpointSlice *discovery.EndpointSlice) (GenerationsBySlice, bool) {
|
||||
serviceNN := getServiceNN(endpointSlice)
|
||||
generations, ok := est.generationsByService[serviceNN]
|
||||
return generations, ok
|
||||
}
|
||||
|
||||
// getServiceNN returns a namespaced name for the Service corresponding to the
|
||||
// provided EndpointSlice.
|
||||
func getServiceNN(endpointSlice *discovery.EndpointSlice) types.NamespacedName {
|
||||
serviceName, _ := endpointSlice.Labels[discovery.LabelServiceName]
|
||||
return types.NamespacedName{Name: serviceName, Namespace: endpointSlice.Namespace}
|
||||
}
|
401
pkg/controller/util/endpointslice/endpointslice_tracker_test.go
Normal file
401
pkg/controller/util/endpointslice/endpointslice_tracker_test.go
Normal file
@@ -0,0 +1,401 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package endpointslice
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
discovery "k8s.io/api/discovery/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
func TestEndpointSliceTrackerUpdate(t *testing.T) {
|
||||
epSlice1 := &discovery.EndpointSlice{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "example-1",
|
||||
Namespace: "ns1",
|
||||
UID: "original",
|
||||
Generation: 1,
|
||||
Labels: map[string]string{discovery.LabelServiceName: "svc1"},
|
||||
},
|
||||
}
|
||||
|
||||
epSlice1DifferentNS := epSlice1.DeepCopy()
|
||||
epSlice1DifferentNS.Namespace = "ns2"
|
||||
epSlice1DifferentNS.UID = "diff-ns"
|
||||
|
||||
epSlice1DifferentService := epSlice1.DeepCopy()
|
||||
epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2"
|
||||
epSlice1DifferentService.UID = "diff-svc"
|
||||
|
||||
epSlice1NewerGen := epSlice1.DeepCopy()
|
||||
epSlice1NewerGen.Generation = 2
|
||||
|
||||
testCases := map[string]struct {
|
||||
updateParam *discovery.EndpointSlice
|
||||
checksParam *discovery.EndpointSlice
|
||||
expectHas bool
|
||||
expectShouldSync bool
|
||||
expectGeneration int64
|
||||
}{
|
||||
"same slice": {
|
||||
updateParam: epSlice1,
|
||||
checksParam: epSlice1,
|
||||
expectHas: true,
|
||||
expectShouldSync: false,
|
||||
expectGeneration: epSlice1.Generation,
|
||||
},
|
||||
"different namespace": {
|
||||
updateParam: epSlice1,
|
||||
checksParam: epSlice1DifferentNS,
|
||||
expectHas: false,
|
||||
expectShouldSync: true,
|
||||
expectGeneration: epSlice1.Generation,
|
||||
},
|
||||
"different service": {
|
||||
updateParam: epSlice1,
|
||||
checksParam: epSlice1DifferentService,
|
||||
expectHas: false,
|
||||
expectShouldSync: true,
|
||||
expectGeneration: epSlice1.Generation,
|
||||
},
|
||||
"newer generation": {
|
||||
updateParam: epSlice1,
|
||||
checksParam: epSlice1NewerGen,
|
||||
expectHas: true,
|
||||
expectShouldSync: true,
|
||||
expectGeneration: epSlice1.Generation,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
esTracker := NewEndpointSliceTracker()
|
||||
esTracker.Update(tc.updateParam)
|
||||
if esTracker.Has(tc.checksParam) != tc.expectHas {
|
||||
t.Errorf("tc.tracker.Has(%+v) == %t, expected %t", tc.checksParam, esTracker.Has(tc.checksParam), tc.expectHas)
|
||||
}
|
||||
if esTracker.ShouldSync(tc.checksParam) != tc.expectShouldSync {
|
||||
t.Errorf("tc.tracker.ShouldSync(%+v) == %t, expected %t", tc.checksParam, esTracker.ShouldSync(tc.checksParam), tc.expectShouldSync)
|
||||
}
|
||||
serviceNN := types.NamespacedName{Namespace: epSlice1.Namespace, Name: "svc1"}
|
||||
gfs, ok := esTracker.generationsByService[serviceNN]
|
||||
if !ok {
|
||||
t.Fatalf("expected tracker to have generations for %s Service", serviceNN.Name)
|
||||
}
|
||||
generation, ok := gfs[epSlice1.UID]
|
||||
if !ok {
|
||||
t.Fatalf("expected tracker to have generation for %s EndpointSlice", epSlice1.Name)
|
||||
}
|
||||
if tc.expectGeneration != generation {
|
||||
t.Fatalf("expected generation to be %d, got %d", tc.expectGeneration, generation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpointSliceTrackerStaleSlices(t *testing.T) {
|
||||
epSlice1 := &discovery.EndpointSlice{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "example-1",
|
||||
Namespace: "ns1",
|
||||
UID: "original",
|
||||
Generation: 1,
|
||||
Labels: map[string]string{discovery.LabelServiceName: "svc1"},
|
||||
},
|
||||
}
|
||||
|
||||
epSlice1NewerGen := epSlice1.DeepCopy()
|
||||
epSlice1NewerGen.Generation = 2
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
tracker *EndpointSliceTracker
|
||||
serviceParam *v1.Service
|
||||
slicesParam []*discovery.EndpointSlice
|
||||
expectNewer bool
|
||||
}{{
|
||||
name: "empty tracker",
|
||||
tracker: &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{},
|
||||
},
|
||||
serviceParam: &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}},
|
||||
slicesParam: []*discovery.EndpointSlice{},
|
||||
expectNewer: false,
|
||||
}, {
|
||||
name: "empty slices",
|
||||
tracker: &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{
|
||||
{Name: "svc1", Namespace: "ns1"}: {},
|
||||
},
|
||||
},
|
||||
serviceParam: &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}},
|
||||
slicesParam: []*discovery.EndpointSlice{},
|
||||
expectNewer: false,
|
||||
}, {
|
||||
name: "matching slices",
|
||||
tracker: &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{
|
||||
{Name: "svc1", Namespace: "ns1"}: {
|
||||
epSlice1.UID: epSlice1.Generation,
|
||||
},
|
||||
},
|
||||
},
|
||||
serviceParam: &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}},
|
||||
slicesParam: []*discovery.EndpointSlice{epSlice1},
|
||||
expectNewer: false,
|
||||
}, {
|
||||
name: "newer slice in tracker",
|
||||
tracker: &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{
|
||||
{Name: "svc1", Namespace: "ns1"}: {
|
||||
epSlice1.UID: epSlice1NewerGen.Generation,
|
||||
},
|
||||
},
|
||||
},
|
||||
serviceParam: &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}},
|
||||
slicesParam: []*discovery.EndpointSlice{epSlice1},
|
||||
expectNewer: true,
|
||||
}, {
|
||||
name: "newer slice in params",
|
||||
tracker: &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{
|
||||
{Name: "svc1", Namespace: "ns1"}: {
|
||||
epSlice1.UID: epSlice1.Generation,
|
||||
},
|
||||
},
|
||||
},
|
||||
serviceParam: &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}},
|
||||
slicesParam: []*discovery.EndpointSlice{epSlice1NewerGen},
|
||||
expectNewer: false,
|
||||
}, {
|
||||
name: "slice in params is expected to be deleted",
|
||||
tracker: &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{
|
||||
{Name: "svc1", Namespace: "ns1"}: {
|
||||
epSlice1.UID: deletionExpected,
|
||||
},
|
||||
},
|
||||
},
|
||||
serviceParam: &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}},
|
||||
slicesParam: []*discovery.EndpointSlice{epSlice1},
|
||||
expectNewer: true,
|
||||
}, {
|
||||
name: "slice in tracker but not in params",
|
||||
tracker: &EndpointSliceTracker{
|
||||
generationsByService: map[types.NamespacedName]GenerationsBySlice{
|
||||
{Name: "svc1", Namespace: "ns1"}: {
|
||||
epSlice1.UID: epSlice1.Generation,
|
||||
},
|
||||
},
|
||||
},
|
||||
serviceParam: &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}},
|
||||
slicesParam: []*discovery.EndpointSlice{},
|
||||
expectNewer: true,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actualNewer := tc.tracker.StaleSlices(tc.serviceParam, tc.slicesParam)
|
||||
if actualNewer != tc.expectNewer {
|
||||
t.Errorf("Expected %t, got %t", tc.expectNewer, actualNewer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestEndpointSliceTrackerDeletion(t *testing.T) {
|
||||
epSlice1 := &discovery.EndpointSlice{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "example-1",
|
||||
Namespace: "ns1",
|
||||
UID: "original",
|
||||
Generation: 1,
|
||||
Labels: map[string]string{discovery.LabelServiceName: "svc1"},
|
||||
},
|
||||
}
|
||||
|
||||
epSlice1DifferentNS := epSlice1.DeepCopy()
|
||||
epSlice1DifferentNS.Namespace = "ns2"
|
||||
epSlice1DifferentNS.UID = "diff-ns"
|
||||
|
||||
epSlice1DifferentService := epSlice1.DeepCopy()
|
||||
epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2"
|
||||
epSlice1DifferentService.UID = "diff-svc"
|
||||
|
||||
epSlice1NewerGen := epSlice1.DeepCopy()
|
||||
epSlice1NewerGen.Generation = 2
|
||||
|
||||
testCases := map[string]struct {
|
||||
expectDeletionParam *discovery.EndpointSlice
|
||||
checksParam *discovery.EndpointSlice
|
||||
deleteParam *discovery.EndpointSlice
|
||||
expectHas bool
|
||||
expectShouldSync bool
|
||||
expectedHandleDeletionResp bool
|
||||
}{
|
||||
"same slice": {
|
||||
expectDeletionParam: epSlice1,
|
||||
checksParam: epSlice1,
|
||||
deleteParam: epSlice1,
|
||||
expectHas: true,
|
||||
expectShouldSync: true,
|
||||
expectedHandleDeletionResp: true,
|
||||
},
|
||||
"different namespace": {
|
||||
expectDeletionParam: epSlice1DifferentNS,
|
||||
checksParam: epSlice1DifferentNS,
|
||||
deleteParam: epSlice1DifferentNS,
|
||||
expectHas: true,
|
||||
expectShouldSync: true,
|
||||
expectedHandleDeletionResp: false,
|
||||
},
|
||||
"different namespace, check original ep slice": {
|
||||
expectDeletionParam: epSlice1DifferentNS,
|
||||
checksParam: epSlice1,
|
||||
deleteParam: epSlice1DifferentNS,
|
||||
expectHas: true,
|
||||
expectShouldSync: false,
|
||||
expectedHandleDeletionResp: false,
|
||||
},
|
||||
"different service": {
|
||||
expectDeletionParam: epSlice1DifferentService,
|
||||
checksParam: epSlice1DifferentService,
|
||||
deleteParam: epSlice1DifferentService,
|
||||
expectHas: true,
|
||||
expectShouldSync: true,
|
||||
expectedHandleDeletionResp: false,
|
||||
},
|
||||
"expectDelete different service, check original ep slice, delete original": {
|
||||
expectDeletionParam: epSlice1DifferentService,
|
||||
checksParam: epSlice1,
|
||||
deleteParam: epSlice1,
|
||||
expectHas: true,
|
||||
expectShouldSync: false,
|
||||
expectedHandleDeletionResp: false,
|
||||
},
|
||||
"different generation": {
|
||||
expectDeletionParam: epSlice1NewerGen,
|
||||
checksParam: epSlice1NewerGen,
|
||||
deleteParam: epSlice1NewerGen,
|
||||
expectHas: true,
|
||||
expectShouldSync: true,
|
||||
expectedHandleDeletionResp: true,
|
||||
},
|
||||
"expectDelete different generation, check original ep slice, delete original": {
|
||||
expectDeletionParam: epSlice1NewerGen,
|
||||
checksParam: epSlice1,
|
||||
deleteParam: epSlice1,
|
||||
expectHas: true,
|
||||
expectShouldSync: true,
|
||||
expectedHandleDeletionResp: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
esTracker := NewEndpointSliceTracker()
|
||||
esTracker.Update(epSlice1)
|
||||
|
||||
esTracker.ExpectDeletion(tc.expectDeletionParam)
|
||||
if esTracker.Has(tc.checksParam) != tc.expectHas {
|
||||
t.Errorf("esTracker.Has(%+v) == %t, expected %t", tc.checksParam, esTracker.Has(tc.checksParam), tc.expectHas)
|
||||
}
|
||||
if esTracker.ShouldSync(tc.checksParam) != tc.expectShouldSync {
|
||||
t.Errorf("esTracker.ShouldSync(%+v) == %t, expected %t", tc.checksParam, esTracker.ShouldSync(tc.checksParam), tc.expectShouldSync)
|
||||
}
|
||||
if esTracker.HandleDeletion(epSlice1) != tc.expectedHandleDeletionResp {
|
||||
t.Errorf("esTracker.ShouldSync(%+v) == %t, expected %t", epSlice1, esTracker.HandleDeletion(epSlice1), tc.expectedHandleDeletionResp)
|
||||
}
|
||||
if esTracker.Has(epSlice1) != false {
|
||||
t.Errorf("esTracker.Has(%+v) == %t, expected false", epSlice1, esTracker.Has(epSlice1))
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpointSliceTrackerDeleteService(t *testing.T) {
|
||||
svcName1, svcNS1 := "svc1", "ns1"
|
||||
svcName2, svcNS2 := "svc2", "ns2"
|
||||
epSlice1 := &discovery.EndpointSlice{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "example-1",
|
||||
Namespace: svcNS1,
|
||||
Generation: 1,
|
||||
Labels: map[string]string{discovery.LabelServiceName: svcName1},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := map[string]struct {
|
||||
updateParam *discovery.EndpointSlice
|
||||
deleteServiceParam *types.NamespacedName
|
||||
expectHas bool
|
||||
expectShouldSync bool
|
||||
expectGeneration int64
|
||||
}{
|
||||
"same service": {
|
||||
updateParam: epSlice1,
|
||||
deleteServiceParam: &types.NamespacedName{Namespace: svcNS1, Name: svcName1},
|
||||
expectHas: false,
|
||||
expectShouldSync: true,
|
||||
},
|
||||
"different namespace": {
|
||||
updateParam: epSlice1,
|
||||
deleteServiceParam: &types.NamespacedName{Namespace: svcNS2, Name: svcName1},
|
||||
expectHas: true,
|
||||
expectShouldSync: false,
|
||||
expectGeneration: epSlice1.Generation,
|
||||
},
|
||||
"different service": {
|
||||
updateParam: epSlice1,
|
||||
deleteServiceParam: &types.NamespacedName{Namespace: svcNS1, Name: svcName2},
|
||||
expectHas: true,
|
||||
expectShouldSync: false,
|
||||
expectGeneration: epSlice1.Generation,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
esTracker := NewEndpointSliceTracker()
|
||||
esTracker.Update(tc.updateParam)
|
||||
esTracker.DeleteService(tc.deleteServiceParam.Namespace, tc.deleteServiceParam.Name)
|
||||
if esTracker.Has(tc.updateParam) != tc.expectHas {
|
||||
t.Errorf("tc.tracker.Has(%+v) == %t, expected %t", tc.updateParam, esTracker.Has(tc.updateParam), tc.expectHas)
|
||||
}
|
||||
if esTracker.ShouldSync(tc.updateParam) != tc.expectShouldSync {
|
||||
t.Errorf("tc.tracker.ShouldSync(%+v) == %t, expected %t", tc.updateParam, esTracker.ShouldSync(tc.updateParam), tc.expectShouldSync)
|
||||
}
|
||||
if tc.expectGeneration != 0 {
|
||||
serviceNN := types.NamespacedName{Namespace: epSlice1.Namespace, Name: "svc1"}
|
||||
gfs, ok := esTracker.generationsByService[serviceNN]
|
||||
if !ok {
|
||||
t.Fatalf("expected tracker to have status for %s Service", serviceNN.Name)
|
||||
}
|
||||
generation, ok := gfs[epSlice1.UID]
|
||||
if !ok {
|
||||
t.Fatalf("expected tracker to have generation for %s EndpointSlice", epSlice1.Name)
|
||||
}
|
||||
if tc.expectGeneration != generation {
|
||||
t.Fatalf("expected generation to be %d, got %d", tc.expectGeneration, generation)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
35
pkg/controller/util/endpointslice/errors.go
Normal file
35
pkg/controller/util/endpointslice/errors.go
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright 2021 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package endpointslice
|
||||
|
||||
// StaleInformerCache errors indicate that the informer cache includes out of
|
||||
// date resources.
|
||||
type StaleInformerCache struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
// NewStaleInformerCache return StaleInformerCache with error mes
|
||||
func NewStaleInformerCache(msg string) *StaleInformerCache {
|
||||
return &StaleInformerCache{msg}
|
||||
}
|
||||
|
||||
func (e *StaleInformerCache) Error() string { return e.msg }
|
||||
|
||||
func IsStaleInformerCacheErr(err error) bool {
|
||||
_, ok := err.(*StaleInformerCache)
|
||||
return ok
|
||||
}
|
Reference in New Issue
Block a user