Skip to content

CM-1040: Extract shared ApplyResource helper and migrate istiocsr to SSA - #420

Open
sebrandon1 wants to merge 1 commit into
openshift:masterfrom
sebrandon1:extract-apply-resource
Open

CM-1040: Extract shared ApplyResource helper and migrate istiocsr to SSA#420
sebrandon1 wants to merge 1 commit into
openshift:masterfrom
sebrandon1:extract-apply-resource

Conversation

@sebrandon1

@sebrandon1 sebrandon1 commented May 6, 2026

Copy link
Copy Markdown
Member

Summary

Extract a generic common.ApplyResourceT helper for Server-Side Apply (SSA) reconciliation and migrate both istiocsr and trustmanager controllers to use it, eliminating duplicated create-or-update boilerplate across the codebase.

  • Add common.ApplyResourceT in pkg/controller/common/applier.go -- a type-safe SSA reconciliation function with pluggable drift-detection callbacks and standardized event recording
  • Migrate trustmanager's 11 createOrApply methods to delegate to the shared helper, removing per-resource SSA plumbing that was already using Patch but duplicated across each resource type
  • Migrate istiocsr's 8 simple createOrApply methods from Create/UpdateWithRetry to SSA via the shared helper (Service, ServiceAccount, Certificate, Role, RoleBinding, NetworkPolicy, plus lease Role/RoleBinding)
  • Keep istiocsr's ClusterRole/ClusterRoleBinding methods unchanged -- they use GenerateName with List fallback and must Delete for immutable RoleRef changes, which does not fit the SSA helper pattern
  • Remove the istioCSRCreateRecon parameter and its "ResourceAlreadyExists" warning events from all migrated methods -- SSA is declarative and idempotent, so an existing resource is expected state rather than an anomaly
  • Handle RoleBinding roleRef immutability: when the roleRef changes, the binding is deleted and recreated rather than producing an update error
  • Net -212 lines of duplicated reconciliation boilerplate

Related PRs

This is part of an ordered refactoring series:

Jira

  • CM-1040 -- Extract shared ApplyResource helper and migrate istiocsr to SSA (To Do)

Test Plan

  • New applier_test.go with direct unit tests for ApplyResource covering create, no-op, update, Exists error, Patch error, and error classification
  • New networkpolicies_test.go with tests for asset decoding, namespace fallback, full apply, and error propagation
  • Updated istiocsr and trustmanager test suites to use Patch stubs instead of Create/Update stubs where methods were migrated to SSA
  • go test ./pkg/controller/common/... passes
  • go test ./pkg/controller/istiocsr/... passes
  • go test ./pkg/controller/trustmanager/... passes
  • go build ./... succeeds
  • make lint shows only pre-existing issues (none introduced)

Summary by CodeRabbit

  • New Features

    • Kubernetes resources are now reconciled using consistent server-side apply behavior across Istio CSR and trust-manager components.
    • Resource ownership and drift handling are standardized, including automatic updates when managed resources change.
    • Improved handling for immutable RBAC changes and retryable conflicts.
  • Bug Fixes

    • Error messages now consistently identify resource types and names.
    • Added coverage for resource creation, updates, conflicts, authorization failures, and patch errors.

@openshift-ci-robot openshift-ci-robot added the jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. label May 6, 2026
@openshift-ci-robot

openshift-ci-robot commented May 6, 2026

Copy link
Copy Markdown

@sebrandon1: This pull request references CM-1040 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the task to target the "5.0.0" version, but no target version was set.

Details

In response to this:

Summary

  • Adds a generic common.ApplyResource[T]() helper for SSA-based reconciliation with pluggable drift detection callbacks
  • Migrates trustmanager's 11 createOrApply methods to use the shared helper
  • Migrates istiocsr's 8 simple createOrApply methods from Create/UpdateWithRetry to SSA via the shared helper
  • Keeps istiocsr's complex ClusterRole/ClusterRoleBinding methods unchanged (they use GenerateName + List fallback + Delete for immutable RoleRef)
  • Drops istioCSRCreateRecon warning events from migrated methods (SSA is inherently idempotent)
  • Net -501 lines of duplicated boilerplate

Test plan

  • go test ./pkg/controller/common/... passes
  • go test ./pkg/controller/istiocsr/... passes
  • go test ./pkg/controller/trustmanager/... passes
  • go build ./... succeeds
  • make lint shows only pre-existing issues (9 total, none introduced)

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: dc7d72ab-70df-4b9f-8bd6-feb26421d7ee

📥 Commits

Reviewing files that changed from the base of the PR and between 4505fa3 and 7008995.

