@@ -131,8 +131,12 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
131131 }
132132
133133 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+
134138 err := n .withRecordLock (name , func () error {
135- existingRecords , err := n .lookupSRVRecords (ctx , name )
139+ existingRecords , err := n .lookupRecords (ctx , firstRecord . Type , name )
136140 if err != nil {
137141 return err
138142 }
@@ -151,15 +155,16 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
151155 combinedRecords [record .Data ] = record
152156 }
153157
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 ]
157158 data := maps .Keys (combinedRecords )
158159 sort .Strings (data )
160+ zone := n .managedZone
161+ if firstRecord .Public {
162+ zone = n .publicZone
163+ }
159164 args := []string {"--project" , n .dnsProject , "dns" , "record-sets" , command , name ,
160165 "--type" , string (firstRecord .Type ),
161166 "--ttl" , strconv .Itoa (firstRecord .TTL ),
162- "--zone" , n . managedZone ,
167+ "--zone" , zone ,
163168 "--rrdatas" , strings .Join (data , "," ),
164169 }
165170 cmd := exec .CommandContext (ctx , "gcloud" , args ... )
@@ -170,10 +175,10 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
170175 n .clearCacheEntry (name )
171176 return rperrors .TransientFailure (errors .Wrapf (err , "output: %s" , out ), dnsProblemLabel )
172177 }
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
174179 // 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 )... )
177182 if err != nil {
178183 return err
179184 }
@@ -190,33 +195,36 @@ func (n *dnsProvider) CreateRecords(ctx context.Context, records ...vm.DNSRecord
190195}
191196
192197// 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 ) {
194201 var records []vm.DNSRecord
195202 var err error
196203 err = n .withRecordLock (name , func () error {
197- if config .FastDNS {
204+ if config .FastDNS && recordType == vm . SRV {
198205 rIdx := randutil .FastUint32 () % uint32 (len (n .resolvers ))
199206 records , err = n .fastLookupSRVRecords (ctx , n .resolvers [rIdx ], name , true )
200207 return err
201208 }
202- records , err = n .lookupSRVRecords (ctx , name )
209+ records , err = n .lookupRecords (ctx , recordType , name )
203210 return err
204211 })
205212 return records , err
206213}
207214
208215// ListRecords implements the vm.DNSProvider interface.
209216func (n * dnsProvider ) ListRecords (ctx context.Context ) ([]vm.DNSRecord , error ) {
210- return n .listSRVRecords (ctx , "" , dnsMaxResults )
217+ return n .listRecords (ctx , vm . SRV , "" , dnsMaxResults )
211218}
212219
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 {
215223 for _ , name := range names {
216224 err := n .withRecordLock (name , func () error {
217225 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 ,
220228 }
221229 cmd := exec .CommandContext (ctx , "gcloud" , args ... )
222230 out , err := n .execFn (cmd )
@@ -235,10 +243,20 @@ func (n *dnsProvider) DeleteRecordsByName(ctx context.Context, names ...string)
235243 return nil
236244}
237245
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+
238256// 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 {
240258 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 )
242260 if err != nil {
243261 return err
244262 }
@@ -256,7 +274,7 @@ func (n *dnsProvider) DeleteRecordsBySubdomain(ctx context.Context, subdomain st
256274 delete (names , name )
257275 }
258276 }
259- return n .DeleteRecordsByName (ctx , maps .Keys (names )... )
277+ return n .DeleteSRVRecordsByName (ctx , maps .Keys (names )... )
260278}
261279
262280// Domain implements the vm.DNSProvider interface.
@@ -272,13 +290,15 @@ func (n *dnsProvider) Domain() string {
272290// network problems. For lookups, we prefer this to using the gcloud command as
273291// it is faster, and preferable when service information is being queried
274292// 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 ) {
276296 // Check the cache first.
277297 if cachedRecords , ok := n .getCache (name ); ok {
278298 return cachedRecords , nil
279299 }
280300 // 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 )
282302 if err != nil {
283303 return nil , err
284304 }
@@ -295,16 +315,21 @@ func (n *dnsProvider) lookupSRVRecords(ctx context.Context, name string) ([]vm.D
295315 return filteredRecords , nil
296316}
297317
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.
299319// The data field of the records could be a comma-separated list of values if multiple
300320// 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 ,
303323) ([]vm.DNSRecord , error ) {
324+ zone := n .managedZone
325+ if recordType == vm .A {
326+ zone = n .publicZone
327+ }
328+
304329 args := []string {"--project" , n .dnsProject , "dns" , "record-sets" , "list" ,
305330 "--limit" , strconv .Itoa (limit ),
306331 "--page-size" , strconv .Itoa (limit ),
307- "--zone" , n . managedZone ,
332+ "--zone" , zone ,
308333 "--format" , "json" ,
309334 }
310335 if filter != "" {
@@ -333,11 +358,11 @@ func (n *dnsProvider) listSRVRecords(
333358 if record .Kind != "dns#resourceRecordSet" {
334359 continue
335360 }
336- if record .RecordType != string (vm . SRV ) {
361+ if record .RecordType != string (recordType ) {
337362 continue
338363 }
339364 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 ))
341366 }
342367 }
343368 return records , nil
0 commit comments