Skip to content

Commit a642c85

Browse files
Fix CRI-O image name resolution for localhost images
Configure CRI-O registries.conf to resolve unqualified image names to localhost first, then docker.io. This fixes InvalidImageName errors when deploying locally built images with podman + CRI-O runtime. Fixes #21251
1 parent 26afd94 commit a642c85

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pkg/minikube/cruntime/crio.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ func generateCRIOConfig(cr CommandRunner, imageRepository string, kv semver.Vers
8989
klog.Warningf("unable to remove /etc/cni/net.mk directory: %v", err)
9090
}
9191

92+
// configure registries for proper local image resolution (fixes #21251)
93+
// this ensures CRI-O can resolve locally built images (stored with localhost/ prefix)
94+
// while maintaining compatibility with Docker runtime behavior
95+
if err := configureRegistries(cr); err != nil {
96+
return errors.Wrap(err, "configuring registries")
97+
}
98+
9299
// add 'net.ipv4.ip_unprivileged_port_start=0' sysctl so that containers that run with non-root user can bind to otherwise privilege ports (like coredns v1.11.0+)
93100
// note: 'net.ipv4.ip_unprivileged_port_start' sysctl was marked as safe since Kubernetes v1.22 (Aug 4, 2021) (ref: https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.22.md#feature-9)
94101
// note: cri-o supports 'default_sysctls' option since v1.12.0 (Oct 19, 2018) (ref: https://github.com/cri-o/cri-o/releases/tag/v1.12.0; https://github.com/cri-o/cri-o/pull/1721)
@@ -519,3 +526,40 @@ func crioImagesPreloaded(runner command.Runner, imgs []string) bool {
519526
func (r *CRIO) ImagesPreloaded(imgs []string) bool {
520527
return crioImagesPreloaded(r.Runner, imgs)
521528
}
529+
530+
// configureRegistries configures CRI-O to properly resolve unqualified image names
531+
// This fixes the issue where locally built images (stored with localhost/ prefix)
532+
// cannot be resolved by CRI-O, causing InvalidImageName errors (issue #21251)
533+
func configureRegistries(cr CommandRunner) error {
534+
registriesPath := "/etc/containers/registries.conf"
535+
536+
// Check if registries.conf exists and create backup
537+
backupCmd := fmt.Sprintf("sudo cp %s %s.bak 2>/dev/null || true", registriesPath, registriesPath)
538+
if _, err := cr.RunCmd(exec.Command("sh", "-c", backupCmd)); err != nil {
539+
klog.Warningf("failed to backup registries.conf: %v", err)
540+
}
541+
542+
// Configure registries.conf with proper unqualified search order
543+
// This ensures locally built images (localhost/) are found before remote registries
544+
registriesConfig := `[registries.search]
545+
registries = ['localhost', 'docker.io']
546+
547+
[registries.insecure]
548+
registries = ['localhost']
549+
550+
# Allow unqualified images to resolve to localhost first, then docker.io
551+
# This fixes CRI-O resolution of locally built podman images
552+
unqualified-search-registries = ['localhost', 'docker.io']
553+
`
554+
555+
// Write the new configuration
556+
writeCmd := fmt.Sprintf("sudo tee %s > /dev/null", registriesPath)
557+
cmd := exec.Command("sh", "-c", writeCmd)
558+
cmd.Stdin = strings.NewReader(registriesConfig)
559+
if _, err := cr.RunCmd(cmd); err != nil {
560+
return errors.Wrap(err, "writing registries configuration")
561+
}
562+
563+
klog.Infof("configured CRI-O registries for localhost image resolution")
564+
return nil
565+
}

0 commit comments

Comments
 (0)