Skip to content

fix: verify full ArgoCD CR ownership before deleting cluster resources - #1619

Open
Rizwana777 wants to merge 1 commit into
redhat-developer:masterfrom
Rizwana777:bug-12045
Open

Rizwana777 wants to merge 1 commit into
redhat-developer:masterfrom
Rizwana777:bug-12045

Conversation

@Rizwana777

@Rizwana777 Rizwana777 commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?
Fixes #1558
Previously, when an ArgoCD CR was deleted, deleteClusterResources selected ClusterRoles, ClusterRoleBindings, RoleBindings, and Promoter ControllerConfigurations using only the app.kubernetes.io/managed-by= label without verifying the owning namespace or UID. In a cluster-wide operator installation, creating and deleting a same-named ArgoCD CR in an unrelated namespace caused the operator to delete another instance's RBAC resources without any permissions in the victim namespace.
This PR enforces full owning CR identity verification (namespace, name, and uid) before deleting resources

PR acceptance criteria:

  • Documentation was updated and verified using make serve-docs
  • Unit tests were updated
  • E2E tests were updated

What type of PR is this?
/kind bug

Special notes to the reviewer:

OPERATOR_NAMESPACE=openshift-gitops-operator make run

  1. Create cluster-scoped victim instance in argocd-e2e-cluster-config
    oc create namespace argocd-e2e-cluster-config 2>/dev/null || true
    oc apply -n argocd-e2e-cluster-config -f - <<EOF
    apiVersion: argoproj.io/v1beta1
    kind: ArgoCD
    metadata:
    name: shared-name
    spec: {}
    EOF

sleep 5

  1. Record BEFORE UIDs & timestamps
    echo "=== BEFORE ==="
    oc get clusterrole,clusterrolebinding -l app.kubernetes.io/managed-by=shared-name
    -o custom-columns=KIND:.kind,NAME:.metadata.name,UID:.metadata.uid,CREATED:.metadata.creationTimestamp
    oc get rolebinding -n argocd-e2e-cluster-config -l app.kubernetes.io/managed-by=shared-name
    -o custom-columns=KIND:.kind,NAME:.metadata.name,UID:.metadata.uid,CREATED:.metadata.creationTimestamp

  2. Create & delete same-named CR in unrelated tenant namespace
    oc create namespace tenant 2>/dev/null || true
    oc apply -n tenant -f - <<EOF
    apiVersion: argoproj.io/v1beta1
    kind: ArgoCD
    metadata:
    name: shared-name
    spec: {}
    EOF

until [ "$(oc get argocd shared-name -n tenant -o jsonpath='{.metadata.finalizers[0]}')" = "argoproj.io/finalizer" ]; do sleep 1; done
oc delete argocd shared-name -n tenant

  1. Verify victim UIDs & timestamps are UNCHANGED and tenant RBAC is gone
    echo "=== AFTER (VICTIM MUST BE UNCHANGED) ==="
    oc get clusterrole,clusterrolebinding -l app.kubernetes.io/managed-by=shared-name
    -o custom-columns=KIND:.kind,NAME:.metadata.name,UID:.metadata.uid,CREATED:.metadata.creationTimestamp
    oc get rolebinding -n argocd-e2e-cluster-config -l app.kubernetes.io/managed-by=shared-name
    -o custom-columns=KIND:.kind,NAME:.metadata.name,UID:.metadata.uid,CREATED:.metadata.creationTimestamp

echo "=== TENANT AFTER DELETE (MUST BE EMPTY) ==="
oc get rolebinding -n tenant -l app.kubernetes.io/managed-by=shared-name

  1. Confirm deleting victim itself still cleans up its own RBAC, then clean up namespaces
    oc delete argocd shared-name -n argocd-e2e-cluster-config
    oc get clusterrole,clusterrolebinding -l app.kubernetes.io/managed-by=shared-name
    oc delete namespace argocd-e2e-cluster-config tenant

