Skip to content

Commit f0db4c2

Browse files
fix(annotations): initialize annotation keys at declaration time (#6159)
* fix(annotations): allow resetting annotation prefix to default value Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com> * fix(annotations): allow resetting annotation prefix to default value Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com> * fix(annotations): allow resetting annotation prefix to default value Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com> --------- Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
1 parent b304dfe commit f0db4c2

File tree

5 files changed

+34
-52
lines changed

5 files changed

+34
-52
lines changed

provider/cloudflare/cloudflare_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ import (
4242
"sigs.k8s.io/external-dns/source/annotations"
4343
)
4444

45-
// TestMain initializes annotation keys before running tests.
46-
// This is needed because init() was removed from annotations package.
47-
func TestMain(m *testing.M) {
48-
annotations.SetAnnotationPrefix("external-dns.alpha.kubernetes.io/")
49-
m.Run()
50-
}
51-
5245
// newCloudflareError creates a cloudflare.Error suitable for testing.
5346
// The v5 SDK's Error type panics when .Error() is called with nil Request/Response fields,
5447
// so this helper initializes them properly.

source/annotations/annotations.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,41 @@ var (
3131
AnnotationKeyPrefix = DefaultAnnotationPrefix
3232

3333
// CloudflareProxiedKey The annotation used for determining if traffic will go through Cloudflare
34-
CloudflareProxiedKey string
35-
CloudflareCustomHostnameKey string
36-
CloudflareRegionKey string
37-
CloudflareRecordCommentKey string
38-
CloudflareTagsKey string
34+
CloudflareProxiedKey = AnnotationKeyPrefix + "cloudflare-proxied"
35+
CloudflareCustomHostnameKey = AnnotationKeyPrefix + "cloudflare-custom-hostname"
36+
CloudflareRegionKey = AnnotationKeyPrefix + "cloudflare-region-key"
37+
CloudflareRecordCommentKey = AnnotationKeyPrefix + "cloudflare-record-comment"
38+
CloudflareTagsKey = AnnotationKeyPrefix + "cloudflare-tags"
3939

40-
AWSPrefix string
41-
CoreDNSPrefix string
42-
SCWPrefix string
43-
WebhookPrefix string
44-
CloudflarePrefix string
40+
AWSPrefix = AnnotationKeyPrefix + "aws-"
41+
CoreDNSPrefix = AnnotationKeyPrefix + "coredns-"
42+
SCWPrefix = AnnotationKeyPrefix + "scw-"
43+
WebhookPrefix = AnnotationKeyPrefix + "webhook-"
44+
CloudflarePrefix = AnnotationKeyPrefix + "cloudflare-"
4545

46-
TtlKey string
47-
SetIdentifierKey string
48-
AliasKey string
49-
TargetKey string
46+
TtlKey = AnnotationKeyPrefix + "ttl"
47+
SetIdentifierKey = AnnotationKeyPrefix + "set-identifier"
48+
AliasKey = AnnotationKeyPrefix + "alias"
49+
TargetKey = AnnotationKeyPrefix + "target"
5050
// ControllerKey The annotation used for figuring out which controller is responsible
51-
ControllerKey string
51+
ControllerKey = AnnotationKeyPrefix + "controller"
5252
// HostnameKey The annotation used for defining the desired hostname
53-
HostnameKey string
53+
HostnameKey = AnnotationKeyPrefix + "hostname"
5454
// AccessKey The annotation used for specifying whether the public or private interface address is used
55-
AccessKey string
55+
AccessKey = AnnotationKeyPrefix + "access"
5656
// EndpointsTypeKey The annotation used for specifying the type of endpoints to use for headless services
57-
EndpointsTypeKey string
57+
EndpointsTypeKey = AnnotationKeyPrefix + "endpoints-type"
5858
// Ingress the annotation used to determine if the gateway is implemented by an Ingress object
59-
Ingress string
59+
Ingress = AnnotationKeyPrefix + "ingress"
6060
// IngressHostnameSourceKey The annotation used to determine the source of hostnames for ingresses. This is an optional field - all
6161
// available hostname sources are used if not specified.
62-
IngressHostnameSourceKey string
62+
IngressHostnameSourceKey = AnnotationKeyPrefix + "ingress-hostname-source"
6363
// ControllerValue The value of the controller annotation so that we feel responsible
6464
ControllerValue = "dns-controller"
6565
// InternalHostnameKey The annotation used for defining the desired hostname
66-
InternalHostnameKey string
66+
InternalHostnameKey = AnnotationKeyPrefix + "internal-hostname"
6767
// The annotation used for defining the desired hostname source for gateways
68-
GatewayHostnameSourceKey string
68+
GatewayHostnameSourceKey = AnnotationKeyPrefix + "gateway-hostname-source"
6969
)
7070

7171
// SetAnnotationPrefix sets a custom annotation prefix and rebuilds all annotation keys.

source/annotations/annotations_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import (
2323
)
2424

2525
func TestSetAnnotationPrefix(t *testing.T) {
26-
// Save original values
27-
originalPrefix := AnnotationKeyPrefix
28-
defer SetAnnotationPrefix(originalPrefix)
26+
t.Cleanup(func() { SetAnnotationPrefix(DefaultAnnotationPrefix) })
2927

3028
// Test custom prefix
3129
customPrefix := "custom.io/"
@@ -59,17 +57,17 @@ func TestSetAnnotationPrefix(t *testing.T) {
5957
}
6058

6159
func TestDefaultAnnotationPrefix(t *testing.T) {
62-
assert.Equal(t, "external-dns.alpha.kubernetes.io/", AnnotationKeyPrefix)
63-
assert.Equal(t, "external-dns.alpha.kubernetes.io/hostname", HostnameKey)
64-
assert.Equal(t, "external-dns.alpha.kubernetes.io/internal-hostname", InternalHostnameKey)
65-
assert.Equal(t, "external-dns.alpha.kubernetes.io/ttl", TtlKey)
66-
assert.Equal(t, "external-dns.alpha.kubernetes.io/controller", ControllerKey)
60+
t.Cleanup(func() { SetAnnotationPrefix(DefaultAnnotationPrefix) })
61+
SetAnnotationPrefix(DefaultAnnotationPrefix)
62+
assert.Equal(t, DefaultAnnotationPrefix, AnnotationKeyPrefix)
63+
assert.Equal(t, DefaultAnnotationPrefix+"hostname", HostnameKey)
64+
assert.Equal(t, DefaultAnnotationPrefix+"internal-hostname", InternalHostnameKey)
65+
assert.Equal(t, DefaultAnnotationPrefix+"ttl", TtlKey)
66+
assert.Equal(t, DefaultAnnotationPrefix+"controller", ControllerKey)
6767
}
6868

6969
func TestSetAnnotationPrefixMultipleTimes(t *testing.T) {
70-
// Save original values
71-
originalPrefix := AnnotationKeyPrefix
72-
defer SetAnnotationPrefix(originalPrefix)
70+
t.Cleanup(func() { SetAnnotationPrefix(DefaultAnnotationPrefix) })
7371

7472
// Set first custom prefix
7573
SetAnnotationPrefix("first.io/")
@@ -82,7 +80,7 @@ func TestSetAnnotationPrefixMultipleTimes(t *testing.T) {
8280
assert.Equal(t, "second.io/hostname", HostnameKey)
8381

8482
// Restore to default
85-
SetAnnotationPrefix("external-dns.alpha.kubernetes.io/")
86-
assert.Equal(t, "external-dns.alpha.kubernetes.io/", AnnotationKeyPrefix)
87-
assert.Equal(t, "external-dns.alpha.kubernetes.io/hostname", HostnameKey)
83+
SetAnnotationPrefix(DefaultAnnotationPrefix)
84+
assert.Equal(t, DefaultAnnotationPrefix, AnnotationKeyPrefix)
85+
assert.Equal(t, DefaultAnnotationPrefix+"hostname", HostnameKey)
8886
}

source/annotations/processors_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,6 @@ func TestInternalHostnamesFromAnnotations(t *testing.T) {
366366
}
367367

368368
func TestIsControllerMismatch(t *testing.T) {
369-
SetAnnotationPrefix(DefaultAnnotationPrefix)
370-
371369
tests := []struct {
372370
name string
373371
annotations map[string]string

source/service_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ import (
4545
"sigs.k8s.io/external-dns/source/informers"
4646
)
4747

48-
// TestMain initializes annotation keys before running tests.
49-
// This is needed because init() was removed from annotations package.
50-
func TestMain(m *testing.M) {
51-
annotations.SetAnnotationPrefix("external-dns.alpha.kubernetes.io/")
52-
m.Run()
53-
}
54-
5548
type ServiceSuite struct {
5649
suite.Suite
5750
sc Source

0 commit comments

Comments
 (0)