Skip to content

Commit 5b8ab7d

Browse files
committed
feat(coredns): rename ownerId and ownedBy to owner
Signed-off-by: Jan Jansen <[email protected]>
1 parent e94df42 commit 5b8ab7d

File tree

3 files changed

+120
-74
lines changed

3 files changed

+120
-74
lines changed

docs/tutorials/coredns.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ This features works directly without any change to CoreDNS. CoreDNS will ignore
3333

3434
### Other entries inside etcd
3535

36-
Service entries in etcd without an `ownedby` field will be filtered out by the provider if `strictly-owned` is activated.
37-
Warning: If you activate `strictly-owned` afterwards, these entries will be ignored as the `ownedby` field is empty.
36+
Service entries in etcd without an `owner` field will be filtered out by the provider if `strictly-owned` is activated.
37+
Warning: If you activate `strictly-owned` afterwards, these entries will be ignored as the `owner` field is empty.
3838

3939
### Ways to migrate to a multi cluster setup
4040

4141
Ways:
4242

43-
1. Add the correct owner to all services inside etcd by adding the field `ownedby` to the JSON.
43+
1. Add the correct owner to all services inside etcd by adding the field `owner` to the JSON.
4444
2. Remove all services and allow them to be required again after restarting the provider. (Possible downtime.)
4545

4646
## Specific service annotation options

provider/coredns/coredns.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type coreDNSClient interface {
5656
type coreDNSProvider struct {
5757
provider.BaseProvider
5858
dryRun bool
59+
strictlyOwned bool
5960
coreDNSPrefix string
6061
domainFilter *endpoint.DomainFilter
6162
client coreDNSClient
@@ -86,13 +87,13 @@ type Service struct {
8687
// Etcd key where we found this service and ignored from json un-/marshaling
8788
Key string `json:"-"`
8889

89-
// OwnedBy is used to prevent service to be added by different external-dns (only used by external-dns)
90-
OwnedBy string `json:"ownedby,omitempty"`
90+
// Owner is used to prevent service to be added by different external-dns (only used by external-dns)
91+
Owner string `json:"owner,omitempty"`
9192
}
9293

9394
type etcdClient struct {
9495
client *etcdcv3.Client
95-
ownerID string
96+
owner string
9697
strictlyOwned bool
9798
}
9899

@@ -116,7 +117,7 @@ func (c etcdClient) GetServices(ctx context.Context, prefix string) ([]*Service,
116117
if err != nil {
117118
return nil, err
118119
}
119-
if c.strictlyOwned && svc.OwnedBy != c.ownerID {
120+
if c.strictlyOwned && svc.Owner != c.owner {
120121
continue
121122
}
122123
b := Service{
@@ -149,7 +150,7 @@ func (c etcdClient) SaveService(ctx context.Context, service *Service) error {
149150
defer cancel()
150151

151152
// check only for empty OwnedBy
152-
if c.strictlyOwned && service.OwnedBy != c.ownerID {
153+
if c.strictlyOwned && service.Owner != c.owner {
153154
r, err := c.client.Get(ctx, service.Key)
154155
if err != nil {
155156
return fmt.Errorf("etcd get %q: %w", service.Key, err)
@@ -160,11 +161,11 @@ func (c etcdClient) SaveService(ctx context.Context, service *Service) error {
160161
if err != nil {
161162
return fmt.Errorf("failed to unmarshal value for key %q: %w", service.Key, err)
162163
}
163-
if svc.OwnedBy != c.ownerID {
164+
if svc.Owner != c.owner {
164165
return fmt.Errorf("key %q is not owned by this provider", service.Key)
165166
}
166167
}
167-
service.OwnedBy = c.ownerID
168+
service.Owner = c.owner
168169
}
169170

170171
value, err := json.Marshal(&service)
@@ -193,7 +194,7 @@ func (c etcdClient) DeleteService(ctx context.Context, key string) error {
193194
if err != nil {
194195
return err
195196
}
196-
if svc.OwnedBy != c.ownerID {
197+
if svc.Owner != c.owner {
197198
continue
198199
}
199200

@@ -248,7 +249,7 @@ func getETCDConfig() (*etcdcv3.Config, error) {
248249
}
249250

250251
// the newETCDClient is an etcd client constructor
251-
func newETCDClient(ownerID string, strictlyOwned bool) (coreDNSClient, error) {
252+
func newETCDClient(owner string, strictlyOwned bool) (coreDNSClient, error) {
252253
cfg, err := getETCDConfig()
253254
if err != nil {
254255
return nil, err
@@ -257,19 +258,20 @@ func newETCDClient(ownerID string, strictlyOwned bool) (coreDNSClient, error) {
257258
if err != nil {
258259
return nil, err
259260
}
260-
return etcdClient{c, ownerID, strictlyOwned}, nil
261+
return etcdClient{c, owner, strictlyOwned}, nil
261262
}
262263

263264
// NewCoreDNSProvider is a CoreDNS provider constructor
264-
func NewCoreDNSProvider(domainFilter *endpoint.DomainFilter, prefix, ownerID string, strictlyOwned, dryRun bool) (provider.Provider, error) {
265-
client, err := newETCDClient(ownerID, strictlyOwned)
265+
func NewCoreDNSProvider(domainFilter *endpoint.DomainFilter, prefix, owner string, strictlyOwned, dryRun bool) (provider.Provider, error) {
266+
client, err := newETCDClient(owner, strictlyOwned)
266267
if err != nil {
267268
return nil, err
268269
}
269270

270271
return coreDNSProvider{
271272
client: client,
272273
dryRun: dryRun,
274+
strictlyOwned: strictlyOwned,
273275
coreDNSPrefix: prefix,
274276
domainFilter: domainFilter,
275277
}, nil
@@ -331,6 +333,9 @@ func (p coreDNSProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
331333
}
332334
log.Debugf("Creating new ep (%s) with new service host (%s)", ep, service.Host)
333335
}
336+
if p.strictlyOwned {
337+
ep.Labels[endpoint.OwnerLabelKey] = service.Owner
338+
}
334339
ep.Labels["originalText"] = service.Text
335340
ep.Labels[randomPrefixLabel] = prefix
336341
ep.Labels[service.Host] = prefix
@@ -342,6 +347,9 @@ func (p coreDNSProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
342347
endpoint.RecordTypeTXT,
343348
service.Text,
344349
)
350+
if p.strictlyOwned {
351+
ep.Labels[endpoint.OwnerLabelKey] = service.Owner
352+
}
345353
ep.Labels[randomPrefixLabel] = prefix
346354
result = append(result, ep)
347355
}

0 commit comments

Comments
 (0)