• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / external-dns / 15014204025

14 May 2025 06:52AM UTC coverage: 72.961% (+0.06%) from 72.897%
15014204025

push

github

web-flow
Merge pull request #5304 from gofogo/feat-code-cleanup-01

chore(source): code cleanup

323 of 333 new or added lines in 23 files covered. (97.0%)

1 existing line in 1 file now uncovered.

14911 of 20437 relevant lines covered (72.96%)

690.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.94
/source/annotations/processors.go
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 {
23✔
29
        aliasAnnotation, ok := annotations[AliasKey]
23✔
30
        return ok && aliasAnnotation == "true"
23✔
31
}
23✔
32

33
// TTLFromAnnotations extracts the TTL from the annotations of the given resource.
34
func TTLFromAnnotations(annotations map[string]string, resource string) endpoint.TTL {
12✔
35
        ttlNotConfigured := endpoint.TTL(0)
12✔
36
        ttlAnnotation, ok := annotations[TtlKey]
12✔
37
        if !ok {
14✔
38
                return ttlNotConfigured
2✔
39
        }
2✔
40
        ttlValue, err := parseTTL(ttlAnnotation)
10✔
41
        if err != nil {
13✔
42
                log.Warnf("%s: %q is not a valid TTL value: %v", resource, ttlAnnotation, err)
3✔
43
                return ttlNotConfigured
3✔
44
        }
3✔
45
        if ttlValue < ttlMinimum || ttlValue > ttlMaximum {
9✔
46
                log.Warnf("TTL value %q must be between [%d, %d]", ttlValue, ttlMinimum, ttlMaximum)
2✔
47
                return ttlNotConfigured
2✔
48
        }
2✔
49
        return endpoint.TTL(ttlValue)
5✔
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) {
10✔
58
        ttlDuration, errDuration := time.ParseDuration(s)
10✔
59
        if errDuration != nil {
18✔
60
                ttlInt, err := strconv.ParseInt(s, 10, 64)
8✔
61
                if err != nil {
11✔
62
                        return 0, errDuration
3✔
63
                }
3✔
64
                return ttlInt, nil
5✔
65
        }
66

67
        return int64(ttlDuration.Seconds()), nil
2✔
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) {
3✔
73
        labelSelector, err := metav1.ParseToLabelSelector(annotationFilter)
3✔
74
        if err != nil {
3✔
NEW
75
                return nil, err
×
NEW
76
        }
×
77
        selector, err := metav1.LabelSelectorAsSelector(labelSelector)
3✔
78
        if err != nil {
3✔
NEW
79
                return nil, err
×
NEW
80
        }
×
81
        return selector, nil
3✔
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 {
5✔
87
        var targets endpoint.Targets
5✔
88
        // Get the desired hostname of the ingress from the annotation.
5✔
89
        targetAnnotation, ok := annotations[TargetKey]
5✔
90
        if ok && targetAnnotation != "" {
9✔
91
                // splits the hostname annotation and removes the trailing periods
4✔
92
                targetsList := SplitHostnameAnnotation(targetAnnotation)
4✔
93
                for _, targetHostname := range targetsList {
11✔
94
                        targetHostname = strings.TrimSuffix(targetHostname, ".")
7✔
95
                        targets = append(targets, targetHostname)
7✔
96
                }
7✔
97
        }
98
        return targets
5✔
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 {
4✔
104
        return extractHostnamesFromAnnotations(input, HostnameKey)
4✔
105
}
4✔
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 {
4✔
110
        return extractHostnamesFromAnnotations(input, InternalHostnameKey)
4✔
111
}
4✔
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 {
14✔
116
        return strings.Split(strings.TrimSpace(strings.ReplaceAll(input, " ", "")), ",")
14✔
117
}
14✔
118

119
func extractHostnamesFromAnnotations(input map[string]string, key string) []string {
8✔
120
        annotation, ok := input[key]
8✔
121
        if !ok {
10✔
122
                return nil
2✔
123
        }
2✔
124
        return SplitHostnameAnnotation(annotation)
6✔
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc