sigs.k8s.io/cluster-api@v1.7.1/util/annotations/helpers.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package annotations implements annotation helper functions.
    18  package annotations
    19  
    20  import (
    21  	"strings"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    26  )
    27  
    28  // IsPaused returns true if the Cluster is paused or the object has the `paused` annotation.
    29  func IsPaused(cluster *clusterv1.Cluster, o metav1.Object) bool {
    30  	if cluster.Spec.Paused {
    31  		return true
    32  	}
    33  	return HasPaused(o)
    34  }
    35  
    36  // IsExternallyManaged returns true if the object has the `managed-by` annotation.
    37  func IsExternallyManaged(o metav1.Object) bool {
    38  	return hasAnnotation(o, clusterv1.ManagedByAnnotation)
    39  }
    40  
    41  // HasPaused returns true if the object has the `paused` annotation.
    42  func HasPaused(o metav1.Object) bool {
    43  	return hasAnnotation(o, clusterv1.PausedAnnotation)
    44  }
    45  
    46  // HasSkipRemediation returns true if the object has the `skip-remediation` annotation.
    47  func HasSkipRemediation(o metav1.Object) bool {
    48  	return hasAnnotation(o, clusterv1.MachineSkipRemediationAnnotation)
    49  }
    50  
    51  // HasRemediateMachine returns true if the object has the `remediate-machine` annotation.
    52  func HasRemediateMachine(o metav1.Object) bool {
    53  	return hasAnnotation(o, clusterv1.RemediateMachineAnnotation)
    54  }
    55  
    56  // HasWithPrefix returns true if at least one of the annotations has the prefix specified.
    57  func HasWithPrefix(prefix string, annotations map[string]string) bool {
    58  	for key := range annotations {
    59  		if strings.HasPrefix(key, prefix) {
    60  			return true
    61  		}
    62  	}
    63  	return false
    64  }
    65  
    66  // ReplicasManagedByExternalAutoscaler returns true if the standard annotation for external autoscaler is present.
    67  func ReplicasManagedByExternalAutoscaler(o metav1.Object) bool {
    68  	return hasTruthyAnnotationValue(o, clusterv1.ReplicasManagedByAnnotation)
    69  }
    70  
    71  // AddAnnotations sets the desired annotations on the object and returns true if the annotations have changed.
    72  func AddAnnotations(o metav1.Object, desired map[string]string) bool {
    73  	if len(desired) == 0 {
    74  		return false
    75  	}
    76  	annotations := o.GetAnnotations()
    77  	if annotations == nil {
    78  		annotations = make(map[string]string)
    79  	}
    80  	hasChanged := false
    81  	for k, v := range desired {
    82  		if cur, ok := annotations[k]; !ok || cur != v {
    83  			annotations[k] = v
    84  			hasChanged = true
    85  		}
    86  	}
    87  	o.SetAnnotations(annotations)
    88  	return hasChanged
    89  }
    90  
    91  // hasAnnotation returns true if the object has the specified annotation.
    92  func hasAnnotation(o metav1.Object, annotation string) bool {
    93  	annotations := o.GetAnnotations()
    94  	if annotations == nil {
    95  		return false
    96  	}
    97  	_, ok := annotations[annotation]
    98  	return ok
    99  }
   100  
   101  // hasTruthyAnnotationValue returns true if the object has an annotation with a value that is not "false".
   102  func hasTruthyAnnotationValue(o metav1.Object, annotation string) bool {
   103  	annotations := o.GetAnnotations()
   104  	if annotations == nil {
   105  		return false
   106  	}
   107  	if val, ok := annotations[annotation]; ok {
   108  		return val != "false"
   109  	}
   110  	return false
   111  }