-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add managed DNS record support for API endpoint (#232)
* Add managed DNS record support for API endpoint * Add DNS propagation check * Address code review comments * Disallow @ and * DNS names * Fix copyright header
- Loading branch information
Showing
12 changed files
with
494 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 @@ | ||
/* | ||
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 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 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.