Refactor common WithRange case

From API call WithRange and WithPrefix work the same, they just set the range end.
The difference is when the range end is provided:
* WithRange(end) requires providing the end while calling
* WithPrefix() calculates the end based on key provided to the Get.

For example, those are equal:
* client.Get(ctx, "/pods/", WithPrefix())
* client.Get(ctx, "/pods/", WithRange(GetPrfixRangeEnd("/pods/")))

As keyPrefix is equal preparedKey there should not be a difference.
This commit is contained in:
Marek Siarkowicz
2023-08-30 16:26:20 +02:00
parent 887ac275a2
commit 1f4f2a5d60

View File

@@ -671,11 +671,7 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
if len(resourceVersion) > 0 && resourceVersion != "0" {
return apierrors.NewBadRequest("specifying resource version is not allowed when using continue")
}
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
options = append(options, clientv3.WithRange(rangeEnd))
preparedKey = continueKey
// If continueRV > 0, the LIST request needs a specific resource version.
// continueRV==0 is invalid.
// If continueRV < 0, the request is for the latest resource version.
@@ -698,9 +694,6 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
return fmt.Errorf("unknown ResourceVersionMatch value: %v", match)
}
}
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
options = append(options, clientv3.WithRange(rangeEnd))
default:
if fromRV != nil {
switch match {
@@ -714,10 +707,11 @@ func (s *store) GetList(ctx context.Context, key string, opts storage.ListOption
return fmt.Errorf("unknown ResourceVersionMatch value: %v", match)
}
}
}
if recursive {
options = append(options, clientv3.WithPrefix())
}
if recursive {
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
options = append(options, clientv3.WithRange(rangeEnd))
}
if withRev != 0 {
options = append(options, clientv3.WithRev(withRev))