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 30, 2020
1 parent 199ea34 commit b79ab3f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/minikube/image/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func SaveToDir(images []string, cacheDir string) error {

// saveToTarFile caches an image
func saveToTarFile(iname, rawDest string) error {
iname = normalizeTagName(iname)
start := time.Now()
defer func() {
klog.Infof("cache image %q -> %q took %s", iname, rawDest, time.Since(start))
Expand Down
19 changes: 19 additions & 0 deletions pkg/minikube/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,22 @@ func cleanImageCacheDir() error {
})
return err
}

// normalizeTagName 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 normalizeTagName(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
}
45 changes: 45 additions & 0 deletions pkg/minikube/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,48 @@ func TestTag(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: "localhost:5000/nginx:3.0",
expected: "localhost:5000/nginx:3.0",
},
{
image: "localhost:5000/nginx:latest",
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 := normalizeTagName(c.image)
if got != c.expected {
t.Errorf("Normalize error: expected: %v, got: %v", c.expected, got)
}
})
}
}
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

0 comments on commit b79ab3f

Please sign in to comment.