Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/tutorials/coredns.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ This features works directly without any change to CoreDNS. CoreDNS will ignore

### Other entries inside etcd

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

### Ways to migrate to a multi cluster setup

Ways:

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

## Specific service annotation options
Expand Down
32 changes: 20 additions & 12 deletions provider/coredns/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type coreDNSClient interface {
type coreDNSProvider struct {
provider.BaseProvider
dryRun bool
strictlyOwned bool
coreDNSPrefix string
domainFilter *endpoint.DomainFilter
client coreDNSClient
Expand Down Expand Up @@ -86,13 +87,13 @@ type Service struct {
// Etcd key where we found this service and ignored from json un-/marshaling
Key string `json:"-"`

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

type etcdClient struct {
client *etcdcv3.Client
ownerID string
owner string
strictlyOwned bool
}

Expand All @@ -116,7 +117,7 @@ func (c etcdClient) GetServices(ctx context.Context, prefix string) ([]*Service,
if err != nil {
return nil, err
}
if c.strictlyOwned && svc.OwnedBy != c.ownerID {
if c.strictlyOwned && svc.Owner != c.owner {
continue
}
b := Service{
Expand Down Expand Up @@ -149,7 +150,7 @@ func (c etcdClient) SaveService(ctx context.Context, service *Service) error {
defer cancel()

// check only for empty OwnedBy
if c.strictlyOwned && service.OwnedBy != c.ownerID {
if c.strictlyOwned && service.Owner != c.owner {
r, err := c.client.Get(ctx, service.Key)
if err != nil {
return fmt.Errorf("etcd get %q: %w", service.Key, err)
Expand All @@ -160,11 +161,11 @@ func (c etcdClient) SaveService(ctx context.Context, service *Service) error {
if err != nil {
return fmt.Errorf("failed to unmarshal value for key %q: %w", service.Key, err)
}
if svc.OwnedBy != c.ownerID {
if svc.Owner != c.owner {
return fmt.Errorf("key %q is not owned by this provider", service.Key)
}
}
service.OwnedBy = c.ownerID
service.Owner = c.owner
}

value, err := json.Marshal(&service)
Expand Down Expand Up @@ -193,7 +194,7 @@ func (c etcdClient) DeleteService(ctx context.Context, key string) error {
if err != nil {
return err
}
if svc.OwnedBy != c.ownerID {
if svc.Owner != c.owner {
continue
}

Expand Down Expand Up @@ -248,7 +249,7 @@ func getETCDConfig() (*etcdcv3.Config, error) {
}

// the newETCDClient is an etcd client constructor
func newETCDClient(ownerID string, strictlyOwned bool) (coreDNSClient, error) {
func newETCDClient(owner string, strictlyOwned bool) (coreDNSClient, error) {
cfg, err := getETCDConfig()
if err != nil {
return nil, err
Expand All @@ -257,19 +258,20 @@ func newETCDClient(ownerID string, strictlyOwned bool) (coreDNSClient, error) {
if err != nil {
return nil, err
}
return etcdClient{c, ownerID, strictlyOwned}, nil
return etcdClient{c, owner, strictlyOwned}, nil
}

// NewCoreDNSProvider is a CoreDNS provider constructor
func NewCoreDNSProvider(domainFilter *endpoint.DomainFilter, prefix, ownerID string, strictlyOwned, dryRun bool) (provider.Provider, error) {
client, err := newETCDClient(ownerID, strictlyOwned)
func NewCoreDNSProvider(domainFilter *endpoint.DomainFilter, prefix, owner string, strictlyOwned, dryRun bool) (provider.Provider, error) {
client, err := newETCDClient(owner, strictlyOwned)
if err != nil {
return nil, err
}

return coreDNSProvider{
client: client,
dryRun: dryRun,
strictlyOwned: strictlyOwned,
coreDNSPrefix: prefix,
domainFilter: domainFilter,
}, nil
Expand Down Expand Up @@ -331,6 +333,9 @@ func (p coreDNSProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
}
log.Debugf("Creating new ep (%s) with new service host (%s)", ep, service.Host)
}
if p.strictlyOwned {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not too clear why this if is required

ep.Labels[endpoint.OwnerLabelKey] = service.Owner
}
ep.Labels["originalText"] = service.Text
ep.Labels[randomPrefixLabel] = prefix
ep.Labels[service.Host] = prefix
Expand All @@ -342,6 +347,9 @@ func (p coreDNSProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
endpoint.RecordTypeTXT,
service.Text,
)
if p.strictlyOwned {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, why if is required?

ep.Labels[endpoint.OwnerLabelKey] = service.Owner
}
ep.Labels[randomPrefixLabel] = prefix
result = append(result, ep)
}
Expand Down
Loading
Loading