-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add dry run option for cleanup subcommand #21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import ( | |
| kubevirtv1 "kubevirt.io/api/core/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/opdev/virtwork/internal/config" | ||
| "github.com/opdev/virtwork/internal/constants" | ||
| ) | ||
|
|
||
|
|
@@ -30,7 +31,7 @@ type CleanupResult struct { | |
| // If runID is non-empty, only resources with that specific virtwork/run-id label are deleted. | ||
| // Individual deletion failures are recorded but do not abort the operation. | ||
| // If deleteNamespace is true, the namespace itself is deleted as the final step. | ||
| func CleanupAll(ctx context.Context, c client.Client, namespace string, deleteNamespace bool, runID string) (*CleanupResult, error) { | ||
| func CleanupAll(ctx context.Context, c client.Client, cfg *config.Config, deleteNamespace bool, runID string) (*CleanupResult, error) { | ||
| result := &CleanupResult{} | ||
| managedLabels := map[string]string{ | ||
| constants.LabelManagedBy: constants.ManagedByValue, | ||
|
|
@@ -41,18 +42,25 @@ func CleanupAll(ctx context.Context, c client.Client, namespace string, deleteNa | |
|
|
||
| runIDSet := make(map[string]struct{}) | ||
|
|
||
| // Setup delete options | ||
| opts := []client.DeleteOption{} | ||
| if cfg.DryRun { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When dry run is true, controller runtime delete will be called with the dry run all option. |
||
| opts = append(opts, client.DryRunAll) | ||
| } | ||
|
|
||
| // Delete VMs by label | ||
| vmList := &kubevirtv1.VirtualMachineList{} | ||
| listOpts := []client.ListOption{ | ||
| client.InNamespace(namespace), | ||
| client.InNamespace(cfg.Namespace), | ||
| client.MatchingLabels(managedLabels), | ||
| } | ||
| if err := c.List(ctx, vmList, listOpts...); err != nil { | ||
| return result, fmt.Errorf("listing VMs in %s: %w", namespace, err) | ||
| return result, fmt.Errorf("listing VMs in %s: %w", cfg.Namespace, err) | ||
| } | ||
|
|
||
| for i := range vmList.Items { | ||
| collectRunID(vmList.Items[i].Labels, runIDSet) | ||
| if err := c.Delete(ctx, &vmList.Items[i]); err != nil { | ||
| if err := c.Delete(ctx, &vmList.Items[i], opts...); err != nil { | ||
| if !apierrors.IsNotFound(err) { | ||
| result.Errors = append(result.Errors, fmt.Errorf("deleting VM %s: %w", vmList.Items[i].Name, err)) | ||
| } | ||
|
|
@@ -64,11 +72,11 @@ func CleanupAll(ctx context.Context, c client.Client, namespace string, deleteNa | |
| // Delete services by label | ||
| svcList := &corev1.ServiceList{} | ||
| if err := c.List(ctx, svcList, listOpts...); err != nil { | ||
| return result, fmt.Errorf("listing services in %s: %w", namespace, err) | ||
| return result, fmt.Errorf("listing services in %s: %w", cfg.Namespace, err) | ||
| } | ||
| for i := range svcList.Items { | ||
| collectRunID(svcList.Items[i].Labels, runIDSet) | ||
| if err := c.Delete(ctx, &svcList.Items[i]); err != nil { | ||
| if err := c.Delete(ctx, &svcList.Items[i], opts...); err != nil { | ||
| if !apierrors.IsNotFound(err) { | ||
| result.Errors = append(result.Errors, fmt.Errorf("deleting service %s: %w", svcList.Items[i].Name, err)) | ||
| } | ||
|
|
@@ -80,11 +88,11 @@ func CleanupAll(ctx context.Context, c client.Client, namespace string, deleteNa | |
| // Delete secrets by label | ||
| secretList := &corev1.SecretList{} | ||
| if err := c.List(ctx, secretList, listOpts...); err != nil { | ||
| return result, fmt.Errorf("listing secrets in %s: %w", namespace, err) | ||
| return result, fmt.Errorf("listing secrets in %s: %w", cfg.Namespace, err) | ||
| } | ||
| for i := range secretList.Items { | ||
| collectRunID(secretList.Items[i].Labels, runIDSet) | ||
| if err := c.Delete(ctx, &secretList.Items[i]); err != nil { | ||
| if err := c.Delete(ctx, &secretList.Items[i], opts...); err != nil { | ||
| if !apierrors.IsNotFound(err) { | ||
| result.Errors = append(result.Errors, fmt.Errorf("deleting secret %s: %w", secretList.Items[i].Name, err)) | ||
| } | ||
|
|
@@ -102,12 +110,12 @@ func CleanupAll(ctx context.Context, c client.Client, namespace string, deleteNa | |
| if deleteNamespace { | ||
| ns := &corev1.Namespace{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: namespace, | ||
| Name: cfg.Namespace, | ||
| }, | ||
| } | ||
| if err := c.Delete(ctx, ns); err != nil { | ||
| if !apierrors.IsNotFound(err) { | ||
| result.Errors = append(result.Errors, fmt.Errorf("deleting namespace %s: %w", namespace, err)) | ||
| result.Errors = append(result.Errors, fmt.Errorf("deleting namespace %s: %w", cfg.Namespace, err)) | ||
| } | ||
| } else { | ||
| result.NamespaceDeleted = true | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In lieu of adding an argument for dry run, I chose to pass in the *config.Config from which we can determine the namespace and if dry run is set to true.