2018-02-02 21:18:52 -05:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
"github.com/minio/minio-go/pkg/set"
|
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
2018-07-12 17:12:40 -04:00
|
|
|
etcd "github.com/coreos/etcd/clientv3"
|
2018-02-02 21:18:52 -05:00
|
|
|
)
|
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
// ErrNoEntriesFound - Indicates no entries were found for the given key (directory)
|
|
|
|
var ErrNoEntriesFound = errors.New("No entries found for this key")
|
|
|
|
|
2019-03-12 20:57:08 -04:00
|
|
|
const etcdPathSeparator = "/"
|
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
// create a new coredns service record for the bucket.
|
2018-05-11 15:02:30 -04:00
|
|
|
func newCoreDNSMsg(bucket, ip string, port int, ttl uint32) ([]byte, error) {
|
2018-02-02 21:18:52 -05:00
|
|
|
return json.Marshal(&SrvRecord{
|
|
|
|
Host: ip,
|
|
|
|
Port: port,
|
|
|
|
TTL: ttl,
|
|
|
|
CreationDate: time.Now().UTC(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
// Retrieves list of DNS entries for the domain.
|
2018-02-02 21:18:52 -05:00
|
|
|
func (c *coreDNS) List() ([]SrvRecord, error) {
|
2019-02-22 22:18:01 -05:00
|
|
|
var srvRecords []SrvRecord
|
|
|
|
for _, domainName := range c.domainNames {
|
|
|
|
key := msg.Path(fmt.Sprintf("%s.", domainName), defaultPrefixPath)
|
|
|
|
records, err := c.list(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srvRecords = append(srvRecords, records...)
|
|
|
|
}
|
|
|
|
return srvRecords, nil
|
2018-05-11 15:02:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieves DNS records for a bucket.
|
|
|
|
func (c *coreDNS) Get(bucket string) ([]SrvRecord, error) {
|
2019-02-22 22:18:01 -05:00
|
|
|
var srvRecords []SrvRecord
|
|
|
|
for _, domainName := range c.domainNames {
|
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, domainName), defaultPrefixPath)
|
|
|
|
records, err := c.list(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srvRecords = append(srvRecords, records...)
|
|
|
|
}
|
|
|
|
return srvRecords, nil
|
2018-05-11 15:02:30 -04:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:57:08 -04:00
|
|
|
// 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, ".")
|
|
|
|
}
|
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
// Retrieves list of entries under the key passed.
|
|
|
|
// Note that this method fetches entries upto only two levels deep.
|
|
|
|
func (c *coreDNS) list(key string) ([]SrvRecord, error) {
|
2018-02-02 21:18:52 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
2018-07-12 17:12:40 -04:00
|
|
|
r, err := c.etcdClient.Get(ctx, key, etcd.WithPrefix())
|
|
|
|
defer cancel()
|
2018-02-02 21:18:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-12 17:12:40 -04:00
|
|
|
if r.Count == 0 {
|
2019-03-12 20:57:08 -04:00
|
|
|
key = strings.TrimSuffix(key, etcdPathSeparator)
|
2018-07-12 17:12:40 -04:00
|
|
|
r, err = c.etcdClient.Get(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Count == 0 {
|
|
|
|
return nil, ErrNoEntriesFound
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 21:18:52 -05:00
|
|
|
|
|
|
|
var srvRecords []SrvRecord
|
2018-07-12 17:12:40 -04:00
|
|
|
for _, n := range r.Kvs {
|
|
|
|
var srvRecord SrvRecord
|
|
|
|
if err = json.Unmarshal([]byte(n.Value), &srvRecord); err != nil {
|
|
|
|
return nil, err
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
2018-07-12 17:12:40 -04:00
|
|
|
srvRecord.Key = strings.TrimPrefix(string(n.Key), key)
|
|
|
|
srvRecord.Key = strings.TrimSuffix(srvRecord.Key, srvRecord.Host)
|
2019-03-08 17:22:58 -05:00
|
|
|
|
|
|
|
// 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).
|
|
|
|
if srvRecord.Key == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:57:08 -04:00
|
|
|
srvRecord.Key = msgUnPath(srvRecord.Key)
|
|
|
|
srvRecords = append(srvRecords, srvRecord)
|
2018-07-12 17:12:40 -04:00
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
2018-12-12 13:47:03 -05:00
|
|
|
if len(srvRecords) == 0 {
|
2018-05-11 15:02:30 -04:00
|
|
|
return nil, ErrNoEntriesFound
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
2018-12-12 13:47:03 -05:00
|
|
|
sort.Slice(srvRecords, func(i int, j int) bool {
|
|
|
|
return srvRecords[i].Key < srvRecords[j].Key
|
|
|
|
})
|
2018-05-11 15:02:30 -04:00
|
|
|
return srvRecords, nil
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
// Adds DNS entries into etcd endpoint in CoreDNS etcd message format.
|
2018-02-02 21:18:52 -05:00
|
|
|
func (c *coreDNS) Put(bucket string) error {
|
2018-05-11 15:02:30 -04:00
|
|
|
for ip := range c.domainIPs {
|
2018-06-18 15:05:17 -04:00
|
|
|
bucketMsg, err := newCoreDNSMsg(bucket, ip, c.domainPort, defaultTTL)
|
2018-05-11 15:02:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-22 22:18:01 -05:00
|
|
|
for _, domainName := range c.domainNames {
|
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s", bucket, domainName), defaultPrefixPath)
|
2019-03-12 20:57:08 -04:00
|
|
|
key = key + etcdPathSeparator + ip
|
2019-02-22 22:18:01 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
_, err = c.etcdClient.Put(ctx, key, string(bucketMsg))
|
2018-07-12 17:12:40 -04:00
|
|
|
defer cancel()
|
2019-02-22 22:18:01 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
c.etcdClient.Delete(ctx, key)
|
|
|
|
defer cancel()
|
|
|
|
return err
|
|
|
|
}
|
2018-06-18 15:05:17 -04:00
|
|
|
}
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
2018-06-18 15:05:17 -04:00
|
|
|
return nil
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes DNS entries added in Put().
|
|
|
|
func (c *coreDNS) Delete(bucket string) error {
|
2019-02-22 22:18:01 -05:00
|
|
|
for _, domainName := range c.domainNames {
|
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, domainName), defaultPrefixPath)
|
|
|
|
srvRecords, err := c.list(key)
|
|
|
|
if err != nil {
|
2018-12-12 13:47:03 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-02-22 22:18:01 -05:00
|
|
|
for _, record := range srvRecords {
|
|
|
|
dctx, dcancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
2019-03-12 20:57:08 -04:00
|
|
|
if _, err = c.etcdClient.Delete(dctx, key+etcdPathSeparator+record.Host); err != nil {
|
2019-02-22 22:18:01 -05:00
|
|
|
dcancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dcancel()
|
|
|
|
}
|
2018-12-12 13:47:03 -05:00
|
|
|
}
|
2019-02-22 22:18:01 -05:00
|
|
|
return nil
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CoreDNS - represents dns config for coredns server.
|
|
|
|
type coreDNS struct {
|
2019-02-22 22:18:01 -05:00
|
|
|
domainNames []string
|
|
|
|
domainIPs set.StringSet
|
|
|
|
domainPort int
|
|
|
|
etcdClient *etcd.Client
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCoreDNS - initialize a new coreDNS set/unset values.
|
2019-02-22 22:18:01 -05:00
|
|
|
func NewCoreDNS(domainNames []string, domainIPs set.StringSet, domainPort string, etcdClient *etcd.Client) (Config, error) {
|
|
|
|
if len(domainNames) == 0 || domainIPs.IsEmpty() {
|
2018-02-02 21:18:52 -05:00
|
|
|
return nil, errors.New("invalid argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
port, err := strconv.Atoi(domainPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &coreDNS{
|
2019-02-22 22:18:01 -05:00
|
|
|
domainNames: domainNames,
|
|
|
|
domainIPs: domainIPs,
|
|
|
|
domainPort: port,
|
|
|
|
etcdClient: etcdClient,
|
2018-02-02 21:18:52 -05:00
|
|
|
}, nil
|
|
|
|
}
|