forked from kubernetes-sigs/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt.go
159 lines (129 loc) · 4.5 KB
/
txt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package registry
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/kubernetes-incubator/external-dns/endpoint"
"github.com/kubernetes-incubator/external-dns/plan"
"github.com/kubernetes-incubator/external-dns/provider"
)
var (
txtLabelRegex = regexp.MustCompile("^\"heritage=external-dns,external-dns/owner=(.+)\"")
txtLabelFormat = "\"heritage=external-dns,external-dns/owner=%s\""
)
// TXTRegistry implements registry interface with ownership implemented via associated TXT records
type TXTRegistry struct {
provider provider.Provider
ownerID string //refers to the owner id of the current instance
mapper nameMapper
}
// NewTXTRegistry returns new TXTRegistry object
func NewTXTRegistry(provider provider.Provider, txtPrefix, ownerID string) (*TXTRegistry, error) {
if ownerID == "" {
return nil, errors.New("owner id cannot be empty")
}
mapper := newPrefixNameMapper(txtPrefix)
return &TXTRegistry{
provider: provider,
ownerID: ownerID,
mapper: mapper,
}, nil
}
// Records returns the current records from the registry excluding TXT Records
// If TXT records was created previously to indicate ownership its corresponding value
// will be added to the endpoints Labels map
func (im *TXTRegistry) Records(zone string) ([]*endpoint.Endpoint, error) {
records, err := im.provider.Records(zone)
if err != nil {
return nil, err
}
endpoints := make([]*endpoint.Endpoint, 0)
ownerMap := map[string]string{}
for _, record := range records {
if record.RecordType != "TXT" {
endpoints = append(endpoints, record)
continue
}
ownerID := im.extractOwnerID(record.Target)
if ownerID == "" {
//case when value of txt record cannot be identified
//record will not be removed as it will have empty owner
endpoints = append(endpoints, record)
continue
}
endpointDNSName := im.mapper.toEndpointName(record.DNSName)
ownerMap[endpointDNSName] = ownerID
}
for _, ep := range endpoints {
ep.Labels[endpoint.OwnerLabelKey] = ownerMap[ep.DNSName]
}
return endpoints, err
}
// ApplyChanges updates dns provider with the changes
// for each created/deleted record it will also take into account TXT records for creation/deletion
func (im *TXTRegistry) ApplyChanges(zone string, changes *plan.Changes) error {
filteredChanges := &plan.Changes{
Create: changes.Create,
UpdateNew: filterOwnedRecords(im.ownerID, changes.UpdateNew),
UpdateOld: filterOwnedRecords(im.ownerID, changes.UpdateOld),
Delete: filterOwnedRecords(im.ownerID, changes.Delete),
}
for _, r := range filteredChanges.Create {
txt := endpoint.NewEndpoint(im.mapper.toTXTName(r.DNSName), im.getTXTLabel(), "TXT")
filteredChanges.Create = append(filteredChanges.Create, txt)
}
for _, r := range filteredChanges.Delete {
txt := endpoint.NewEndpoint(im.mapper.toTXTName(r.DNSName), im.getTXTLabel(), "TXT")
filteredChanges.Delete = append(filteredChanges.Delete, txt)
}
return im.provider.ApplyChanges(zone, filteredChanges)
}
/**
TXT registry specific private methods
*/
func (im *TXTRegistry) getTXTLabel() string {
return fmt.Sprintf(txtLabelFormat, im.ownerID)
}
func (im *TXTRegistry) extractOwnerID(txtLabel string) string {
if matches := txtLabelRegex.FindStringSubmatch(txtLabel); len(matches) == 2 {
return matches[1]
}
return ""
}
/**
nameMapper defines interface which maps the dns name defined for the source
to the dns name which TXT record will be created with
*/
type nameMapper interface {
toEndpointName(string) string
toTXTName(string) string
}
type prefixNameMapper struct {
prefix string
}
var _ nameMapper = prefixNameMapper{}
func newPrefixNameMapper(prefix string) prefixNameMapper {
return prefixNameMapper{prefix: prefix}
}
func (pr prefixNameMapper) toEndpointName(txtDNSName string) string {
if strings.HasPrefix(txtDNSName, pr.prefix) {
return strings.TrimPrefix(txtDNSName, pr.prefix)
}
return ""
}
func (pr prefixNameMapper) toTXTName(endpointDNSName string) string {
return pr.prefix + endpointDNSName
}