Skip to content

Commit

Permalink
Fix error message on invalid scheme-prefixed ref
Browse files Browse the repository at this point in the history
Adds a descriptive error when an invalid image ref containing a scheme
is given to the ImageRepository.

Fixes #146

Signed-off-by: Aurel Canciu <aurelcanciu@gmail.com>
  • Loading branch information
relu committed Oct 25, 2021
1 parent 931513a commit 7acdbb2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions controllers/imagerepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func (r *ImageRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ

ref, err := name.ParseReference(imageRepo.Spec.Image)
if err != nil {
if u, _ := url.Parse(imageRepo.Spec.Image); u != nil && u.Scheme != "" {
err = fmt.Errorf("image repository attribute should not be scheme-prefixed ('%s://')", u.Scheme)
}
imagev1.SetImageRepositoryReadiness(
&imageRepo,
metav1.ConditionFalse,
Expand Down
36 changes: 36 additions & 0 deletions controllers/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

Expand Down Expand Up @@ -275,6 +276,41 @@ var _ = Describe("ImageRepository controller", func() {
Expect(repo.Status.CanonicalImageName).To(Equal(imgRepo))
Expect(repo.Status.LastScanResult.TagCount).To(Equal(len(versions)))
})
})

Context("ImageRepository image attribute is invalid", func() {
It("if fails with an error when prefixed with a scheme", func() {
imgRepo := "https://" + loadImages(registryServer, "test-fetch", []string{"1.0.0"})

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
err := r.Create(ctx, &repo)
Expect(err).ToNot(HaveOccurred())

var ready *metav1.Condition
Eventually(func() bool {
_ = r.Get(ctx, objectName, &repo)
ready = apimeta.FindStatusCondition(*repo.GetStatusConditions(), meta.ReadyCondition)
fmt.Printf("%v", ready)
return ready != nil && ready.Reason == imagev1.ImageURLInvalidReason
}, timeout, interval).Should(BeTrue())
Expect(ready.Message).To(ContainSubstring("should not be scheme-prefixed"))
})
})
})

0 comments on commit 7acdbb2

Please sign in to comment.