From dc9f5bd0cedbec322691f76fddc4c93a7cb24fa0 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Fri, 15 Nov 2024 01:21:01 +0800 Subject: [PATCH] Remove unmaintained `github.com/golang-collections/collections` dependency The `github.com/golang-collections/collections` module has not been updated in since July 2013. As we are only using its stack package, we can adapt the required code directly and remove this dependency. Signed-off-by: Eng Zer Jun --- felix/idalloc/index_allocator.go | 43 +++++++++++++++++++++++++++----- go.mod | 1 - go.sum | 2 -- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/felix/idalloc/index_allocator.go b/felix/idalloc/index_allocator.go index 56e494226cf..d5ecb14b9cf 100644 --- a/felix/idalloc/index_allocator.go +++ b/felix/idalloc/index_allocator.go @@ -18,8 +18,6 @@ import ( "errors" "sort" - "github.com/golang-collections/collections/stack" - "github.com/projectcalico/calico/libcalico-go/lib/set" ) @@ -45,7 +43,7 @@ func (i ByMaxIndex) Less(a, b int) bool { return i[a].Max < i[b].Max } func (i ByMaxIndex) Swap(a, b int) { i[a], i[b] = i[b], i[a] } type IndexAllocator struct { - indexStack *stack.Stack + indexStack *stack exclusions []IndexRange } @@ -58,7 +56,7 @@ func NewIndexAllocator(indexRanges []IndexRange, exclusions []IndexRange) *Index } r := &IndexAllocator{ - indexStack: stack.New(), + indexStack: &stack{}, exclusions: exclusions, } @@ -91,10 +89,10 @@ func NewIndexAllocator(indexRanges []IndexRange, exclusions []IndexRange) *Index } func (r *IndexAllocator) GrabIndex() (int, error) { - if r.indexStack.Len() == 0 { + if r.indexStack.IsEmpty() { return 0, errors.New("no more indices available") } - return r.indexStack.Pop().(int), nil + return r.indexStack.Pop(), nil } func (r *IndexAllocator) ReleaseIndex(index int) { @@ -113,3 +111,36 @@ func (r *IndexAllocator) GrabBlock(len int) (set.Set[int], error) { } return indices, nil } + +type ( + // stack is an implementation of the stack data structure. + // Adapted from https://github.com/golang-collections/collections/blob/604e922904d35e97f98a774db7881f049cd8d970/stack/stack.go. + stack struct { + top *node + } + node struct { + value int + prev *node + } +) + +// IsEmpty returns true if the stack is empty. +func (s *stack) IsEmpty() bool { + return s.top == nil +} + +// Pop the top item of the stack and return it +func (s *stack) Pop() int { + if s.IsEmpty() { + return 0 + } + + n := s.top + s.top = n.prev + return n.value +} + +// Push a value onto the top of the stack +func (s *stack) Push(value int) { + s.top = &node{value, s.top} +} diff --git a/go.mod b/go.mod index 02d22f61d41..d71fc844c6f 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,6 @@ require ( github.com/gofrs/flock v0.8.1 github.com/gogo/googleapis v1.4.1 github.com/gogo/protobuf v1.3.2 - github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 github.com/golang/protobuf v1.5.4 github.com/golang/snappy v0.0.4 github.com/google/btree v1.1.2 diff --git a/go.sum b/go.sum index 499cea42d59..0eaebd1476e 100644 --- a/go.sum +++ b/go.sum @@ -286,8 +286,6 @@ github.com/gogo/googleapis v1.4.1 h1:1Yx4Myt7BxzvUr5ldGSbwYiZG6t9wGBZ+8/fX3Wvtq0 github.com/gogo/googleapis v1.4.1/go.mod h1:2lpHqI5OcWCtVElxXnPt+s8oJvMpySlOyM6xDCrzib4= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4= -github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=