Skip to content

Commit

Permalink
Merge pull request #130 from gianlucam76/delete-options
Browse files Browse the repository at this point in the history
Expose delete options
  • Loading branch information
gianlucam76 authored Sep 30, 2024
2 parents d544369 + 9a3a38a commit 8898a62
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 5 deletions.
27 changes: 27 additions & 0 deletions api/v1alpha1/cleaner_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ const (
CleanerFinalizer = "projectsveltos.io/cleaner-finalizer"
)

// DeleteOptions contains options for delete requests. It's generally a subset
// of metav1.DeleteOptions.
type DeleteOptions struct {
// GracePeriodSeconds is the duration in seconds before the object should be
// deleted. Value must be non-negative integer. The value zero indicates
// delete immediately. If this value is nil, the default grace period for the
// specified type will be used.
// +optional
GracePeriodSeconds *int64 `json:"gracePeriodSeconds,omitempty"`

// PropagationPolicy determined whether and how garbage collection will be
// performed. Either this field or OrphanDependents may be set, but not both.
// The default policy is decided by the existing finalizer set in the
// metadata.finalizers and the resource-specific default policy.
// Acceptable values are: 'Orphan' - orphan the dependents; 'Background' -
// allow the garbage collector to delete the dependents in the background;
// 'Foreground' - a cascading policy that deletes all dependents in the
// foreground.
// +optional
PropagationPolicy *metav1.DeletionPropagation `json:"propagationPolicy,omitempty"`
}

type ResourceSelector struct {
// Namespace of the resource deployed in the Cluster.
// Empty for resources scoped at cluster level.
Expand Down Expand Up @@ -147,6 +169,11 @@ type CleanerSpec struct {
// +kubebuilder:default:=Delete
Action Action `json:"action,omitempty"`

// DeleteOption is some configuration that modifies options for a delete request.
// This will be used only when action is delete
// +optional
DeleteOptions *DeleteOptions `json:"deleteOptions,omitempty"`

// Transform contains a function "transform" in lua language.
// When Action is set to *Transform*, this function will be invoked
// and be passed one of the object selected based on
Expand Down
35 changes: 33 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions config/crd/bases/apps.projectsveltos.io_cleaners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ spec:
- Transform
- Scan
type: string
deleteOptions:
description: |-
DeleteOption is some configuration that modifies options for a delete request.
This will be used only when action is delete
properties:
gracePeriodSeconds:
description: |-
GracePeriodSeconds is the duration in seconds before the object should be
deleted. Value must be non-negative integer. The value zero indicates
delete immediately. If this value is nil, the default grace period for the
specified type will be used.
format: int64
type: integer
propagationPolicy:
description: |-
PropagationPolicy determined whether and how garbage collection will be
performed. Either this field or OrphanDependents may be set, but not both.
The default policy is decided by the existing finalizer set in the
metadata.finalizers and the resource-specific default policy.
Acceptable values are: 'Orphan' - orphan the dependents; 'Background' -
allow the garbage collector to delete the dependents in the background;
'Foreground' - a cascading policy that deletes all dependents in the
foreground.
type: string
type: object
notifications:
description: Notification is a list of source of events to evaluate.
items:
Expand Down
13 changes: 10 additions & 3 deletions internal/controller/executor/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func processCleanerInstance(ctx context.Context, cleanerName string, logger logr
var processedResources []ResourceResult
switch cleaner.Spec.Action {
case appsv1alpha1.ActionDelete:
processedResources, err = deleteMatchingResources(ctx, resources, logger)
processedResources, err = deleteMatchingResources(ctx, resources, cleaner.Spec.DeleteOptions, logger)
case appsv1alpha1.ActionTransform:
processedResources, err = updateMatchingResources(ctx, resources, cleaner.Spec.Transform, logger)
case appsv1alpha1.ActionScan:
Expand Down Expand Up @@ -249,7 +249,7 @@ func getMatchingResources(ctx context.Context, sr *appsv1alpha1.ResourceSelector
}

func deleteMatchingResources(ctx context.Context, resources []ResourceResult,
logger logr.Logger) ([]ResourceResult, error) {
deleteOptions *appsv1alpha1.DeleteOptions, logger logr.Logger) ([]ResourceResult, error) {

processedResources := make([]ResourceResult, 0)

Expand All @@ -260,7 +260,14 @@ func deleteMatchingResources(ctx context.Context, resources []ResourceResult,
resource.Resource.GetNamespace(),
resource.Resource.GetName()))
l.Info("deleting resource")
if err := k8sClient.Delete(ctx, resource.Resource); err != nil {

options := &client.DeleteOptions{}
if deleteOptions != nil {
options.GracePeriodSeconds = deleteOptions.GracePeriodSeconds
options.PropagationPolicy = deleteOptions.PropagationPolicy
}

if err := k8sClient.Delete(ctx, resource.Resource, options); err != nil {
l.Info(fmt.Sprintf("failed to delete resource: %v", err))
return processedResources, err
}
Expand Down
25 changes: 25 additions & 0 deletions manifest/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ spec:
- Transform
- Scan
type: string
deleteOptions:
description: |-
DeleteOption is some configuration that modifies options for a delete request.
This will be used only when action is delete
properties:
gracePeriodSeconds:
description: |-
GracePeriodSeconds is the duration in seconds before the object should be
deleted. Value must be non-negative integer. The value zero indicates
delete immediately. If this value is nil, the default grace period for the
specified type will be used.
format: int64
type: integer
propagationPolicy:
description: |-
PropagationPolicy determined whether and how garbage collection will be
performed. Either this field or OrphanDependents may be set, but not both.
The default policy is decided by the existing finalizer set in the
metadata.finalizers and the resource-specific default policy.
Acceptable values are: 'Orphan' - orphan the dependents; 'Background' -
allow the garbage collector to delete the dependents in the background;
'Foreground' - a cascading policy that deletes all dependents in the
foreground.
type: string
type: object
notifications:
description: Notification is a list of source of events to evaluate.
items:
Expand Down

0 comments on commit 8898a62

Please sign in to comment.