Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions applicationset/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ func (r *ApplicationSetReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return ctrl.Result{}, err
}

// ensure finalizer exists if deletionOrder is set as Reverse
if r.EnableProgressiveSyncs && isProgressiveSyncDeletionOrderReversed(&applicationSetInfo) {
if !controllerutil.ContainsFinalizer(&applicationSetInfo, argov1alpha1.ResourcesFinalizerName) {
controllerutil.AddFinalizer(&applicationSetInfo, argov1alpha1.ResourcesFinalizerName)
if err := r.Update(ctx, &applicationSetInfo); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
Copy link
Copy Markdown
Member Author

@ranakan19 ranakan19 Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can likely remove this - updating the annotations would already reconcile Appset again - so explicit reconcile has no real effect.

}
}

// Log a warning if there are unrecognized generators
_ = utils.CheckInvalidGenerators(&applicationSetInfo)
// desiredApplications is the main list of all expected Applications from all generators in this appset.
Expand Down
217 changes: 217 additions & 0 deletions applicationset/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7277,6 +7277,223 @@ func TestIsRollingSyncDeletionReversed(t *testing.T) {
}
}

func TestReconcileAddsFinalizer_WhenDeletionOrderReverse(t *testing.T) {
scheme := runtime.NewScheme()
err := v1alpha1.AddToScheme(scheme)
require.NoError(t, err)

kubeclientset := kubefake.NewClientset([]runtime.Object{}...)

for _, cc := range []struct {
name string
appSet v1alpha1.ApplicationSet
progressiveSyncEnabled bool
expectedFinalizers []string
}{
{
name: "adds finalizer when DeletionOrder is Reverse",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-appset",
Namespace: "argocd",
// No finalizers initially
},
Spec: v1alpha1.ApplicationSetSpec{
Strategy: &v1alpha1.ApplicationSetStrategy{
Type: "RollingSync",
RollingSync: &v1alpha1.ApplicationSetRolloutStrategy{
Steps: []v1alpha1.ApplicationSetRolloutStep{
{
MatchExpressions: []v1alpha1.ApplicationMatchExpression{
{
Key: "env",
Operator: "In",
Values: []string{"dev"},
},
},
},
},
},
DeletionOrder: ReverseDeletionOrder,
},
Template: v1alpha1.ApplicationSetTemplate{},
},
},
progressiveSyncEnabled: true,
expectedFinalizers: []string{v1alpha1.ResourcesFinalizerName},
},
{
name: "does not add finalizer when already exists and DeletionOrder is Reverse",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-appset",
Namespace: "argocd",
Finalizers: []string{
v1alpha1.ResourcesFinalizerName,
},
},
Spec: v1alpha1.ApplicationSetSpec{
Strategy: &v1alpha1.ApplicationSetStrategy{
Type: "RollingSync",
RollingSync: &v1alpha1.ApplicationSetRolloutStrategy{
Steps: []v1alpha1.ApplicationSetRolloutStep{
{
MatchExpressions: []v1alpha1.ApplicationMatchExpression{
{
Key: "env",
Operator: "In",
Values: []string{"dev"},
},
},
},
},
},
DeletionOrder: ReverseDeletionOrder,
},
Template: v1alpha1.ApplicationSetTemplate{},
},
},
progressiveSyncEnabled: true,
expectedFinalizers: []string{v1alpha1.ResourcesFinalizerName},
},
{
name: "does not add finalizer when DeletionOrder is AllAtOnce",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-appset",
Namespace: "argocd",
},
Spec: v1alpha1.ApplicationSetSpec{
Strategy: &v1alpha1.ApplicationSetStrategy{
Type: "RollingSync",
RollingSync: &v1alpha1.ApplicationSetRolloutStrategy{
Steps: []v1alpha1.ApplicationSetRolloutStep{
{
MatchExpressions: []v1alpha1.ApplicationMatchExpression{
{
Key: "env",
Operator: "In",
Values: []string{"dev"},
},
},
},
},
},
DeletionOrder: AllAtOnceDeletionOrder,
},
Template: v1alpha1.ApplicationSetTemplate{},
},
},
progressiveSyncEnabled: true,
expectedFinalizers: nil,
},
{
name: "does not add finalizer when DeletionOrder is not set",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-appset",
Namespace: "argocd",
},
Spec: v1alpha1.ApplicationSetSpec{
Strategy: &v1alpha1.ApplicationSetStrategy{
Type: "RollingSync",
RollingSync: &v1alpha1.ApplicationSetRolloutStrategy{
Steps: []v1alpha1.ApplicationSetRolloutStep{
{
MatchExpressions: []v1alpha1.ApplicationMatchExpression{
{
Key: "env",
Operator: "In",
Values: []string{"dev"},
},
},
},
},
},
},
Template: v1alpha1.ApplicationSetTemplate{},
},
},
progressiveSyncEnabled: true,
expectedFinalizers: nil,
},
{
name: "does not add finalizer when progressive sync not enabled",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-appset",
Namespace: "argocd",
},
Spec: v1alpha1.ApplicationSetSpec{
Strategy: &v1alpha1.ApplicationSetStrategy{
Type: "RollingSync",
RollingSync: &v1alpha1.ApplicationSetRolloutStrategy{
Steps: []v1alpha1.ApplicationSetRolloutStep{
{
MatchExpressions: []v1alpha1.ApplicationMatchExpression{
{
Key: "env",
Operator: "In",
Values: []string{"dev"},
},
},
},
},
},
DeletionOrder: ReverseDeletionOrder,
},
Template: v1alpha1.ApplicationSetTemplate{},
},
},
progressiveSyncEnabled: false,
expectedFinalizers: nil,
},
} {
t.Run(cc.name, func(t *testing.T) {
client := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(&cc.appSet).
WithStatusSubresource(&cc.appSet).
WithIndex(&v1alpha1.Application{}, ".metadata.controller", appControllerIndexer).
Build()
metrics := appsetmetrics.NewFakeAppsetMetrics()
argodb := db.NewDB("argocd", settings.NewSettingsManager(t.Context(), kubeclientset, "argocd"), kubeclientset)

r := ApplicationSetReconciler{
Client: client,
Scheme: scheme,
Renderer: &utils.Render{},
Recorder: record.NewFakeRecorder(1),
Generators: map[string]generators.Generator{},
ArgoDB: argodb,
KubeClientset: kubeclientset,
Metrics: metrics,
EnableProgressiveSyncs: cc.progressiveSyncEnabled,
}

req := ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: cc.appSet.Namespace,
Name: cc.appSet.Name,
},
}

// Run reconciliation
_, err = r.Reconcile(t.Context(), req)
require.NoError(t, err)

// Fetch the updated ApplicationSet
var updatedAppSet v1alpha1.ApplicationSet
err = r.Get(t.Context(), req.NamespacedName, &updatedAppSet)
require.NoError(t, err)

// Verify the finalizers
assert.Equal(t, cc.expectedFinalizers, updatedAppSet.Finalizers,
"finalizers should match expected value")
})
}
}

func TestReconcileProgressiveSyncDisabled(t *testing.T) {
scheme := runtime.NewScheme()
err := v1alpha1.AddToScheme(scheme)
Expand Down
Loading