Skip to content

Delete a Workload if the related object is absent - #779

Merged
Timofei Larkin (lllamnyp) merged 1 commit into
mainfrom
fix/delete-stale-workloads
Apr 9, 2025
Merged

Delete a Workload if the related object is absent#779
Timofei Larkin (lllamnyp) merged 1 commit into
mainfrom
fix/delete-stale-workloads

Conversation

@lllamnyp

@lllamnyp Timofei Larkin (lllamnyp) commented Apr 9, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • New Features
    • Introduced enhanced workload management that improves resource monitoring and lifecycle handling.
    • Added graceful error handling when workload resources are missing or marked for deletion, ensuring a more stable and reliable system operation.

Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
@coderabbitai

coderabbitai Bot commented Apr 9, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes add a new controller, WorkloadReconciler, to the project. In cmd/cozystack-controller/main.go, the reconciler is initialized with the manager’s client and scheme and registered with the controller manager, with error handling for registration failure. A new file, internal/controller/workload_controller.go, implements the Kubernetes controller pattern to reconcile workload resources. It includes a Reconcile method for handling workload events and a helper to fetch monitored objects, along with a SetupWithManager method that registers watches for workload objects.

Changes

File(s) Change Summary
cmd/cozystack-controller/main.go Added initialization for WorkloadReconciler with the manager; includes error logging and exit on setup failure.
internal/controller/workload_controller.go New file defining WorkloadReconciler with methods:
- Reconcile: processes workload events, fetches workload objects, handles deletion and error cases.
- SetupWithManager: registers controller with the manager.
- Helper: getMonitoredObject.

Sequence Diagram(s)

sequenceDiagram
    participant Main
    participant Manager
    participant WorkloadReconciler
    participant Workload

    Main->>Manager: Initialize controller
    Manager->>WorkloadReconciler: SetupWithManager()
    WorkloadReconciler-->>Manager: Registered
    Note over Manager: On workload event
    Manager->>WorkloadReconciler: Invoke Reconcile(request)
    WorkloadReconciler->>Workload: Fetch Workload Object
    alt Workload Found
        Workload-->>WorkloadReconciler: Return object details
        WorkloadReconciler->>Manager: Return success result
    else Workload Not Found / Error
        Workload-->>WorkloadReconciler: NotFound/Error
        WorkloadReconciler->>Manager: Log error / Delete workload
    end
Loading

Poem

I'm a hopping rabbit with a coder's delight,
New controllers spring forth, making workload handling bright.
I nibble on errors, logging them with skill,
Reconciler magic now working at will.
With each line of code, my joy takes flight—
Cheers to this change, a garden of code in sight! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 0

🧹 Nitpick comments (2)
internal/controller/workload_controller.go (2)

62-68: Update comment to reflect “Workload” rather than “WorkloadMonitor.”

The controller registers watches for Workload objects, but the comment still references WorkloadMonitor. Adjusting the comment ensures consistency.

-		// Watch WorkloadMonitor objects
+		// Watch Workload objects

70-87: Consider a more robust approach for identifying monitored object types.

Relying on name prefixes works but can be brittle if naming conflicts arise. As an optional enhancement, storing a resource type in Workload spec or using labels/annotations could improve clarity and avoid collisions.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4b57529 and fea1427.

📒 Files selected for processing (2)
  • cmd/cozystack-controller/main.go (1 hunks)
  • internal/controller/workload_controller.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
internal/controller/workload_controller.go (2)
pkg/apiserver/apiserver.go (1)
  • Scheme (41-41)
api/v1alpha1/workload_types.go (1)
  • Workload (52-57)
cmd/cozystack-controller/main.go (2)
internal/controller/workload_controller.go (1)
  • WorkloadReconciler (19-22)
pkg/apiserver/apiserver.go (1)
  • Scheme (41-41)
🔇 Additional comments (3)
cmd/cozystack-controller/main.go (1)

181-188: Ensure proper test coverage and confirm alignment with existing reconcilers.

This code block correctly initializes the new WorkloadReconciler and logs any setup failure before exiting. Please confirm that:

  1. Required unit or integration tests exist for this new controller setup.
  2. Logging categories and error messages remain consistent with your existing WorkloadMonitorReconciler logic.
internal/controller/workload_controller.go (2)

18-22: No immediate issues; consider verifying concurrency tolerances.

Defining the WorkloadReconciler struct embedding client.Client with a Scheme is a standard controller-runtime approach. Ensure any concurrent updates to shared fields (if introduced later) are handled properly.


24-60: Verify deletion logic to avoid accidental resource race conditions.

This reconciler removes the Workload if the dependent object (PVC, Service, or Pod) does not exist. While it meets the PR objective, confirm you won’t accidentally delete the Workload if the monitored object is briefly unavailable or still being created. You may want to:

  • Re-check the object after a short delay, or
  • Add a finalizer approach for more controlled cleanup.

Copilot AI 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.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

@kvaps Andrei Kvapil (kvaps) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

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