Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backup): delete snapshot immediately after backup completed #3505

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mantissahz
Copy link
Contributor

Which issue(s) this PR fixes:

Issue # longhorn/longhorn#9213

What this PR does / why we need it:

  • Introduce a new boolean field Spec.CleanupBackupSnasphot to check if it needs to delete the snapshot after the backup is completed when doing a recurring job.
  • Introduce a new field Spec.CleanupSnapshot to specify the backup will delete the snapshot after the backup is completed.

Special notes for your reviewer:

Additional documentation or context

@mantissahz mantissahz requested review from ChanYiLin, derekbit, c3y1huang and a team January 22, 2025 15:44
@mantissahz mantissahz self-assigned this Jan 22, 2025
Copy link

coderabbitai bot commented Jan 22, 2025

Warning

Rate limit exceeded

@mantissahz has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 17 minutes and 15 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 93ede1f and 7134b90.

📒 Files selected for processing (13)
  • api/model.go (1 hunks)
  • api/recurringjob.go (1 hunks)
  • api/snapshot.go (1 hunks)
  • app/recurringjob/job.go (1 hunks)
  • app/recurringjob/type.go (1 hunks)
  • app/recurringjob/volume.go (1 hunks)
  • client/generated_snapshot_input.go (1 hunks)
  • controller/backup_controller.go (1 hunks)
  • k8s/crds.yaml (2 hunks)
  • k8s/pkg/apis/longhorn/v1beta2/backup.go (1 hunks)
  • k8s/pkg/apis/longhorn/v1beta2/recurringjob.go (1 hunks)
  • manager/engine.go (2 hunks)
  • manager/recurringjob.go (1 hunks)

Walkthrough

This pull request introduces a new CleanupBackupSnapshot boolean field across multiple components of the Longhorn system. The change enables more granular control over backup snapshot cleanup during backup and recurring job operations. The modification spans several files in the API, client, controller, and manager layers, adding the ability to specify whether a backup snapshot should be automatically cleaned up after the backup process is completed.

Changes

File Change Summary
api/model.go Added CleanupBackupSnapshot bool to SnapshotInput struct
api/recurringjob.go Updated RecurringJobCreate to include CleanupBackupSnapshot
api/snapshot.go Modified SnapshotBackup method to pass CleanupBackupSnapshot
app/recurringjob/job.go Added cleanupBackupSnapshot to Job struct initialization
app/recurringjob/type.go Added cleanupBackupSnapshot bool to Job struct
app/recurringjob/volume.go Updated ActionSnapshotBackup method call with CleanupBackupSnapshot
client/generated_snapshot_input.go Added CleanupBackupSnapshot bool to SnapshotInput
k8s/crds.yaml Added cleanupSnapshot and cleanupBackupSnapshot fields to CRDs
k8s/pkg/apis/longhorn/v1beta2/backup.go Added CleanupSnapshot bool to BackupSpec
k8s/pkg/apis/longhorn/v1beta2/recurringjob.go Added CleanupBackupSnapshot bool to RecurringJobSpec
manager/engine.go Updated BackupSnapshot method signature to include cleanupSnap
manager/recurringjob.go Updated UpdateRecurringJob to handle CleanupBackupSnapshot

Sequence Diagram

sequenceDiagram
    participant User
    participant RecurringJob
    participant VolumeManager
    participant BackupController

    User->>RecurringJob: Create/Update with CleanupBackupSnapshot
    RecurringJob->>VolumeManager: Trigger Backup
    VolumeManager->>BackupController: Perform Backup
    BackupController->>BackupController: Check CleanupBackupSnapshot
    alt CleanupBackupSnapshot is true
        BackupController->>BackupController: Delete Snapshot
    end
Loading

Possibly related PRs

Suggested reviewers

  • derekbit
  • c3y1huang
  • innobead

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

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.

Copy link

@coderabbitai coderabbitai bot left a comment

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 (4)
client/generated_snapshot_input.go (1)

10-11: Add documentation for CleanupBackupSnapshot field

Consider adding a comment to document the purpose and behavior of this field, especially since it's part of the client API.

+	// CleanupBackupSnapshot determines whether to delete the snapshot immediately after backup completion
 	CleanupBackupSnapshot bool `json:"cleanupBackupSnapshot,omitempty" yaml:"cleanupBackupSnapshot,omitempty"`
app/recurringjob/type.go (1)

23-29: Improve field organization and documentation consistency

