Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable specifying prefixed zone names in DNSRecord spec.zone #350

Merged
merged 1 commit into from
Nov 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 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,16 @@ 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 +122,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 +137,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