-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp.go
395 lines (325 loc) · 9.02 KB
/
gcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package gcp
import (
"context"
"fmt"
"time"
"github.com/tanelmae/private-dns/internal/pdns"
"google.golang.org/api/dns/v1"
"google.golang.org/api/option"
"k8s.io/klog/v2"
)
const (
typeA = "A"
typeSRV = "SRV"
typePTR = "PTR"
defaultTTL int64 = 60
statusPending = "pending"
)
// CloudDNS is a wrapper for GCP SDK api to hold relevant conf
type CloudDNS struct {
api *dns.Service
zone string
reverseZone string
project string
}
// FromJSON creaties DNS client instance with JSON key file
func FromJSON(filePath, zone, reverseZone, project string) *CloudDNS {
dnsSvc, err := dns.NewService(context.Background(), option.WithCredentialsFile(filePath))
if err != nil {
klog.Fatalln(err)
}
return &CloudDNS{
api: dnsSvc,
zone: zone,
reverseZone: reverseZone,
project: project,
}
}
func (c *CloudDNS) applyChange(changes *dns.Change) error {
chg, err := c.api.Changes.Create(c.project, c.zone, changes).Do()
if err != nil {
return err
}
// wait for change to be acknowledged
for chg.Status == statusPending {
time.Sleep(time.Second)
chg, err = c.api.Changes.Get(c.project, c.zone, chg.Id).Do()
if err != nil {
return err
}
}
return nil
}
func (c *CloudDNS) applyRevChange(changes *dns.Change) error {
chg, err := c.api.Changes.Create(c.project, c.reverseZone, changes).Do()
if err != nil {
return err
}
// wait for change to be acknowledged
for chg.Status == statusPending {
time.Sleep(time.Second)
chg, err = c.api.Changes.Get(c.project, c.reverseZone, chg.Id).Do()
if err != nil {
return err
}
}
return nil
}
func (c *CloudDNS) checkForRec(rec *dns.ResourceRecordSet) *dns.ResourceRecordSet {
list, err := c.api.ResourceRecordSets.List(c.project, c.zone).Name(rec.Name).Type(rec.Type).MaxResults(1).Do()
if err != nil {
klog.Errorln(err)
return nil
}
if len(list.Rrsets) < 1 {
return nil
}
return list.Rrsets[0]
}
func (c *CloudDNS) NewRequest() pdns.DNSRequest {
return &DNSRequest{
client: c,
change: &dns.Change{},
revChange: &dns.Change{},
}
}
type DNSRequest struct {
client *CloudDNS
change *dns.Change
revChange *dns.Change
}
// Do makes the request with all the attached changes
// No error would be returned when no changes have been added
func (d *DNSRequest) Do() error {
var err error
if len(d.change.Deletions) > 1 || len(d.change.Additions) > 1 {
err = d.client.applyChange(d.change)
if err != nil {
return err
}
}
if len(d.revChange.Deletions) > 1 || len(d.revChange.Additions) > 1 {
err = d.client.applyRevChange(d.revChange)
if err != nil {
return err
}
}
return err
}
func (d *DNSRequest) deletion(rec *dns.ResourceRecordSet) {
d.change.Deletions = append(d.change.Deletions, rec)
}
func (d *DNSRequest) addition(rec *dns.ResourceRecordSet) {
d.change.Additions = append(d.change.Additions, rec)
}
func (d *DNSRequest) revDeletion(rec *dns.ResourceRecordSet) {
d.revChange.Deletions = append(d.revChange.Deletions, rec)
}
func (d *DNSRequest) revAddition(rec *dns.ResourceRecordSet) {
d.revChange.Additions = append(d.revChange.Additions, rec)
}
// AddRecord adds A record with single IP
func (d *DNSRequest) AddRecord(domain, ip string) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.", domain),
Rrdatas: []string{ip},
Ttl: defaultTTL,
Type: typeA,
}
oldRec := d.client.checkForRec(rec)
if oldRec != nil && rec.Rrdatas[0] == oldRec.Rrdatas[0] {
klog.V(2).Infof("Record exists: %+v\n", rec)
return
}
// Just a safeguard for case there is some stale record
// as it would fail the API request
if oldRec != nil && rec.Rrdatas[0] != oldRec.Rrdatas[0] {
klog.V(2).Infof("Stale record found: %+v\n", oldRec)
d.deletion(rec)
}
d.addition(rec)
if d.client.reverseZone != "" {
d.AddReverseRecord(domain, ip)
}
}
// RemoveRecord deletes A record with a single IP
func (d *DNSRequest) RemoveRecord(domain, ip string) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.", domain),
Rrdatas: []string{ip},
Ttl: defaultTTL,
Type: typeA,
}
// We get the existing record from the DNS zone to check if it exists
list, err := d.client.api.ResourceRecordSets.List(
d.client.project, d.client.zone).Name(rec.Name).Type(rec.Type).MaxResults(1).Do()
if err != nil {
klog.V(2).Infoln(err)
return
}
if len(list.Rrsets) == 0 {
klog.V(2).Infof("No DNS record found for %s/%s", rec.Name, ip)
return
}
// If records and pods have somehow got into inconsistent state
// we avoid deleting records that don't match the event.
if ip != list.Rrsets[0].Rrdatas[0] {
klog.V(2).Infof("No DNS record found for %s with the same IP (%s)", rec.Name, ip)
return
}
d.deletion(rec)
if d.client.reverseZone != "" {
d.RemoveReverseRecord(domain, ip)
}
}
// AddReverseRecord adds a PTR record for the reverse lookup
func (d *DNSRequest) AddReverseRecord(domain, ip string) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.in-addr.arpa.", ip),
Rrdatas: []string{domain},
Ttl: defaultTTL,
Type: typePTR,
}
oldRec := d.client.checkForRec(rec)
if oldRec != nil && rec.Rrdatas[0] == oldRec.Rrdatas[0] {
klog.V(2).Infof("Record exists: %+v\n", rec)
return
}
// Just a safeguard for case there is some stale record
// as it would fail the API request
if oldRec != nil && rec.Rrdatas[0] != oldRec.Rrdatas[0] {
klog.V(2).Infof("Stale record found: %+v\n", oldRec)
d.deletion(rec)
}
d.revAddition(rec)
}
// RemoveReverseRecord removes a PTR record from the reverse lookup zone
func (d *DNSRequest) RemoveReverseRecord(domain, ip string) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.in-addr.arpa.", ip),
Rrdatas: []string{domain},
Ttl: defaultTTL,
Type: typePTR,
}
// We get the existing record from the DNS zone to check if it exists
list, err := d.client.api.ResourceRecordSets.List(
d.client.project, d.client.reverseZone).Name(rec.Name).Type(rec.Type).MaxResults(1).Do()
if err != nil {
klog.V(2).Infoln(err)
return
}
if len(list.Rrsets) == 0 {
klog.V(2).Infof("No PTR record found for %s/%s", rec.Name, ip)
return
}
// If records and pods have somehow got into inconsistent state
// we avoid deleting records that don't match the event.
if domain != list.Rrsets[0].Rrdatas[0] {
klog.V(2).Infof("No PTR record found for %s with the same domain (%s)", rec.Name, domain)
return
}
d.revDeletion(rec)
}
// AddToService adds the given IP to A record with multiple IPs
func (d *DNSRequest) AddToService(domain, ip string) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.", domain),
Rrdatas: []string{ip},
Ttl: defaultTTL,
Type: typeA,
}
oldRec := d.client.checkForRec(rec)
if oldRec != nil && dataContains(oldRec, ip) {
klog.V(2).Infof("Service %s record exists and contains %s\n", domain, ip)
return
}
// Service exists and we need to add the IP
if oldRec != nil {
rec.Rrdatas = append(rec.Rrdatas, oldRec.Rrdatas...)
d.deletion(oldRec)
}
d.addition(rec)
}
// RemoveFromService removes given IP from an A record with multiple IPs
func (d *DNSRequest) RemoveFromService(domain, ip string) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.", domain),
Rrdatas: nil,
Ttl: defaultTTL,
Type: typeA,
}
oldRec := d.client.checkForRec(rec)
if oldRec == nil {
klog.V(2).Infof("No record exists for %s\n", domain)
return
}
if newRec, ok := removeData(oldRec, ip); ok {
d.addition(newRec)
} else {
klog.V(2).Infof("%s service doesn't include %s\n", domain, ip)
}
d.deletion(oldRec)
}
// AddToSRV adds domain to SRV record
func (d *DNSRequest) AddToSRV(srv, domain string, priority int) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.", srv),
Rrdatas: []string{domain},
Ttl: defaultTTL,
Type: typeSRV,
}
oldRec := d.client.checkForRec(rec)
if oldRec != nil {
// Failsafe
if rec.Name == oldRec.Name && dataContains(oldRec, domain) {
klog.V(2).Infof("Record exists: %+v\n", oldRec)
return
}
// We need to add the new endpoint
if rec.Name == oldRec.Name {
rec.Rrdatas = append(rec.Rrdatas, oldRec.Rrdatas...)
d.deletion(oldRec)
}
}
d.addition(rec)
}
// RemoveFromSRV removes domain from SRV record
func (d *DNSRequest) RemoveFromSRV(srv, domain string) {
rec := &dns.ResourceRecordSet{
Name: fmt.Sprintf("%s.", srv),
Rrdatas: nil,
Ttl: defaultTTL,
Type: typeSRV,
}
oldRec := d.client.checkForRec(rec)
if oldRec == nil {
klog.V(2).Infof("No record exists for %s\n", srv)
return
}
if newRec, ok := removeData(rec, domain); ok {
d.addition(newRec)
} else {
klog.V(2).Infof("%s doesn't include %s\n", srv, domain)
}
d.deletion(oldRec)
}
// UTILS
func dataContains(rec *dns.ResourceRecordSet, data string) bool {
for _, d := range rec.Rrdatas {
if d == data {
return true
}
}
return false
}
func removeData(rec *dns.ResourceRecordSet, data string) (*dns.ResourceRecordSet, bool) {
newRec := *rec
newRec.Rrdatas = []string{}
for _, v := range rec.Rrdatas {
if v != data {
newRec.Rrdatas = append(newRec.Rrdatas, v)
return &newRec, true
}
}
return nil, false
}