|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package annotations |
| 15 | + |
| 16 | +import ( |
| 17 | + "strconv" |
| 18 | + "strings" |
| 19 | + "time" |
| 20 | + |
| 21 | + log "github.com/sirupsen/logrus" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/labels" |
| 24 | + |
| 25 | + "sigs.k8s.io/external-dns/endpoint" |
| 26 | +) |
| 27 | + |
| 28 | +func hasAliasFromAnnotations(annotations map[string]string) bool { |
| 29 | + aliasAnnotation, ok := annotations[AliasKey] |
| 30 | + return ok && aliasAnnotation == "true" |
| 31 | +} |
| 32 | + |
| 33 | +// TTLFromAnnotations extracts the TTL from the annotations of the given resource. |
| 34 | +func TTLFromAnnotations(annotations map[string]string, resource string) endpoint.TTL { |
| 35 | + ttlNotConfigured := endpoint.TTL(0) |
| 36 | + ttlAnnotation, ok := annotations[TtlKey] |
| 37 | + if !ok { |
| 38 | + return ttlNotConfigured |
| 39 | + } |
| 40 | + ttlValue, err := parseTTL(ttlAnnotation) |
| 41 | + if err != nil { |
| 42 | + log.Warnf("%s: %q is not a valid TTL value: %v", resource, ttlAnnotation, err) |
| 43 | + return ttlNotConfigured |
| 44 | + } |
| 45 | + if ttlValue < ttlMinimum || ttlValue > ttlMaximum { |
| 46 | + log.Warnf("TTL value %q must be between [%d, %d]", ttlValue, ttlMinimum, ttlMaximum) |
| 47 | + return ttlNotConfigured |
| 48 | + } |
| 49 | + return endpoint.TTL(ttlValue) |
| 50 | +} |
| 51 | + |
| 52 | +// parseTTL parses TTL from string, returning duration in seconds. |
| 53 | +// parseTTL supports both integers like "600" and durations based |
| 54 | +// on Go Duration like "10m", hence "600" and "10m" represent the same value. |
| 55 | +// |
| 56 | +// Note: for durations like "1.5s" the fraction is omitted (resulting in 1 second for the example). |
| 57 | +func parseTTL(s string) (int64, error) { |
| 58 | + ttlDuration, errDuration := time.ParseDuration(s) |
| 59 | + if errDuration != nil { |
| 60 | + ttlInt, err := strconv.ParseInt(s, 10, 64) |
| 61 | + if err != nil { |
| 62 | + return 0, errDuration |
| 63 | + } |
| 64 | + return ttlInt, nil |
| 65 | + } |
| 66 | + |
| 67 | + return int64(ttlDuration.Seconds()), nil |
| 68 | +} |
| 69 | + |
| 70 | +// ParseFilter parses an annotation filter string into a labels.Selector. |
| 71 | +// Returns nil if the annotation filter is invalid. |
| 72 | +func ParseFilter(annotationFilter string) (labels.Selector, error) { |
| 73 | + labelSelector, err := metav1.ParseToLabelSelector(annotationFilter) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + selector, err := metav1.LabelSelectorAsSelector(labelSelector) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + return selector, nil |
| 82 | +} |
| 83 | + |
| 84 | +// TargetsFromTargetAnnotation gets endpoints from optional "target" annotation. |
| 85 | +// Returns empty endpoints array if none are found. |
| 86 | +func TargetsFromTargetAnnotation(annotations map[string]string) endpoint.Targets { |
| 87 | + var targets endpoint.Targets |
| 88 | + // Get the desired hostname of the ingress from the annotation. |
| 89 | + targetAnnotation, ok := annotations[TargetKey] |
| 90 | + if ok && targetAnnotation != "" { |
| 91 | + // splits the hostname annotation and removes the trailing periods |
| 92 | + targetsList := SplitHostnameAnnotation(targetAnnotation) |
| 93 | + for _, targetHostname := range targetsList { |
| 94 | + targetHostname = strings.TrimSuffix(targetHostname, ".") |
| 95 | + targets = append(targets, targetHostname) |
| 96 | + } |
| 97 | + } |
| 98 | + return targets |
| 99 | +} |
| 100 | + |
| 101 | +// HostnamesFromAnnotations extracts the hostnames from the given annotations map. |
| 102 | +// It returns a slice of hostnames if the HostnameKey annotation is present, otherwise it returns nil. |
| 103 | +func HostnamesFromAnnotations(input map[string]string) []string { |
| 104 | + return extractHostnamesFromAnnotations(input, HostnameKey) |
| 105 | +} |
| 106 | + |
| 107 | +// InternalHostnamesFromAnnotations extracts the internal hostnames from the given annotations map. |
| 108 | +// It returns a slice of internal hostnames if the InternalHostnameKey annotation is present, otherwise it returns nil. |
| 109 | +func InternalHostnamesFromAnnotations(input map[string]string) []string { |
| 110 | + return extractHostnamesFromAnnotations(input, InternalHostnameKey) |
| 111 | +} |
| 112 | + |
| 113 | +// SplitHostnameAnnotation splits a comma-separated hostname annotation string into a slice of hostnames. |
| 114 | +// It trims any leading or trailing whitespace and removes any spaces within the anno |
| 115 | +func SplitHostnameAnnotation(input string) []string { |
| 116 | + return strings.Split(strings.TrimSpace(strings.ReplaceAll(input, " ", "")), ",") |
| 117 | +} |
| 118 | + |
| 119 | +func extractHostnamesFromAnnotations(input map[string]string, key string) []string { |
| 120 | + annotation, ok := input[key] |
| 121 | + if !ok { |
| 122 | + return nil |
| 123 | + } |
| 124 | + return SplitHostnameAnnotation(annotation) |
| 125 | +} |
0 commit comments