Skip to content

Commit 1d61bfd

Browse files
committed
Avoid reusing list objects across serve-readiness probes
Signed-off-by: Andreas Fritzler <andreas.fritzler@sap.com>
1 parent 822e018 commit 1d61bfd

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

utils/envtest/envtest.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -690,15 +690,15 @@ const serveReadinessStableDuration = 500 * time.Millisecond
690690
// aggregated server, so it observes the transient 503 startup window and absorbs
691691
// it before the test suite proceeds.
692692
func waitUntilGroupVersionsServe(ctx context.Context, c client.Client, scheme *runtime.Scheme, gvs ...schema.GroupVersion) error {
693-
var lists []client.ObjectList
693+
var listTypes []reflect.Type
694694
for _, gv := range gvs {
695-
list, ok := newListForGroupVersion(scheme, gv)
695+
listType, ok := firstListTypeForGroupVersion(scheme, gv)
696696
if !ok {
697697
continue
698698
}
699-
lists = append(lists, list)
699+
listTypes = append(listTypes, listType)
700700
}
701-
if len(lists) == 0 {
701+
if len(listTypes) == 0 {
702702
return nil
703703
}
704704

@@ -710,7 +710,8 @@ func waitUntilGroupVersionsServe(ctx context.Context, c client.Client, scheme *r
710710
successes := 0
711711
var lastErr error
712712
if err := wait.PollUntilContextCancel(ctx, serveReadinessPollInterval, true, func(ctx context.Context) (bool, error) {
713-
for _, list := range lists {
713+
for _, listType := range listTypes {
714+
list := reflect.New(listType).Interface().(client.ObjectList)
714715
if err := c.List(ctx, list); err != nil {
715716
if !isTransientServeError(err) {
716717
return false, fmt.Errorf("unexpected error probing aggregated api server: %w", err)
@@ -731,10 +732,12 @@ func waitUntilGroupVersionsServe(ctx context.Context, c client.Client, scheme *r
731732
return nil
732733
}
733734

734-
// newListForGroupVersion returns a fresh list object for the first list kind
735-
// registered for the given group version (e.g. *VolumeList), constructed via the
736-
// scheme so the scheme stays the single source of truth for type instantiation.
737-
func newListForGroupVersion(scheme *runtime.Scheme, gv schema.GroupVersion) (client.ObjectList, bool) {
735+
// firstListTypeForGroupVersion returns the reflect.Type of the first list kind
736+
// registered for the given group version (e.g. *VolumeList). Kind resolution is
737+
// deterministic (sorted) and the scheme is the single source of truth for type
738+
// discovery, so callers can safely hold the resulting type and instantiate fresh
739+
// list objects from it.
740+
func firstListTypeForGroupVersion(scheme *runtime.Scheme, gv schema.GroupVersion) (reflect.Type, bool) {
738741
kinds := make([]string, 0, len(scheme.KnownTypes(gv)))
739742
for kind := range scheme.KnownTypes(gv) {
740743
kinds = append(kinds, kind)
@@ -746,7 +749,7 @@ func newListForGroupVersion(scheme *runtime.Scheme, gv schema.GroupVersion) (cli
746749
continue
747750
}
748751
if list, ok := obj.(client.ObjectList); ok {
749-
return list, true
752+
return reflect.TypeOf(list), true
750753
}
751754
}
752755
return nil, false

0 commit comments

Comments
 (0)