@openshift-ci openshift-ci Bot added the kind/bug Something isn't working label Dec 1, 2026
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Cleanup now preserves cluster-scoped and namespaced resources that belong to other Argo CD instances, including instances with matching names.
    • Ownership checks use instance identity to avoid deleting resources not associated with the Argo CD instance being removed.
  • Improvements
    • Operator-managed permissions and promoter resources now include Argo CD instance identity metadata.
    • Promoter permissions can be reconciled in a specified namespace, including kube-system.

The changes add ArgoCD UID annotations and ownership checks for generated and existing resources. Cleanup now receives the ArgoCD resource and skips resources it does not own. Promoter RoleBindings can target an explicit namespace.

Changes: ArgoCD resource ownership and cleanup

Layer / File(s) Summary
Define ArgoCD resource ownership
argocd-operator/common/annotations.go, argocd-operator/controllers/argoutil/resource.go, argocd-operator/controllers/argoutil/resource_test.go
The ownership helper checks owner references, explicit annotations, and legacy resource identity. Cluster annotations include the ArgoCD UID when set. Tests cover matching and mismatched identities and legacy resources.
Annotate generated resources
argocd-operator/controllers/argocdagent/..., argocd-operator/controllers/gitopspromoter/apiservice.go, argocd-operator/controllers/gitopspromoter/controllerconfiguration.go, argocd-operator/controllers/gitopspromoter/role.go, argocd-operator/controllers/gitopspromoter/rolebinding.go
Agent and promoter resources receive cluster annotations. Promoter RoleBindings can target an explicit namespace. APIService ownership checks reject a mismatched non-empty UID.
Restrict cleanup to owned resources
argocd-operator/controllers/argocd/argocd_controller_test.go, argocd-operator/controllers/argocd/role.go, argocd-operator/controllers/argocd/rolebinding.go, argocd-operator/controllers/argocd/util.go, argocd-operator/controllers/gitopspromoter/controllerconfiguration.go
RBAC and ControllerConfiguration deletion paths skip resources not owned by the ArgoCD instance. Regression tests cover same-name instances across namespaces and different UIDs.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant ArgoCDReconciler
  participant ResourceCleanup
  participant IsOwnedByArgoCD
  participant KubernetesClient
  ArgoCDReconciler->>ResourceCleanup: pass ArgoCD resource to cleanup
  ResourceCleanup->>IsOwnedByArgoCD: check listed resource ownership
  IsOwnedByArgoCD-->>ResourceCleanup: return ownership result
  ResourceCleanup->>KubernetesClient: delete owned resource
Loading
Merge Risk: Minimal · up to 9dc24

