generated from kubernetes/kubernetes-template-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not cleanup excess pods when awaiting to observe previous clean ups (
#1623) * Do not cleanup excess pods when awaiting to observe previous clean ups Change-Id: Icc748dcfb47591144da91e6ffad59676f5df9ad2 * Review Change-Id: Ie63c4d709fdabeb84d7463dbf66696f9a8f387b0 * More resilient UID observation on Pod delete Change-Id: Ie418ea7788d626ff8bd3a2adcc2c25086ea38fe8
- Loading branch information
1 parent
46c07f0
commit 44adc22
Showing
5 changed files
with
443 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
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 pod | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
type uids = sets.Set[types.UID] | ||
|
||
// expectationsStore contains UIDs for which we are waiting to observe some change through event handlers. | ||
type expectationsStore struct { | ||
sync.Mutex | ||
name string | ||
|
||
store map[types.NamespacedName]uids | ||
} | ||
|
||
func newUIDExpectations(name string) *expectationsStore { | ||
return &expectationsStore{ | ||
name: name, | ||
store: make(map[types.NamespacedName]uids), | ||
} | ||
} | ||
|
||
func (e *expectationsStore) ExpectUIDs(log logr.Logger, key types.NamespacedName, UIDs []types.UID) { | ||
log.V(3).Info("Expecting UIDs", "store", e.name, "key", key, "uids", UIDs) | ||
expectedUIDs := sets.New[types.UID](UIDs...) | ||
e.Lock() | ||
defer e.Unlock() | ||
|
||
stored, found := e.store[key] | ||
if !found { | ||
e.store[key] = expectedUIDs | ||
} else { | ||
e.store[key] = stored.Union(expectedUIDs) | ||
} | ||
} | ||
|
||
func (e *expectationsStore) ObservedUID(log logr.Logger, key types.NamespacedName, uid types.UID) { | ||
log.V(3).Info("Observed UID", "store", e.name, "key", key, "uid", uid) | ||
e.Lock() | ||
defer e.Unlock() | ||
|
||
stored, found := e.store[key] | ||
if !found { | ||
return | ||
} | ||
stored.Delete(uid) | ||
|
||
// clean up key if empty. | ||
if stored.Len() == 0 { | ||
delete(e.store, key) | ||
} | ||
} | ||
|
||
func (e *expectationsStore) Satisfied(log logr.Logger, key types.NamespacedName) bool { | ||
e.Lock() | ||
_, found := e.store[key] | ||
e.Unlock() | ||
|
||
if logV := log.V(4); logV.Enabled() { | ||
log.V(4).Info("Retrieved satisfied expectations", "store", e.name, "key", key, "satisfied", !found) | ||
} | ||
return !found | ||
} |
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,96 @@ | ||
/* | ||
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 pod | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"sigs.k8s.io/kueue/pkg/util/parallelize" | ||
utiltesting "sigs.k8s.io/kueue/pkg/util/testing" | ||
) | ||
|
||
type keyUIDs struct { | ||
key types.NamespacedName | ||
uids []types.UID | ||
} | ||
|
||
func TestExpectations(t *testing.T) { | ||
ctx, log := utiltesting.ContextWithLog(t) | ||
initial := []keyUIDs{ | ||
{ | ||
key: types.NamespacedName{Name: "g1"}, | ||
uids: []types.UID{"a", "b", "c"}, | ||
}, | ||
{ | ||
key: types.NamespacedName{Name: "g2"}, | ||
uids: []types.UID{"x", "y", "z"}, | ||
}, | ||
{ | ||
|
||
key: types.NamespacedName{Name: "g3"}, | ||
uids: []types.UID{"a", "b", "c", "x", "y", "z"}, | ||
}, | ||
} | ||
expectations := newUIDExpectations("test") | ||
err := parallelize.Until(ctx, len(initial), func(i int) error { | ||
e := initial[i] | ||
expectations.ExpectUIDs(log, e.key, e.uids) | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Fatalf("Inserting initial UIDs: %v", err) | ||
} | ||
|
||
observe := []keyUIDs{ | ||
{ | ||
key: types.NamespacedName{Name: "g1"}, | ||
uids: []types.UID{"a", "b", "c"}, | ||
}, | ||
{ | ||
key: types.NamespacedName{Name: "g2"}, | ||
uids: []types.UID{"x", "y", "z", "x", "y", "z"}, | ||
}, | ||
{ | ||
key: types.NamespacedName{Name: "g3"}, | ||
uids: []types.UID{"a", "b", "c", "x", "y"}, | ||
}, | ||
} | ||
err = parallelize.Until(ctx, len(observe), func(i int) error { | ||
e := observe[i] | ||
return parallelize.Until(ctx, len(e.uids), func(j int) error { | ||
expectations.ObservedUID(log, e.key, e.uids[j]) | ||
return nil | ||
}) | ||
}) | ||
if err != nil { | ||
t.Fatalf("Observing UIDs: %v", err) | ||
} | ||
|
||
wantSatisfied := map[types.NamespacedName]bool{ | ||
{Name: "g1"}: true, | ||
{Name: "g2"}: true, | ||
{Name: "g3"}: false, | ||
} | ||
for key, want := range wantSatisfied { | ||
got := expectations.Satisfied(log, key) | ||
if got != want { | ||
t.Errorf("Got key %s satisfied: %t, want %t", key, got, want) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.