The fields should be logically grouped and documented consistently. Consider reorganizing the fields to keep related ones together.

 type Job struct {
 	api      *longhornclient.RancherClient // Rancher client used to interact with the Longhorn API.
 	lhClient *lhclientset.Clientset        // Kubernetes clientset for Longhorn resources.

 	eventRecorder record.EventRecorder // Used to record events related to the job.
 	logger        logrus.FieldLogger   // Log messages related to the job.

-	name                  string // Name for the RecurringJob.
-	namespace             string // Kubernetes namespace in which the RecurringJob is running.
-	retain                int    // Number of task CRs to retain.+
-	cleanupBackupSnapshot bool
-	task                  longhorn.RecurringJobType // Type of task to be executed.
-	parameters            map[string]string         // Additional parameters for the task.
-	executionCount        int                       // Number of times the job has been executed.
+	// Job configuration
+	name      string                    // Name for the RecurringJob.
+	namespace string                    // Kubernetes namespace in which the RecurringJob is running.
+	task      longhorn.RecurringJobType // Type of task to be executed.
+
+	// Task configuration
+	retain                int               // Number of task CRs to retain.
+	cleanupBackupSnapshot bool             // Whether to delete the snapshot after backup completion.
+	parameters            map[string]string // Additional parameters for the task.
+
+	// Runtime state
+	executionCount int // Number of times the job has been executed.
 }
manager/engine.go (1)

257-257: Add documentation for the new parameter.

Consider adding a comment to document the purpose of the cleanupSnap parameter.

+// BackupSnapshot creates a backup of the specified snapshot.
+// Parameters:
+//   backupName: name of the backup
+//   backupTargetName: name of the backup target
+//   volumeName: name of the volume
+//   snapshotName: name of the snapshot to backup
+//   labels: labels to apply to the backup
+//   backupMode: mode of the backup (full or incremental)
+//   cleanupSnap: whether to delete the snapshot after successful backup
 func (m *VolumeManager) BackupSnapshot(backupName, backupTargetName, volumeName, snapshotName string, labels map[string]string, backupMode string, cleanupSnap bool) error {
k8s/crds.yaml (1)

2879-2882: Consider simplifying the description, but the implementation is good.

The field is well-implemented and serves its purpose. Consider making the description more concise, similar to other fields in the CRD, e.g., "Whether to cleanup the snapshot after the backup is completed."

  cleanupBackupSnapshot:
-   description: The flag of cleaning up the corresponding snapshot after
-     the backup is completed.
+   description: Whether to cleanup the snapshot after the backup is completed.
    type: boolean
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca32967 and 93ede1f.

📒 Files selected for processing (13)
  • api/model.go (1 hunks)
  • api/recurringjob.go (1 hunks)
  • api/snapshot.go (1 hunks)
  • app/recurringjob/job.go (1 hunks)
  • app/recurringjob/type.go (1 hunks)
  • app/recurringjob/volume.go (1 hunks)
  • client/generated_snapshot_input.go (1 hunks)
  • controller/backup_controller.go (1 hunks)
  • k8s/crds.yaml (2 hunks)
  • k8s/pkg/apis/longhorn/v1beta2/backup.go (1 hunks)
  • k8s/pkg/apis/longhorn/v1beta2/recurringjob.go (1 hunks)
  • manager/engine.go (2 hunks)
  • manager/recurringjob.go (1 hunks)
👮 Files not reviewed due to content moderation or server errors (4)
  • app/recurringjob/volume.go
  • controller/backup_controller.go
  • api/model.go
  • api/recurringjob.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (7)
manager/recurringjob.go (1)

75-75: Consider adding validation for CleanupBackupSnapshot field

While the field update is implemented correctly, consider adding validation to ensure the field value is handled appropriately when used with different job types (e.g., backup vs. snapshot).

app/recurringjob/job.go (1)

67-73: Add validation for cleanupBackupSnapshot based on task type

The cleanupBackupSnapshot field should only be used with backup tasks. Consider adding validation to ensure this field is only set when appropriate.

k8s/pkg/apis/longhorn/v1beta2/recurringjob.go (1)

54-56: LGTM! Well-documented field addition.

The new CleanupBackupSnapshot field is properly documented, correctly tagged, and follows the existing struct field pattern.

k8s/pkg/apis/longhorn/v1beta2/backup.go (1)

54-56: LGTM! Well-documented field addition.

The new CleanupSnapshot field is properly documented, correctly tagged, and follows the existing struct field pattern.

api/snapshot.go (1)

198-198: LGTM! Proper propagation of cleanup flag.

The CleanupBackupSnapshot flag is correctly passed through to the manager layer while maintaining all existing validations and error handling.

manager/engine.go (1)

274-277: LGTM! Proper initialization of BackupSpec.

The CleanupSnapshot field is correctly initialized in the BackupSpec struct while maintaining the existing initialization pattern.

k8s/crds.yaml (1)

908-910: LGTM! The cleanupSnapshot field is well-defined.

The boolean field is appropriately added to the BackupSpec with a clear description that matches the PR objectives.

Introduce a new field `Spec.CleanupSnapshot` to specify the backup
will delete the snapshot after the backup is completed.

ref: longhorn/longhorn 9213

Signed-off-by: James Lu <james.lu@suse.com>
after backup completed for recurring jobs.

Introduce a new boolean field `Spec.CleanupBackupSnasphot` to
check if it needs to delete the snapshot after the backup is
completed when doing a recurring job.

ref: longhorn/longhorn 9213

Signed-off-by: James Lu <james.lu@suse.com>
ref: longhorn/longhorn 9213

Signed-off-by: James Lu <james.lu@suse.com>
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.

1 participant