Skip to content

Commit dc8a6d0

Browse files
committed
support configurable zone reload time + formating
Signed-off-by: Khash Sajadi <[email protected]>
1 parent a742d27 commit dc8a6d0

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ In the Corefile, `mysql` can be configured with the following parameters:
3232
`max_open_connections` Maximum number of open connections to the database server. Default is 10.
3333
`max_idle_connections` Maximum number of idle connections in the database connection pool. Default is 10.
3434
`ttl` Default TTL for records without a specified TTL in seconds. Default is 360 (seconds)
35+
`zone_update_interval` Maximum time interval between loading all the zones from the database. Default is 10 minutes.
3536

3637
## Database Setup
3738
This plugin doesn't create or migrate database schema for its use yet. To create the database and tables, use the following table structure (note the table name prefix):

handler.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import (
1111
)
1212

1313
type CoreDNSMySql struct {
14-
Next plugin.Handler
15-
Dsn string
16-
TablePrefix string
17-
MaxLifetime time.Duration
14+
Next plugin.Handler
15+
Dsn string
16+
TablePrefix string
17+
MaxLifetime time.Duration
1818
MaxOpenConnections int
1919
MaxIdleConnections int
20-
Ttl uint32
20+
Ttl uint32
2121

22-
tableName string
22+
tableName string
2323
lastZoneUpdate time.Time
2424
zoneUpdateTime time.Duration
25-
zones []string
25+
zones []string
2626
}
2727

2828
// ServeDNS implements the plugin.Handler interface.
@@ -52,9 +52,9 @@ func (handler *CoreDNSMySql) ServeDNS(ctx context.Context, w dns.ResponseWriter,
5252
if len(records) == 0 {
5353
// no record found but we are going to return a SOA
5454
rec := &Record{
55-
Name: qName,
55+
Name: qName,
5656
RecordType: "SOA",
57-
Content: "{}",
57+
Content: "{}",
5858
}
5959
records = append(records, rec)
6060
}

setup.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
)
1313

1414
const (
15-
defaultTtl = 360
16-
defaultMaxLifeTime = 1*time.Minute
15+
defaultTtl = 360
16+
defaultMaxLifeTime = 1 * time.Minute
1717
defaultMaxOpenConnections = 10
1818
defaultMaxIdleConnections = 10
19+
defaultZoneUpdateTime = 10 * time.Minute
1920
)
2021

