Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

policy: Handle failure due to invalid semver range #176

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}