Skip to content

refactor: improve code quality and error handling#4586

Open
bhargavpillati21 wants to merge 1 commit into
open-policy-agent:masterfrom
bhargavpillati21:feature/bhargav
Open

refactor: improve code quality and error handling#4586
bhargavpillati21 wants to merge 1 commit into
open-policy-agent:masterfrom
bhargavpillati21:feature/bhargav

Conversation

@bhargavpillati21

Copy link
Copy Markdown

Summary

This PR includes targeted refactoring improvements to enhance code quality, error handling, and maintainability across key packages.

Changes

1. ConcurrentErrorSlice Safety (pkg/syncutil/concurrent_slice.go)

  • Added bounds checking to Last() method to prevent index out of range panics
  • Returns nil for empty slices instead of panicking
  • Improves reliability of concurrent error collection

2. Audit Manager Optimizations (pkg/audit/manager.go)

  • readUnstructured(): Removed unnecessary empty map initialization

    • Direct JSON unmarshaling initializes the Object field automatically
    • Reduces memory allocations and improves performance
  • removeAllFromDir(): Enhanced error handling

    • Properly distinguishes between io.EOF and other errors
    • Prevents incorrectly ignoring read errors
    • Improved variable shadowing with inline error checks

3. Readiness Package (pkg/readiness/list.go)

  • Reduced redundant logging in retryLister()
    • Removed duplicate 'err' parameter from log.Error call
    • Cleaner and more accurate error logging

4. Upgrade Manager (pkg/upgrade/manager.go)

  • Improved initialization in New() constructor

    • Initialize client field at construction instead of leaving it nil
    • Prevents nil pointer dereference risks
    • Cleaner and more efficient code
  • Fixed documentation: Corrected comment from "audit" to "upgrade"

Impact

  • ✅ Prevents runtime panics in concurrent error handling
  • ✅ Reduces memory allocations in JSON unmarshaling
  • ✅ Improves error handling robustness
  • ✅ Enhances code maintainability and clarity
  • ✅ No breaking changes or functional modifications

Testing

All changes maintain existing functionality while improving implementation details. Code compiles successfully without errors.

Type

  • Refactoring
  • Code Quality Improvement
  • Bug Prevention

- Add bounds checking to ConcurrentErrorSlice.Last() to prevent panics on empty slices
- Optimize readUnstructured() by removing unnecessary map initialization in JSON unmarshaling
- Improve removeAllFromDir() error handling to properly distinguish between EOF and other errors
- Fix redundant error logging in retryLister() by removing duplicate 'err' parameter
- Initialize client field in upgrade Manager.New() instead of leaving it nil
- Fix incorrect comment in upgrade Manager (audit -> upgrade)
@bhargavpillati21 bhargavpillati21 requested a review from a team as a code owner May 23, 2026 15:19
Copilot AI review requested due to automatic review settings May 23, 2026 15:19
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR tightens up several utility behaviors and logging, and refactors manager construction.

Changes:

  • Refactor upgrade.Manager constructor to return an initialized struct literal (and update its doc comment).
  • Make ConcurrentErrorSlice.Last() safe on empty slices by returning nil.
  • Improve audit dir cleanup error handling and simplify unstructured JSON unmarshalling; adjust readiness listing log message.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
pkg/upgrade/manager.go Refactors New() and updates constructor behavior for the upgrade manager.
pkg/syncutil/concurrent_slice.go Prevents panic in Last() when the slice is empty.
pkg/readiness/list.go Tweaks error log message/fields when listing fails.
pkg/audit/manager.go Fixes error handling when reading directory entries and simplifies JSON unmarshalling into Unstructured.

Comment thread pkg/upgrade/manager.go
Comment on lines 34 to 43
mgr manager.Manager
}

// New creates a new manager for audit.
// New creates a new manager for upgrade.
func New(mgr manager.Manager) *Manager {
am := &Manager{
mgr: mgr,
return &Manager{
client: mgr.GetClient(),
mgr: mgr,
}
return am
}
@JaydipGabani

Copy link
Copy Markdown
Contributor

@bhargavpillati21 can you fix DCO?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants