-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(coredns): use txt-owner-id to strictly separated external-dns instances #5921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -84,10 +84,15 @@ 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"` | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| type etcdClient struct { | ||||||||||||||||
| client *etcdcv3.Client | ||||||||||||||||
| client *etcdcv3.Client | ||||||||||||||||
| ownerID string | ||||||||||||||||
| strictlyOwned bool | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| var _ coreDNSClient = etcdClient{} | ||||||||||||||||
|
|
@@ -110,12 +115,25 @@ func (c etcdClient) GetServices(ctx context.Context, prefix string) ([]*Service, | |||||||||||||||
| if err := json.Unmarshal(n.Value, svc); err != nil { | ||||||||||||||||
| return nil, fmt.Errorf("%s: %w", n.Key, err) | ||||||||||||||||
| } | ||||||||||||||||
| b := Service{Host: svc.Host, Port: svc.Port, Priority: svc.Priority, Weight: svc.Weight, Text: svc.Text, Key: string(n.Key)} | ||||||||||||||||
| b := Service{ | ||||||||||||||||
| Host: svc.Host, | ||||||||||||||||
| Port: svc.Port, | ||||||||||||||||
| Priority: svc.Priority, | ||||||||||||||||
| Weight: svc.Weight, | ||||||||||||||||
| Text: svc.Text, | ||||||||||||||||
| Key: string(n.Key), | ||||||||||||||||
| } | ||||||||||||||||
| if c.strictlyOwned { | ||||||||||||||||
| b.OwnedBy = svc.OwnedBy | ||||||||||||||||
| } | ||||||||||||||||
| if _, ok := bx[b]; ok { | ||||||||||||||||
| // skip the service if already added to service list. | ||||||||||||||||
| // the same service might be found in multiple etcd nodes. | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
| if c.strictlyOwned && b.OwnedBy != c.ownerID { | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
| bx[b] = true | ||||||||||||||||
|
|
||||||||||||||||
| svc.Key = string(n.Key) | ||||||||||||||||
|
|
@@ -132,6 +150,16 @@ func (c etcdClient) SaveService(ctx context.Context, service *Service) error { | |||||||||||||||
| ctx, cancel := context.WithTimeout(ctx, etcdTimeout) | ||||||||||||||||
| defer cancel() | ||||||||||||||||
|
|
||||||||||||||||
| if ownedBy, err := c.isOwnedBy(ctx, service.Key); err != nil { | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we even need this check? Is there is an endpoint to create, we could create it and attach an owner. If there is same key but different owner, why we would even care? |
||||||||||||||||
| return err | ||||||||||||||||
| } else if !ownedBy { | ||||||||||||||||
| return fmt.Errorf("key %q is not owned by this service", service.Key) | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we trowing an error? This should be just ignore and continue with next? |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if c.strictlyOwned { | ||||||||||||||||
| service.OwnedBy = c.ownerID | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| value, err := json.Marshal(&service) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
|
|
@@ -148,10 +176,45 @@ func (c etcdClient) DeleteService(ctx context.Context, key string) error { | |||||||||||||||
| ctx, cancel := context.WithTimeout(ctx, etcdTimeout) | ||||||||||||||||
| defer cancel() | ||||||||||||||||
|
|
||||||||||||||||
| if owned, err := c.isOwnedBy(ctx, key); err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } else if !owned { | ||||||||||||||||
| return fmt.Errorf("key %q is not owned by this service", key) | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, why we throwing an error? Is this not just a skip? |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| _, err := c.client.Delete(ctx, key, etcdcv3.WithPrefix()) | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (c etcdClient) isOwnedBy(ctx context.Context, key string) (bool, error) { | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to review that |
||||||||||||||||
| if !c.strictlyOwned { | ||||||||||||||||
| return true, nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| r, err := c.client.Get(ctx, key) | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| switch { | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not look like go code. Let's not have switch, especially when there is error handling |
||||||||||||||||
| case err != nil: | ||||||||||||||||
| return false, err | ||||||||||||||||
| case r == nil: | ||||||||||||||||
| return true, nil | ||||||||||||||||
| case len(r.Kvs) > 1: | ||||||||||||||||
| return false, fmt.Errorf("found multiple keys with the same key this service") | ||||||||||||||||
|
||||||||||||||||
| case len(r.Kvs) == 0: | ||||||||||||||||
| return true, nil | ||||||||||||||||
| } | ||||||||||||||||
| for _, n := range r.Kvs { | ||||||||||||||||
| svc := new(Service) | ||||||||||||||||
| if err := json.Unmarshal(n.Value, svc); err != nil { | ||||||||||||||||
| return false, fmt.Errorf("%s: %w", n.Key, err) | ||||||||||||||||
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if svc.OwnedBy == c.ownerID { | ||||||||||||||||
| return true, nil | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| return false, nil | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
As we passing full key, not sure how is possible to return 2 results |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // builds etcd client config depending on connection scheme and TLS parameters | ||||||||||||||||
| func getETCDConfig() (*etcdcv3.Config, error) { | ||||||||||||||||
| etcdURLsStr := os.Getenv("ETCD_URLS") | ||||||||||||||||
|
|
@@ -183,7 +246,7 @@ func getETCDConfig() (*etcdcv3.Config, error) { | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // the newETCDClient is an etcd client constructor | ||||||||||||||||
| func newETCDClient() (coreDNSClient, error) { | ||||||||||||||||
| func newETCDClient(ownerID string, strictlyOwned bool) (coreDNSClient, error) { | ||||||||||||||||
| cfg, err := getETCDConfig() | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return nil, err | ||||||||||||||||
|
|
@@ -192,12 +255,12 @@ func newETCDClient() (coreDNSClient, error) { | |||||||||||||||
| if err != nil { | ||||||||||||||||
| return nil, err | ||||||||||||||||
| } | ||||||||||||||||
| return etcdClient{c}, nil | ||||||||||||||||
| return etcdClient{c, ownerID, strictlyOwned}, nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // NewCoreDNSProvider is a CoreDNS provider constructor | ||||||||||||||||
| func NewCoreDNSProvider(domainFilter *endpoint.DomainFilter, prefix string, dryRun bool) (provider.Provider, error) { | ||||||||||||||||
| client, err := newETCDClient() | ||||||||||||||||
| func NewCoreDNSProvider(domainFilter *endpoint.DomainFilter, prefix, ownerID string, strictlyOwned, dryRun bool) (provider.Provider, error) { | ||||||||||||||||
| client, err := newETCDClient(ownerID, strictlyOwned) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return nil, err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just do Early ownership check: skip records not owned by this instance when strictlyOwned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so most likely lines
and
no longer required