Skip to content

Commit

Permalink
Automatically use latest tag for command cache
Browse files Browse the repository at this point in the history
Signed-off-by: Ling Samuel <[email protected]>
  • Loading branch information
lingsamuel committed Dec 29, 2020
1 parent 199ea34 commit ea5c686
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/minikube/image/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/util"
"k8s.io/minikube/pkg/util/lock"
)

Expand Down Expand Up @@ -76,6 +77,7 @@ func SaveToDir(images []string, cacheDir string) error {

// saveToTarFile caches an image
func saveToTarFile(iname, rawDest string) error {
iname = util.NormalizeImageName(iname)
start := time.Now()
defer func() {
klog.Infof("cache image %q -> %q took %s", iname, rawDest, time.Since(start))
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/machine/cache_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func CacheAndLoadImages(images []string) error {
if err != nil {
failed = append(failed, m)
klog.Warningf("Failed to load cached images for profile %s. make sure the profile is running. %v", pName, err)
continue
}
succeeded = append(succeeded, m)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/user"
"path/filepath"
"strconv"
"strings"

"github.com/blang/semver"
units "github.com/docker/go-units"
Expand Down Expand Up @@ -109,3 +110,22 @@ func MaybeChownDirRecursiveToMinikubeUser(dir string) error {
func ParseKubernetesVersion(version string) (semver.Version, error) {
return semver.Make(version[1:])
}

// NormalizeImageName automatically tag latest to image
// Example:
// nginx -> nginx:latest
// localhost:5000/nginx -> localhost:5000/nginx:latest
// localhost:5000/nginx:latest -> localhost:5000/nginx:latest
// docker.io/dotnet/core/sdk -> docker.io/dotnet/core/sdk:latest
func NormalizeImageName(image string) string {
base := image
tag := "latest"

// From google/go-containerregistry/pkg/name/tag.go
parts := strings.Split(strings.TrimSpace(image), ":")
if len(parts) > 1 && !strings.Contains(parts[len(parts)-1], "/") {
base = strings.Join(parts[:len(parts)-1], ":")
tag = parts[len(parts)-1]
}
return base + ":" + tag
}
37 changes: 37 additions & 0 deletions pkg/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,40 @@ func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
})
}
}

func TestNormalizeImageName(t *testing.T) {
cases := []struct {
image string
expected string
}{
{
image: "nginx",
expected: "nginx:latest",
},
{
image: "localhost:5000/nginx",
expected: "localhost:5000/nginx:latest",
},
{
image: "docker.io/nginx",
expected: "docker.io/nginx:latest",
},
{
image: "nginx:3.0",
expected: "nginx:3.0",
},
{
image: "docker.io/dotnet/core/sdk",
expected: "docker.io/dotnet/core/sdk:latest",
},
}

for _, c := range cases {
t.Run(c.image, func(t *testing.T) {
got := NormalizeImageName(c.image)
if got != c.expected {
t.Errorf("Normalize error: expected: %v, got: %v", c.expected, got)
}
})
}
}

0 comments on commit ea5c686

Please sign in to comment.