...

Source file src/sigs.k8s.io/cli-utils/pkg/apply/task/prune_task.go

Documentation: sigs.k8s.io/cli-utils/pkg/apply/task

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package task
     5  
     6  import (
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  	"k8s.io/klog/v2"
     9  	"sigs.k8s.io/cli-utils/pkg/apply/event"
    10  	"sigs.k8s.io/cli-utils/pkg/apply/filter"
    11  	"sigs.k8s.io/cli-utils/pkg/apply/prune"
    12  	"sigs.k8s.io/cli-utils/pkg/apply/taskrunner"
    13  	"sigs.k8s.io/cli-utils/pkg/common"
    14  	"sigs.k8s.io/cli-utils/pkg/object"
    15  )
    16  
    17  // PruneTask prunes objects from the cluster
    18  // by using the PruneOptions. The provided Objects is the
    19  // set of resources that have just been applied.
    20  type PruneTask struct {
    21  	TaskName string
    22  
    23  	Pruner            *prune.Pruner
    24  	Objects           object.UnstructuredSet
    25  	Filters           []filter.ValidationFilter
    26  	DryRunStrategy    common.DryRunStrategy
    27  	PropagationPolicy metav1.DeletionPropagation
    28  	// True if we are destroying, which deletes the inventory object
    29  	// as well (possibly) the inventory namespace.
    30  	Destroy bool
    31  }
    32  
    33  func (p *PruneTask) Name() string {
    34  	return p.TaskName
    35  }
    36  
    37  func (p *PruneTask) Action() event.ResourceAction {
    38  	action := event.PruneAction
    39  	if p.Destroy {
    40  		action = event.DeleteAction
    41  	}
    42  	return action
    43  }
    44  
    45  func (p *PruneTask) Identifiers() object.ObjMetadataSet {
    46  	return object.UnstructuredSetToObjMetadataSet(p.Objects)
    47  }
    48  
    49  // Start creates a new goroutine that will invoke
    50  // the Run function on the PruneOptions to update
    51  // the cluster. It will push a TaskResult on the taskChannel
    52  // to signal to the taskrunner that the task has completed (or failed).
    53  func (p *PruneTask) Start(taskContext *taskrunner.TaskContext) {
    54  	go func() {
    55  		klog.V(2).Infof("prune task starting (name: %q, objects: %d)",
    56  			p.Name(), len(p.Objects))
    57  		// Create filter to prevent deletion of currently applied
    58  		// objects. Must be done here to wait for applied UIDs.
    59  		uidFilter := filter.CurrentUIDFilter{
    60  			CurrentUIDs: taskContext.InventoryManager().AppliedResourceUIDs(),
    61  		}
    62  		p.Filters = append(p.Filters, uidFilter)
    63  		err := p.Pruner.Prune(
    64  			p.Objects,
    65  			p.Filters,
    66  			taskContext,
    67  			p.Name(),
    68  			prune.Options{
    69  				DryRunStrategy:    p.DryRunStrategy,
    70  				PropagationPolicy: p.PropagationPolicy,
    71  				Destroy:           p.Destroy,
    72  			},
    73  		)
    74  		klog.V(2).Infof("prune task completing (name: %q)", p.Name())
    75  		taskContext.TaskChannel() <- taskrunner.TaskResult{
    76  			Err: err,
    77  		}
    78  	}()
    79  }
    80  
    81  // Cancel is not supported by the PruneTask.
    82  func (p *PruneTask) Cancel(_ *taskrunner.TaskContext) {}
    83  
    84  // StatusUpdate is not supported by the PruneTask.
    85  func (p *PruneTask) StatusUpdate(_ *taskrunner.TaskContext, _ object.ObjMetadata) {}
    86  

View as plain text