2122
func init() {
@@ -90,6 +91,16 @@ func mysqlParse(c *caddy.Controller) (*CoreDNSMySql, error) {
9091
val = defaultMaxIdleConnections
9192
}
9293
mysql.MaxIdleConnections = val
94+
case "zone_update_interval":
95+
if !c.NextArg() {
96+
return &CoreDNSMySql{}, c.ArgErr()
97+
}
98+
var val time.Duration
99+
val, err = time.ParseDuration(c.Val())
100+
if err != nil {
101+
val = defaultZoneUpdateTime
102+
}
103+
mysql.zoneUpdateTime = val
93104
case "ttl":
94105
if !c.NextArg() {
95106
return &CoreDNSMySql{}, c.ArgErr()

types.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ type Record struct {
1212
Zone string
1313
Name string
1414
RecordType string
15-
Ttl uint32
16-
Content string
15+
Ttl uint32
16+
Content string
1717

1818
handler *CoreDNSMySql
1919
}
2020

2121
type ARecord struct {
22-
Ip net.IP `json:"ip"`
22+
Ip net.IP `json:"ip"`
2323
}
2424

2525
type AAAARecord struct {
26-
Ip net.IP `json:"ip"`
26+
Ip net.IP `json:"ip"`
2727
}
2828

2929
type TXTRecord struct {
@@ -60,7 +60,7 @@ type SOARecord struct {
6060
}
6161

6262
type CAARecord struct {
63-
Flag uint8 `json:"flag"`
63+
Flag uint8 `json:"flag"`
6464
Tag string `json:"tag"`
6565
Value string `json:"value"`
6666
}
@@ -80,7 +80,7 @@ func (rec *Record) AsARecord() (record dns.RR, extras []dns.RR, err error) {
8080
}
8181

8282
if aRec.Ip == nil {
83-
return nil, nil,nil
83+
return nil, nil, nil
8484
}
8585
r.A = aRec.Ip
8686
return r, nil, nil
@@ -97,7 +97,7 @@ func (rec *Record) AsAAAARecord() (record dns.RR, extras []dns.RR, err error) {
9797
var aRec *AAAARecord
9898
err = json.Unmarshal([]byte(rec.Content), &aRec)
9999
if err != nil {
100-
return nil,nil, err
100+
return nil, nil, err
101101
}
102102

103103
if aRec.Ip == nil {
@@ -123,7 +123,7 @@ func (rec *Record) AsTXTRecord() (record dns.RR, extras []dns.RR, err error) {
123123
}
124124

125125
if len(aRec.Text) == 0 {
126-
return nil, nil,nil
126+
return nil, nil, nil
127127
}
128128

129129
r.Txt = split255(aRec.Text)
@@ -145,7 +145,7 @@ func (rec *Record) AsCNAMERecord() (record dns.RR, extras []dns.RR, err error) {
145145
}
146146

147147
if len(aRec.Host) == 0 {
148-
return nil, nil,nil
148+
return nil, nil, nil
149149
}
150150
r.Target = dns.Fqdn(aRec.Host)
151151
return r, nil, nil
@@ -166,7 +166,7 @@ func (rec *Record) AsNSRecord() (record dns.RR, extras []dns.RR, err error) {
166166
}
167167

168168
if len(aRec.Host) == 0 {
169-
return nil, nil,nil
169+
return nil, nil, nil
170170
}
171171

172172
r.Ns = aRec.Host
@@ -192,7 +192,7 @@ func (rec *Record) AsMXRecord() (record dns.RR, extras []dns.RR, err error) {
192192
}
193193

194194
if len(aRec.Host) == 0 {
195-
return nil, nil,nil
195+
return nil, nil, nil
196196
}
197197

198198
r.Mx = aRec.Host
@@ -220,7 +220,7 @@ func (rec *Record) AsSRVRecord() (record dns.RR, extras []dns.RR, err error) {
220220
}
221221

222222
if len(aRec.Target) == 0 {
223-
return nil, nil,nil
223+
return nil, nil, nil
224224
}
225225

226226
r.Target = aRec.Target
@@ -240,10 +240,10 @@ func (rec *Record) AsSOARecord() (record dns.RR, extras []dns.RR, err error) {
240240

241241
if aRec.Ns == "" {
242242
r.Hdr = dns.RR_Header{
243-
Name: dns.Fqdn(rec.fqdn()),
243+
Name: dns.Fqdn(rec.fqdn()),
244244
Rrtype: dns.TypeSOA,
245-
Class: dns.ClassINET,
246-
Ttl: rec.minTtl(),
245+
Class: dns.ClassINET,
246+
Ttl: rec.minTtl(),
247247
}
248248
r.Ns = "ns1." + rec.Name
249249
r.Mbox = "hostmaster." + rec.Name
@@ -253,10 +253,10 @@ func (rec *Record) AsSOARecord() (record dns.RR, extras []dns.RR, err error) {
253253
r.Minttl = rec.minTtl()
254254
} else {
255255
r.Hdr = dns.RR_Header{
256-
Name: dns.Fqdn(rec.Zone),
256+
Name: dns.Fqdn(rec.Zone),
257257
Rrtype: dns.TypeSOA,
258-
Class: dns.ClassINET,
259-
Ttl: rec.minTtl(),
258+
Class: dns.ClassINET,
259+
Ttl: rec.minTtl(),
260260
}
261261
r.Ns = aRec.Ns
262262
r.Mbox = aRec.MBox
@@ -284,8 +284,8 @@ func (rec *Record) AsCAARecord() (record dns.RR, extras []dns.RR, err error) {
284284
return nil, nil, err
285285
}
286286

287-
if aRec.Value == "" || aRec.Tag == ""{
288-
return nil, nil,nil
287+
if aRec.Value == "" || aRec.Tag == "" {
288+
return nil, nil, nil
289289
}
290290

291291
r.Flag = aRec.Flag
@@ -328,4 +328,4 @@ func split255(s string) []string {
328328

329329
func (rec *Record) fqdn() string {
330330
return rec.Zone + rec.Name
331-
}
331+
}

0 commit comments

Comments
 (0)