Skip to content

Commit ea5c686

Browse files
committed
Automatically use latest tag for command cache
Signed-off-by: Ling Samuel <[email protected]>
1 parent 199ea34 commit ea5c686

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

pkg/minikube/image/cache.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/klog/v2"
3232
"k8s.io/minikube/pkg/minikube/constants"
3333
"k8s.io/minikube/pkg/minikube/localpath"
34+
"k8s.io/minikube/pkg/util"
3435
"k8s.io/minikube/pkg/util/lock"
3536
)
3637

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

7778
// saveToTarFile caches an image
7879
func saveToTarFile(iname, rawDest string) error {
80+
iname = util.NormalizeImageName(iname)
7981
start := time.Now()
8082
defer func() {
8183
klog.Infof("cache image %q -> %q took %s", iname, rawDest, time.Since(start))

pkg/minikube/machine/cache_images.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func CacheAndLoadImages(images []string) error {
218218
if err != nil {
219219
failed = append(failed, m)
220220
klog.Warningf("Failed to load cached images for profile %s. make sure the profile is running. %v", pName, err)
221+
continue
221222
}
222223
succeeded = append(succeeded, m)
223224
}

pkg/util/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os/user"
2323
"path/filepath"
2424
"strconv"
25+
"strings"
2526

2627
"github.com/blang/semver"
2728
units "github.com/docker/go-units"
@@ -109,3 +110,22 @@ func MaybeChownDirRecursiveToMinikubeUser(dir string) error {
109110
func ParseKubernetesVersion(version string) (semver.Version, error) {
110111
return semver.Make(version[1:])
111112
}
113+
114+
// NormalizeImageName automatically tag latest to image
115+
// Example:
116+
// nginx -> nginx:latest
117+
// localhost:5000/nginx -> localhost:5000/nginx:latest
118+
// localhost:5000/nginx:latest -> localhost:5000/nginx:latest
119+
// docker.io/dotnet/core/sdk -> docker.io/dotnet/core/sdk:latest
120+
func NormalizeImageName(image string) string {
121+
base := image
122+
tag := "latest"
123+
124+
// From google/go-containerregistry/pkg/name/tag.go
125+
parts := strings.Split(strings.TrimSpace(image), ":")
126+
if len(parts) > 1 && !strings.Contains(parts[len(parts)-1], "/") {
127+
base = strings.Join(parts[:len(parts)-1], ":")
128+
tag = parts[len(parts)-1]
129+
}
130+
return base + ":" + tag
131+
}

pkg/util/utils_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,40 @@ func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
194194
})
195195
}
196196
}
197+
198+
func TestNormalizeImageName(t *testing.T) {
199+
cases := []struct {
200+
image string
201+
expected string
202+
}{
203+
{
204+
image: "nginx",
205+
expected: "nginx:latest",
206+
},
207+
{
208+
image: "localhost:5000/nginx",
209+
expected: "localhost:5000/nginx:latest",
210+
},
211+
{
212+
image: "docker.io/nginx",
213+
expected: "docker.io/nginx:latest",
214+
},
215+
{
216+
image: "nginx:3.0",
217+
expected: "nginx:3.0",
218+
},
219+
{
220+
image: "docker.io/dotnet/core/sdk",
221+
expected: "docker.io/dotnet/core/sdk:latest",
222+
},
223+
}
224+
225+
for _, c := range cases {
226+
t.Run(c.image, func(t *testing.T) {
227+
got := NormalizeImageName(c.image)
228+
if got != c.expected {
229+
t.Errorf("Normalize error: expected: %v, got: %v", c.expected, got)
230+
}
231+
})
232+
}
233+
}

0 commit comments

Comments
 (0)