@@ -131,8 +131,12 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
131
131
}
132
132
133
133
for name , recordGroup := range recordsByName {
134
+ // We assume that all records in a group have the same name, type, and ttl.
135
+ // TODO(herko): Add error checking to ensure that the above is the case.
136
+ firstRecord := recordGroup [0 ]
137
+
134
138
err := n .withRecordLock (name , func () error {
135
- existingRecords , err := n .lookupSRVRecords (ctx , name )
139
+ existingRecords , err := n .lookupRecords (ctx , firstRecord . Type , name )
136
140
if err != nil {
137
141
return err
138
142
}
@@ -151,9 +155,6 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
151
155
combinedRecords [record .Data ] = record
152
156
}
153
157
154
- // We assume that all records in a group have the same name, type, and ttl.
155
- // TODO(herko): Add error checking to ensure that the above is the case.
156
- firstRecord := recordGroup [0 ]
157
158
data := maps .Keys (combinedRecords )
158
159
sort .Strings (data )
159
160
zone := n .managedZone
@@ -194,24 +195,26 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
194
195
}
195
196
196
197
// LookupSRVRecords implements the vm.DNSProvider interface.
197
- func (n * dnsProvider ) LookupSRVRecords (ctx context.Context , name string ) ([]vm.DNSRecord , error ) {
198
+ func (n * dnsProvider ) LookupRecords (
199
+ ctx context.Context , recordType vm.DNSType , name string ,
200
+ ) ([]vm.DNSRecord , error ) {
198
201
var records []vm.DNSRecord
199
202
var err error
200
203
err = n .withRecordLock (name , func () error {
201
- if config .FastDNS {
204
+ if config .FastDNS && recordType == vm . SRV {
202
205
rIdx := randutil .FastUint32 () % uint32 (len (n .resolvers ))
203
206
records , err = n .fastLookupSRVRecords (ctx , n .resolvers [rIdx ], name , true )
204
207
return err
205
208
}
206
- records , err = n .lookupSRVRecords (ctx , name )
209
+ records , err = n .lookupRecords (ctx , recordType , name )
207
210
return err
208
211
})
209
212
return records , err
210
213
}
211
214
212
215
// ListRecords implements the vm.DNSProvider interface.
213
216
func (n * dnsProvider ) ListRecords (ctx context.Context ) ([]vm.DNSRecord , error ) {
214
- return n .listSRVRecords (ctx , "" , dnsMaxResults )
217
+ return n .listRecords (ctx , vm . SRV , "" , dnsMaxResults )
215
218
}
216
219
217
220
func (n * dnsProvider ) deleteRecords (
@@ -253,7 +256,7 @@ func (n *dnsProvider) DeletePublicRecordsByName(ctx context.Context, names ...st
253
256
// DeleteRecordsBySubdomain implements the vm.DNSProvider interface.
254
257
func (n * dnsProvider ) DeleteSRVRecordsBySubdomain (ctx context.Context , subdomain string ) error {
255
258
suffix := fmt .Sprintf ("%s.%s." , subdomain , n .Domain ())
256
- records , err := n .listSRVRecords (ctx , suffix , dnsMaxResults )
259
+ records , err := n .listRecords (ctx , vm . SRV , suffix , dnsMaxResults )
257
260
if err != nil {
258
261
return err
259
262
}
@@ -287,13 +290,15 @@ func (n *dnsProvider) Domain() string {
287
290
// network problems. For lookups, we prefer this to using the gcloud command as
288
291
// it is faster, and preferable when service information is being queried
289
292
// regularly.
290
- func (n * dnsProvider ) lookupSRVRecords (ctx context.Context , name string ) ([]vm.DNSRecord , error ) {
293
+ func (n * dnsProvider ) lookupRecords (
294
+ ctx context.Context , recordType vm.DNSType , name string ,
295
+ ) ([]vm.DNSRecord , error ) {
291
296
// Check the cache first.
292
297
if cachedRecords , ok := n .getCache (name ); ok {
293
298
return cachedRecords , nil
294
299
}
295
300
// Lookup the records, if no records are found in the cache.
296
- records , err := n .listSRVRecords (ctx , name , dnsMaxResults )
301
+ records , err := n .listRecords (ctx , recordType , name , dnsMaxResults )
297
302
if err != nil {
298
303
return nil , err
299
304
}
@@ -310,16 +315,21 @@ func (n *dnsProvider) lookupSRVRecords(ctx context.Context, name string) ([]vm.D
310
315
return filteredRecords , nil
311
316
}
312
317
313
- // listSRVRecords returns all SRV records that match the given filter from Google Cloud DNS.
318
+ // listRecords returns all records that match the given filter from Google Cloud DNS.
314
319
// The data field of the records could be a comma-separated list of values if multiple
315
320
// records are returned for the same name.
316
- func (n * dnsProvider ) listSRVRecords (
317
- ctx context.Context , filter string , limit int ,
321
+ func (n * dnsProvider ) listRecords (
322
+ ctx context.Context , recordType vm. DNSType , filter string , limit int ,
318
323
) ([]vm.DNSRecord , error ) {
324
+ zone := n .managedZone
325
+ if recordType == vm .A {
326
+ zone = n .publicZone
327
+ }
328
+
319
329
args := []string {"--project" , n .dnsProject , "dns" , "record-sets" , "list" ,
320
330
"--limit" , strconv .Itoa (limit ),
321
331
"--page-size" , strconv .Itoa (limit ),
322
- "--zone" , n . managedZone ,
332
+ "--zone" , zone ,
323
333
"--format" , "json" ,
324
334
}
325
335
if filter != "" {
@@ -348,11 +358,11 @@ func (n *dnsProvider) listSRVRecords(
348
358
if record .Kind != "dns#resourceRecordSet" {
349
359
continue
350
360
}
351
- if record .RecordType != string (vm . SRV ) {
361
+ if record .RecordType != string (recordType ) {
352
362
continue
353
363
}
354
364
for _ , data := range record .RRDatas {
355
- records = append (records , vm .CreateDNSRecord (record .Name , vm . SRV , data , record .TTL ))
365
+ records = append (records , vm .CreateDNSRecord (record .Name , recordType , data , record .TTL ))
356
366
}
357
367
}
358
368
return records , nil
0 commit comments