...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/finalizers.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8s
    16  
    17  import (
    18  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    19  )
    20  
    21  func EnsureFinalizer(o metav1.Object, finalizer string) (found bool) {
    22  	for _, f := range o.GetFinalizers() {
    23  		if f == finalizer {
    24  			return true
    25  		}
    26  	}
    27  	o.SetFinalizers(append(o.GetFinalizers(), finalizer))
    28  	return false
    29  }
    30  
    31  func EnsureFinalizers(o metav1.Object, finalizers ...string) (found bool) {
    32  	found = true
    33  	for _, f := range finalizers {
    34  		partialFound := EnsureFinalizer(o, f)
    35  		if !partialFound {
    36  			found = false
    37  		}
    38  	}
    39  	return found
    40  }
    41  
    42  func RemoveFinalizer(o metav1.Object, finalizer string) {
    43  	found := false
    44  	var finalizers []string
    45  	for _, f := range o.GetFinalizers() {
    46  		if f != finalizer {
    47  			finalizers = append(finalizers, f)
    48  		} else {
    49  			found = true
    50  		}
    51  	}
    52  	if found {
    53  		o.SetFinalizers(finalizers)
    54  	}
    55  }
    56  
    57  func HasFinalizer(o metav1.Object, finalizer string) bool {
    58  	for _, f := range o.GetFinalizers() {
    59  		if f == finalizer {
    60  			return true
    61  		}
    62  	}
    63  	return false
    64  }
    65  

View as plain text