...

Source file src/sigs.k8s.io/cli-utils/pkg/apply/filter/current-uids-filter.go

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

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package filter
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    10  	"k8s.io/apimachinery/pkg/types"
    11  	"k8s.io/apimachinery/pkg/util/sets"
    12  )
    13  
    14  // CurrentUIDFilter implements ValidationFilter interface to determine
    15  // if an object should not be pruned (deleted) because it has recently
    16  // been applied.
    17  type CurrentUIDFilter struct {
    18  	CurrentUIDs sets.String
    19  }
    20  
    21  // Name returns a filter identifier for logging.
    22  func (cuf CurrentUIDFilter) Name() string {
    23  	return "CurrentUIDFilter"
    24  }
    25  
    26  // Filter returns a ApplyPreventedDeletionError if the object prune/delete
    27  // should be skipped.
    28  func (cuf CurrentUIDFilter) Filter(obj *unstructured.Unstructured) error {
    29  	uid := obj.GetUID()
    30  	if cuf.CurrentUIDs.Has(string(uid)) {
    31  		return &ApplyPreventedDeletionError{UID: uid}
    32  	}
    33  	return nil
    34  }
    35  
    36  type ApplyPreventedDeletionError struct {
    37  	UID types.UID
    38  }
    39  
    40  func (e *ApplyPreventedDeletionError) Error() string {
    41  	return fmt.Sprintf("object just applied (UID: %q)", e.UID)
    42  }
    43  
    44  func (e *ApplyPreventedDeletionError) Is(err error) bool {
    45  	if err == nil {
    46  		return false
    47  	}
    48  	tErr, ok := err.(*ApplyPreventedDeletionError)
    49  	if !ok {
    50  		return false
    51  	}
    52  	return e.UID == tErr.UID
    53  }
    54  

View as plain text