Skip to content

Commit 03bd179

Browse files
Add back more v1beta1 util functions from v1.10.3
With v1beta2, some v1beta1 util functions have been saved in util/deprecated/v1beta1 for consumers that still support v1beta1 objects. This patch adds more v1beta1-focused utils packages to the deprecated/v1beta1 folder: * util * util/annnotations * util/collections * util/predicates
1 parent d1e0fd4 commit 03bd179

17 files changed

+4757
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
//
19+
// Deprecated: This package is deprecated and is going to be removed when support for v1beta1 will be dropped. Please see https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20240916-improve-status-in-CAPI-resources.md for more details.
20+
package annotations
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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
18+
19+
import (
20+
"regexp"
21+
"strings"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
25+
clusterv1 "sigs.k8s.io/cluster-api/api/core/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+
// GetManagedAnnotations filters out and returns the CAPI-managed annotations for a Machine, with an optional list of regex patterns for user-specified annotations.
92+
func GetManagedAnnotations(m *clusterv1.Machine, additionalSyncMachineAnnotations ...*regexp.Regexp) map[string]string {
93+
// Always sync CAPI's bookkeeping annotations
94+
managedAnnotations := map[string]string{
95+
clusterv1.ClusterNameAnnotation: m.Spec.ClusterName,
96+
clusterv1.ClusterNamespaceAnnotation: m.GetNamespace(),
97+
clusterv1.MachineAnnotation: m.Name,
98+
}
99+
if owner := metav1.GetControllerOfNoCopy(m); owner != nil {
100+
managedAnnotations[clusterv1.OwnerKindAnnotation] = owner.Kind
101+
managedAnnotations[clusterv1.OwnerNameAnnotation] = owner.Name
102+
}
103+
for key, value := range m.GetAnnotations() {
104+
// Always sync CAPI's default annotation node domain
105+
dnsSubdomainOrName := strings.Split(key, "/")[0]
106+
if dnsSubdomainOrName == clusterv1.ManagedNodeAnnotationDomain || strings.HasSuffix(dnsSubdomainOrName, "."+clusterv1.ManagedNodeAnnotationDomain) {
107+
managedAnnotations[key] = value
108+
continue
109+
}
110+
// Sync if the annotations matches at least one user provided regex
111+
for _, regex := range additionalSyncMachineAnnotations {
112+
if regex.MatchString(key) {
113+
managedAnnotations[key] = value
114+
break
115+
}
116+
}
117+
}
118+
return managedAnnotations
119+
}
120+
121+
// hasAnnotation returns true if the object has the specified annotation.
122+
func hasAnnotation(o metav1.Object, annotation string) bool {
123+
annotations := o.GetAnnotations()
124+
if annotations == nil {
125+
return false
126+
}
127+
_, ok := annotations[annotation]
128+
return ok
129+
}
130+
131+
// hasTruthyAnnotationValue returns true if the object has an annotation with a value that is not "false".
132+
func hasTruthyAnnotationValue(o metav1.Object, annotation string) bool {
133+
annotations := o.GetAnnotations()
134+
if annotations == nil {
135+
return false
136+
}
137+
if val, ok := annotations[annotation]; ok {
138+
return val != "false"
139+
}
140+
return false
141+
}

0 commit comments

Comments
 (0)