Skip to content

Commit 41ee371

Browse files
committed
refactor(marketplace): fail-safe re-tap rollback and single-source artifact naming
Close the two non-blocking notes from the third review round: - objectExists now fails safe: it returns 'not existing' (eligible for rollback) only on a definite NotFound; any other Get error (RBAC, throttling, timeout) is treated as possibly-pre-existing, so an idempotent re-tap that hits a transient probe error cannot delete a pre-existing resource on rollback. Mirrors the fail-direction of sourceStillReferenced; covered by a new interceptor-based test. - The PackageSource reconciler now calls internal/marketplace/naming.ArtifactName instead of inlining the formula, so the artifact-name convention has a single definition shared by the producer, the marketplace backend, and the validator, and the parity test guards producer-vs-helper. Signed-off-by: IvanHunters <[email protected]>
1 parent 4f3c479 commit 41ee371

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

‎cmd/cozypkg/cmd/tap.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
fluxmeta "github.com/fluxcd/pkg/apis/meta"
2828
sourcev1 "github.com/fluxcd/source-controller/api/v1"
2929
"github.com/spf13/cobra"
30+
apierrors "k8s.io/apimachinery/pkg/api/errors"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/apimachinery/pkg/runtime"
3233
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -362,13 +363,17 @@ refused.`,
362363
}
363364

364365
// objectExists reports whether obj already exists on the cluster, so an
365-
// idempotent re-tap does not roll back resources it did not create.
366+
// idempotent re-tap does not roll back resources it did not create. It fails
367+
// safe: only a definite NotFound means "this run created it" (eligible for
368+
// rollback). Any other error (RBAC, throttling, timeout) is treated as
369+
// possibly-pre-existing so rollback never deletes an object it did not create.
366370
func objectExists(ctx context.Context, k8sClient client.Client, obj client.Object) bool {
367371
probe, ok := obj.DeepCopyObject().(client.Object)
368372
if !ok {
369-
return false
373+
return true
370374
}
371-
return k8sClient.Get(ctx, client.ObjectKeyFromObject(obj), probe) == nil
375+
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(obj), probe)
376+
return !apierrors.IsNotFound(err)
372377
}
373378

374379
// sourceStillReferenced reports whether any PackageSource other than excludeName

‎cmd/cozypkg/cmd/tap_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ package cmd
1818

1919
import (
2020
"context"
21+
"errors"
2122
"testing"
2223

2324
sourcev1 "github.com/fluxcd/source-controller/api/v1"
25+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2426
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2527
"k8s.io/apimachinery/pkg/runtime"
2628
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
2730
"sigs.k8s.io/controller-runtime/pkg/client/fake"
31+
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2832

2933
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
3034
)
@@ -111,6 +115,17 @@ func TestObjectExists(t *testing.T) {
111115
if objectExists(context.Background(), cl, absent) {
112116
t.Error("expected an absent object to be reported as not existing (eligible for rollback)")
113117
}
118+
119+
// Fail-safe: a non-NotFound Get error (RBAC/throttling/timeout) must report
120+
// "exists" so rollback never deletes a possibly-pre-existing object.
121+
errCl := fake.NewClientBuilder().WithScheme(scheme).WithInterceptorFuncs(interceptor.Funcs{
122+
Get: func(_ context.Context, _ client.WithWatch, _ client.ObjectKey, _ client.Object, _ ...client.GetOption) error {
123+
return apierrors.NewInternalError(errors.New("apiserver unavailable"))
124+
},
125+
}).Build()
126+
if !objectExists(context.Background(), errCl, absent) {
127+
t.Error("a non-NotFound Get error must be treated as possibly-existing (spared from rollback)")
128+
}
114129
}
115130

116131
func TestBuildTapOCIRepository(t *testing.T) {

‎internal/operator/packagesource_reconciler.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
27+
"github.com/cozystack/cozystack/internal/marketplace/naming"
2728
sourcewatcherv1beta1 "github.com/fluxcd/source-watcher/api/v2/v1beta1"
2829
apierrors "k8s.io/apimachinery/pkg/api/errors"
2930
"k8s.io/apimachinery/pkg/api/meta"
@@ -231,12 +232,11 @@ func (r *PackageSourceReconciler) reconcileArtifactGenerators(ctx context.Contex
231232
})
232233
}
233234

234-
// Artifact name: <packagesource>-<variant>-<componentname>
235-
// Replace dots with dashes to comply with Kubernetes naming requirements
236-
artifactName := fmt.Sprintf("%s-%s-%s",
237-
strings.ReplaceAll(packageSource.Name, ".", "-"),
238-
strings.ReplaceAll(variant.Name, ".", "-"),
239-
strings.ReplaceAll(component.Name, ".", "-"))
235+
// Artifact name: <packagesource>-<variant>-<componentname>, dots
236+
// replaced by dashes to comply with Kubernetes naming. This is the
237+
// single definition shared with the marketplace backend and the
238+
// validator via internal/marketplace/naming.
239+
artifactName := naming.ArtifactName(packageSource.Name, variant.Name, component.Name)
240240

241241
outputArtifacts = append(outputArtifacts, sourcewatcherv1beta1.OutputArtifact{
242242
Name: artifactName,
@@ -1019,4 +1019,3 @@ func (r *PackageSourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
10191019
Owns(&sourcewatcherv1beta1.ArtifactGenerator{}).
10201020
Complete(r)
10211021
}
1022-

0 commit comments

Comments
 (0)