@ -47,14 +47,30 @@ func newCoreDNSMsg(bucket, ip string, port int, ttl uint32) ([]byte, error) {
// Retrieves list of DNS entries for the domain.
// Retrieves list of DNS entries for the domain.
func ( c * coreDNS ) List ( ) ( [ ] SrvRecord , error ) {
func ( c * coreDNS ) List ( ) ( [ ] SrvRecord , error ) {
key := msg . Path ( fmt . Sprintf ( "%s." , c . domainName ) , defaultPrefixPath )
var srvRecords [ ] SrvRecord
return c . list ( key )
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
}
}
// Retrieves DNS records for a bucket.
// Retrieves DNS records for a bucket.
func ( c * coreDNS ) Get ( bucket string ) ( [ ] SrvRecord , error ) {
func ( c * coreDNS ) Get ( bucket string ) ( [ ] SrvRecord , error ) {
key := msg . Path ( fmt . Sprintf ( "%s.%s." , bucket , c . domainName ) , defaultPrefixPath )
var srvRecords [ ] SrvRecord
return c . list ( key )
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
}
}
// Retrieves list of entries under the key passed.
// Retrieves list of entries under the key passed.
@ -92,12 +108,14 @@ func (c *coreDNS) list(key string) ([]SrvRecord, error) {
//
//
// In all other situations when we want to list all DNS records,
// In all other situations when we want to list all DNS records,
// which is handled in the else clause.
// which is handled in the else clause.
if key != msg . Path ( fmt . Sprintf ( ".%s." , c . domainName ) , defaultPrefixPath ) {
for _ , domainName := range c . domainNames {
if srvRecord . Key == "/" {
if key != msg . Path ( fmt . Sprintf ( ".%s." , domainName ) , defaultPrefixPath ) {
if srvRecord . Key == "/" {
srvRecords = append ( srvRecords , srvRecord )
}
} else {
srvRecords = append ( srvRecords , srvRecord )
srvRecords = append ( srvRecords , srvRecord )
}
}
} else {
srvRecords = append ( srvRecords , srvRecord )
}
}
}
}
@ -117,16 +135,18 @@ func (c *coreDNS) Put(bucket string) error {
if err != nil {
if err != nil {
return err
return err
}
}
key := msg . Path ( fmt . Sprintf ( "%s.%s" , bucket , c . domainName ) , defaultPrefixPath )
for _ , domainName := range c . domainNames {
key = key + "/" + ip
key := msg . Path ( fmt . Sprintf ( "%s.%s" , bucket , domainName ) , defaultPrefixPath )
ctx , cancel := context . WithTimeout ( context . Background ( ) , defaultContextTimeout )
key = key + "/" + ip
_ , err = c . etcdClient . Put ( ctx , key , string ( bucketMsg ) )
ctx , cancel := context . WithTimeout ( context . Background ( ) , defaultContextTimeout )
defer cancel ( )
_ , err = c . etcdClient . Put ( ctx , key , string ( bucketMsg ) )
if err != nil {
ctx , cancel = context . WithTimeout ( context . Background ( ) , defaultContextTimeout )
c . etcdClient . Delete ( ctx , key )
defer cancel ( )
defer cancel ( )
return err
if err != nil {
ctx , cancel = context . WithTimeout ( context . Background ( ) , defaultContextTimeout )
c . etcdClient . Delete ( ctx , key )
defer cancel ( )
return err
}
}
}
}
}
return nil
return nil
@ -134,33 +154,35 @@ func (c *coreDNS) Put(bucket string) error {
// Removes DNS entries added in Put().
// Removes DNS entries added in Put().
func ( c * coreDNS ) Delete ( bucket string ) error {
func ( c * coreDNS ) Delete ( bucket string ) error {
key := msg . Path ( fmt . Sprintf ( "%s.%s." , bucket , c . domainName ) , defaultPrefixPath )
for _ , domainName := range c . domainNames {
srvRecords , err := c . list ( key )
key := msg . Path ( fmt . Sprintf ( "%s.%s." , bucket , domainName ) , defaultPrefixPath )
if err != nil {
srvRecords , err := c . list ( key )
return err
if err != nil {
}
for _ , record := range srvRecords {
dctx , dcancel := context . WithTimeout ( context . Background ( ) , defaultContextTimeout )
if _ , err = c . etcdClient . Delete ( dctx , key + "/" + record . Host ) ; err != nil {
dcancel ( )
return err
return err
}
}
dcancel ( )
for _ , record := range srvRecords {
dctx , dcancel := context . WithTimeout ( context . Background ( ) , defaultContextTimeout )
if _ , err = c . etcdClient . Delete ( dctx , key + "/" + record . Host ) ; err != nil {
dcancel ( )
return err
}
dcancel ( )
}
}
}
return err
return nil
}
}
// CoreDNS - represents dns config for coredns server.
// CoreDNS - represents dns config for coredns server.
type coreDNS struct {
type coreDNS struct {
domainName string
domainNames [ ] string
domainIPs set . StringSet
domainIPs set . StringSet
domainPort int
domainPort int
etcdClient * etcd . Client
etcdClient * etcd . Client
}
}
// NewCoreDNS - initialize a new coreDNS set/unset values.
// NewCoreDNS - initialize a new coreDNS set/unset values.
func NewCoreDNS ( domainName string , domainIPs set . StringSet , domainPort string , etcdClient * etcd . Client ) ( Config , error ) {
func NewCoreDNS ( domainNames [ ] string , domainIPs set . StringSet , domainPort string , etcdClient * etcd . Client ) ( Config , error ) {
if domainName == "" || domainIPs . IsEmpty ( ) {
if len ( domainNames ) == 0 || domainIPs . IsEmpty ( ) {
return nil , errors . New ( "invalid argument" )
return nil , errors . New ( "invalid argument" )
}
}
@ -170,9 +192,9 @@ func NewCoreDNS(domainName string, domainIPs set.StringSet, domainPort string, e
}
}
return & coreDNS {
return & coreDNS {
domainName : domainName ,
domainNames : domainNames ,
domainIPs : domainIPs ,
domainIPs : domainIPs ,
domainPort : port ,
domainPort : port ,
etcdClient : etcdClient ,
etcdClient : etcdClient ,
} , nil
} , nil
}
}