@@ -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,15 +155,16 @@ 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 )
160
+ zone := n .managedZone
161
+ if firstRecord .Public {
162
+ zone = n .publicZone
163
+ }
159
164
args := []string {"--project" , n .dnsProject , "dns" , "record-sets" , command , name ,
160
165
"--type" , string (firstRecord .Type ),
161
166
"--ttl" , strconv .Itoa (firstRecord .TTL ),
162
- "--zone" , n . managedZone ,
167
+ "--zone" , zone ,
163
168
"--rrdatas" , strings .Join (data , "," ),
164
169
}
165
170
cmd := exec .CommandContext (ctx , "gcloud" , args ... )
@@ -170,10 +175,10 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
170
175
n .clearCacheEntry (name )
171
176
return rperrors .TransientFailure (errors .Wrapf (err , "output: %s" , out ), dnsProblemLabel )
172
177
}
173
- // If fastDNS is enabled, we need to wait for the records to become available
178
+ // If fastDNS is enabled, we need to wait for the SRV records to become available
174
179
// on the Google DNS servers.
175
- if config .FastDNS {
176
- err = n .waitForRecordsAvailable (ctx , maps .Values (combinedRecords )... )
180
+ if config .FastDNS && ! firstRecord . Public {
181
+ err = n .waitForSRVRecordsAvailable (ctx , maps .Values (combinedRecords )... )
177
182
if err != nil {
178
183
return err
179
184
}
@@ -190,33 +195,36 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
190
195
}
191
196
192
197
// LookupSRVRecords implements the vm.DNSProvider interface.
193
- 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 ) {
194
201
var records []vm.DNSRecord
195
202
var err error
196
203
err = n .withRecordLock (name , func () error {
197
- if config .FastDNS {
204
+ if config .FastDNS && recordType == vm . SRV {
198
205
rIdx := randutil .FastUint32 () % uint32 (len (n .resolvers ))
199
206
records , err = n .fastLookupSRVRecords (ctx , n .resolvers [rIdx ], name , true )
200
207
return err
201
208
}
202
- records , err = n .lookupSRVRecords (ctx , name )
209
+ records , err = n .lookupRecords (ctx , recordType , name )
203
210
return err
204
211
})
205
212
return records , err
206
213
}
207
214
208
215
// ListRecords implements the vm.DNSProvider interface.
209
216
func (n * dnsProvider ) ListRecords (ctx context.Context ) ([]vm.DNSRecord , error ) {
210
- return n .listSRVRecords (ctx , "" , dnsMaxResults )
217
+ return n .listRecords (ctx , vm . SRV , "" , dnsMaxResults )
211
218
}
212
219
213
- // DeleteRecordsByName implements the vm.DNSProvider interface.
214
- func (n * dnsProvider ) DeleteRecordsByName (ctx context.Context , names ... string ) error {
220
+ func (n * dnsProvider ) deleteRecords (
221
+ ctx context.Context , zone string , recordType vm.DNSType , names ... string ,
222
+ ) error {
215
223
for _ , name := range names {
216
224
err := n .withRecordLock (name , func () error {
217
225
args := []string {"--project" , n .dnsProject , "dns" , "record-sets" , "delete" , name ,
218
- "--type" , string (vm . SRV ),
219
- "--zone" , n . managedZone ,
226
+ "--type" , string (recordType ),
227
+ "--zone" , zone ,
220
228
}
221
229
cmd := exec .CommandContext (ctx , "gcloud" , args ... )
222
230
out , err := n .execFn (cmd )
@@ -235,10 +243,20 @@ func (n *dnsProvider) DeleteRecordsByName(ctx context.Context, names ...string)
235
243
return nil
236
244
}
237
245
246
+ // DeleteSRVRecordsByName implements the vm.DNSProvider interface.
247
+ func (n * dnsProvider ) DeleteSRVRecordsByName (ctx context.Context , names ... string ) error {
248
+ return n .deleteRecords (ctx , n .managedZone , vm .SRV , names ... )
249
+ }
250
+
251
+ // DeletePublicRecordsByName implements the vm.DNSProvider interface
252
+ func (n * dnsProvider ) DeletePublicRecordsByName (ctx context.Context , names ... string ) error {
253
+ return n .deleteRecords (ctx , n .publicZone , vm .A , names ... )
254
+ }
255
+
238
256
// DeleteRecordsBySubdomain implements the vm.DNSProvider interface.
239
- func (n * dnsProvider ) DeleteRecordsBySubdomain (ctx context.Context , subdomain string ) error {
257
+ func (n * dnsProvider ) DeleteSRVRecordsBySubdomain (ctx context.Context , subdomain string ) error {
240
258
suffix := fmt .Sprintf ("%s.%s." , subdomain , n .Domain ())
241
- records , err := n .listSRVRecords (ctx , suffix , dnsMaxResults )
259
+ records , err := n .listRecords (ctx , vm . SRV , suffix , dnsMaxResults )
242
260
if err != nil {
243
261
return err
244
262
}
@@ -256,7 +274,7 @@ func (n *dnsProvider) DeleteRecordsBySubdomain(ctx context.Context, subdomain st
256
274
delete (names , name )
257
275
}
258
276
}
259
- return n .DeleteRecordsByName (ctx , maps .Keys (names )... )
277
+ return n .DeleteSRVRecordsByName (ctx , maps .Keys (names )... )
260
278
}
261
279
262
280
// Domain implements the vm.DNSProvider interface.
@@ -272,13 +290,15 @@ func (n *dnsProvider) Domain() string {
272
290
// network problems. For lookups, we prefer this to using the gcloud command as
273
291
// it is faster, and preferable when service information is being queried
274
292
// regularly.
275
- 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 ) {
276
296
// Check the cache first.
277
297
if cachedRecords , ok := n .getCache (name ); ok {
278
298
return cachedRecords , nil
279
299
}
280
300
// Lookup the records, if no records are found in the cache.
281
- records , err := n .listSRVRecords (ctx , name , dnsMaxResults )
301
+ records , err := n .listRecords (ctx , recordType , name , dnsMaxResults )
282
302
if err != nil {
283
303
return nil , err
284
304
}
@@ -295,16 +315,21 @@ func (n *dnsProvider) lookupSRVRecords(ctx context.Context, name string) ([]vm.D
295
315
return filteredRecords , nil
296
316
}
297
317
298
- // 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.
299
319
// The data field of the records could be a comma-separated list of values if multiple
300
320
// records are returned for the same name.
301
- func (n * dnsProvider ) listSRVRecords (
302
- ctx context.Context , filter string , limit int ,
321
+ func (n * dnsProvider ) listRecords (
322
+ ctx context.Context , recordType vm. DNSType , filter string , limit int ,
303
323
) ([]vm.DNSRecord , error ) {
324
+ zone := n .managedZone
325
+ if recordType == vm .A {
326
+ zone = n .publicZone
327
+ }
328
+
304
329
args := []string {"--project" , n .dnsProject , "dns" , "record-sets" , "list" ,
305
330
"--limit" , strconv .Itoa (limit ),
306
331
"--page-size" , strconv .Itoa (limit ),
307
- "--zone" , n . managedZone ,
332
+ "--zone" , zone ,
308
333
"--format" , "json" ,
309
334
}
310
335
if filter != "" {
@@ -333,11 +358,11 @@ func (n *dnsProvider) listSRVRecords(
333
358
if record .Kind != "dns#resourceRecordSet" {
334
359
continue
335
360
}
336
- if record .RecordType != string (vm . SRV ) {
361
+ if record .RecordType != string (recordType ) {
337
362
continue
338
363
}
339
364
for _ , data := range record .RRDatas {
340
- records = append (records , vm .CreateDNSRecord (record .Name , vm . SRV , data , record .TTL ))
365
+ records = append (records , vm .CreateDNSRecord (record .Name , recordType , data , record .TTL ))
341
366
}
342
367
}
343
368
return records , nil
0 commit comments