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-04-23 14:05:56 -04:00
|
|
|
etcd "github.com/coreos/etcd/client"
|
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")
|
|
|
|
|
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) {
|
|
|
|
key := msg.Path(fmt.Sprintf("%s.", c.domainName), defaultPrefixPath)
|
2018-05-11 15:02:30 -04:00
|
|
|
return c.list(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieves DNS records for a bucket.
|
|
|
|
func (c *coreDNS) Get(bucket string) ([]SrvRecord, error) {
|
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, c.domainName), defaultPrefixPath)
|
|
|
|
return c.list(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
kapi := etcd.NewKeysAPI(c.etcdClient)
|
2018-02-02 21:18:52 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
2018-05-11 15:02:30 -04:00
|
|
|
r, err := kapi.Get(ctx, key, &etcd.GetOptions{Recursive: true})
|
2018-02-02 21:18:52 -05:00
|
|
|
cancel()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var srvRecords []SrvRecord
|
|
|
|
for _, n := range r.Node.Nodes {
|
2018-05-11 15:02:30 -04:00
|
|
|
if !n.Dir {
|
|
|
|
var srvRecord SrvRecord
|
|
|
|
if err = json.Unmarshal([]byte(n.Value), &srvRecord); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srvRecord.Key = strings.TrimPrefix(n.Key, key)
|
|
|
|
srvRecords = append(srvRecords, srvRecord)
|
|
|
|
} else {
|
2018-05-15 21:20:22 -04:00
|
|
|
// As this is a directory, loop through all the nodes inside (assuming all nodes are non-directories)
|
2018-05-11 15:02:30 -04:00
|
|
|
for _, n1 := range n.Nodes {
|
|
|
|
var srvRecord SrvRecord
|
|
|
|
if err = json.Unmarshal([]byte(n1.Value), &srvRecord); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srvRecord.Key = strings.TrimPrefix(n1.Key, key)
|
|
|
|
srvRecord.Key = strings.TrimSuffix(srvRecord.Key, srvRecord.Host)
|
|
|
|
srvRecords = append(srvRecords, srvRecord)
|
|
|
|
}
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 15:02:30 -04:00
|
|
|
if srvRecords != nil {
|
|
|
|
sort.Slice(srvRecords, func(i int, j int) bool {
|
|
|
|
return srvRecords[i].Key < srvRecords[j].Key
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return nil, ErrNoEntriesFound
|
2018-02-02 21:18:52 -05:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
kapi := etcd.NewKeysAPI(c.etcdClient)
|
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s", bucket, c.domainName), defaultPrefixPath)
|
|
|
|
key = key + "/" + ip
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
_, err = kapi.Set(ctx, key, string(bucketMsg), nil)
|
|
|
|
cancel()
|
2018-06-18 15:05:17 -04:00
|
|
|
if err != nil {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
kapi.Delete(ctx, key, &etcd.DeleteOptions{Recursive: true})
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
2018-04-23 14:05:56 -04:00
|
|
|
kapi := etcd.NewKeysAPI(c.etcdClient)
|
2018-02-02 21:18:52 -05:00
|
|
|
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, c.domainName), defaultPrefixPath)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
2018-05-15 21:20:22 -04:00
|
|
|
_, err := kapi.Delete(ctx, key, &etcd.DeleteOptions{Recursive: true})
|
2018-02-02 21:18:52 -05:00
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CoreDNS - represents dns config for coredns server.
|
|
|
|
type coreDNS struct {
|
2018-05-11 15:02:30 -04:00
|
|
|
domainName 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.
|
2018-05-11 15:02:30 -04:00
|
|
|
func NewCoreDNS(domainName string, domainIPs set.StringSet, domainPort string, etcdClient etcd.Client) (Config, error) {
|
|
|
|
if domainName == "" || domainIPs.IsEmpty() || etcdClient == nil {
|
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{
|
|
|
|
domainName: domainName,
|
2018-05-11 15:02:30 -04:00
|
|
|
domainIPs: domainIPs,
|
2018-02-02 21:18:52 -05:00
|
|
|
domainPort: port,
|
|
|
|
etcdClient: etcdClient,
|
|
|
|
}, nil
|
|
|
|
}
|