Skip to content

Commit

Permalink
Align types in API server's resoruce package.
Browse files Browse the repository at this point in the history
There was an awkward mix of Atoi -> int, int64 and uint64 with
casting between them.  int is int64 on most platforms that we run on
and resoruce IDs are unlikely to wrap an int64 but let's just use
uint64 throughout so the code behaves the same on all systems.
  • Loading branch information
fasaxc committed Nov 1, 2023
1 parent bf23336 commit a3203e8
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions apiserver/pkg/storage/calico/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package calico
import (
"bytes"
"fmt"
"math"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -291,7 +290,7 @@ func (rs *resourceStore) List(ctx context.Context, key string, optsK8s storage.L
type objState struct {
obj runtime.Object
meta *storage.ResponseMeta
rev int64
rev uint64
data []byte
}

Expand All @@ -305,8 +304,8 @@ func (rs *resourceStore) getStateFromObject(obj runtime.Object) (*objState, erro
if err != nil {
return nil, fmt.Errorf("couldn't get resource version: %v", err)
}
state.rev = int64(rv)
state.meta.ResourceVersion = uint64(state.rev)
state.rev = rv
state.meta.ResourceVersion = rv

state.data, err = runtime.Encode(rs.codec, obj)
if err != nil {
Expand All @@ -331,7 +330,7 @@ func decode(
return nil
}

// GuaranteedUpdate keers calling 'tryUpdate()' to update key 'key' (of type 'ptrToType')
// GuaranteedUpdate keeps calling 'tryUpdate()' to update key 'key' (of type 'ptrToType')
// retrying the update until success if there is index conflict.
// Note that object passed to tryUpdate may change across invocations of tryUpdate() if
// other writers are simultaneously updating it, so tryUpdate() needs to take into account
Expand All @@ -351,7 +350,7 @@ func decode(
//
// "myKey", &MyType{}, true,
// func(input runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
// // Before each incovation of the user defined function, "input" is reset to
// // Before each invocation of the user defined function, "input" is reset to
// // current contents for "myKey" in database.
// curr := input.(*MyType) // Guaranteed to succeed.
//
Expand Down Expand Up @@ -428,15 +427,18 @@ func (rs *resourceStore) GuaranteedUpdate(
if err != nil {
return err
}
revInt, _ := strconv.Atoi(accessor.GetResourceVersion())
versionStr := accessor.GetResourceVersion()
var revInt uint64
if versionStr != "" {
revInt, err = strconv.ParseUint(versionStr, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse non-empty resource version %q key=%q: %w", versionStr, key, err)
}
}
updatedRes := updatedObj.(resourceObject)
if !shouldCreateOnUpdate() {
if curState.rev < math.MinInt || curState.rev > math.MaxInt {
klog.Errorf("revision number %d overflows", curState.rev)
return fmt.Errorf("Revision is outside the range of an int32: %d", curState.rev)
}
if updatedRes.GetObjectMeta().GetResourceVersion() == "" || revInt < int(curState.rev) {
updatedRes.(resourceObject).GetObjectMeta().SetResourceVersion(strconv.FormatInt(curState.rev, 10))
if updatedRes.GetObjectMeta().GetResourceVersion() == "" || revInt < curState.rev {
updatedRes.(resourceObject).GetObjectMeta().SetResourceVersion(strconv.FormatUint(curState.rev, 10))
}
}
libcalicoObj := rs.converter.convertToLibcalico(updatedRes)
Expand Down

0 comments on commit a3203e8

Please sign in to comment.