Move locking code to download.go.

pull/11223/head
Andriy Dzikh 2021-04-29 09:30:49 -07:00
parent b57022d2c8
commit 1cc80e1e7b
4 changed files with 19 additions and 18 deletions

View File

@ -23,11 +23,9 @@ import (
"runtime" "runtime"
"github.com/blang/semver" "github.com/blang/semver"
"github.com/juju/mutex"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/util/lock"
) )
// binaryWithChecksumURL gets the location of a Kubernetes binary // binaryWithChecksumURL gets the location of a Kubernetes binary
@ -55,10 +53,9 @@ func Binary(binary, version, osName, archName string) (string, error) {
return "", err return "", err
} }
spec := lock.PathMutexSpec(targetLock) releaser, err := lockDownload(targetLock)
releaser, err := mutex.Acquire(spec)
if err != nil { if err != nil {
return "", errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", targetLock, spec) return "", err
} }
defer releaser.Release() defer releaser.Release()

View File

@ -24,9 +24,11 @@ import (
"strings" "strings"
"github.com/hashicorp/go-getter" "github.com/hashicorp/go-getter"
"github.com/juju/mutex"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/util/lock"
) )
var ( var (
@ -92,3 +94,12 @@ func withinUnitTest() bool {
return flag.Lookup("test.v") != nil || strings.HasSuffix(os.Args[0], "test") 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
}

View File

@ -30,12 +30,10 @@ import (
"github.com/google/go-containerregistry/pkg/v1/daemon" "github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball" "github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/juju/mutex"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/util/lock"
) )
var ( var (
@ -87,10 +85,9 @@ func ImageToCache(img string) error {
f = localpath.SanitizeCacheDir(f) f = localpath.SanitizeCacheDir(f)
fileLock := f + ".lock" fileLock := f + ".lock"
spec := lock.PathMutexSpec(fileLock) releaser, err := lockDownload(fileLock)
releaser, err := mutex.Acquire(spec)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", fileLock, spec) return err
} }
defer releaser.Release() defer releaser.Release()
@ -171,10 +168,9 @@ func ImageToDaemon(img string) error {
fileLock := filepath.Join(constants.KICCacheDir, path.Base(img)+".d.lock") fileLock := filepath.Join(constants.KICCacheDir, path.Base(img)+".d.lock")
fileLock = localpath.SanitizeCacheDir(fileLock) fileLock = localpath.SanitizeCacheDir(fileLock)
spec := lock.PathMutexSpec(fileLock) releaser, err := lockDownload(fileLock)
releaser, err := mutex.Acquire(spec)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", fileLock, spec) return err
} }
defer releaser.Release() defer releaser.Release()

View File

@ -30,14 +30,12 @@ import (
"cloud.google.com/go/storage" "cloud.google.com/go/storage"
"google.golang.org/api/option" "google.golang.org/api/option"
"github.com/juju/mutex"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/viper" "github.com/spf13/viper"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/style" "k8s.io/minikube/pkg/minikube/style"
"k8s.io/minikube/pkg/util/lock"
) )
const ( const (
@ -133,10 +131,9 @@ func Preload(k8sVersion, containerRuntime string) error {
targetPath := TarballPath(k8sVersion, containerRuntime) targetPath := TarballPath(k8sVersion, containerRuntime)
targetLock := targetPath + ".lock" targetLock := targetPath + ".lock"
spec := lock.PathMutexSpec(targetLock) releaser, err := lockDownload(targetLock)
releaser, err := mutex.Acquire(spec)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to acquire lock \"%s\": %+v", targetLock, spec) return err
} }
defer releaser.Release() defer releaser.Release()