Skip to content

Commit

Permalink
Fix schema validation check when using host:port
Browse files Browse the repository at this point in the history
The validation check for the presence of a schema did not take into
account situations when the registry hostname would be accompanied by a
port number. In this situation the hostname would be erroneously parsed
as a schema and validation would fail.

Fixes #220

Signed-off-by: Aurel Canciu <[email protected]>
  • Loading branch information
relu committed Jan 18, 2022
1 parent 3a2683b commit d424c8c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions controllers/imagerepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
31 changes: 31 additions & 0 deletions controllers/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http/httptest"
"strings"

"github.com/fluxcd/pkg/apis/meta"
"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -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))
})
})
})

0 comments on commit d424c8c

Please sign in to comment.