github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/finalizer/finalizer.go (about)

     1  package finalizer
     2  
     3  type finalizer struct {
     4  	name string
     5  }
     6  
     7  type FinalizerManager interface {
     8  	GetFinalizers() []string
     9  	SetFinalizers(finalizers []string)
    10  }
    11  
    12  type Finalizer interface {
    13  	AddTo(instance FinalizerManager)
    14  	DeleteFrom(instance FinalizerManager)
    15  	IsDefinedIn(instance FinalizerManager) bool
    16  }
    17  
    18  func New(name string) Finalizer {
    19  	return &finalizer{
    20  		name: name,
    21  	}
    22  }
    23  
    24  func (f *finalizer) AddTo(instance FinalizerManager) {
    25  	finalizers := instance.GetFinalizers()
    26  
    27  	if f.containsString(finalizers, f.name) {
    28  		return
    29  	}
    30  
    31  	finalizers = append(finalizers, f.name)
    32  	instance.SetFinalizers(finalizers)
    33  }
    34  
    35  func (f *finalizer) DeleteFrom(instance FinalizerManager) {
    36  	finalizers := instance.GetFinalizers()
    37  	finalizers = f.removeString(finalizers, f.name)
    38  	instance.SetFinalizers(finalizers)
    39  }
    40  
    41  func (f *finalizer) IsDefinedIn(instance FinalizerManager) bool {
    42  	return f.containsString(instance.GetFinalizers(), f.name)
    43  }
    44  
    45  func (f *finalizer) containsString(slice []string, s string) bool {
    46  	for _, item := range slice {
    47  		if item == s {
    48  			return true
    49  		}
    50  	}
    51  	return false
    52  }
    53  
    54  func (f *finalizer) removeString(slice []string, s string) (result []string) {
    55  	for _, item := range slice {
    56  		if item == s {
    57  			continue
    58  		}
    59  		result = append(result, item)
    60  	}
    61  	return
    62  }