|
| 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 | + "regexp" |
| 22 | + "strings" |
| 23 | + |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + |
| 26 | + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1" |
| 27 | +) |
| 28 | + |
| 29 | +// IsPaused returns true if the Cluster is paused or the object has the `paused` annotation. |
| 30 | +func IsPaused(cluster *clusterv1.Cluster, o metav1.Object) bool { |
| 31 | + if cluster.Spec.Paused { |
| 32 | + return true |
| 33 | + } |
| 34 | + return HasPaused(o) |
| 35 | +} |
| 36 | + |
| 37 | +// IsExternallyManaged returns true if the object has the `managed-by` annotation. |
| 38 | +func IsExternallyManaged(o metav1.Object) bool { |
| 39 | + return hasAnnotation(o, clusterv1.ManagedByAnnotation) |
| 40 | +} |
| 41 | + |
| 42 | +// HasPaused returns true if the object has the `paused` annotation. |
| 43 | +func HasPaused(o metav1.Object) bool { |
| 44 | + return hasAnnotation(o, clusterv1.PausedAnnotation) |
| 45 | +} |
| 46 | + |
| 47 | +// HasSkipRemediation returns true if the object has the `skip-remediation` annotation. |
| 48 | +func HasSkipRemediation(o metav1.Object) bool { |
| 49 | + return hasAnnotation(o, clusterv1.MachineSkipRemediationAnnotation) |
| 50 | +} |
| 51 | + |
| 52 | +// HasRemediateMachine returns true if the object has the `remediate-machine` annotation. |
| 53 | +func HasRemediateMachine(o metav1.Object) bool { |
| 54 | + return hasAnnotation(o, clusterv1.RemediateMachineAnnotation) |
| 55 | +} |
| 56 | + |
| 57 | +// HasWithPrefix returns true if at least one of the annotations has the prefix specified. |
| 58 | +func HasWithPrefix(prefix string, annotations map[string]string) bool { |
| 59 | + for key := range annotations { |
| 60 | + if strings.HasPrefix(key, prefix) { |
| 61 | + return true |
| 62 | + } |
| 63 | + } |
| 64 | + return false |
| 65 | +} |
| 66 | + |
| 67 | +// ReplicasManagedByExternalAutoscaler returns true if the standard annotation for external autoscaler is present. |
| 68 | +func ReplicasManagedByExternalAutoscaler(o metav1.Object) bool { |
| 69 | + return hasTruthyAnnotationValue(o, clusterv1.ReplicasManagedByAnnotation) |
| 70 | +} |
| 71 | + |
| 72 | +// AddAnnotations sets the desired annotations on the object and returns true if the annotations have changed. |
| 73 | +func AddAnnotations(o metav1.Object, desired map[string]string) bool { |
| 74 | + if len(desired) == 0 { |
| 75 | + return false |
| 76 | + } |
| 77 | + annotations := o.GetAnnotations() |
| 78 | + if annotations == nil { |
| 79 | + annotations = make(map[string]string) |
| 80 | + } |
| 81 | + hasChanged := false |
| 82 | + for k, v := range desired { |
| 83 | + if cur, ok := annotations[k]; !ok || cur != v { |
| 84 | + annotations[k] = v |
| 85 | + hasChanged = true |
| 86 | + } |
| 87 | + } |
| 88 | + o.SetAnnotations(annotations) |
| 89 | + return hasChanged |
| 90 | +} |
| 91 | + |
| 92 | +// GetManagedAnnotations filters out and returns the CAPI-managed annotations for a Machine, with an optional list of regex patterns for user-specified annotations. |
| 93 | +func GetManagedAnnotations(m *clusterv1.Machine, additionalSyncMachineAnnotations ...*regexp.Regexp) map[string]string { |
| 94 | + // Always sync CAPI's bookkeeping annotations |
| 95 | + managedAnnotations := map[string]string{ |
| 96 | + clusterv1.ClusterNameAnnotation: m.Spec.ClusterName, |
| 97 | + clusterv1.ClusterNamespaceAnnotation: m.GetNamespace(), |
| 98 | + clusterv1.MachineAnnotation: m.Name, |
| 99 | + } |
| 100 | + if owner := metav1.GetControllerOfNoCopy(m); owner != nil { |
| 101 | + managedAnnotations[clusterv1.OwnerKindAnnotation] = owner.Kind |
| 102 | + managedAnnotations[clusterv1.OwnerNameAnnotation] = owner.Name |
| 103 | + } |
| 104 | + for key, value := range m.GetAnnotations() { |
| 105 | + // Always sync CAPI's default annotation node domain |
| 106 | + dnsSubdomainOrName := strings.Split(key, "/")[0] |
| 107 | + if dnsSubdomainOrName == clusterv1.ManagedNodeAnnotationDomain || strings.HasSuffix(dnsSubdomainOrName, "."+clusterv1.ManagedNodeAnnotationDomain) { |
| 108 | + managedAnnotations[key] = value |
| 109 | + continue |
| 110 | + } |
| 111 | + // Sync if the annotations matches at least one user provided regex |
| 112 | + for _, regex := range additionalSyncMachineAnnotations { |
| 113 | + if regex.MatchString(key) { |
| 114 | + managedAnnotations[key] = value |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return managedAnnotations |
| 120 | +} |
| 121 | + |
| 122 | +// hasAnnotation returns true if the object has the specified annotation. |
| 123 | +func hasAnnotation(o metav1.Object, annotation string) bool { |
| 124 | + annotations := o.GetAnnotations() |
| 125 | + if annotations == nil { |
| 126 | + return false |
| 127 | + } |
| 128 | + _, ok := annotations[annotation] |
| 129 | + return ok |
| 130 | +} |
| 131 | + |
| 132 | +// hasTruthyAnnotationValue returns true if the object has an annotation with a value that is not "false". |
| 133 | +func hasTruthyAnnotationValue(o metav1.Object, annotation string) bool { |
| 134 | + annotations := o.GetAnnotations() |
| 135 | + if annotations == nil { |
| 136 | + return false |
| 137 | + } |
| 138 | + if val, ok := annotations[annotation]; ok { |
| 139 | + return val != "false" |
| 140 | + } |
| 141 | + return false |
| 142 | +} |
0 commit comments