Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix "cache add" command error when not specifying :latest tag #10058

Merged
merged 1 commit into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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