diff --git a/go.mod b/go.mod index 5a482ca5bb..b32ef07505 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( github.com/jinzhu/copier v0.3.5 github.com/kubernetes-csi/csi-lib-utils v0.6.1 github.com/longhorn/backing-image-manager v1.5.4-rc3 - github.com/longhorn/backupstore v0.0.0-20231114103026-af339bb498d3 + github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77 github.com/longhorn/go-iscsi-helper v0.0.0-20231113050545-9df1e6b605c7 github.com/longhorn/go-spdk-helper v0.0.0-20231113055029-9acddd184246 github.com/longhorn/longhorn-engine v1.5.4-rc3 @@ -67,15 +67,15 @@ require ( google.golang.org/grpc v1.61.0 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c gopkg.in/yaml.v2 v2.4.0 - k8s.io/api v0.28.2 + k8s.io/api v0.28.6 k8s.io/apiextensions-apiserver v0.25.4 - k8s.io/apimachinery v0.28.2 + k8s.io/apimachinery v0.28.6 k8s.io/cli-runtime v0.28.2 - k8s.io/client-go v0.28.2 + k8s.io/client-go v0.28.6 k8s.io/kubernetes v1.28.2 k8s.io/metrics v0.28.2 k8s.io/mount-utils v0.28.2 - k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 + k8s.io/utils v0.0.0-20240102154912-e7106e64919e sigs.k8s.io/controller-runtime v0.10.1 ) @@ -152,7 +152,7 @@ require ( github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.10.1 // indirect - github.com/rancher/lasso v0.0.0-20230830164424-d684fdeb6f29 + github.com/rancher/lasso v0.0.0-20240123150939-7055397d6dfa github.com/robfig/cron/v3 v3.0.1 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect diff --git a/go.sum b/go.sum index 4e0380de64..97d4bdf9d8 100644 --- a/go.sum +++ b/go.sum @@ -1045,6 +1045,8 @@ github.com/longhorn/backupstore v0.0.0-20231025000449-5316e8b5320d h1:WrjW6b0Vma github.com/longhorn/backupstore v0.0.0-20231025000449-5316e8b5320d/go.mod h1:wiEYTbvxEAIUxAAY1DmvMeuFuGqwWmJTzfVhZiBKlNo= github.com/longhorn/backupstore v0.0.0-20231114103026-af339bb498d3 h1:z1+k7ANPYjYyq0FMljsM/0jXmS/tB0pPLi18RWBUQ9c= github.com/longhorn/backupstore v0.0.0-20231114103026-af339bb498d3/go.mod h1:JzuHEmLyQPhGuxtKpJ2vCXPfGbylXJiBVg4KLVKsj9c= +github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77 h1:iJRq59kA22f9HIjFtY/lz5rKCorZJrrYXju70XoWdmE= +github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77/go.mod h1:4cbJWtlrD2cGTQxQLtdlPTYopiJiusXH7CpOBrn/s3k= github.com/longhorn/go-iscsi-helper v0.0.0-20231113050545-9df1e6b605c7 h1:YOOhIENIfhVCOaD00oWFeothjP0UyItBScUmpwGg8TM= github.com/longhorn/go-iscsi-helper v0.0.0-20231113050545-9df1e6b605c7/go.mod h1:xz30xMoSLdYxfUygCzM/s99CA8/LNrkcEULFWExhqz4= github.com/longhorn/go-spdk-helper v0.0.0-20231113055029-9acddd184246 h1:Pry2QHJEqEy9jsRIaACmq/1Hre8N9wfE061+gQKspKg= @@ -1225,6 +1227,8 @@ github.com/rancher/go-rancher v0.1.1-0.20220412083059-ff12399dd57b h1:so40GMVZOZ github.com/rancher/go-rancher v0.1.1-0.20220412083059-ff12399dd57b/go.mod h1:7oQvGNiJsGvrUgB+7AH8bmdzuR0uhULfwKb43Ht0hUk= github.com/rancher/lasso v0.0.0-20230830164424-d684fdeb6f29 h1:+kige/h8/LnzWgPjB5NUIHz/pWiW/lFpqcTUkN5uulY= github.com/rancher/lasso v0.0.0-20230830164424-d684fdeb6f29/go.mod h1:kgk9kJVMj9FIrrXU0iyM6u/9Je4bEjPImqswkTVaKsQ= +github.com/rancher/lasso v0.0.0-20240123150939-7055397d6dfa h1:eRhvQJjIpPxJunlS3e1J3qTghUy9MIrMjQa2aXYSC3k= +github.com/rancher/lasso v0.0.0-20240123150939-7055397d6dfa/go.mod h1:utdskbIL7kdVvPCUFPEJQDWJwPHGFpUCRfVkX2G2Xxg= github.com/rancher/wrangler v1.1.1 h1:wmqUwqc2M7ADfXnBCJTFkTB5ZREWpD78rnZMzmxwMvM= github.com/rancher/wrangler v1.1.1/go.mod h1:ioVbKupzcBOdzsl55MvEDN0R1wdGggj8iNCYGFI5JvM= github.com/rancher/wrangler v1.1.2 h1:oXbXo9k7y/H4drUpb4RM1c++vT9O3rpoNEfyusGykiU= @@ -2223,6 +2227,8 @@ k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/ k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk= k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ= +k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= modernc.org/cc/v3 v3.36.0/go.mod h1:NFUHyPn4ekoC/JHeZFfZurN6ixxawE1BnVonP/oahEI= diff --git a/vendor/github.com/longhorn/backupstore/Dockerfile.dapper b/vendor/github.com/longhorn/backupstore/Dockerfile.dapper index 8a28f8ab5c..ee79381e69 100644 --- a/vendor/github.com/longhorn/backupstore/Dockerfile.dapper +++ b/vendor/github.com/longhorn/backupstore/Dockerfile.dapper @@ -18,13 +18,13 @@ RUN wget -O - ${!DOCKER_URL} > /usr/bin/docker && chmod +x /usr/bin/docker ENV GOLANG_ARCH_amd64=amd64 GOLANG_ARCH_arm=armv6l GOLANG_ARCH=GOLANG_ARCH_${ARCH} \ GOPATH=/go PATH=/go/bin:/usr/local/go/bin:${PATH} SHELL=/bin/bash -RUN wget -O - https://storage.googleapis.com/golang/go1.17.10.linux-${!GOLANG_ARCH}.tar.gz | tar -xzf - -C /usr/local && \ - go get -u golang.org/x/lint/golint +RUN wget -O - https://storage.googleapis.com/golang/go1.17.10.linux-${!GOLANG_ARCH}.tar.gz | tar -xzf - -C /usr/local +RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2 ENV DAPPER_SOURCE /go/src/github.com/longhorn/backupstore ENV DAPPER_OUTPUT ./bin ENV DAPPER_DOCKER_SOCKET true -ENV DAPPER_ENV IMAGE REPO VERSION TAG +ENV DAPPER_ENV IMAGE REPO VERSION TAG DRONE_REPO DRONE_PULL_REQUEST DRONE_COMMIT_REF ENV DAPPER_RUN_ARGS --privileged --tmpfs /go/src/github.com/longhorn/longhorn/integration/.venv:exec --tmpfs /go/src/github.com/longhorn/longhorn/integration/.tox:exec -v /dev:/host/dev ENV HOME ${DAPPER_SOURCE} WORKDIR ${DAPPER_SOURCE} diff --git a/vendor/github.com/longhorn/backupstore/backupstore.go b/vendor/github.com/longhorn/backupstore/backupstore.go index 174b037c2d..9e79e8c8e8 100644 --- a/vendor/github.com/longhorn/backupstore/backupstore.go +++ b/vendor/github.com/longhorn/backupstore/backupstore.go @@ -22,7 +22,7 @@ type Volume struct { BackingImageChecksum string `json:",string"` CompressionMethod string `json:",string"` StorageClassName string `json:",string"` - BackendStoreDriver string `json:",string"` + DataEngine string `json:",string"` } type Snapshot struct { diff --git a/vendor/github.com/longhorn/backupstore/config.go b/vendor/github.com/longhorn/backupstore/config.go index 3694922fab..a996ba0dbc 100644 --- a/vendor/github.com/longhorn/backupstore/config.go +++ b/vendor/github.com/longhorn/backupstore/config.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "path/filepath" + "runtime" "strings" "time" @@ -139,6 +140,27 @@ func volumeExists(driver BackupStoreDriver, volumeName string) bool { return driver.FileExists(getVolumeFilePath(volumeName)) } +// volumeFolderExists checks if volume folder exists on backupstore +// by listing all the backup volume name based on the folders on the backupstore +// since s3 does not support checking folder exist. +func volumeFolderExists(driver BackupStoreDriver, volumeName string) (bool, error) { + jobQueues := workerpool.New(runtime.NumCPU() * 16) + defer jobQueues.StopWait() + + volumeNames, err := getVolumeNames(jobQueues, driver) + if err != nil { + return false, err + } + + for _, name := range volumeNames { + if volumeName == name { + return true, nil + } + } + + return false, nil +} + func getVolumePath(volumeName string) string { checksum := util.GetChecksum([]byte(volumeName)) volumeLayer1 := checksum[0:VOLUME_SEPARATE_LAYER1] @@ -269,8 +291,8 @@ func loadVolume(driver BackupStoreDriver, volumeName string) (*Volume, error) { log.Infof("Falling back compression method to %v for volume %v", LEGACY_COMPRESSION_METHOD, v.Name) v.CompressionMethod = LEGACY_COMPRESSION_METHOD } - if v.BackendStoreDriver == "" { - v.BackendStoreDriver = string(BackendStoreDriverV1) + if v.DataEngine == "" { + v.DataEngine = string(DataEngineV1) } return v, nil } diff --git a/vendor/github.com/longhorn/backupstore/deltablock.go b/vendor/github.com/longhorn/backupstore/deltablock.go index c9f4d5d644..73851e53b5 100644 --- a/vendor/github.com/longhorn/backupstore/deltablock.go +++ b/vendor/github.com/longhorn/backupstore/deltablock.go @@ -168,7 +168,7 @@ func CreateDeltaBlockBackup(backupName string, config *DeltaBackupConfig) (isInc } config.Volume.CompressionMethod = volume.CompressionMethod - config.Volume.BackendStoreDriver = volume.BackendStoreDriver + config.Volume.DataEngine = volume.DataEngine if err := deltaOps.OpenSnapshot(snapshot.Name, volume.Name); err != nil { return false, err @@ -553,7 +553,7 @@ func performBackup(bsDriver BackupStoreDriver, config *DeltaBackupConfig, delta volume.BackingImageChecksum = config.Volume.BackingImageChecksum volume.CompressionMethod = config.Volume.CompressionMethod volume.StorageClassName = config.Volume.StorageClassName - volume.BackendStoreDriver = config.Volume.BackendStoreDriver + volume.DataEngine = config.Volume.DataEngine if err := saveVolume(bsDriver, volume); err != nil { return progress.progress, "", err @@ -607,7 +607,7 @@ func mergeSnapshotMap(deltaBackup, lastBackup *Backup) *Backup { } // RestoreDeltaBlockBackup restores a delta block backup for the given configuration -func RestoreDeltaBlockBackup(config *DeltaRestoreConfig) error { +func RestoreDeltaBlockBackup(ctx context.Context, config *DeltaRestoreConfig) error { if config == nil { return fmt.Errorf("invalid empty config for restore") } @@ -686,7 +686,8 @@ func RestoreDeltaBlockBackup(config *DeltaRestoreConfig) error { if err := lock.Lock(); err != nil { return err } - go func() { + + go func(ctx context.Context) { var err error currentProgress := 0 @@ -713,9 +714,6 @@ func RestoreDeltaBlockBackup(config *DeltaRestoreConfig) error { } } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - blockChan, errChan := populateBlocksForFullRestore(bsDriver, backup) errorChans := []<-chan error{errChan} @@ -731,7 +729,7 @@ func RestoreDeltaBlockBackup(config *DeltaRestoreConfig) error { return } currentProgress = PROGRESS_PERCENTAGE_BACKUP_TOTAL - }() + }(ctx) return nil } @@ -754,7 +752,7 @@ func restoreBlockToFile(bsDriver BackupStoreDriver, volumeName string, volDev *o return err } -func RestoreDeltaBlockBackupIncrementally(config *DeltaRestoreConfig) error { +func RestoreDeltaBlockBackupIncrementally(ctx context.Context, config *DeltaRestoreConfig) error { if config == nil { return fmt.Errorf("invalid empty config for restore") } @@ -866,7 +864,7 @@ func RestoreDeltaBlockBackupIncrementally(config *DeltaRestoreConfig) error { } } - if err := performIncrementalRestore(bsDriver, config, srcVolumeName, volDevName, lastBackup, backup); err != nil { + if err := performIncrementalRestore(ctx, bsDriver, config, srcVolumeName, volDevName, lastBackup, backup); err != nil { deltaOps.UpdateRestoreStatus(volDevName, 0, err) return } @@ -998,11 +996,10 @@ func restoreBlocks(ctx context.Context, bsDriver BackupStoreDriver, deltaOps Del for { select { case <-ctx.Done(): - logrus.Infof("Closing goroutine for restoring blocks for volume %v", volumeName) + err = fmt.Errorf(types.ErrorMsgRestoreCancelled+" since server stop for volume %v", volumeName) return case <-deltaOps.GetStopChan(): - logrus.Infof("Closing goroutine for restoring blocks for %v since received stop signal", volumeName) - err = fmt.Errorf("restoration is cancelled since received stop signal") + err = fmt.Errorf(types.ErrorMsgRestoreCancelled+" since received stop signal for volume %v", volumeName) return case block, open := <-in: if !open { @@ -1020,7 +1017,7 @@ func restoreBlocks(ctx context.Context, bsDriver BackupStoreDriver, deltaOps Del return errChan } -func performIncrementalRestore(bsDriver BackupStoreDriver, config *DeltaRestoreConfig, +func performIncrementalRestore(ctx context.Context, bsDriver BackupStoreDriver, config *DeltaRestoreConfig, srcVolumeName, volDevName string, lastBackup *Backup, backup *Backup) error { var err error concurrentLimit := config.ConcurrentLimit @@ -1029,9 +1026,6 @@ func performIncrementalRestore(bsDriver BackupStoreDriver, config *DeltaRestoreC totalBlockCounts: int64(len(backup.Blocks) + len(lastBackup.Blocks)), } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - blockChan, errChan := populateBlocksForIncrementalRestore(bsDriver, lastBackup, backup) errorChans := []<-chan error{errChan} @@ -1057,6 +1051,17 @@ func DeleteBackupVolume(volumeName string, destURL string) error { if err != nil { return err } + + backupVolumeFolderExists, err := volumeFolderExists(bsDriver, volumeName) + if err != nil { + return err + } + + // No need to lock and remove volume if it does not exist. + if !backupVolumeFolderExists { + return nil + } + lock, err := New(bsDriver, volumeName, DELETION_LOCK) if err != nil { return err diff --git a/vendor/github.com/longhorn/backupstore/inspect.go b/vendor/github.com/longhorn/backupstore/inspect.go index 4bddac84f9..590709aa54 100644 --- a/vendor/github.com/longhorn/backupstore/inspect.go +++ b/vendor/github.com/longhorn/backupstore/inspect.go @@ -76,7 +76,7 @@ func fillVolumeInfo(volume *Volume) *VolumeInfo { BackingImageName: volume.BackingImageName, BackingImageChecksum: volume.BackingImageChecksum, StorageClassname: volume.StorageClassName, - BackendStoreDriver: volume.BackendStoreDriver, + DataEngine: volume.DataEngine, } } diff --git a/vendor/github.com/longhorn/backupstore/list.go b/vendor/github.com/longhorn/backupstore/list.go index 10ba292895..c61967fc18 100644 --- a/vendor/github.com/longhorn/backupstore/list.go +++ b/vendor/github.com/longhorn/backupstore/list.go @@ -28,7 +28,7 @@ type VolumeInfo struct { BackingImageName string BackingImageChecksum string StorageClassname string - BackendStoreDriver string + DataEngine string } type BackupInfo struct { diff --git a/vendor/github.com/longhorn/backupstore/renovate.json b/vendor/github.com/longhorn/backupstore/renovate.json new file mode 100644 index 0000000000..c297cdcff1 --- /dev/null +++ b/vendor/github.com/longhorn/backupstore/renovate.json @@ -0,0 +1,3 @@ +{ + "extends": ["github>longhorn/release:renovate-default"] +} diff --git a/vendor/github.com/longhorn/backupstore/types.go b/vendor/github.com/longhorn/backupstore/types.go index 023f48d284..1751804d4f 100644 --- a/vendor/github.com/longhorn/backupstore/types.go +++ b/vendor/github.com/longhorn/backupstore/types.go @@ -13,9 +13,9 @@ const ( PROGRESS_PERCENTAGE_BACKUP_TOTAL = 100 ) -type BackendStoreDriver string +type DataEngine string const ( - BackendStoreDriverV1 = BackendStoreDriver("v1") - BackendStoreDriverV2 = BackendStoreDriver("v2") + DataEngineV1 = DataEngine("v1") + DataEngineV2 = DataEngine("v2") ) diff --git a/vendor/github.com/longhorn/backupstore/types/types.go b/vendor/github.com/longhorn/backupstore/types/types.go index c2f8e87955..bc626cc5f4 100644 --- a/vendor/github.com/longhorn/backupstore/types/types.go +++ b/vendor/github.com/longhorn/backupstore/types/types.go @@ -3,9 +3,11 @@ package types type ProgressState string const ( + ProgressStateUndefined = ProgressState("") ProgressStateInProgress = ProgressState("in_progress") ProgressStateComplete = ProgressState("complete") ProgressStateError = ProgressState("error") + ProgressStateCanceled = ProgressState("canceled") ) const ( @@ -49,3 +51,17 @@ type JobResult struct { Payload interface{} Err error } + +const ( + // This is used for all the BackingImages since they all share the same block pool. + // For lock mechanism, please refer to: https://github.com/longhorn/longhorn/blob/master/enhancements/20200701-backupstore-file-locks.md#proposal + // Currently the lock file is stored in each BackupVolume folder. + // For BackingImage Lock it is also stored there with the folder name "BACKINGIMAGE" defined here. + // To prevent Longhorn from accidently considering it as another normal BackupVolume, + // we use upppercase here so it will be filtered out when listing. + BackupBackingImageLockName = "BACKINGIMAGE" +) + +const ( + ErrorMsgRestoreCancelled = "backup restoration is cancelled" +) diff --git a/vendor/k8s.io/utils/integer/integer.go b/vendor/k8s.io/utils/integer/integer.go index e4e740cad4..e0811e8344 100644 --- a/vendor/k8s.io/utils/integer/integer.go +++ b/vendor/k8s.io/utils/integer/integer.go @@ -16,6 +16,8 @@ limitations under the License. package integer +import "math" + // IntMax returns the maximum of the params func IntMax(a, b int) int { if b > a { @@ -65,9 +67,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/pointer/pointer.go b/vendor/k8s.io/utils/pointer/pointer.go index b8103223ad..b673a64257 100644 --- a/vendor/k8s.io/utils/pointer/pointer.go +++ b/vendor/k8s.io/utils/pointer/pointer.go @@ -14,12 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Deprecated: Use functions in k8s.io/utils/ptr instead: ptr.To to obtain +// a pointer, ptr.Deref to dereference a pointer, ptr.Equal to compare +// dereferenced pointers. package pointer import ( - "fmt" - "reflect" "time" + + "k8s.io/utils/ptr" ) // AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when, @@ -28,383 +31,219 @@ import ( // // 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 -} +// +// Deprecated: Use ptr.AllPtrFieldsNil instead. +var AllPtrFieldsNil = ptr.AllPtrFieldsNil + +// Int returns a pointer to an int. +var Int = ptr.To[int] // IntPtr is a function variable referring to Int. // -// Deprecated: Use Int instead. +// Deprecated: Use ptr.To 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 -} +var IntDeref = ptr.Deref[int] // IntPtrDerefOr is a function variable referring to IntDeref. // -// Deprecated: Use IntDeref instead. +// Deprecated: Use ptr.Deref instead. var IntPtrDerefOr = IntDeref // for back-compat // Int32 returns a pointer to an int32. -func Int32(i int32) *int32 { - return &i -} +var Int32 = ptr.To[int32] // Int32Ptr is a function variable referring to Int32. // -// Deprecated: Use Int32 instead. +// Deprecated: Use ptr.To 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 -} +var Int32Deref = ptr.Deref[int32] // Int32PtrDerefOr is a function variable referring to Int32Deref. // -// Deprecated: Use Int32Deref instead. +// Deprecated: Use ptr.Deref 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 -} +var Int32Equal = ptr.Equal[int32] // Uint returns a pointer to an uint -func Uint(i uint) *uint { - return &i -} +var Uint = ptr.To[uint] // UintPtr is a function variable referring to Uint. // -// Deprecated: Use Uint instead. +// Deprecated: Use ptr.To 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 -} +var UintDeref = ptr.Deref[uint] // UintPtrDerefOr is a function variable referring to UintDeref. // -// Deprecated: Use UintDeref instead. +// Deprecated: Use ptr.Deref instead. var UintPtrDerefOr = UintDeref // for back-compat // Uint32 returns a pointer to an uint32. -func Uint32(i uint32) *uint32 { - return &i -} +var Uint32 = ptr.To[uint32] // Uint32Ptr is a function variable referring to Uint32. // -// Deprecated: Use Uint32 instead. +// Deprecated: Use ptr.To 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 -} +var Uint32Deref = ptr.Deref[uint32] // Uint32PtrDerefOr is a function variable referring to Uint32Deref. // -// Deprecated: Use Uint32Deref instead. +// Deprecated: Use ptr.Deref 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 -} +var Uint32Equal = ptr.Equal[uint32] // Int64 returns a pointer to an int64. -func Int64(i int64) *int64 { - return &i -} +var Int64 = ptr.To[int64] // Int64Ptr is a function variable referring to Int64. // -// Deprecated: Use Int64 instead. +// Deprecated: Use ptr.To 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 -} +var Int64Deref = ptr.Deref[int64] // Int64PtrDerefOr is a function variable referring to Int64Deref. // -// Deprecated: Use Int64Deref instead. +// Deprecated: Use ptr.Deref 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 -} +var Int64Equal = ptr.Equal[int64] // Uint64 returns a pointer to an uint64. -func Uint64(i uint64) *uint64 { - return &i -} +var Uint64 = ptr.To[uint64] // Uint64Ptr is a function variable referring to Uint64. // -// Deprecated: Use Uint64 instead. +// Deprecated: Use ptr.To 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 -} +var Uint64Deref = ptr.Deref[uint64] // Uint64PtrDerefOr is a function variable referring to Uint64Deref. // -// Deprecated: Use Uint64Deref instead. +// Deprecated: Use ptr.Deref 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 -} +var Uint64Equal = ptr.Equal[uint64] // Bool returns a pointer to a bool. -func Bool(b bool) *bool { - return &b -} +var Bool = ptr.To[bool] // BoolPtr is a function variable referring to Bool. // -// Deprecated: Use Bool instead. +// Deprecated: Use ptr.To 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 -} +var BoolDeref = ptr.Deref[bool] // BoolPtrDerefOr is a function variable referring to BoolDeref. // -// Deprecated: Use BoolDeref instead. +// Deprecated: Use ptr.Deref 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 -} +var BoolEqual = ptr.Equal[bool] // String returns a pointer to a string. -func String(s string) *string { - return &s -} +var String = ptr.To[string] // StringPtr is a function variable referring to String. // -// Deprecated: Use String instead. +// Deprecated: Use ptr.To 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 -} +var StringDeref = ptr.Deref[string] // StringPtrDerefOr is a function variable referring to StringDeref. // -// Deprecated: Use StringDeref instead. +// Deprecated: Use ptr.Deref 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 -} +var StringEqual = ptr.Equal[string] // Float32 returns a pointer to a float32. -func Float32(i float32) *float32 { - return &i -} +var Float32 = ptr.To[float32] // Float32Ptr is a function variable referring to Float32. // -// Deprecated: Use Float32 instead. +// Deprecated: Use ptr.To 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 -} +var Float32Deref = ptr.Deref[float32] // Float32PtrDerefOr is a function variable referring to Float32Deref. // -// Deprecated: Use Float32Deref instead. +// Deprecated: Use ptr.Deref 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 -} +var Float32Equal = ptr.Equal[float32] // Float64 returns a pointer to a float64. -func Float64(i float64) *float64 { - return &i -} +var Float64 = ptr.To[float64] // Float64Ptr is a function variable referring to Float64. // -// Deprecated: Use Float64 instead. +// Deprecated: Use ptr.To 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 -} +var Float64Deref = ptr.Deref[float64] // Float64PtrDerefOr is a function variable referring to Float64Deref. // -// Deprecated: Use Float64Deref instead. +// Deprecated: Use ptr.Deref 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 -} +var Float64Equal = ptr.Equal[float64] // Duration returns a pointer to a time.Duration. -func Duration(d time.Duration) *time.Duration { - return &d -} +var Duration = ptr.To[time.Duration] // 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 -} +var DurationDeref = ptr.Deref[time.Duration] // 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 -} +var DurationEqual = ptr.Equal[time.Duration] diff --git a/vendor/k8s.io/utils/ptr/OWNERS b/vendor/k8s.io/utils/ptr/OWNERS new file mode 100644 index 0000000000..0d6392752a --- /dev/null +++ b/vendor/k8s.io/utils/ptr/OWNERS @@ -0,0 +1,10 @@ +# See the OWNERS docs at https://go.k8s.io/owners + +approvers: +- apelisse +- stewart-yu +- thockin +reviewers: +- apelisse +- stewart-yu +- thockin diff --git a/vendor/k8s.io/utils/ptr/README.md b/vendor/k8s.io/utils/ptr/README.md new file mode 100644 index 0000000000..2ca8073dc7 --- /dev/null +++ b/vendor/k8s.io/utils/ptr/README.md @@ -0,0 +1,3 @@ +# Pointer + +This package provides some functions for pointer-based operations. diff --git a/vendor/k8s.io/utils/ptr/ptr.go b/vendor/k8s.io/utils/ptr/ptr.go new file mode 100644 index 0000000000..659ed3b9e2 --- /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 b32cc40d3d..6e2b43a05a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -221,7 +221,7 @@ github.com/longhorn/backing-image-manager/pkg/meta github.com/longhorn/backing-image-manager/pkg/rpc github.com/longhorn/backing-image-manager/pkg/types github.com/longhorn/backing-image-manager/pkg/util -# github.com/longhorn/backupstore v0.0.0-20231114103026-af339bb498d3 +# github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77 ## explicit; go 1.21 github.com/longhorn/backupstore github.com/longhorn/backupstore/logging @@ -352,7 +352,7 @@ github.com/rancher/dynamiclistener/storage/memory ## explicit github.com/rancher/go-rancher/api github.com/rancher/go-rancher/client -# github.com/rancher/lasso v0.0.0-20230830164424-d684fdeb6f29 +# github.com/rancher/lasso v0.0.0-20240123150939-7055397d6dfa ## explicit; go 1.20 github.com/rancher/lasso/pkg/cache github.com/rancher/lasso/pkg/client @@ -754,7 +754,7 @@ gopkg.in/yaml.v2 # gopkg.in/yaml.v3 v3.0.1 ## explicit gopkg.in/yaml.v3 -# k8s.io/api v0.28.2 => k8s.io/api v0.28.2 +# k8s.io/api v0.28.6 => k8s.io/api v0.28.2 ## explicit; go 1.20 k8s.io/api/admission/v1 k8s.io/api/admission/v1beta1 @@ -832,7 +832,7 @@ k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/internalint k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1 k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1beta1 k8s.io/apiextensions-apiserver/pkg/features -# k8s.io/apimachinery v0.28.2 => k8s.io/apimachinery v0.28.2 +# k8s.io/apimachinery v0.28.6 => k8s.io/apimachinery v0.28.2 ## explicit; go 1.20 k8s.io/apimachinery/pkg/api/equality k8s.io/apimachinery/pkg/api/errors @@ -1040,7 +1040,7 @@ k8s.io/apiserver/plugin/pkg/authorizer/webhook # k8s.io/cli-runtime v0.28.2 => k8s.io/cli-runtime v0.28.2 ## explicit; go 1.20 k8s.io/cli-runtime/pkg/printers -# k8s.io/client-go v0.28.2 => k8s.io/client-go v0.28.2 +# k8s.io/client-go v0.28.6 => k8s.io/client-go v0.28.2 ## explicit; go 1.20 k8s.io/client-go/applyconfigurations/admissionregistration/v1 k8s.io/client-go/applyconfigurations/admissionregistration/v1alpha1 @@ -1484,7 +1484,7 @@ k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1 # k8s.io/mount-utils v0.28.2 => k8s.io/mount-utils v0.28.2 ## explicit; go 1.20 k8s.io/mount-utils -# k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 +# k8s.io/utils v0.0.0-20240102154912-e7106e64919e ## explicit; go 1.18 k8s.io/utils/buffer k8s.io/utils/clock @@ -1499,6 +1499,7 @@ k8s.io/utils/lru k8s.io/utils/net k8s.io/utils/path k8s.io/utils/pointer +k8s.io/utils/ptr k8s.io/utils/strings/slices k8s.io/utils/trace # sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.1.2