You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
299 lines
8.0 KiB
299 lines
8.0 KiB
7 years ago
|
/*
|
||
4 years ago
|
* MinIO Cloud Storage, (C) 2018,2020 MinIO, Inc.
|
||
7 years ago
|
*
|
||
|
* 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 dns
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
5 years ago
|
"net"
|
||
7 years ago
|
"sort"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
||
4 years ago
|
"github.com/minio/minio-go/v7/pkg/set"
|
||
4 years ago
|
"go.etcd.io/etcd/v3/clientv3"
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
// ErrNoEntriesFound - Indicates no entries were found for the given key (directory)
|
||
|
var ErrNoEntriesFound = errors.New("No entries found for this key")
|
||
|
|
||
6 years ago
|
const etcdPathSeparator = "/"
|
||
|
|
||
7 years ago
|
// create a new coredns service record for the bucket.
|
||
5 years ago
|
func newCoreDNSMsg(ip string, port string, ttl uint32, t time.Time) ([]byte, error) {
|
||
7 years ago
|
return json.Marshal(&SrvRecord{
|
||
|
Host: ip,
|
||
5 years ago
|
Port: json.Number(port),
|
||
7 years ago
|
TTL: ttl,
|
||
5 years ago
|
CreationDate: t,
|
||
7 years ago
|
})
|
||
|
}
|
||
|
|
||
5 years ago
|
// Close closes the internal etcd client and cannot be used further
|
||
4 years ago
|
func (c *CoreDNS) Close() error {
|
||
5 years ago
|
c.etcdClient.Close()
|
||
4 years ago
|
return nil
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// List - Retrieves list of DNS entries for the domain.
|
||
5 years ago
|
func (c *CoreDNS) List() (map[string][]SrvRecord, error) {
|
||
|
var srvRecords = map[string][]SrvRecord{}
|
||
6 years ago
|
for _, domainName := range c.domainNames {
|
||
5 years ago
|
key := msg.Path(fmt.Sprintf("%s.", domainName), c.prefixPath)
|
||
6 years ago
|
records, err := c.list(key)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
6 years ago
|
for _, record := range records {
|
||
|
if record.Key == "" {
|
||
|
continue
|
||
|
}
|
||
5 years ago
|
srvRecords[record.Key] = append(srvRecords[record.Key], record)
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
return srvRecords, nil
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// Get - Retrieves DNS records for a bucket.
|
||
|
func (c *CoreDNS) Get(bucket string) ([]SrvRecord, error) {
|
||
6 years ago
|
var srvRecords []SrvRecord
|
||
|
for _, domainName := range c.domainNames {
|
||
5 years ago
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, domainName), c.prefixPath)
|
||
6 years ago
|
records, err := c.list(key)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
6 years ago
|
// Make sure we have record.Key is empty
|
||
|
// this can only happen when record.Key
|
||
|
// has bucket entry with exact prefix
|
||
|
// match any record.Key which do not
|
||
|
// match the prefixes we skip them.
|
||
|
for _, record := range records {
|
||
|
if record.Key != "" {
|
||
|
continue
|
||
|
}
|
||
5 years ago
|
srvRecords = append(srvRecords, record)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
if len(srvRecords) == 0 {
|
||
|
return nil, ErrNoEntriesFound
|
||
6 years ago
|
}
|
||
|
return srvRecords, nil
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// msgUnPath converts a etcd path to domainname.
|
||
|
func msgUnPath(s string) string {
|
||
|
ks := strings.Split(strings.Trim(s, etcdPathSeparator), etcdPathSeparator)
|
||
|
for i, j := 0, len(ks)-1; i < j; i, j = i+1, j-1 {
|
||
|
ks[i], ks[j] = ks[j], ks[i]
|
||
|
}
|
||
|
return strings.Join(ks, ".")
|
||
|
}
|
||
|
|
||
7 years ago
|
// Retrieves list of entries under the key passed.
|
||
|
// Note that this method fetches entries upto only two levels deep.
|
||
5 years ago
|
func (c *CoreDNS) list(key string) ([]SrvRecord, error) {
|
||
7 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||
5 years ago
|
r, err := c.etcdClient.Get(ctx, key, clientv3.WithPrefix())
|
||
6 years ago
|
defer cancel()
|
||
7 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
6 years ago
|
if r.Count == 0 {
|
||
6 years ago
|
key = strings.TrimSuffix(key, etcdPathSeparator)
|
||
6 years ago
|
r, err = c.etcdClient.Get(ctx, key)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
var srvRecords []SrvRecord
|
||
6 years ago
|
for _, n := range r.Kvs {
|
||
|
var srvRecord SrvRecord
|
||
|
if err = json.Unmarshal([]byte(n.Value), &srvRecord); err != nil {
|
||
|
return nil, err
|
||
7 years ago
|
}
|
||
6 years ago
|
srvRecord.Key = strings.TrimPrefix(string(n.Key), key)
|
||
|
srvRecord.Key = strings.TrimSuffix(srvRecord.Key, srvRecord.Host)
|
||
6 years ago
|
|
||
|
// Skip non-bucket entry like for a key
|
||
|
// /skydns/net/miniocloud/10.0.0.1 that may exist as
|
||
|
// dns entry for the server (rather than the bucket
|
||
|
// itself).
|
||
6 years ago
|
if srvRecord.Key == "" {
|
||
6 years ago
|
continue
|
||
|
}
|
||
|
|
||
6 years ago
|
srvRecord.Key = msgUnPath(srvRecord.Key)
|
||
|
srvRecords = append(srvRecords, srvRecord)
|
||
6 years ago
|
|
||
7 years ago
|
}
|
||
6 years ago
|
sort.Slice(srvRecords, func(i int, j int) bool {
|
||
|
return srvRecords[i].Key < srvRecords[j].Key
|
||
|
})
|
||
7 years ago
|
return srvRecords, nil
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// Put - Adds DNS entries into etcd endpoint in CoreDNS etcd message format.
|
||
|
func (c *CoreDNS) Put(bucket string) error {
|
||
5 years ago
|
c.Delete(bucket) // delete any existing entries.
|
||
|
|
||
5 years ago
|
t := time.Now().UTC()
|
||
7 years ago
|
for ip := range c.domainIPs {
|
||
5 years ago
|
bucketMsg, err := newCoreDNSMsg(ip, c.domainPort, defaultTTL, t)
|
||
7 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
6 years ago
|
for _, domainName := range c.domainNames {
|
||
5 years ago
|
key := msg.Path(fmt.Sprintf("%s.%s", bucket, domainName), c.prefixPath)
|
||
6 years ago
|
key = key + etcdPathSeparator + ip
|
||
6 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||
|
_, err = c.etcdClient.Put(ctx, key, string(bucketMsg))
|
||
6 years ago
|
defer cancel()
|
||
6 years ago
|
if err != nil {
|
||
|
ctx, cancel = context.WithTimeout(context.Background(), defaultContextTimeout)
|
||
|
c.etcdClient.Delete(ctx, key)
|
||
|
defer cancel()
|
||
|
return err
|
||
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
return nil
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// Delete - Removes DNS entries added in Put().
|
||
|
func (c *CoreDNS) Delete(bucket string) error {
|
||
6 years ago
|
for _, domainName := range c.domainNames {
|
||
5 years ago
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, domainName), c.prefixPath)
|
||
6 years ago
|
srvRecords, err := c.list(key)
|
||
|
if err != nil {
|
||
6 years ago
|
return err
|
||
|
}
|
||
6 years ago
|
for _, record := range srvRecords {
|
||
|
dctx, dcancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||
6 years ago
|
if _, err = c.etcdClient.Delete(dctx, key+etcdPathSeparator+record.Host); err != nil {
|
||
6 years ago
|
dcancel()
|
||
|
return err
|
||
|
}
|
||
|
dcancel()
|
||
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
return nil
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// DeleteRecord - Removes a specific DNS entry
|
||
|
func (c *CoreDNS) DeleteRecord(record SrvRecord) error {
|
||
5 years ago
|
for _, domainName := range c.domainNames {
|
||
5 years ago
|
key := msg.Path(fmt.Sprintf("%s.%s.", record.Key, domainName), c.prefixPath)
|
||
5 years ago
|
|
||
|
dctx, dcancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||
|
if _, err := c.etcdClient.Delete(dctx, key+etcdPathSeparator+record.Host); err != nil {
|
||
|
dcancel()
|
||
|
return err
|
||
|
}
|
||
|
dcancel()
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
// String stringer name for this implementation of dns.Store
|
||
|
func (c *CoreDNS) String() string {
|
||
|
return "etcdDNS"
|
||
|
}
|
||
|
|
||
7 years ago
|
// CoreDNS - represents dns config for coredns server.
|
||
5 years ago
|
type CoreDNS struct {
|
||
6 years ago
|
domainNames []string
|
||
|
domainIPs set.StringSet
|
||
5 years ago
|
domainPort string
|
||
|
prefixPath string
|
||
5 years ago
|
etcdClient *clientv3.Client
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
// EtcdOption - functional options pattern style
|
||
|
type EtcdOption func(*CoreDNS)
|
||
5 years ago
|
|
||
|
// DomainNames set a list of domain names used by this CoreDNS
|
||
|
// client setting, note this will fail if set to empty when
|
||
|
// constructor initializes.
|
||
4 years ago
|
func DomainNames(domainNames []string) EtcdOption {
|
||
5 years ago
|
return func(args *CoreDNS) {
|
||
|
args.domainNames = domainNames
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// DomainIPs set a list of custom domain IPs, note this will
|
||
|
// fail if set to empty when constructor initializes.
|
||
4 years ago
|
func DomainIPs(domainIPs set.StringSet) EtcdOption {
|
||
5 years ago
|
return func(args *CoreDNS) {
|
||
|
args.domainIPs = domainIPs
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// DomainPort - is a string version of server port
|
||
4 years ago
|
func DomainPort(domainPort string) EtcdOption {
|
||
5 years ago
|
return func(args *CoreDNS) {
|
||
|
args.domainPort = domainPort
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// CoreDNSPath - custom prefix on etcd to populate DNS
|
||
|
// service records, optional and can be empty.
|
||
|
// if empty then c.prefixPath is used i.e "/skydns"
|
||
4 years ago
|
func CoreDNSPath(prefix string) EtcdOption {
|
||
5 years ago
|
return func(args *CoreDNS) {
|
||
|
args.prefixPath = prefix
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// NewCoreDNS - initialize a new coreDNS set/unset values.
|
||
4 years ago
|
func NewCoreDNS(cfg clientv3.Config, setters ...EtcdOption) (Store, error) {
|
||
5 years ago
|
etcdClient, err := clientv3.New(cfg)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
args := &CoreDNS{
|
||
|
etcdClient: etcdClient,
|
||
|
}
|
||
|
|
||
|
for _, setter := range setters {
|
||
|
setter(args)
|
||
|
}
|
||
|
|
||
|
if len(args.domainNames) == 0 || args.domainIPs.IsEmpty() {
|
||
|
return nil, errors.New("invalid argument")
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// strip ports off of domainIPs
|
||
5 years ago
|
domainIPsWithoutPorts := args.domainIPs.ApplyFunc(func(ip string) string {
|
||
5 years ago
|
host, _, err := net.SplitHostPort(ip)
|
||
|
if err != nil {
|
||
|
if strings.Contains(err.Error(), "missing port in address") {
|
||
|
host = ip
|
||
|
}
|
||
|
}
|
||
|
return host
|
||
|
})
|
||
5 years ago
|
args.domainIPs = domainIPsWithoutPorts
|
||
5 years ago
|
|
||
5 years ago
|
return args, nil
|
||
7 years ago
|
}
|