diff --git a/controllers/imagerepository_controller.go b/controllers/imagerepository_controller.go index df3e5038..63d1e969 100644 --- a/controllers/imagerepository_controller.go +++ b/controllers/imagerepository_controller.go @@ -142,8 +142,8 @@ func (r *ImageRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ var err error var ref name.Reference - if u, _ := url.Parse(imageRepo.Spec.Image); u != nil && u.Scheme != "" { - err = fmt.Errorf(".spec.image value should not start with URL scheme; remove '%s://'", u.Scheme) + if s := strings.Split(imageRepo.Spec.Image, "://"); len(s) > 1 { + err = fmt.Errorf(".spec.image value should not start with URL scheme; remove '%s://'", s[0]) } else { ref, err = name.ParseReference(imageRepo.Spec.Image) } diff --git a/controllers/scan_test.go b/controllers/scan_test.go index ddf57669..48e30e95 100644 --- a/controllers/scan_test.go +++ b/controllers/scan_test.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "net/http/httptest" + "strings" "github.com/fluxcd/pkg/apis/meta" "github.com/google/go-containerregistry/pkg/authn" @@ -311,5 +312,35 @@ var _ = Describe("ImageRepository controller", func() { }, timeout, interval).Should(BeTrue()) Expect(ready.Message).To(ContainSubstring("should not start with URL scheme")) }) + It("does not fail if using a hostname with a port number", func() { + imgRepo := loadImages(registryServer, "test-fetch", []string{"1.0.0"}) + imgRepo = strings.ReplaceAll(imgRepo, "127.0.0.1", "localhost") + + repo = imagev1.ImageRepository{ + Spec: imagev1.ImageRepositorySpec{ + Interval: metav1.Duration{Duration: reconciliationInterval}, + Image: imgRepo, + }, + } + objectName := types.NamespacedName{ + Name: "random", + Namespace: "default", + } + + repo.Name = objectName.Name + repo.Namespace = objectName.Namespace + + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) + defer cancel() + + r := imageRepoReconciler + Expect(r.Create(ctx, &repo)).To(Succeed()) + + Eventually(func() bool { + err := r.Get(context.Background(), objectName, &repo) + return err == nil && repo.Status.LastScanResult != nil + }, timeout, interval).Should(BeTrue()) + Expect(repo.Status.CanonicalImageName).To(Equal(imgRepo)) + }) }) })