aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar soulfy <soulfy@163.com> 2022-02-01 22:45:43 +0800
committerGravatar GitHub <noreply@github.com> 2022-02-01 09:45:43 -0500
commit49ee97994ee7677b521885eed965dac49728c77e (patch)
treeb477c6fdb0a0b1c695d939871ed4931a27321e29
parent6377e6a9d93a5a3b23d5ff62ec01a3bc273edcef (diff)
downloadcoredns-49ee97994ee7677b521885eed965dac49728c77e.tar.gz
coredns-49ee97994ee7677b521885eed965dac49728c77e.tar.zst
coredns-49ee97994ee7677b521885eed965dac49728c77e.zip
fix: convert key to domain (#5064)
fix convert key to domain when key ends with '/'
-rw-r--r--plugin/etcd/msg/path.go3
-rw-r--r--plugin/etcd/msg/path_test.go12
2 files changed, 15 insertions, 0 deletions
diff --git a/plugin/etcd/msg/path.go b/plugin/etcd/msg/path.go
index bfa458863..2c6cbff0f 100644
--- a/plugin/etcd/msg/path.go
+++ b/plugin/etcd/msg/path.go
@@ -22,6 +22,9 @@ func Path(s, prefix string) string {
// Domain is the opposite of Path.
func Domain(s string) string {
l := strings.Split(s, "/")
+ if l[len(l)-1] == "" {
+ l = l[:len(l)-1]
+ }
// start with 1, to strip /skydns
for i, j := 1, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
diff --git a/plugin/etcd/msg/path_test.go b/plugin/etcd/msg/path_test.go
index a9ec59713..a20d78333 100644
--- a/plugin/etcd/msg/path_test.go
+++ b/plugin/etcd/msg/path_test.go
@@ -10,3 +10,15 @@ func TestPath(t *testing.T) {
}
}
}
+
+func TestDomain(t *testing.T) {
+ result1 := Domain("/skydns/local/cluster/staging/service/")
+ if result1 != "service.staging.cluster.local." {
+ t.Errorf("Failure to get domain from etcd key (with a trailing '/'), expect: 'service.staging.cluster.local.', actually get: '%s'", result1)
+ }
+
+ result2 := Domain("/skydns/local/cluster/staging/service")
+ if result2 != "service.staging.cluster.local." {
+ t.Errorf("Failure to get domain from etcd key (without trailing '/'), expect: 'service.staging.cluster.local.' actually get: '%s'", result2)
+ }
+}