Skip to content

Commit 9c444c7

Browse files
committed
Mark resource as stalled on invalid URL
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
1 parent 63c9439 commit 9c444c7

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

controllers/ocirepository_controller.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ var ociRepositoryFailConditions = []string{
102102
sourcev1.StorageOperationFailedCondition,
103103
}
104104

105+
type invalidOCIURLError struct {
106+
err error
107+
}
108+
109+
func (e invalidOCIURLError) Error() string {
110+
return e.err.Error()
111+
}
112+
105113
// ociRepositoryReconcileFunc is the function type for all the v1beta2.OCIRepository
106114
// (sub)reconcile functions. The type implementations are grouped and
107115
// executed serially to perform the complete reconcile of the object.
@@ -337,9 +345,16 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, obj *sour
337345
// Determine which artifact revision to pull
338346
url, err := r.getArtifactURL(obj, options)
339347
if err != nil {
348+
if _, ok := err.(invalidOCIURLError); ok {
349+
e := serror.NewStalling(
350+
fmt.Errorf("failed to determine the artifact address for '%s': %w", obj.Spec.URL, err),
351+
sourcev1.URLInvalidReason)
352+
return sreconcile.ResultEmpty, e
353+
}
354+
340355
e := serror.NewGeneric(
341-
fmt.Errorf("failed to determine the artifact address for '%s': %w", obj.Spec.URL, err),
342-
sourcev1.URLInvalidReason)
356+
fmt.Errorf("failed to determine the artifact tag for '%s': %w", obj.Spec.URL, err),
357+
sourcev1.OCIOperationFailedReason)
343358
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
344359
return sreconcile.ResultEmpty, e
345360
}
@@ -464,7 +479,7 @@ func (r *OCIRepositoryReconciler) parseRepositoryURL(obj *sourcev1.OCIRepository
464479
func (r *OCIRepositoryReconciler) getArtifactURL(obj *sourcev1.OCIRepository, options []crane.Option) (string, error) {
465480
url, err := r.parseRepositoryURL(obj)
466481
if err != nil {
467-
return "", err
482+
return "", invalidOCIURLError{err}
468483
}
469484

470485
if obj.Spec.Reference != nil {

controllers/ocirepository_controller_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,48 @@ func TestOCIRepository_getArtifactURL(t *testing.T) {
10641064
}
10651065
}
10661066

1067+
func TestOCIRepository_stalled(t *testing.T) {
1068+
g := NewWithT(t)
1069+
1070+
ns, err := testEnv.CreateNamespace(ctx, "ocirepository-stalled-test")
1071+
g.Expect(err).ToNot(HaveOccurred())
1072+
defer func() { g.Expect(testEnv.Delete(ctx, ns)).To(Succeed()) }()
1073+
1074+
obj := &sourcev1.OCIRepository{
1075+
ObjectMeta: metav1.ObjectMeta{
1076+
GenerateName: "ocirepository-reconcile",
1077+
Namespace: ns.Name,
1078+
},
1079+
Spec: sourcev1.OCIRepositorySpec{
1080+
URL: "oci://ghcr.io/test/test:v1",
1081+
Interval: metav1.Duration{Duration: 60 * time.Minute},
1082+
},
1083+
}
1084+
1085+
g.Expect(testEnv.Create(ctx, obj)).To(Succeed())
1086+
1087+
key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace}
1088+
resultobj := sourcev1.OCIRepository{}
1089+
1090+
// Wait for the object to fail
1091+
g.Eventually(func() bool {
1092+
if err := testEnv.Get(ctx, key, &resultobj); err != nil {
1093+
return false
1094+
}
1095+
readyCondition := conditions.Get(&resultobj, meta.ReadyCondition)
1096+
if readyCondition == nil {
1097+
return false
1098+
}
1099+
return obj.Generation == readyCondition.ObservedGeneration &&
1100+
!conditions.IsReady(&resultobj)
1101+
}, timeout).Should(BeTrue())
1102+
1103+
// Verify that stalled condition is present in status
1104+
stalledCondition := conditions.Get(&resultobj, meta.StalledCondition)
1105+
g.Expect(stalledCondition).ToNot(BeNil())
1106+
g.Expect(stalledCondition.Reason).Should(Equal(sourcev1.URLInvalidReason))
1107+
}
1108+
10671109
func TestOCIRepository_reconcileStorage(t *testing.T) {
10681110
g := NewWithT(t)
10691111

0 commit comments

Comments
 (0)