-
Notifications
You must be signed in to change notification settings - Fork 40.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125858 from tnqn/automated-cherry-pick-of-#125675…
…-upstream-release-1.29 Automated cherry pick of #125675: Fix endpoints status out-of-sync when the pod state changes
- Loading branch information
Showing
5 changed files
with
375 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright 2024 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 endpoint | ||
|
||
import ( | ||
"sync" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
// staleEndpointsTracker tracks Endpoints and their stale resource versions to | ||
// help determine if an Endpoints is stale. | ||
type staleEndpointsTracker struct { | ||
// lock protects staleResourceVersionByEndpoints. | ||
lock sync.RWMutex | ||
// staleResourceVersionByEndpoints tracks the stale resource version of Endpoints. | ||
staleResourceVersionByEndpoints map[types.NamespacedName]string | ||
} | ||
|
||
func newStaleEndpointsTracker() *staleEndpointsTracker { | ||
return &staleEndpointsTracker{ | ||
staleResourceVersionByEndpoints: map[types.NamespacedName]string{}, | ||
} | ||
} | ||
|
||
func (t *staleEndpointsTracker) Stale(endpoints *v1.Endpoints) { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
nn := types.NamespacedName{Name: endpoints.Name, Namespace: endpoints.Namespace} | ||
t.staleResourceVersionByEndpoints[nn] = endpoints.ResourceVersion | ||
} | ||
|
||
func (t *staleEndpointsTracker) IsStale(endpoints *v1.Endpoints) bool { | ||
t.lock.RLock() | ||
defer t.lock.RUnlock() | ||
nn := types.NamespacedName{Name: endpoints.Name, Namespace: endpoints.Namespace} | ||
staleResourceVersion, exists := t.staleResourceVersionByEndpoints[nn] | ||
if exists && staleResourceVersion == endpoints.ResourceVersion { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (t *staleEndpointsTracker) Delete(namespace, name string) { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
nn := types.NamespacedName{Namespace: namespace, Name: name} | ||
delete(t.staleResourceVersionByEndpoints, nn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2024 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 endpoint | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestStaleEndpointsTracker(t *testing.T) { | ||
ns := metav1.NamespaceDefault | ||
tracker := newStaleEndpointsTracker() | ||
|
||
endpoints := &v1.Endpoints{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: ns, | ||
ResourceVersion: "1", | ||
}, | ||
Subsets: []v1.EndpointSubset{{ | ||
Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}}, | ||
Ports: []v1.EndpointPort{{Port: 1000}}, | ||
}}, | ||
} | ||
|
||
assert.False(t, tracker.IsStale(endpoints), "IsStale should return false before the endpoint is staled") | ||
|
||
tracker.Stale(endpoints) | ||
assert.True(t, tracker.IsStale(endpoints), "IsStale should return true after the endpoint is staled") | ||
|
||
endpoints.ResourceVersion = "2" | ||
assert.False(t, tracker.IsStale(endpoints), "IsStale should return false after the endpoint is updated") | ||
|
||
tracker.Delete(endpoints.Namespace, endpoints.Name) | ||
assert.Empty(t, tracker.staleResourceVersionByEndpoints) | ||
} |
Oops, something went wrong.