Merge pull request #121049 from siyuanfoundation/refactor

k8s.io/apiserver/storage/etcd: refactor etcd GetList.

Kubernetes-commit: e8d45596dfbdf69fd42aa6881dfdeb089a20ab33
This commit is contained in:
Kubernetes Publisher 2023-10-31 10:49:33 +01:00
commit 89717616cb
1 changed files with 26 additions and 35 deletions

View File

@ -627,17 +627,13 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
limitOption = &options[len(options)-1] limitOption = &options[len(options)-1]
} }
newItemFunc := getNewItemFunc(listObj, v) if opts.Recursive {
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
var fromRV *uint64 options = append(options, clientv3.WithRange(rangeEnd))
if len(opts.ResourceVersion) > 0 {
parsedRV, err := s.versioner.ParseResourceVersion(opts.ResourceVersion)
if err != nil {
return apierrors.NewBadRequest(fmt.Sprintf("invalid resource version: %v", err))
}
fromRV = &parsedRV
} }
newItemFunc := getNewItemFunc(listObj, v)
var continueRV, withRev int64 var continueRV, withRev int64
var continueKey string var continueKey string
switch { switch {
@ -657,28 +653,26 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
if continueRV > 0 { if continueRV > 0 {
withRev = continueRV withRev = continueRV
} }
default: case len(opts.ResourceVersion) > 0:
if fromRV != nil { parsedRV, err := s.versioner.ParseResourceVersion(opts.ResourceVersion)
switch opts.ResourceVersionMatch { if err != nil {
case metav1.ResourceVersionMatchNotOlderThan: return apierrors.NewBadRequest(fmt.Sprintf("invalid resource version: %v", err))
// The not older than constraint is checked after we get a response from etcd, }
// and returnedRV is then set to the revision we get from the etcd response. switch opts.ResourceVersionMatch {
case metav1.ResourceVersionMatchExact: case metav1.ResourceVersionMatchNotOlderThan:
withRev = int64(*fromRV) // The not older than constraint is checked after we get a response from etcd,
case "": // legacy case // and returnedRV is then set to the revision we get from the etcd response.
if opts.Recursive && opts.Predicate.Limit > 0 && *fromRV > 0 { case metav1.ResourceVersionMatchExact:
withRev = int64(*fromRV) withRev = int64(parsedRV)
} case "": // legacy case
default: if opts.Recursive && opts.Predicate.Limit > 0 && parsedRV > 0 {
return fmt.Errorf("unknown ResourceVersionMatch value: %v", opts.ResourceVersionMatch) withRev = int64(parsedRV)
} }
default:
return fmt.Errorf("unknown ResourceVersionMatch value: %v", opts.ResourceVersionMatch)
} }
} }
if opts.Recursive {
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
options = append(options, clientv3.WithRange(rangeEnd))
}
if withRev != 0 { if withRev != 0 {
options = append(options, clientv3.WithRev(withRev)) options = append(options, clientv3.WithRev(withRev))
} }
@ -717,6 +711,11 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
if len(getResp.Kvs) == 0 && getResp.More { if len(getResp.Kvs) == 0 && getResp.More {
return fmt.Errorf("no results were found, but etcd indicated there were more values remaining") return fmt.Errorf("no results were found, but etcd indicated there were more values remaining")
} }
// indicate to the client which resource version was returned, and use the same resource version for subsequent requests.
if withRev == 0 {
withRev = getResp.Header.Revision
options = append(options, clientv3.WithRev(withRev))
}
// avoid small allocations for the result slice, since this can be called in many // avoid small allocations for the result slice, since this can be called in many
// different contexts and we don't know how significantly the result will be filtered // 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
*limitOption = clientv3.WithLimit(limit) *limitOption = clientv3.WithLimit(limit)
} }
preparedKey = string(lastKey) + "\x00" preparedKey = string(lastKey) + "\x00"
if withRev == 0 {
withRev = getResp.Header.Revision
options = append(options, clientv3.WithRev(withRev))
}
}
// indicate to the client which resource version was returned
if withRev == 0 {
withRev = getResp.Header.Revision
} }
if v.IsNil() { if v.IsNil() {