Skip to content

Commit

Permalink
Merge pull request #176 from darkowlzz/policy-semver-range-invalid
Browse files Browse the repository at this point in the history
policy: Handle failure due to invalid semver range
  • Loading branch information
stefanprodan authored Oct 26, 2021
2 parents ef6e775 + 22e5ad1 commit 72bd994
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
12 changes: 12 additions & 0 deletions controllers/imagepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
const (
ImageRepositoryNotReadyReason = "ImageRepositoryNotReady"
AccessDeniedReason = "AccessDenied"
ImagePolicyInvalidReason = "InvalidPolicy"
)

const (
Expand Down Expand Up @@ -154,6 +155,17 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

policer, err := policy.PolicerFromSpec(pol.Spec.Policy)
if err != nil {
msg := fmt.Sprintf("invalid policy: %s", err.Error())
conditions.MarkFalse(
&pol,
meta.ReadyCondition,
ImagePolicyInvalidReason,
msg,
)
log.Error(err, "invalid policy")
return ctrl.Result{}, nil
}

var latest string
if policer != nil {
Expand Down
30 changes: 25 additions & 5 deletions controllers/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestImagePolicyReconciler_calculateImageFromRepoTags(t *testing.T) {
versions []string
policy imagev1.ImagePolicyChoice
wantImageTag string
wantFailure bool
}{
{
name: "using SemVerPolicy",
Expand All @@ -51,6 +52,16 @@ func TestImagePolicyReconciler_calculateImageFromRepoTags(t *testing.T) {
},
wantImageTag: ":1.0.2",
},
{
name: "using SemVerPolicy with invalid range",
versions: []string{"0.1.0", "0.1.1", "0.2.0", "1.0.0", "1.0.1", "1.0.2", "1.1.0-alpha"},
policy: imagev1.ImagePolicyChoice{
SemVer: &imagev1.SemVerPolicy{
Range: "*-*",
},
},
wantFailure: true,
},
{
name: "using AlphabeticalPolicy",
versions: []string{"xenial", "yakkety", "zesty", "artful", "bionic"},
Expand Down Expand Up @@ -113,11 +124,20 @@ func TestImagePolicyReconciler_calculateImageFromRepoTags(t *testing.T) {

g.Expect(testEnv.Create(ctx, &pol)).To(Succeed())

g.Eventually(func() bool {
err := testEnv.Get(ctx, polName, &pol)
return err == nil && pol.Status.LatestImage != ""
}, timeout, interval).Should(BeTrue())
g.Expect(pol.Status.LatestImage).To(Equal(imgRepo + tt.wantImageTag))
if !tt.wantFailure {
g.Eventually(func() bool {
err := testEnv.Get(ctx, polName, &pol)
return err == nil && pol.Status.LatestImage != ""
}, timeout, interval).Should(BeTrue())
g.Expect(pol.Status.LatestImage).To(Equal(imgRepo + tt.wantImageTag))
} else {
g.Eventually(func() bool {
err := testEnv.Get(ctx, polName, &pol)
return err == nil && apimeta.IsStatusConditionFalse(pol.Status.Conditions, meta.ReadyCondition)
}, timeout, interval).Should(BeTrue())
ready := apimeta.FindStatusCondition(pol.Status.Conditions, meta.ReadyCondition)
g.Expect(ready.Message).To(ContainSubstring("invalid policy"))
}
g.Expect(testEnv.Delete(ctx, &pol)).To(Succeed())
})
}
Expand Down
5 changes: 4 additions & 1 deletion internal/policy/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ func PolicerFromSpec(choice imagev1.ImagePolicyChoice) (Policer, error) {
return nil, fmt.Errorf("given ImagePolicyChoice object is invalid")
}

return p, err
if err != nil {
return nil, err
}
return p, nil
}
9 changes: 9 additions & 0 deletions internal/policy/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ func TestFactory_PolicerFromSpec(t *testing.T) {
if err != nil {
t.Error("should not return error")
}

// A nil checkable Policer for invalid policy.
p, err := PolicerFromSpec(imagev1.ImagePolicyChoice{SemVer: &imagev1.SemVerPolicy{Range: "*-*"}})
if err == nil {
t.Error("should return error")
}
if p != nil {
t.Error("should be nil")
}
}

0 comments on commit 72bd994

Please sign in to comment.