📒 Files selected for processing (1)
  • pkg/controller/common/client_test.go

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.


Walkthrough

This PR adds a generic Server-Side Apply helper. IstioCSR and TrustManager controllers use it for multiple Kubernetes resources. Tests now validate patch-based reconciliation, immutable RBAC handling, and standardized errors.

Changes

Shared Server-Side Apply reconciliation

Layer / File(s) Summary
Shared apply helper and validation
pkg/controller/common/applier.go, pkg/controller/common/applier_test.go, pkg/controller/common/client_test.go, pkg/controller/istiocsr/constants.go
Adds generic existence checks, drift detection, field ownership, forced ownership, contextual errors, event recording, error classification, and common client operation tests.
IstioCSR resource migration
pkg/controller/istiocsr/*
Migrates services, certificates, service accounts, network policies, Roles, and RoleBindings to common.ApplyResource. Immutable roleRef changes delete bindings before apply. Tests use patch calls and standardized apply errors.
TrustManager resource migration
pkg/controller/trustmanager/*
Migrates certificates, deployments, RBAC resources, services, service accounts, and webhook configurations to common.ApplyResource. Tests update propagated error messages.

Estimated code review effort: 4 (Complex) | ~60 minutes

Merge Risk: ⚪ Minimal · up to 70089

This PR centralizes resource reconciliation and migrates selected controllers to server-side apply without any identified merge-blocking risk at the current head; it is merge-ready after normal checks and review.

🚥 Pre-merge checks | ✅ 14 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 2.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (14 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the shared ApplyResource helper and the istiocsr migration to Server-Side Apply.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Stable And Deterministic Test Names ✅ Passed The PR diff adds no Ginkgo It, Describe, Context, or When titles; changed tests use static Go test names, and the only new subtest names come from fixed asset paths.
Test Structure And Quality ✅ Passed The PR changes only standard Go testing/Testify tests: 58 Test functions, no Ginkgo/Gomega imports, specs, lifecycle hooks, or Eventually/Consistently calls.
Microshift Test Compatibility ✅ Passed The diff adds or changes only Go controller unit tests and production code; it adds no test/e2e files or Ginkgo It/Describe/Context/When tests.
Single Node Openshift (Sno) Test Compatibility ✅ Passed The complete PR diff changes only pkg/controller files; it adds no test/e2e files or Ginkgo It/Describe/Context/When tests, so no SNO multi-node assumption is introduced.
Topology-Aware Scheduling Compatibility ✅ Passed The PR diff adds no production scheduling fields, topology constraints, or manifests; it only changes reconciliation logic. The existing trust-manager OS selector is unchanged.
Ote Binary Stdout Contract ✅ Passed The PR adds no process-level entry points or stdout writes. ApplyResource uses logr only, and the existing Ginkgo suite setup is unchanged with output directed to GinkgoWriter.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed The PR adds only standard Go Testify unit tests; no new Ginkgo e2e tests, IPv4 assumptions, or external connectivity requirements appear in the diff.
No-Weak-Crypto ✅ Passed The 29-file diff adds no MD5, SHA1, DES, RC4, Blowfish, ECB, custom crypto, or secret/token comparisons; crypto references found are unchanged existing code.
Container-Privileges ✅ Passed The PR changes only Go source and tests. The base-to-tip diff has no YAML, JSON, or Dockerfile changes and adds no privileged container settings.
No-Sensitive-Data-In-Logs ✅ Passed Introduced logs and events contain only resource kind and namespace/name keys; diff shows no passwords, tokens, API keys, PII, hostnames, or customer data.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@openshift-ci
openshift-ci Bot requested review from mytreya-rh and swghosh May 6, 2026 18:32
@openshift-ci

openshift-ci Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sebrandon1
Once this PR has been reviewed and has the lgtm label, please assign mytreya-rh for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
pkg/controller/istiocsr/install_instiocsr_test.go (1)

68-84: ⚡ Quick win

Preserve generated-name simulation for ClusterRole in these fakes.

createOrApplyClusterRoles() still depends on Create mutating the object with a generated name before status is written and before the ClusterRoleBinding gets its RoleRef.Name. These stubs only backfill ClusterRoleBinding, so the test can still pass even if the role name is left empty. I'd add a *rbacv1.ClusterRole branch in each CreateCalls block as well.

Representative tweak
m.CreateCalls(func(ctx context.Context, obj client.Object, option ...client.CreateOption) error {
	switch o := obj.(type) {
+	case *rbacv1.ClusterRole:
+		role := testClusterRole()
+		role.DeepCopyInto(o)
 	case *appsv1.Deployment:
 		if !reflect.DeepEqual(o.GetLabels(), labels) {
 			return fmt.Errorf("labels mismatch in %v resource; got: %v, want: %v", o, o.GetLabels(), labels)
 		}

Also applies to: 110-117, 131-138

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/controller/istiocsr/install_instiocsr_test.go` around lines 68 - 84, The
CreateCalls test stubs need to simulate Create mutating a ClusterRole with a
generated name so createOrApplyClusterRoles() sees a non-empty Role.Name; update
the CreateCalls handlers (the ones that currently switch over *appsv1.Deployment
and *rbacv1.ClusterRoleBinding) to also include a case for *rbacv1.ClusterRole
that sets o.Name to a generated value (e.g., append "-generated" or a
deterministic string) and preserves labels so subsequent logic that reads the
ClusterRole's name (and the ClusterRoleBinding RoleRef.Name) behaves as in real
Create; apply this same addition to the other CreateCalls blocks mentioned.
pkg/controller/trustmanager/webhooks_test.go (1)

207-221: ⚡ Quick win

Keep the error assertion specific to the webhook config.

ApplyResource still includes the resource name in the returned error, so shortening these expectations to just failed to ... resource makes this table much less discriminating. A wrong object name or an extra apply call in this path would still satisfy the assertion. I'd keep the expected substring specific to trustManagerWebhookConfigName here, and mirror that in the other migrated reconciliation tests.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/controller/trustmanager/webhooks_test.go` around lines 207 - 221, The
test's error expectation is too generic; update the table entries (the case with
name "patch error propagates" and similar migrated reconciliation tests) to
assert the error message includes the specific webhook resource name by matching
the substring that contains trustManagerWebhookConfigName (the Reconciler's
ApplyResource error includes the resource name), e.g. expect the returned error
to contain trustManagerWebhookConfigName along with "failed to apply resource",
and adjust other tests that assert "failed to check if resource" / "failed to
apply resource" to similarly include trustManagerWebhookConfigName so the
assertions target the specific webhook config rather than any resource.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/controller/common/applier.go`:
- Around line 33-36: The resourceName construction in applier.go currently uses
fmt.Sprintf("%s/%s", desired.GetNamespace(), desired.GetName()) which yields
"/name" for cluster-scoped objects; change the logic to detect an empty
namespace (desired.GetNamespace() == "") and build resourceName as just
desired.GetName(), otherwise build it as namespace + "/" + name so
cluster-scoped resources do not get a leading slash; update any use of the
existing resourceName variable accordingly.

---

Nitpick comments:
In `@pkg/controller/istiocsr/install_instiocsr_test.go`:
- Around line 68-84: The CreateCalls test stubs need to simulate Create mutating
a ClusterRole with a generated name so createOrApplyClusterRoles() sees a
non-empty Role.Name; update the CreateCalls handlers (the ones that currently
switch over *appsv1.Deployment and *rbacv1.ClusterRoleBinding) to also include a
case for *rbacv1.ClusterRole that sets o.Name to a generated value (e.g., append
"-generated" or a deterministic string) and preserves labels so subsequent logic
that reads the ClusterRole's name (and the ClusterRoleBinding RoleRef.Name)
behaves as in real Create; apply this same addition to the other CreateCalls
blocks mentioned.

In `@pkg/controller/trustmanager/webhooks_test.go`:
- Around line 207-221: The test's error expectation is too generic; update the
table entries (the case with name "patch error propagates" and similar migrated
reconciliation tests) to assert the error message includes the specific webhook
resource name by matching the substring that contains
trustManagerWebhookConfigName (the Reconciler's ApplyResource error includes the
resource name), e.g. expect the returned error to contain
trustManagerWebhookConfigName along with "failed to apply resource", and adjust
other tests that assert "failed to check if resource" / "failed to apply
resource" to similarly include trustManagerWebhookConfigName so the assertions
target the specific webhook config rather than any resource.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 683e1b36-07c6-463b-8cd3-d29a46add8d5

📥 Commits

Reviewing files that changed from the base of the PR and between a2e7514 and f8b7c81.

📒 Files selected for processing (26)
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/rbacs.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/trustmanager/webhooks_test.go

Comment thread pkg/controller/common/applier.go Outdated
@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from ca8b171 to fdd735e Compare May 6, 2026 18:48

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/controller/istiocsr/serviceaccounts_test.go (1)

22-25: ⚡ Quick win

Cover the no-op branch explicitly.

Now that this table has assertCalls, the "serviceaccount reconciliation successful" case should also assert PatchCallCount() == 0. Right now that case still passes if ApplyResource starts patching unchanged ServiceAccounts on every reconcile, which is one of the main regression risks in this SSA migration.

Suggested assertion
 		{
 			name: "serviceaccount reconciliation successful",
 			preReq: func(r *Reconciler, m *fakes.FakeCtrlClient) {
 				m.ExistsCalls(func(ctx context.Context, ns types.NamespacedName, obj client.Object) (bool, error) {
 					switch o := obj.(type) {
 					case *corev1.ServiceAccount:
 						serviceaccount := testServiceAccount()
 						serviceaccount.DeepCopyInto(o)
 					}
 					return true, nil
 				})
 			},
+			assertCalls: func(t *testing.T, mock *fakes.FakeCtrlClient) {
+				if mock.PatchCallCount() != 0 {
+					t.Errorf("createOrApplyServiceAccounts() Patch call count: %d, want 0", mock.PatchCallCount())
+				}
+			},
 		},
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/controller/istiocsr/serviceaccounts_test.go` around lines 22 - 25, Update
the "serviceaccount reconciliation successful" testcase in the table-driven test
in serviceaccounts_test.go to explicitly assert that no patch operations
occurred: inside its assertCalls function, call mock.PatchCallCount() and
require it equals 0 (on the provided *fakes.FakeCtrlClient) to ensure
ApplyResource did not issue patches for unchanged ServiceAccounts; keep other
existing assertions and reference the Reconciler and ApplyResource behavior as
context.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/controller/istiocsr/rbacs.go`:
- Around line 265-269: createOrApplyRoleBindings and
createOrApplyRoleBindingForLeases currently always call common.ApplyResource
(Server-Side Apply) but RoleBinding.roleRef is immutable—mirror the
ClusterRoleBinding handler's pattern: after calling common.ApplyResource (with
hasObjectChanged from getRoleBindingObject/hasObjectChanged), detect when the
failure or diff indicates only a roleRef drift (compare desired.RoleRef to the
live object.RoleRef), then delete the existing rbacv1.RoleBinding and recreate
the desired object (preserving owner refs/events/fieldOwner) as a fallback;
implement this delete-then-create flow in both createOrApplyRoleBindings and
createOrApplyRoleBindingForLeases using the same logic used in the
ClusterRoleBinding handler to avoid permanent SSA validation errors.

---

Nitpick comments:
In `@pkg/controller/istiocsr/serviceaccounts_test.go`:
- Around line 22-25: Update the "serviceaccount reconciliation successful"
testcase in the table-driven test in serviceaccounts_test.go to explicitly
assert that no patch operations occurred: inside its assertCalls function, call
mock.PatchCallCount() and require it equals 0 (on the provided
*fakes.FakeCtrlClient) to ensure ApplyResource did not issue patches for
unchanged ServiceAccounts; keep other existing assertions and reference the
Reconciler and ApplyResource behavior as context.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 3367fe15-60ea-4966-a17e-6222e23128a6

📥 Commits

Reviewing files that changed from the base of the PR and between f8b7c81 and fdd735e.

📒 Files selected for processing (26)
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/rbacs.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/trustmanager/webhooks_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/certificates_test.go

Comment thread pkg/controller/istiocsr/rbacs.go
@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from fdd735e to 26c1bb6 Compare May 14, 2026 19:07
@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from 26c1bb6 to 8b48aaa Compare May 29, 2026 15:51
@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest-required

@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from 8b48aaa to 4442995 Compare June 15, 2026 16:25
@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from 4442995 to 786945d Compare June 24, 2026 19:51
@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from 786945d to 81c1b58 Compare July 6, 2026 16:17
@sebrandon1

Copy link
Copy Markdown
Member Author

@coderabbitai resume

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
✅ Action performed

Reviews resumed.

@sebrandon1

Copy link
Copy Markdown
Member Author

Addressed CodeRabbit feedback on RoleBinding.roleRef immutability:

  • Added roleRef drift detection to createOrApplyRoleBindings and createOrApplyRoleBindingForLeases
  • If roleRef has changed, the existing binding is deleted before ApplyResource is called
  • This prevents SSA validation errors when roleRef changes (roleRef is immutable)
  • Mirrors the pattern already used in handleClusterRoleBindingModification

All tests pass with 85.2% coverage on istiocsr package.

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from d4919c6 to edaac18 Compare July 13, 2026 17:58

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/controller/istiocsr/rbacs.go (1)

285-306: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extract shared helper for RoleBinding roleRef drift/delete logic.

createOrApplyRoleBindings and createOrApplyRoleBindingForLeases duplicate ~18 nearly-identical lines (Exists check, rbacRoleBindingRefModified check, log, Delete with NotFound tolerance). Given this PR's stated goal of removing duplicated boilerplate, extract this into a shared helper, e.g. deleteRoleBindingIfRoleRefChanged(desired *rbacv1.RoleBinding) error.

♻️ Proposed refactor
+func (r *Reconciler) deleteRoleBindingIfRoleRefChanged(desired *rbacv1.RoleBinding) error {
+	key := client.ObjectKeyFromObject(desired)
+	existing := &rbacv1.RoleBinding{}
+	exists, err := r.Exists(r.ctx, key, existing)
+	if err != nil {
+		return common.FromClientError(err, "failed to check if RoleBinding %q exists", key)
+	}
+	if exists && rbacRoleBindingRefModified(desired, existing) {
+		r.log.V(1).Info("rolebinding roleRef changed, deleting for recreation (roleRef is immutable)", "name", key)
+		if err := r.Delete(r.ctx, existing); err != nil && !apierrors.IsNotFound(err) {
+			return common.FromClientError(err, "failed to delete RoleBinding %q to replace roleRef", key)
+		}
+	}
+	return nil
+}
+
 func (r *Reconciler) createOrApplyRoleBindings(istiocsr *v1alpha1.IstioCSR, serviceAccount string, resourceLabels map[string]string) error {
 	desired := r.getRoleBindingObject(serviceAccount, istiocsr.GetNamespace(), istiocsr.Spec.IstioCSRConfig.Istio.Namespace, resourceLabels)
-
-	// RoleBinding.roleRef is immutable; if it has changed, delete the existing binding first
-	key := client.ObjectKeyFromObject(desired)
-	existing := &rbacv1.RoleBinding{}
-	exists, err := r.Exists(r.ctx, key, existing)
-	if err != nil {
-		return common.FromClientError(err, "failed to check if RoleBinding %q exists", key)
-	}
-	if exists && rbacRoleBindingRefModified(desired, existing) {
-		r.log.V(1).Info("rolebinding roleRef changed, deleting for recreation (roleRef is immutable)", "name", key)
-		if err := r.Delete(r.ctx, existing); err != nil {
-			if !apierrors.IsNotFound(err) {
-				return common.FromClientError(err, "failed to delete RoleBinding %q to replace roleRef", key)
-			}
-		}
-	}
+	if err := r.deleteRoleBindingIfRoleRefChanged(desired); err != nil {
+		return err
+	}
 
 	return common.ApplyResource(r.ctx, r.CtrlClient, r.log, r.eventRecorder, istiocsr, desired, &rbacv1.RoleBinding{}, fieldOwner,
 		func(d, e *rbacv1.RoleBinding) bool { return hasObjectChanged(d, e) },
 	)
 }

Apply the analogous change to createOrApplyRoleBindingForLeases.

Also applies to: 331-352

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/controller/istiocsr/rbacs.go` around lines 285 - 306, Extract the
duplicated RoleBinding roleRef drift handling from createOrApplyRoleBindings and
createOrApplyRoleBindingForLeases into a shared helper such as
deleteRoleBindingIfRoleRefChanged, accepting the desired RoleBinding. Move the
Exists check, rbacRoleBindingRefModified comparison, diagnostic log, deletion,
and NotFound tolerance into that helper, then invoke it from both creation
methods before applying the resource.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/controller/istiocsr/rbacs.go`:
- Around line 285-306: Extract the duplicated RoleBinding roleRef drift handling
from createOrApplyRoleBindings and createOrApplyRoleBindingForLeases into a
shared helper such as deleteRoleBindingIfRoleRefChanged, accepting the desired
RoleBinding. Move the Exists check, rbacRoleBindingRefModified comparison,
diagnostic log, deletion, and NotFound tolerance into that helper, then invoke
it from both creation methods before applying the resource.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a0bb173e-0351-4833-bd59-fa1f25c895cb

📥 Commits

Reviewing files that changed from the base of the PR and between 8b48aaa and edaac18.

📒 Files selected for processing (26)
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/rbacs.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/trustmanager/webhooks_test.go
🚧 Files skipped from review as they are similar to previous changes (23)
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/trustmanager/webhooks_test.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/istiocsr/networkpolicies.go

@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from edaac18 to b919b00 Compare July 17, 2026 18:55

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/controller/istiocsr/install_instiocsr_test.go (1)

51-67: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert the SSA patch contract, not only labels.

This stub accepts any patch type/options and does not assert the expected patched resource set. A regression to a non-SSA patch—or skipping a migrated resource—would still pass. Assert client.Apply, the field-owner/force options, and expected Patch calls via the fake’s recorded arguments.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/controller/istiocsr/install_instiocsr_test.go` around lines 51 - 67,
Update the PatchCalls assertion around the SSA-migrated resources to validate
the full apply contract: require a client.Apply patch, the expected field-owner
and force options, and exactly the expected Service, ServiceAccount,
Certificate, Role, and RoleBinding patch calls using the fake’s recorded
arguments. Preserve the existing label assertions while rejecting skipped
resources or non-SSA patches.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/controller/istiocsr/install_instiocsr_test.go`:
- Around line 51-67: Update the PatchCalls assertion around the SSA-migrated
resources to validate the full apply contract: require a client.Apply patch, the
expected field-owner and force options, and exactly the expected Service,
ServiceAccount, Certificate, Role, and RoleBinding patch calls using the fake’s
recorded arguments. Preserve the existing label assertions while rejecting
skipped resources or non-SSA patches.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a104d795-c66e-4f2b-acf9-4a6a4f27e78f

📥 Commits

Reviewing files that changed from the base of the PR and between edaac18 and b919b00.

📒 Files selected for processing (26)
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/rbacs.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/trustmanager/webhooks_test.go
🚧 Files skipped from review as they are similar to previous changes (22)
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/webhooks_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/trustmanager/rbacs.go

@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from b919b00 to 45acb13 Compare July 17, 2026 19:00

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/controller/common/applier_test.go (1)

47-49: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert the SSA patch contract, not just the call count.

This stub discards client.Apply, FieldOwner, and ForceOwnership. A regression to a non-SSA patch or missing ownership options would still pass every test here. Capture the patch/options and assert them in a successful apply case.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/controller/common/applier_test.go` around lines 47 - 49, Update
stubCtrlClient.Patch to retain the received client.Patch and PatchOption values,
then extend the successful apply test assertions to verify client.Apply is used
with the expected FieldOwner and ForceOwnership options, in addition to checking
the patch count.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/controller/common/applier_test.go`:
- Around line 47-49: Update stubCtrlClient.Patch to retain the received
client.Patch and PatchOption values, then extend the successful apply test
assertions to verify client.Apply is used with the expected FieldOwner and
ForceOwnership options, in addition to checking the patch count.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 37a2c255-13f8-4e8a-9a03-a2cad993605a

📥 Commits

Reviewing files that changed from the base of the PR and between b919b00 and 45acb13.

📒 Files selected for processing (28)
  • pkg/controller/common/applier.go
  • pkg/controller/common/applier_test.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/networkpolicies_test.go
  • pkg/controller/istiocsr/rbacs.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/trustmanager/webhooks_test.go
🚧 Files skipped from review as they are similar to previous changes (23)
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/webhooks_test.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/rbacs.go

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

1 similar comment
@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

1 similar comment
@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from 45acb13 to 1f4112d Compare August 3, 2026 15:23

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/controller/istiocsr/rbacs.go (1)

285-307: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Duplicated RoleRef-immutability handling in createOrApplyRoleBindings and createOrApplyRoleBindingForLeases. Both functions repeat the identical Exists-check, log-message, Delete-with-NotFound-ignored block used to work around the immutable RoleRef field. Root cause: no shared helper for this delete-before-apply pattern.

  • pkg/controller/istiocsr/rbacs.go#L285-L307: extract the Exists/Delete block into a shared helper, e.g. deleteRoleBindingIfRoleRefChanged(desired *rbacv1.RoleBinding) error, and call it here before common.ApplyResource.
  • pkg/controller/istiocsr/rbacs.go#L331-L353: call the same extracted helper here instead of repeating the block.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/controller/istiocsr/rbacs.go` around lines 285 - 307, Extract the
duplicated RoleRef immutability Exists/Delete logic from
createOrApplyRoleBindings and createOrApplyRoleBindingForLeases into a shared
helper such as deleteRoleBindingIfRoleRefChanged(desired *rbacv1.RoleBinding)
error, preserving the existing logging, error conversion, and NotFound handling.
Update pkg/controller/istiocsr/rbacs.go lines 285-307 and 331-353 to call the
helper before common.ApplyResource; both sites require this replacement.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/controller/istiocsr/rbacs.go`:
- Around line 285-307: Extract the duplicated RoleRef immutability Exists/Delete
logic from createOrApplyRoleBindings and createOrApplyRoleBindingForLeases into
a shared helper such as deleteRoleBindingIfRoleRefChanged(desired
*rbacv1.RoleBinding) error, preserving the existing logging, error conversion,
and NotFound handling. Update pkg/controller/istiocsr/rbacs.go lines 285-307 and
331-353 to call the helper before common.ApplyResource; both sites require this
replacement.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 0c3bb0e3-481f-48c7-a4f6-3417bb869627

📥 Commits

Reviewing files that changed from the base of the PR and between 45acb13 and 1f4112d.

📒 Files selected for processing (28)
  • pkg/controller/common/applier.go
  • pkg/controller/common/applier_test.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/networkpolicies_test.go
  • pkg/controller/istiocsr/rbacs.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/istiocsr/serviceaccounts_test.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/trustmanager/webhooks_test.go
🚧 Files skipped from review as they are similar to previous changes (26)
  • pkg/controller/trustmanager/rbacs_test.go
  • pkg/controller/trustmanager/serviceaccounts.go
  • pkg/controller/trustmanager/serviceaccounts_test.go
  • pkg/controller/istiocsr/constants.go
  • pkg/controller/trustmanager/deployments_test.go
  • pkg/controller/istiocsr/install_istiocsr.go
  • pkg/controller/trustmanager/webhooks_test.go
  • pkg/controller/trustmanager/services.go
  • pkg/controller/trustmanager/services_test.go
  • pkg/controller/trustmanager/controller_test.go
  • pkg/controller/trustmanager/certificates_test.go
  • pkg/controller/istiocsr/serviceaccounts.go
  • pkg/controller/common/applier.go
  • pkg/controller/istiocsr/certificates.go
  • pkg/controller/istiocsr/networkpolicies.go
  • pkg/controller/istiocsr/services.go
  • pkg/controller/istiocsr/certificates_test.go
  • pkg/controller/trustmanager/webhooks.go
  • pkg/controller/istiocsr/install_instiocsr_test.go
  • pkg/controller/trustmanager/deployments.go
  • pkg/controller/trustmanager/certificates.go
  • pkg/controller/istiocsr/rbacs_test.go
  • pkg/controller/common/applier_test.go
  • pkg/controller/istiocsr/services_test.go
  • pkg/controller/trustmanager/rbacs.go
  • pkg/controller/istiocsr/networkpolicies_test.go

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

1 similar comment
@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest ci/prow/e2e-operator

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

5 similar comments
@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@sebrandon1

Copy link
Copy Markdown
Member Author

/retest

@openshift-ci-robot

openshift-ci-robot commented Aug 18, 2026

Copy link
Copy Markdown

@sebrandon1: This pull request references CM-1040 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the task to target the "5.1.0" version, but no target version was set.

Details

In response to this:

Summary

Extract a generic common.ApplyResourceT helper for Server-Side Apply (SSA) reconciliation and migrate both istiocsr and trustmanager controllers to use it, eliminating duplicated create-or-update boilerplate across the codebase.

  • Add common.ApplyResourceT in pkg/controller/common/applier.go -- a type-safe SSA reconciliation function with pluggable drift-detection callbacks and standardized event recording
  • Migrate trustmanager's 11 createOrApply methods to delegate to the shared helper, removing per-resource SSA plumbing that was already using Patch but duplicated across each resource type
  • Migrate istiocsr's 8 simple createOrApply methods from Create/UpdateWithRetry to SSA via the shared helper (Service, ServiceAccount, Certificate, Role, RoleBinding, NetworkPolicy, plus lease Role/RoleBinding)
  • Keep istiocsr's ClusterRole/ClusterRoleBinding methods unchanged -- they use GenerateName with List fallback and must Delete for immutable RoleRef changes, which does not fit the SSA helper pattern
  • Remove the istioCSRCreateRecon parameter and its "ResourceAlreadyExists" warning events from all migrated methods -- SSA is declarative and idempotent, so an existing resource is expected state rather than an anomaly
  • Handle RoleBinding roleRef immutability: when the roleRef changes, the binding is deleted and recreated rather than producing an update error
  • Net -212 lines of duplicated reconciliation boilerplate

Related PRs

This is part of an ordered refactoring series:

Jira

  • CM-1040 -- Extract shared ApplyResource helper and migrate istiocsr to SSA (To Do)

Test Plan

  • New applier_test.go with direct unit tests for ApplyResource covering create, no-op, update, Exists error, Patch error, and error classification
  • New networkpolicies_test.go with tests for asset decoding, namespace fallback, full apply, and error propagation
  • Updated istiocsr and trustmanager test suites to use Patch stubs instead of Create/Update stubs where methods were migrated to SSA
  • go test ./pkg/controller/common/... passes
  • go test ./pkg/controller/istiocsr/... passes
  • go test ./pkg/controller/trustmanager/... passes
  • go build ./... succeeds
  • make lint shows only pre-existing issues (none introduced)

Summary by CodeRabbit

  • New Features

  • Kubernetes resources are now reconciled using consistent server-side apply behavior across Istio CSR and trust-manager components.

  • Resource ownership and drift handling are standardized, including automatic updates when managed resources change.

  • Improved handling for immutable RBAC changes and retryable conflicts.

  • Bug Fixes

  • Error messages now consistently identify resource types and names.

  • Added coverage for resource creation, updates, conflicts, authorization failures, and patch errors.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

sebrandon1 added a commit to sebrandon1/cert-manager-operator that referenced this pull request Aug 18, 2026
Full-codebase unit test audit identified coverage gaps across 12 packages.
This PR addresses the general coverage gaps not tied to specific bug-fix PRs.

Tests for specific bugs have been moved to their respective fix PRs:
- updateCondition error aggregation -> PR openshift#462
- unsafe.Pointer validation tests -> PR openshift#438
- context-threading finalizer/status tests -> PR openshift#419
- client wrapper and network policy tests -> PR openshift#420
- reconcile_result and port/probe tests -> PR openshift#417

Remaining coverage in this PR:
- pkg/controller/certmanager: network policy validation, default controller,
  log level hook, deployment overrides, related images
- pkg/controller/common: utility functions (UpdateName, DecodeObjBytes)
- pkg/controller/istiocsr: validateIstioCSRConfig
- pkg/controller/trustmanager: managedAnnotationsModified, webhook drift
- pkg/features: IsIstioCSRFeatureGateEnabled, SetupWithFlagValue
- pkg/operator/operatorclient: GetOperatorState, EnsureFinalizer,
  RemoveFinalizer, ApplyOperatorStatus, GetUnsupportedConfigOverrides
- pkg/operator: buildCacheObjectList, addControllerCacheConfig,
  findExistingCacheEntry

Co-authored-by: Cursor <cursoragent@cursor.com>
@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from 7008995 to 3939028 Compare August 18, 2026 21:38
- Adds common.ApplyResource[T]() in pkg/controller/common/applier.go
- Migrates trustmanager's 11 createOrApply methods to the shared helper
- Migrates istiocsr's 8 simple createOrApply methods from Create/UpdateWithRetry
  to SSA via the shared helper
- Handles RoleBinding roleRef immutability with delete-and-recreate
- Net -212 lines of duplicated reconciliation boilerplate

Includes unit tests for:
- ApplyResource (create, no-op, update, Exists error, Patch error)
- Network policy asset decoding and apply
- CtrlClient wrapper methods (Exists, Get, Create, Update, Delete, List,
  UpdateWithRetry with conflict retry, Patch, StatusUpdate)

Co-authored-by: Cursor <cursoragent@cursor.com>
@sebrandon1
sebrandon1 force-pushed the extract-apply-resource branch from 3939028 to 2f612f8 Compare August 18, 2026 21:50
sebrandon1 added a commit to sebrandon1/cert-manager-operator that referenced this pull request Aug 18, 2026
Full-codebase unit test audit identified coverage gaps across 12 packages.
This PR addresses the general coverage gaps not tied to specific bug-fix PRs.

Tests for specific bugs have been moved to their respective fix PRs:
- updateCondition error aggregation -> PR openshift#462
- unsafe.Pointer validation tests -> PR openshift#438
- context-threading finalizer/status tests -> PR openshift#419
- client wrapper and network policy tests -> PR openshift#420
- reconcile_result and port/probe tests -> PR openshift#417

Remaining coverage in this PR:
- pkg/controller/certmanager: network policy validation, default controller,
  log level hook, deployment overrides, related images
- pkg/controller/common: utility functions (UpdateName, DecodeObjBytes)
- pkg/controller/istiocsr: validateIstioCSRConfig
- pkg/controller/trustmanager: managedAnnotationsModified, webhook drift
- pkg/features: IsIstioCSRFeatureGateEnabled, SetupWithFlagValue
- pkg/operator/operatorclient: GetOperatorState, EnsureFinalizer,
  RemoveFinalizer, ApplyOperatorStatus, GetUnsupportedConfigOverrides
- pkg/operator: buildCacheObjectList, addControllerCacheConfig,
  findExistingCacheEntry

Co-authored-by: Cursor <cursoragent@cursor.com>
@openshift-ci

openshift-ci Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

@sebrandon1: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-operator 2f612f8 link true /test e2e-operator
ci/prow/e2e-operator-tech-preview 2f612f8 link false /test e2e-operator-tech-preview
ci/prow/e2e-operator-consoleless 2f612f8 link false /test e2e-operator-consoleless

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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

Labels

jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants