From 1cc80e1e7be9503a9d7007c0c00ce68d90ffbf17 Mon Sep 17 00:00:00 2001 From: Andriy Dzikh Date: Thu, 29 Apr 2021 09:30:49 -0700 Subject: [PATCH] Move locking code to download.go. --- pkg/minikube/download/binary.go | 7 ++----- pkg/minikube/download/download.go | 11 +++++++++++ pkg/minikube/download/image.go | 12 ++++-------- pkg/minikube/download/preload.go | 7 ++----- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pkg/minikube/download/binary.go b/pkg/minikube/download/binary.go index c466c2a0bd..ddd81ee952 100644 --- a/pkg/minikube/download/binary.go +++ b/pkg/minikube/download/binary.go @@ -23,11 +23,9 @@ import ( "runtime" "github.com/blang/semver" - "github.com/juju/mutex" "github.com/pkg/errors" "k8s.io/klog/v2" "k8s.io/minikube/pkg/minikube/localpath" - "k8s.io/minikube/pkg/util/lock" ) // binaryWithChecksumURL gets the location of a Kubernetes binary @@ -55,10 +53,9 @@ func Binary(binary, version, osName, archName string) (string, error) { return "", err } - spec := lock.PathMutexSpec(targetLock) - releaser, err := mutex.Acquire(spec) + releaser, err := lockDownload(targetLock) if err != nil { - return "", errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", targetLock, spec) + return "", err } defer releaser.Release() diff --git a/pkg/minikube/download/download.go b/pkg/minikube/download/download.go index eb3cf3bf48..b606f4539a 100644 --- a/pkg/minikube/download/download.go +++ b/pkg/minikube/download/download.go @@ -24,9 +24,11 @@ import ( "strings" "github.com/hashicorp/go-getter" + "github.com/juju/mutex" "github.com/pkg/errors" "k8s.io/klog/v2" "k8s.io/minikube/pkg/minikube/out" + "k8s.io/minikube/pkg/util/lock" ) var ( @@ -92,3 +94,12 @@ func withinUnitTest() bool { return flag.Lookup("test.v") != nil || strings.HasSuffix(os.Args[0], "test") } + +func lockDownload(file string) (mutex.Releaser, error) { + spec := lock.PathMutexSpec(file) + releaser, err := mutex.Acquire(spec) + if err != nil { + return nil, errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", file, spec) + } + return releaser, err +} diff --git a/pkg/minikube/download/image.go b/pkg/minikube/download/image.go index 19b231ba18..d44ddce724 100644 --- a/pkg/minikube/download/image.go +++ b/pkg/minikube/download/image.go @@ -30,12 +30,10 @@ import ( "github.com/google/go-containerregistry/pkg/v1/daemon" "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/tarball" - "github.com/juju/mutex" "github.com/pkg/errors" "k8s.io/klog/v2" "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/localpath" - "k8s.io/minikube/pkg/util/lock" ) var ( @@ -87,10 +85,9 @@ func ImageToCache(img string) error { f = localpath.SanitizeCacheDir(f) fileLock := f + ".lock" - spec := lock.PathMutexSpec(fileLock) - releaser, err := mutex.Acquire(spec) + releaser, err := lockDownload(fileLock) if err != nil { - return errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", fileLock, spec) + return err } defer releaser.Release() @@ -171,10 +168,9 @@ func ImageToDaemon(img string) error { fileLock := filepath.Join(constants.KICCacheDir, path.Base(img)+".d.lock") fileLock = localpath.SanitizeCacheDir(fileLock) - spec := lock.PathMutexSpec(fileLock) - releaser, err := mutex.Acquire(spec) + releaser, err := lockDownload(fileLock) if err != nil { - return errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", fileLock, spec) + return err } defer releaser.Release() diff --git a/pkg/minikube/download/preload.go b/pkg/minikube/download/preload.go index 0f279c022f..f9aaf17d88 100644 --- a/pkg/minikube/download/preload.go +++ b/pkg/minikube/download/preload.go @@ -30,14 +30,12 @@ import ( "cloud.google.com/go/storage" "google.golang.org/api/option" - "github.com/juju/mutex" "github.com/pkg/errors" "github.com/spf13/viper" "k8s.io/klog/v2" "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/minikube/style" - "k8s.io/minikube/pkg/util/lock" ) const ( @@ -133,10 +131,9 @@ func Preload(k8sVersion, containerRuntime string) error { targetPath := TarballPath(k8sVersion, containerRuntime) targetLock := targetPath + ".lock" - spec := lock.PathMutexSpec(targetLock) - releaser, err := mutex.Acquire(spec) + releaser, err := lockDownload(targetLock) if err != nil { - return errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", targetLock, spec) + return err } defer releaser.Release()