From 0eafac0f7033c5952606d5331ee7ddb860551fb8 Mon Sep 17 00:00:00 2001 From: Saketh Kalaga <51327242+renormalize@users.noreply.github.com> Date: Wed, 9 Oct 2024 14:10:31 +0530 Subject: [PATCH] Upgrade the `k8s.io/utils` dependency. * Remove all usages of the deprecated `k8s.io/utils/pointer` package and replace them with `k8s.io/utils/ptr`. --- go.mod | 2 +- go.sum | 4 +- .../membergarbagecollector_test.go | 4 +- pkg/miscellaneous/miscellaneous.go | 6 +- pkg/miscellaneous/miscellaneous_test.go | 12 +- pkg/snapstore/abs_snapstore.go | 4 +- pkg/snapstore/s3_snapstore.go | 14 +- vendor/k8s.io/utils/integer/integer.go | 26 +- vendor/k8s.io/utils/net/multi_listen.go | 195 +++++++++ vendor/k8s.io/utils/pointer/pointer.go | 410 ------------------ vendor/k8s.io/utils/{pointer => ptr}/OWNERS | 0 .../k8s.io/utils/{pointer => ptr}/README.md | 0 vendor/k8s.io/utils/ptr/ptr.go | 73 ++++ vendor/modules.txt | 4 +- 14 files changed, 309 insertions(+), 445 deletions(-) create mode 100644 vendor/k8s.io/utils/net/multi_listen.go delete mode 100644 vendor/k8s.io/utils/pointer/pointer.go rename vendor/k8s.io/utils/{pointer => ptr}/OWNERS (100%) rename vendor/k8s.io/utils/{pointer => ptr}/README.md (100%) create mode 100644 vendor/k8s.io/utils/ptr/ptr.go diff --git a/go.mod b/go.mod index 52e53d68d..f4f1997f7 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( k8s.io/apimachinery v0.26.1 k8s.io/client-go v0.26.1 k8s.io/helm v2.17.0+incompatible // haven't upgraded yet - k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 + k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 sigs.k8s.io/controller-runtime v0.14.4 // haven't upgraded yet sigs.k8s.io/yaml v1.4.0 ) diff --git a/go.sum b/go.sum index 0ab4b9a93..3e3839e1f 100644 --- a/go.sum +++ b/go.sum @@ -485,8 +485,8 @@ k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4= k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E= k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280/go.mod h1:+Axhij7bCpeqhklhUTe3xmOn6bWxolyZEeyaFpjGtl4= -k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 h1:KTgPnR10d5zhztWptI952TNtt/4u5h3IzDXkdIMuo2Y= -k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 h1:MDF6h2H/h4tbzmtIKTuctcwZmY0tY9mD9fNT47QO6HI= +k8s.io/utils v0.0.0-20240921022957-49e7df575cb6/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/controller-runtime v0.14.4 h1:Kd/Qgx5pd2XUL08eOV2vwIq3L9GhIbJ5Nxengbd4/0M= sigs.k8s.io/controller-runtime v0.14.4/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0= sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 h1:iXTIw73aPyC+oRdyqqvVJuloN1p0AC/kzH07hu3NE+k= diff --git a/pkg/health/membergarbagecollector/membergarbagecollector_test.go b/pkg/health/membergarbagecollector/membergarbagecollector_test.go index 7674afe71..273181a42 100644 --- a/pkg/health/membergarbagecollector/membergarbagecollector_test.go +++ b/pkg/health/membergarbagecollector/membergarbagecollector_test.go @@ -22,7 +22,7 @@ import ( coordv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" ) var _ = Describe("Membergarbagecollector", func() { @@ -226,7 +226,7 @@ func getStsWithName(name string, replicas int32) *appsv1.StatefulSet { Namespace: os.Getenv("POD_NAMESPACE"), }, Spec: appsv1.StatefulSetSpec{ - Replicas: pointer.Int32Ptr(replicas), + Replicas: ptr.To(int32(replicas)), }, Status: appsv1.StatefulSetStatus{ Replicas: replicas, diff --git a/pkg/miscellaneous/miscellaneous.go b/pkg/miscellaneous/miscellaneous.go index e2f54a57b..580e1d5cb 100644 --- a/pkg/miscellaneous/miscellaneous.go +++ b/pkg/miscellaneous/miscellaneous.go @@ -31,7 +31,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/util/retry" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" "sigs.k8s.io/yaml" appsv1 "k8s.io/api/apps/v1" @@ -436,11 +436,11 @@ func GetInitialClusterStateIfScaleup(ctx context.Context, logger logrus.Entry, c } if IsAnnotationPresent(etcdSts, ScaledToMultiNodeAnnotationKey) { - return pointer.StringPtr(ClusterStateExisting), nil + return ptr.To(ClusterStateExisting), nil } if *etcdSts.Spec.Replicas > 1 && *etcdSts.Spec.Replicas > etcdSts.Status.UpdatedReplicas { - return pointer.StringPtr(ClusterStateExisting), nil + return ptr.To(ClusterStateExisting), nil } return nil, nil } diff --git a/pkg/miscellaneous/miscellaneous_test.go b/pkg/miscellaneous/miscellaneous_test.go index 8299deddd..7b0d13cdb 100644 --- a/pkg/miscellaneous/miscellaneous_test.go +++ b/pkg/miscellaneous/miscellaneous_test.go @@ -26,7 +26,7 @@ import ( "go.etcd.io/etcd/etcdserver/etcdserverpb" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" ) var ( @@ -466,7 +466,7 @@ var _ = Describe("Miscellaneous Tests", func() { Context("In single node etcd: no scale-up", func() { BeforeEach(func() { sts.Spec = appsv1.StatefulSetSpec{ - Replicas: pointer.Int32Ptr(1), + Replicas: ptr.To(int32(1)), } sts.Status = appsv1.StatefulSetStatus{ UpdatedReplicas: 1, @@ -488,7 +488,7 @@ var _ = Describe("Miscellaneous Tests", func() { Context("In multi-node etcd bootstrap: no scale-up", func() { BeforeEach(func() { sts.Spec = appsv1.StatefulSetSpec{ - Replicas: pointer.Int32Ptr(3), + Replicas: ptr.To(int32(3)), } sts.Status = appsv1.StatefulSetStatus{ UpdatedReplicas: 3, @@ -510,7 +510,7 @@ var _ = Describe("Miscellaneous Tests", func() { Context("In case of Scaling up from single node to multi-node etcd with no scale-up annotation set", func() { BeforeEach(func() { sts.Spec = appsv1.StatefulSetSpec{ - Replicas: pointer.Int32Ptr(3), + Replicas: ptr.To(int32(3)), } sts.Status = appsv1.StatefulSetStatus{ UpdatedReplicas: 1, @@ -532,7 +532,7 @@ var _ = Describe("Miscellaneous Tests", func() { Context("scaling of single node to multi-node etcd with scale-up annotation set", func() { BeforeEach(func() { sts.Spec = appsv1.StatefulSetSpec{ - Replicas: pointer.Int32Ptr(3), + Replicas: ptr.To(int32(3)), } sts.Status = appsv1.StatefulSetStatus{ UpdatedReplicas: 3, @@ -567,7 +567,7 @@ var _ = Describe("Miscellaneous Tests", func() { Namespace: namespace, }, Spec: appsv1.StatefulSetSpec{ - Replicas: pointer.Int32Ptr(3), + Replicas: ptr.To(int32(3)), }, Status: appsv1.StatefulSetStatus{ UpdatedReplicas: 1, diff --git a/pkg/snapstore/abs_snapstore.go b/pkg/snapstore/abs_snapstore.go index 8322a4d20..752a1e3cc 100644 --- a/pkg/snapstore/abs_snapstore.go +++ b/pkg/snapstore/abs_snapstore.go @@ -28,7 +28,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container" "github.com/sirupsen/logrus" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" brtypes "github.com/gardener/etcd-backup-restore/pkg/types" ) @@ -247,7 +247,7 @@ func readABSCredentialFiles(dirname string) (*absCredentials, error) { if err != nil { return nil, err } - absConfig.Domain = pointer.String(string(data)) + absConfig.Domain = ptr.To(string(data)) } } diff --git a/pkg/snapstore/s3_snapstore.go b/pkg/snapstore/s3_snapstore.go index fa2737e94..9151dd99a 100644 --- a/pkg/snapstore/s3_snapstore.go +++ b/pkg/snapstore/s3_snapstore.go @@ -30,7 +30,7 @@ import ( "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3iface" "github.com/sirupsen/logrus" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" brtypes "github.com/gardener/etcd-backup-restore/pkg/types" ) @@ -163,7 +163,7 @@ func readAWSCredentialsJSONFile(filename string) (session.Options, SSECredential return session.Options{ Config: aws.Config{ Credentials: credentials.NewStaticCredentials(awsConfig.AccessKeyID, awsConfig.SecretAccessKey, ""), - Region: pointer.String(awsConfig.Region), + Region: ptr.To(awsConfig.Region), Endpoint: awsConfig.Endpoint, S3ForcePathStyle: awsConfig.S3ForcePathStyle, HTTPClient: httpClient, @@ -216,7 +216,7 @@ func readAWSCredentialFiles(dirname string) (session.Options, SSECredentials, er return session.Options{ Config: aws.Config{ Credentials: credentials.NewStaticCredentials(awsConfig.AccessKeyID, awsConfig.SecretAccessKey, ""), - Region: pointer.String(awsConfig.Region), + Region: ptr.To(awsConfig.Region), Endpoint: awsConfig.Endpoint, S3ForcePathStyle: awsConfig.S3ForcePathStyle, HTTPClient: httpClient, @@ -257,7 +257,7 @@ func readAWSCredentialFromDir(dirname string) (*awsCredentials, error) { if err != nil { return nil, err } - awsConfig.Endpoint = pointer.String(string(data)) + awsConfig.Endpoint = ptr.To(string(data)) case "s3ForcePathStyle": data, err := os.ReadFile(dirname + "/s3ForcePathStyle") if err != nil { @@ -283,19 +283,19 @@ func readAWSCredentialFromDir(dirname string) (*awsCredentials, error) { if err != nil { return nil, err } - awsConfig.TrustedCaCert = pointer.String(string(data)) + awsConfig.TrustedCaCert = ptr.To(string(data)) case "sseCustomerKey": data, err := os.ReadFile(dirname + "/sseCustomerKey") if err != nil { return nil, err } - awsConfig.SSECustomerKey = pointer.String(string(data)) + awsConfig.SSECustomerKey = ptr.To(string(data)) case "sseCustomerAlgorithm": data, err := os.ReadFile(dirname + "/sseCustomerAlgorithm") if err != nil { return nil, err } - awsConfig.SSECustomerAlgorithm = pointer.String(string(data)) + awsConfig.SSECustomerAlgorithm = ptr.To(string(data)) } } diff --git a/vendor/k8s.io/utils/integer/integer.go b/vendor/k8s.io/utils/integer/integer.go index e4e740cad..f64d64955 100644 --- a/vendor/k8s.io/utils/integer/integer.go +++ b/vendor/k8s.io/utils/integer/integer.go @@ -16,7 +16,10 @@ limitations under the License. package integer -// IntMax returns the maximum of the params +import "math" + +// IntMax returns the maximum of the params. +// Deprecated: for new code, use the max() builtin instead. func IntMax(a, b int) int { if b > a { return b @@ -24,7 +27,8 @@ func IntMax(a, b int) int { return a } -// IntMin returns the minimum of the params +// IntMin returns the minimum of the params. +// Deprecated: for new code, use the min() builtin instead. func IntMin(a, b int) int { if b < a { return b @@ -32,7 +36,8 @@ func IntMin(a, b int) int { return a } -// Int32Max returns the maximum of the params +// Int32Max returns the maximum of the params. +// Deprecated: for new code, use the max() builtin instead. func Int32Max(a, b int32) int32 { if b > a { return b @@ -40,7 +45,8 @@ func Int32Max(a, b int32) int32 { return a } -// Int32Min returns the minimum of the params +// Int32Min returns the minimum of the params. +// Deprecated: for new code, use the min() builtin instead. func Int32Min(a, b int32) int32 { if b < a { return b @@ -48,7 +54,8 @@ func Int32Min(a, b int32) int32 { return a } -// Int64Max returns the maximum of the params +// Int64Max returns the maximum of the params. +// Deprecated: for new code, use the max() builtin instead. func Int64Max(a, b int64) int64 { if b > a { return b @@ -56,7 +63,8 @@ func Int64Max(a, b int64) int64 { return a } -// Int64Min returns the minimum of the params +// Int64Min returns the minimum of the params. +// Deprecated: for new code, use the min() builtin instead. func Int64Min(a, b int64) int64 { if b < a { return b @@ -65,9 +73,7 @@ func Int64Min(a, b int64) int64 { } // RoundToInt32 rounds floats into integer numbers. +// Deprecated: use math.Round() and a cast directly. func RoundToInt32(a float64) int32 { - if a < 0 { - return int32(a - 0.5) - } - return int32(a + 0.5) + return int32(math.Round(a)) } diff --git a/vendor/k8s.io/utils/net/multi_listen.go b/vendor/k8s.io/utils/net/multi_listen.go new file mode 100644 index 000000000..7cb7795be --- /dev/null +++ b/vendor/k8s.io/utils/net/multi_listen.go @@ -0,0 +1,195 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package net + +import ( + "context" + "fmt" + "net" + "sync" +) + +// connErrPair pairs conn and error which is returned by accept on sub-listeners. +type connErrPair struct { + conn net.Conn + err error +} + +// multiListener implements net.Listener +type multiListener struct { + listeners []net.Listener + wg sync.WaitGroup + + // connCh passes accepted connections, from child listeners to parent. + connCh chan connErrPair + // stopCh communicates from parent to child listeners. + stopCh chan struct{} +} + +// compile time check to ensure *multiListener implements net.Listener +var _ net.Listener = &multiListener{} + +// MultiListen returns net.Listener which can listen on and accept connections for +// the given network on multiple addresses. Internally it uses stdlib to create +// sub-listener and multiplexes connection requests using go-routines. +// The network must be "tcp", "tcp4" or "tcp6". +// It follows the semantics of net.Listen that primarily means: +// 1. If the host is an unspecified/zero IP address with "tcp" network, MultiListen +// listens on all available unicast and anycast IP addresses of the local system. +// 2. Use "tcp4" or "tcp6" to exclusively listen on IPv4 or IPv6 family, respectively. +// 3. The host can accept names (e.g, localhost) and it will create a listener for at +// most one of the host's IP. +func MultiListen(ctx context.Context, network string, addrs ...string) (net.Listener, error) { + var lc net.ListenConfig + return multiListen( + ctx, + network, + addrs, + func(ctx context.Context, network, address string) (net.Listener, error) { + return lc.Listen(ctx, network, address) + }) +} + +// multiListen implements MultiListen by consuming stdlib functions as dependency allowing +// mocking for unit-testing. +func multiListen( + ctx context.Context, + network string, + addrs []string, + listenFunc func(ctx context.Context, network, address string) (net.Listener, error), +) (net.Listener, error) { + if !(network == "tcp" || network == "tcp4" || network == "tcp6") { + return nil, fmt.Errorf("network %q not supported", network) + } + if len(addrs) == 0 { + return nil, fmt.Errorf("no address provided to listen on") + } + + ml := &multiListener{ + connCh: make(chan connErrPair), + stopCh: make(chan struct{}), + } + for _, addr := range addrs { + l, err := listenFunc(ctx, network, addr) + if err != nil { + // close all the sub-listeners and exit + _ = ml.Close() + return nil, err + } + ml.listeners = append(ml.listeners, l) + } + + for _, l := range ml.listeners { + ml.wg.Add(1) + go func(l net.Listener) { + defer ml.wg.Done() + for { + // Accept() is blocking, unless ml.Close() is called, in which + // case it will return immediately with an error. + conn, err := l.Accept() + // This assumes that ANY error from Accept() will terminate the + // sub-listener. We could maybe be more precise, but it + // doesn't seem necessary. + terminate := err != nil + + select { + case ml.connCh <- connErrPair{conn: conn, err: err}: + case <-ml.stopCh: + // In case we accepted a connection AND were stopped, and + // this select-case was chosen, just throw away the + // connection. This avoids potentially blocking on connCh + // or leaking a connection. + if conn != nil { + _ = conn.Close() + } + terminate = true + } + // Make sure we don't loop on Accept() returning an error and + // the select choosing the channel case. + if terminate { + return + } + } + }(l) + } + return ml, nil +} + +// Accept implements net.Listener. It waits for and returns a connection from +// any of the sub-listener. +func (ml *multiListener) Accept() (net.Conn, error) { + // wait for any sub-listener to enqueue an accepted connection + connErr, ok := <-ml.connCh + if !ok { + // The channel will be closed only when Close() is called on the + // multiListener. Closing of this channel implies that all + // sub-listeners are also closed, which causes a "use of closed + // network connection" error on their Accept() calls. We return the + // same error for multiListener.Accept() if multiListener.Close() + // has already been called. + return nil, fmt.Errorf("use of closed network connection") + } + return connErr.conn, connErr.err +} + +// Close implements net.Listener. It will close all sub-listeners and wait for +// the go-routines to exit. +func (ml *multiListener) Close() error { + // Make sure this can be called repeatedly without explosions. + select { + case <-ml.stopCh: + return fmt.Errorf("use of closed network connection") + default: + } + + // Tell all sub-listeners to stop. + close(ml.stopCh) + + // Closing the listeners causes Accept() to immediately return an error in + // the sub-listener go-routines. + for _, l := range ml.listeners { + _ = l.Close() + } + + // Wait for all the sub-listener go-routines to exit. + ml.wg.Wait() + close(ml.connCh) + + // Drain any already-queued connections. + for connErr := range ml.connCh { + if connErr.conn != nil { + _ = connErr.conn.Close() + } + } + return nil +} + +// Addr is an implementation of the net.Listener interface. It always returns +// the address of the first listener. Callers should use conn.LocalAddr() to +// obtain the actual local address of the sub-listener. +func (ml *multiListener) Addr() net.Addr { + return ml.listeners[0].Addr() +} + +// Addrs is like Addr, but returns the address for all registered listeners. +func (ml *multiListener) Addrs() []net.Addr { + var ret []net.Addr + for _, l := range ml.listeners { + ret = append(ret, l.Addr()) + } + return ret +} diff --git a/vendor/k8s.io/utils/pointer/pointer.go b/vendor/k8s.io/utils/pointer/pointer.go deleted file mode 100644 index b8103223a..000000000 --- a/vendor/k8s.io/utils/pointer/pointer.go +++ /dev/null @@ -1,410 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package pointer - -import ( - "fmt" - "reflect" - "time" -) - -// AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when, -// for example, an API struct is handled by plugins which need to distinguish -// "no plugin accepted this spec" from "this spec is empty". -// -// This function is only valid for structs and pointers to structs. Any other -// type will cause a panic. Passing a typed nil pointer will return true. -func AllPtrFieldsNil(obj interface{}) bool { - v := reflect.ValueOf(obj) - if !v.IsValid() { - panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj)) - } - if v.Kind() == reflect.Ptr { - if v.IsNil() { - return true - } - v = v.Elem() - } - for i := 0; i < v.NumField(); i++ { - if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() { - return false - } - } - return true -} - -// Int returns a pointer to an int -func Int(i int) *int { - return &i -} - -// IntPtr is a function variable referring to Int. -// -// Deprecated: Use Int instead. -var IntPtr = Int // for back-compat - -// IntDeref dereferences the int ptr and returns it if not nil, or else -// returns def. -func IntDeref(ptr *int, def int) int { - if ptr != nil { - return *ptr - } - return def -} - -// IntPtrDerefOr is a function variable referring to IntDeref. -// -// Deprecated: Use IntDeref instead. -var IntPtrDerefOr = IntDeref // for back-compat - -// Int32 returns a pointer to an int32. -func Int32(i int32) *int32 { - return &i -} - -// Int32Ptr is a function variable referring to Int32. -// -// Deprecated: Use Int32 instead. -var Int32Ptr = Int32 // for back-compat - -// Int32Deref dereferences the int32 ptr and returns it if not nil, or else -// returns def. -func Int32Deref(ptr *int32, def int32) int32 { - if ptr != nil { - return *ptr - } - return def -} - -// Int32PtrDerefOr is a function variable referring to Int32Deref. -// -// Deprecated: Use Int32Deref instead. -var Int32PtrDerefOr = Int32Deref // for back-compat - -// Int32Equal returns true if both arguments are nil or both arguments -// dereference to the same value. -func Int32Equal(a, b *int32) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// Uint returns a pointer to an uint -func Uint(i uint) *uint { - return &i -} - -// UintPtr is a function variable referring to Uint. -// -// Deprecated: Use Uint instead. -var UintPtr = Uint // for back-compat - -// UintDeref dereferences the uint ptr and returns it if not nil, or else -// returns def. -func UintDeref(ptr *uint, def uint) uint { - if ptr != nil { - return *ptr - } - return def -} - -// UintPtrDerefOr is a function variable referring to UintDeref. -// -// Deprecated: Use UintDeref instead. -var UintPtrDerefOr = UintDeref // for back-compat - -// Uint32 returns a pointer to an uint32. -func Uint32(i uint32) *uint32 { - return &i -} - -// Uint32Ptr is a function variable referring to Uint32. -// -// Deprecated: Use Uint32 instead. -var Uint32Ptr = Uint32 // for back-compat - -// Uint32Deref dereferences the uint32 ptr and returns it if not nil, or else -// returns def. -func Uint32Deref(ptr *uint32, def uint32) uint32 { - if ptr != nil { - return *ptr - } - return def -} - -// Uint32PtrDerefOr is a function variable referring to Uint32Deref. -// -// Deprecated: Use Uint32Deref instead. -var Uint32PtrDerefOr = Uint32Deref // for back-compat - -// Uint32Equal returns true if both arguments are nil or both arguments -// dereference to the same value. -func Uint32Equal(a, b *uint32) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// Int64 returns a pointer to an int64. -func Int64(i int64) *int64 { - return &i -} - -// Int64Ptr is a function variable referring to Int64. -// -// Deprecated: Use Int64 instead. -var Int64Ptr = Int64 // for back-compat - -// Int64Deref dereferences the int64 ptr and returns it if not nil, or else -// returns def. -func Int64Deref(ptr *int64, def int64) int64 { - if ptr != nil { - return *ptr - } - return def -} - -// Int64PtrDerefOr is a function variable referring to Int64Deref. -// -// Deprecated: Use Int64Deref instead. -var Int64PtrDerefOr = Int64Deref // for back-compat - -// Int64Equal returns true if both arguments are nil or both arguments -// dereference to the same value. -func Int64Equal(a, b *int64) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// Uint64 returns a pointer to an uint64. -func Uint64(i uint64) *uint64 { - return &i -} - -// Uint64Ptr is a function variable referring to Uint64. -// -// Deprecated: Use Uint64 instead. -var Uint64Ptr = Uint64 // for back-compat - -// Uint64Deref dereferences the uint64 ptr and returns it if not nil, or else -// returns def. -func Uint64Deref(ptr *uint64, def uint64) uint64 { - if ptr != nil { - return *ptr - } - return def -} - -// Uint64PtrDerefOr is a function variable referring to Uint64Deref. -// -// Deprecated: Use Uint64Deref instead. -var Uint64PtrDerefOr = Uint64Deref // for back-compat - -// Uint64Equal returns true if both arguments are nil or both arguments -// dereference to the same value. -func Uint64Equal(a, b *uint64) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// Bool returns a pointer to a bool. -func Bool(b bool) *bool { - return &b -} - -// BoolPtr is a function variable referring to Bool. -// -// Deprecated: Use Bool instead. -var BoolPtr = Bool // for back-compat - -// BoolDeref dereferences the bool ptr and returns it if not nil, or else -// returns def. -func BoolDeref(ptr *bool, def bool) bool { - if ptr != nil { - return *ptr - } - return def -} - -// BoolPtrDerefOr is a function variable referring to BoolDeref. -// -// Deprecated: Use BoolDeref instead. -var BoolPtrDerefOr = BoolDeref // for back-compat - -// BoolEqual returns true if both arguments are nil or both arguments -// dereference to the same value. -func BoolEqual(a, b *bool) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// String returns a pointer to a string. -func String(s string) *string { - return &s -} - -// StringPtr is a function variable referring to String. -// -// Deprecated: Use String instead. -var StringPtr = String // for back-compat - -// StringDeref dereferences the string ptr and returns it if not nil, or else -// returns def. -func StringDeref(ptr *string, def string) string { - if ptr != nil { - return *ptr - } - return def -} - -// StringPtrDerefOr is a function variable referring to StringDeref. -// -// Deprecated: Use StringDeref instead. -var StringPtrDerefOr = StringDeref // for back-compat - -// StringEqual returns true if both arguments are nil or both arguments -// dereference to the same value. -func StringEqual(a, b *string) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// Float32 returns a pointer to a float32. -func Float32(i float32) *float32 { - return &i -} - -// Float32Ptr is a function variable referring to Float32. -// -// Deprecated: Use Float32 instead. -var Float32Ptr = Float32 - -// Float32Deref dereferences the float32 ptr and returns it if not nil, or else -// returns def. -func Float32Deref(ptr *float32, def float32) float32 { - if ptr != nil { - return *ptr - } - return def -} - -// Float32PtrDerefOr is a function variable referring to Float32Deref. -// -// Deprecated: Use Float32Deref instead. -var Float32PtrDerefOr = Float32Deref // for back-compat - -// Float32Equal returns true if both arguments are nil or both arguments -// dereference to the same value. -func Float32Equal(a, b *float32) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// Float64 returns a pointer to a float64. -func Float64(i float64) *float64 { - return &i -} - -// Float64Ptr is a function variable referring to Float64. -// -// Deprecated: Use Float64 instead. -var Float64Ptr = Float64 - -// Float64Deref dereferences the float64 ptr and returns it if not nil, or else -// returns def. -func Float64Deref(ptr *float64, def float64) float64 { - if ptr != nil { - return *ptr - } - return def -} - -// Float64PtrDerefOr is a function variable referring to Float64Deref. -// -// Deprecated: Use Float64Deref instead. -var Float64PtrDerefOr = Float64Deref // for back-compat - -// Float64Equal returns true if both arguments are nil or both arguments -// dereference to the same value. -func Float64Equal(a, b *float64) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} - -// Duration returns a pointer to a time.Duration. -func Duration(d time.Duration) *time.Duration { - return &d -} - -// DurationDeref dereferences the time.Duration ptr and returns it if not nil, or else -// returns def. -func DurationDeref(ptr *time.Duration, def time.Duration) time.Duration { - if ptr != nil { - return *ptr - } - return def -} - -// DurationEqual returns true if both arguments are nil or both arguments -// dereference to the same value. -func DurationEqual(a, b *time.Duration) bool { - if (a == nil) != (b == nil) { - return false - } - if a == nil { - return true - } - return *a == *b -} diff --git a/vendor/k8s.io/utils/pointer/OWNERS b/vendor/k8s.io/utils/ptr/OWNERS similarity index 100% rename from vendor/k8s.io/utils/pointer/OWNERS rename to vendor/k8s.io/utils/ptr/OWNERS diff --git a/vendor/k8s.io/utils/pointer/README.md b/vendor/k8s.io/utils/ptr/README.md similarity index 100% rename from vendor/k8s.io/utils/pointer/README.md rename to vendor/k8s.io/utils/ptr/README.md diff --git a/vendor/k8s.io/utils/ptr/ptr.go b/vendor/k8s.io/utils/ptr/ptr.go new file mode 100644 index 000000000..659ed3b9e --- /dev/null +++ b/vendor/k8s.io/utils/ptr/ptr.go @@ -0,0 +1,73 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ptr + +import ( + "fmt" + "reflect" +) + +// AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when, +// for example, an API struct is handled by plugins which need to distinguish +// "no plugin accepted this spec" from "this spec is empty". +// +// This function is only valid for structs and pointers to structs. Any other +// type will cause a panic. Passing a typed nil pointer will return true. +func AllPtrFieldsNil(obj interface{}) bool { + v := reflect.ValueOf(obj) + if !v.IsValid() { + panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj)) + } + if v.Kind() == reflect.Ptr { + if v.IsNil() { + return true + } + v = v.Elem() + } + for i := 0; i < v.NumField(); i++ { + if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() { + return false + } + } + return true +} + +// To returns a pointer to the given value. +func To[T any](v T) *T { + return &v +} + +// Deref dereferences ptr and returns the value it points to if no nil, or else +// returns def. +func Deref[T any](ptr *T, def T) T { + if ptr != nil { + return *ptr + } + return def +} + +// Equal returns true if both arguments are nil or both arguments +// dereference to the same value. +func Equal[T comparable](a, b *T) bool { + if (a == nil) != (b == nil) { + return false + } + if a == nil { + return true + } + return *a == *b +} diff --git a/vendor/modules.txt b/vendor/modules.txt index d1b1fb7c4..2c7c1c751 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1084,14 +1084,14 @@ k8s.io/kube-openapi/pkg/schemamutation k8s.io/kube-openapi/pkg/spec3 k8s.io/kube-openapi/pkg/util/proto k8s.io/kube-openapi/pkg/validation/spec -# k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 +# k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 ## explicit; go 1.18 k8s.io/utils/clock k8s.io/utils/clock/testing k8s.io/utils/integer k8s.io/utils/internal/third_party/forked/golang/net k8s.io/utils/net -k8s.io/utils/pointer +k8s.io/utils/ptr k8s.io/utils/strings/slices # sigs.k8s.io/controller-runtime v0.14.4 ## explicit; go 1.19