-
Notifications
You must be signed in to change notification settings - Fork 50
Add managed DNS record support for API endpoint #232
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
Merged
k8s-ci-robot
merged 5 commits into
kubernetes-sigs:master
from
gottwald:dns-api-endpoint-upstream
Feb 17, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d111f36
Add managed DNS record support for API endpoint
gottwald df3aaed
Add DNS propagation check
gottwald 4fcc845
Address code review comments
gottwald 91c3381
Disallow @ and * DNS names
gottwald 42531fb
Fix copyright header
gottwald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
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. Maybe we can move this file to another directory like |
||
Copyright 2021 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 networking | ||
|
||
import ( | ||
"github.com/miekg/dns" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type DNSQuerier interface { | ||
Query(servers []string, msg *dns.Msg) (*dns.Msg, error) | ||
LocalQuery(msg *dns.Msg) (*dns.Msg, error) | ||
} | ||
|
||
type DNSResolver struct { | ||
config *dns.ClientConfig | ||
client *dns.Client | ||
} | ||
|
||
func NewDNSResolver() (*DNSResolver, error) { | ||
dnsConfig, err := dns.ClientConfigFromFile("/etc/resolv.conf") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "unable to get DNS config") | ||
} | ||
sq := &DNSResolver{ | ||
config: dnsConfig, | ||
client: new(dns.Client), | ||
} | ||
return sq, nil | ||
} | ||
|
||
func (dr *DNSResolver) Query(servers []string, msg *dns.Msg) (*dns.Msg, error) { | ||
for _, server := range servers { | ||
r, _, err := dr.client.Exchange(msg, server+":"+dr.config.Port) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if r == nil || r.Rcode == dns.RcodeNameError || r.Rcode == dns.RcodeSuccess { | ||
return r, err | ||
} | ||
} | ||
return nil, errors.New("No name server to answer the question") | ||
} | ||
|
||
func (dr *DNSResolver) LocalQuery(msg *dns.Msg) (*dns.Msg, error) { | ||
return dr.Query(dr.config.Servers, msg) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2021 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 networking | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/digitalocean/godo" | ||
) | ||
|
||
// GetDomainRecord retrieves a single domain record from DO. | ||
func (s *Service) GetDomainRecord(domain, name, rType string) (*godo.DomainRecord, error) { | ||
fqdn := fmt.Sprintf("%s.%s", name, domain) | ||
records, resp, err := s.scope.Domains.RecordsByTypeAndName(s.ctx, domain, rType, fqdn, nil) | ||
if err != nil { | ||
if resp != nil && resp.StatusCode == http.StatusNotFound { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
switch len(records) { | ||
case 0: | ||
return nil, nil | ||
case 1: | ||
return &records[0], nil | ||
default: | ||
return nil, fmt.Errorf("multiple DNS records (%d) found for '%s.%s' type %s", | ||
len(records), name, domain, rType) | ||
} | ||
} | ||
|
||
// UpsertDomainRecord creates or updates a DO domain record. | ||
func (s *Service) UpsertDomainRecord(domain, name, rType, data string) error { | ||
record, err := s.GetDomainRecord(domain, name, rType) | ||
if err != nil { | ||
return fmt.Errorf("unable to get current DNS record from API: %s", err) | ||
} | ||
recordReq := &godo.DomainRecordEditRequest{ | ||
Type: rType, | ||
Name: name, | ||
Data: data, | ||
TTL: 30, | ||
} | ||
if record == nil { | ||
_, _, err = s.scope.Domains.CreateRecord(s.ctx, domain, recordReq) | ||
} else { | ||
_, _, err = s.scope.Domains.EditRecord(s.ctx, domain, record.ID, recordReq) | ||
} | ||
return err | ||
} | ||
|
||
// DeleteDomainRecord removes a DO domain record. | ||
func (s *Service) DeleteDomainRecord(domain, name, rType string) error { | ||
record, err := s.GetDomainRecord(domain, name, rType) | ||
if err != nil { | ||
return fmt.Errorf("unable to get current DNS record from API: %s", err) | ||
} | ||
if record == nil { | ||
return nil | ||
} | ||
_, err = s.scope.Domains.DeleteRecord(s.ctx, domain, record.ID) | ||
return err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.