The change restricts cleanup of cluster-scoped and RBAC resources to those owned by the deleted ArgoCD instance. No merge-blocking risk was identified in the reviewed changes. The test and documentation checklist items remain open on the PR.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: cluster resource deletion now verifies full ArgoCD CR ownership (namespace, name, UID). The changeset adds IsOwnedByArgoCD and gates deletion of ClusterRoles, ClusterR…
Description check Passed The description explains the bug, the cause (deletion matched only the managed-by label), and the fix (verify namespace, name, and UID before deleting). It also gives a reproduction and verification p…
Linked Issues check Passed [#1558] requires that ArgoCD deletion remove only resources owned by that instance. Cleanup must verify namespace and, preferably, UID. Regression tests must cover same-named instances in different na…
Out of Scope Changes check Passed All changes support the ownership model for #1558:
- common/annotations.go adds AnnotationUID, used by the ownership check.
- argoutil/resource.go adds IsOwnedByArgoCD and tightens the ClusterRole and…
Full details: Title check

Explanation

The title names the main change: cluster resource deletion now verifies full ArgoCD CR ownership (namespace, name, UID). The changeset adds IsOwnedByArgoCD and gates deletion of ClusterRoles, ClusterRoleBindings, RoleBindings, and Promoter ControllerConfigurations on it. The 'fix:' prefix matches the bug nature.

Full details: Description check

Explanation

The description explains the bug, the cause (deletion matched only the managed-by label), and the fix (verify namespace, name, and UID before deleting). It also gives a reproduction and verification procedure. It relates directly to the changeset.

Full details: Linked Issues check

Explanation

[#1558] requires that ArgoCD deletion remove only resources owned by that instance. Cleanup must verify namespace and, preferably, UID. Regression tests must cover same-named instances in different namespaces.

Implementation at head (whole-PR merge base a617103 to head 9dc24bd):

  • util.go deleteClusterResources now passes the ArgoCD CR to the ClusterRole, ClusterRoleBinding, RoleBinding, and Promoter ControllerConfiguration deletion helpers. Previously the CR was omitted.
  • role.go, rolebinding.go, and controllerconfiguration.go skip objects that fail argoutil.IsOwnedByArgoCD or the CheckClusterRole/CheckClusterRoleBinding ownership checks.
  • resource.go IsOwnedByArgoCD checks owner references, then the argocds.argoproj.io/uid annotation (AnnotationUID), then a legacy fallback. Conflicting UID or namespace rejects the object.
  • AnnotationsForCluster records the CR UID on generated resources.
  • argocd_controller_test.go adds a cross-namespace test (tenant deletion removes tenant resources and preserves the same-named victim's resources) and a same-namespace different-UID test.
  • resource_test.go adds unit tests for IsOwnedByArgoCD and cluster-role ownership across namespace and UID mismatches.

I read gitopspromoter/rolebinding.go at head. It matches the summary. The change from the previous review is supporting code, and the core ownership path is unchanged in the summary.

Unresolved uncertainty: I did not trace the full legacy fallback logic for ClusterRoles. I did not execute the live-cluster reproduction. The unit tests cover the cross-namespace path.

Full details: Out of Scope Changes check

Explanation

All changes support the ownership model for #1558:

  • common/annotations.go adds AnnotationUID, used by the ownership check.
  • argoutil/resource.go adds IsOwnedByArgoCD and tightens the ClusterRole and ClusterRoleBinding ownership checks. Tests are in resource_test.go.
  • argocdagent and gitopspromoter role, rolebinding, apiservice, and controllerconfiguration files add AnnotationsForCluster to generated resources. This lets the operator record the owner UID. apiservice.go rejects a non-empty UID mismatch.
  • gitopspromoter/rolebinding.go adds reconcilePromoterRoleBindingInNamespace. The kube-system extension-auth-reader RoleBinding uses it directly. Public ReconcilePromoterRoleBinding still targets cr.Namespace. This refactor is adjacent. It is not required by #1558, but it does not change deletion semantics for tenant or victim resources.
  • The ClusterRoleBinding name is used directly. The name label is still set as before.

No change appears unrelated to ownership verification or its supporting annotations and tests.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 1dc5fad and 9dc24bd.

Files selected for processing: 1

  • argocd-operator/common/annotations.go
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
Assisted-by: Antigravity
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.24658% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.85%. Comparing base (a617103) to head (9dc24bd).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
argocd-operator/controllers/argoutil/resource.go 77.46% 16 Missing ⚠️
...trollers/gitopspromoter/controllerconfiguration.go 45.45% 6 Missing ⚠️
...-operator/controllers/gitopspromoter/apiservice.go 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1619      +/-   ##
==========================================
- Coverage   70.82%   68.85%   -1.98%     
==========================================
  Files          25      118      +93     
  Lines        3873    25416   +21543     
==========================================
+ Hits         2743    17499   +14756     
- Misses       1130     7917    +6787     
Flag Coverage Δ
unit-tests 68.85% <84.24%> (-1.98%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting an ArgoCD instance removes RBAC belonging to same-named instances

1 participant