Skip to content

Commit

Permalink
Enable specifying prefixed zone names in DNSRecord spec.zone
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyanr committed Nov 12, 2021
1 parent f917f77 commit 8e43d2b
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions pkg/gcp/client/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"github.com/gardener/gardener-extension-provider-gcp/pkg/gcp"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
googledns "google.golang.org/api/dns/v1"
Expand Down Expand Up @@ -67,12 +68,13 @@ func NewDNSClientFromSecretRef(ctx context.Context, c client.Client, secretRef c
return newDNSClient(ctx, serviceAccount)
}

// GetManagedZones returns a map of all managed zone DNS names mapped to their user assigned resource names.
// GetManagedZones returns a map of all managed zone DNS names mapped to their IDs, composed of the project ID and
// their user assigned resource names.
func (s *dnsClient) GetManagedZones(ctx context.Context) (map[string]string, error) {
zones := make(map[string]string)
f := func(resp *googledns.ManagedZonesListResponse) error {
for _, zone := range resp.ManagedZones {
zones[normalizeZoneName(zone.DnsName)] = zone.Name
zones[normalizeZoneName(zone.DnsName)] = s.zoneID(zone.Name)
}
return nil
}
Expand All @@ -84,10 +86,11 @@ func (s *dnsClient) GetManagedZones(ctx context.Context) (map[string]string, err
}

// CreateOrUpdateRecordSet creates or updates the resource recordset with the given name, record type, rrdatas, and ttl
// in the managed zone with the given name.
// in the managed zone with the given name or ID.
func (s *dnsClient) CreateOrUpdateRecordSet(ctx context.Context, managedZone, name, recordType string, rrdatas []string, ttl int64) error {
project, managedZone := s.projectAndManagedZone(managedZone)
name = ensureTrailingDot(name)
rrs, err := s.getResourceRecordSet(ctx, managedZone, name, recordType)
rrs, err := s.getResourceRecordSet(ctx, project, managedZone, name, recordType)
if err != nil {
return err
}
Expand All @@ -100,14 +103,15 @@ func (s *dnsClient) CreateOrUpdateRecordSet(ctx context.Context, managedZone, na
change.Deletions = append(change.Deletions, rrs)
}
change.Additions = append(change.Additions, &googledns.ResourceRecordSet{Name: name, Type: recordType, Rrdatas: rrdatas, Ttl: ttl})
_, err = s.service.Changes.Create(s.projectID, managedZone, change).Context(ctx).Do()
_, err = s.service.Changes.Create(project, managedZone, change).Context(ctx).Do()
return err
}

// DeleteRecordSet deletes the resource recordset with the given name and record type in the managed zone with the given name.
// DeleteRecordSet deletes the resource recordset with the given name and record type in the managed zone with the given name or ID.
func (s *dnsClient) DeleteRecordSet(ctx context.Context, managedZone, name, recordType string) error {
project, managedZone := s.projectAndManagedZone(managedZone)
name = ensureTrailingDot(name)
rrs, err := s.getResourceRecordSet(ctx, managedZone, name, recordType)
rrs, err := s.getResourceRecordSet(ctx, project, managedZone, name, recordType)
if err != nil {
return err
}
Expand All @@ -117,12 +121,12 @@ func (s *dnsClient) DeleteRecordSet(ctx context.Context, managedZone, name, reco
change := &googledns.Change{
Deletions: []*googledns.ResourceRecordSet{rrs},
}
_, err = s.service.Changes.Create(s.projectID, managedZone, change).Context(ctx).Do()
_, err = s.service.Changes.Create(project, managedZone, change).Context(ctx).Do()
return err
}

func (s *dnsClient) getResourceRecordSet(ctx context.Context, managedZone, name, recordType string) (*googledns.ResourceRecordSet, error) {
resp, err := s.service.ResourceRecordSets.List(s.projectID, managedZone).Context(ctx).Name(name).Type(recordType).Do()
func (s *dnsClient) getResourceRecordSet(ctx context.Context, project, managedZone, name, recordType string) (*googledns.ResourceRecordSet, error) {
resp, err := s.service.ResourceRecordSets.List(project, managedZone).Context(ctx).Name(name).Type(recordType).Do()
if err != nil {
return nil, err
}
Expand All @@ -132,6 +136,18 @@ func (s *dnsClient) getResourceRecordSet(ctx context.Context, managedZone, name,
return nil, nil
}

func (s *dnsClient) zoneID(managedZone string) string {
return s.projectID + "/" + managedZone
}

func (s *dnsClient) projectAndManagedZone(zoneID string) (string, string) {
parts := strings.Split(zoneID, "/")
if len(parts) != 2 {
return s.projectID, zoneID
}
return parts[0], parts[1]
}

func normalizeZoneName(zoneName string) string {
if strings.HasPrefix(zoneName, "\\052.") {
zoneName = "*" + zoneName[4:]
Expand Down

0 comments on commit 8e43d2b

Please sign in to comment.