Skip to content

Commit e8d4559

Browse files
authored
Merge pull request kubernetes#121049 from siyuanfoundation/refactor
k8s.io/apiserver/storage/etcd: refactor etcd GetList.
2 parents f5a5d83 + a968f51 commit e8d4559

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

staging/src/k8s.io/apiserver/pkg/storage/etcd3/store.go

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -627,17 +627,13 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
627627
limitOption = &options[len(options)-1]
628628
}
629629

630-
newItemFunc := getNewItemFunc(listObj, v)
631-
632-
var fromRV *uint64
633-
if len(opts.ResourceVersion) > 0 {
634-
parsedRV, err := s.versioner.ParseResourceVersion(opts.ResourceVersion)
635-
if err != nil {
636-
return apierrors.NewBadRequest(fmt.Sprintf("invalid resource version: %v", err))
637-
}
638-
fromRV = &parsedRV
630+
if opts.Recursive {
631+
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
632+
options = append(options, clientv3.WithRange(rangeEnd))
639633
}
640634

635+
newItemFunc := getNewItemFunc(listObj, v)
636+
641637
var continueRV, withRev int64
642638
var continueKey string
643639
switch {
@@ -657,28 +653,26 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
657653
if continueRV > 0 {
658654
withRev = continueRV
659655
}
660-
default:
661-
if fromRV != nil {
662-
switch opts.ResourceVersionMatch {
663-
case metav1.ResourceVersionMatchNotOlderThan:
664-
// The not older than constraint is checked after we get a response from etcd,
665-
// and returnedRV is then set to the revision we get from the etcd response.
666-
case metav1.ResourceVersionMatchExact:
667-
withRev = int64(*fromRV)
668-
case "": // legacy case
669-
if opts.Recursive && opts.Predicate.Limit > 0 && *fromRV > 0 {
670-
withRev = int64(*fromRV)
671-
}
672-
default:
673-
return fmt.Errorf("unknown ResourceVersionMatch value: %v", opts.ResourceVersionMatch)
656+
case len(opts.ResourceVersion) > 0:
657+
parsedRV, err := s.versioner.ParseResourceVersion(opts.ResourceVersion)
658+
if err != nil {
659+
return apierrors.NewBadRequest(fmt.Sprintf("invalid resource version: %v", err))
660+
}
661+
switch opts.ResourceVersionMatch {
662+
case metav1.ResourceVersionMatchNotOlderThan:
663+
// The not older than constraint is checked after we get a response from etcd,
664+
// and returnedRV is then set to the revision we get from the etcd response.
665+
case metav1.ResourceVersionMatchExact:
666+
withRev = int64(parsedRV)
667+
case "": // legacy case
668+
if opts.Recursive && opts.Predicate.Limit > 0 && parsedRV > 0 {
669+
withRev = int64(parsedRV)
674670
}
671+
default:
672+
return fmt.Errorf("unknown ResourceVersionMatch value: %v", opts.ResourceVersionMatch)
675673
}
676674
}
677675

678-
if opts.Recursive {
679-
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
680-
options = append(options, clientv3.WithRange(rangeEnd))
681-
}
682676
if withRev != 0 {
683677
options = append(options, clientv3.WithRev(withRev))
684678
}
@@ -717,6 +711,11 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
717711
if len(getResp.Kvs) == 0 && getResp.More {
718712
return fmt.Errorf("no results were found, but etcd indicated there were more values remaining")
719713
}
714+
// indicate to the client which resource version was returned, and use the same resource version for subsequent requests.
715+
if withRev == 0 {
716+
withRev = getResp.Header.Revision
717+
options = append(options, clientv3.WithRev(withRev))
718+
}
720719

721720
// avoid small allocations for the result slice, since this can be called in many
722721
// different contexts and we don't know how significantly the result will be filtered
@@ -776,14 +775,6 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
776775
*limitOption = clientv3.WithLimit(limit)
777776
}
778777
preparedKey = string(lastKey) + "\x00"
779-
if withRev == 0 {
780-
withRev = getResp.Header.Revision
781-
options = append(options, clientv3.WithRev(withRev))
782-
}
783-
}
784-
// indicate to the client which resource version was returned
785-
if withRev == 0 {
786-
withRev = getResp.Header.Revision
787778
}
788779

789780
if v.IsNil() {

test/integration/apiserver/apiserver_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,6 @@ func testListOptionsCase(t *testing.T, rsClient appsv1.ReplicaSetInterface, watc
527527
}
528528
return
529529
}
530-
if opts.ResourceVersion == invalidResourceVersion {
531-
if err == nil || !strings.Contains(err.Error(), "Invalid value") {
532-
t.Fatalf("expecting invalid value error, but got: %v", err)
533-
}
534-
return
535-
}
536530
if opts.Continue == invalidContinueToken {
537531
if err == nil || !strings.Contains(err.Error(), "continue key is not valid") {
538532
t.Fatalf("expected continue key not valid error, but got: %v", err)
@@ -546,6 +540,12 @@ func testListOptionsCase(t *testing.T, rsClient appsv1.ReplicaSetInterface, watc
546540
}
547541
return
548542
}
543+
if opts.ResourceVersion == invalidResourceVersion {
544+
if err == nil || !strings.Contains(err.Error(), "Invalid value") {
545+
t.Fatalf("expecting invalid value error, but got: %v", err)
546+
}
547+
return
548+
}
549549

550550
// Check for too old errors
551551
isExact := opts.ResourceVersionMatch == metav1.ResourceVersionMatchExact

0 commit comments

Comments
 (0)