Skip to content

Commit

Permalink
Merge pull request #1791 from sonasingh46/byo_dns
Browse files Browse the repository at this point in the history
chore(private_dns): tag dns zone and delete only if owned
  • Loading branch information
k8s-ci-robot authored Dec 13, 2021
2 parents 2b4a27e + d060583 commit 92dcd55
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 27 deletions.
25 changes: 24 additions & 1 deletion azure/services/privatedns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import (

// Client wraps go-sdk.
type client interface {
GetZone(context.Context, string, string) (privatedns.PrivateZone, error)
CreateOrUpdateZone(context.Context, string, string, privatedns.PrivateZone) error
DeleteZone(context.Context, string, string) error
GetLink(context.Context, string, string, string) (privatedns.VirtualNetworkLink, error)
CreateOrUpdateLink(context.Context, string, string, string, privatedns.VirtualNetworkLink) error
DeleteLink(context.Context, string, string, string) error
CreateOrUpdateRecordSet(context.Context, string, string, privatedns.RecordType, string, privatedns.RecordSet) error
Expand Down Expand Up @@ -74,11 +76,21 @@ func newRecordSetsClient(subscriptionID string, baseURI string, authorizer autor
return recordsClient
}

// GetZone returns a private zone.
func (ac *azureClient) GetZone(ctx context.Context, resourceGroupName, zoneName string) (privatedns.PrivateZone, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.GetZone")
defer done()
zone, err := ac.privatezones.Get(ctx, resourceGroupName, zoneName)
if err != nil {
return privatedns.PrivateZone{}, err
}
return zone, nil
}

// CreateOrUpdateZone creates or updates a private zone.
func (ac *azureClient) CreateOrUpdateZone(ctx context.Context, resourceGroupName string, zoneName string, zone privatedns.PrivateZone) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.CreateOrUpdateZone")
defer done()

future, err := ac.privatezones.CreateOrUpdate(ctx, resourceGroupName, zoneName, zone, "", "")
if err != nil {
return err
Expand Down Expand Up @@ -108,6 +120,17 @@ func (ac *azureClient) DeleteZone(ctx context.Context, resourceGroupName, name s
return err
}

// GetLink returns a vnet link.
func (ac *azureClient) GetLink(ctx context.Context, resourceGroupName, zoneName, vnetLinkName string) (privatedns.VirtualNetworkLink, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.GetLink")
defer done()
vnetLink, err := ac.vnetlinks.Get(ctx, resourceGroupName, zoneName, vnetLinkName)
if err != nil {
return privatedns.VirtualNetworkLink{}, err
}
return vnetLink, nil
}

// CreateOrUpdateLink creates or updates a virtual network link to the specified Private DNS zone.
func (ac *azureClient) CreateOrUpdateLink(ctx context.Context, resourceGroupName, privateZoneName, name string, link privatedns.VirtualNetworkLink) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.CreateOrUpdateLink")
Expand Down
30 changes: 30 additions & 0 deletions azure/services/privatedns/mock_privatedns/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 89 additions & 8 deletions azure/services/privatedns/privatedns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package privatedns
import (
"context"

infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"

"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
Expand Down Expand Up @@ -55,15 +57,52 @@ func (s *Service) Reconcile(ctx context.Context) error {

zoneSpec := s.Scope.PrivateDNSSpec()
if zoneSpec != nil {
// Skip the reconciliation of private DNS zone which is not managed by capz.
isManaged, err := s.isPrivateDNSManaged(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName)
if err != nil && !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "could not get private DNS zone state of %s in resource group %s", zoneSpec.ZoneName, s.Scope.ResourceGroup())
}
// If resource is not found, it means it should be created and hence setting isVnetLinkManaged to true
// will allow the reconciliation to continue
if err != nil && azure.ResourceNotFound(err) {
isManaged = true
}
if !isManaged {
log.V(1).Info("Skipping reconciliation of unmanaged private DNS zone", "private DNS", zoneSpec.ZoneName)
log.V(1).Info("Tag the DNS manually from azure to manage it with capz."+
"Please see https://capz.sigs.k8s.io/topics/custom-dns.html#manage-dns-via-capz-tool", "private DNS", zoneSpec.ZoneName)
return nil
}
// Create the private DNS zone.
log.V(2).Info("creating private DNS zone", "private dns zone", zoneSpec.ZoneName)
err := s.client.CreateOrUpdateZone(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName, privatedns.PrivateZone{Location: to.StringPtr(azure.Global)})
pDNS := privatedns.PrivateZone{
Location: to.StringPtr(azure.Global),
Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{
ClusterName: s.Scope.ClusterName(),
Lifecycle: infrav1.ResourceLifecycleOwned,
Additional: s.Scope.AdditionalTags(),
})),
}
err = s.client.CreateOrUpdateZone(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName, pDNS)
if err != nil {
return errors.Wrapf(err, "failed to create private DNS zone %s", zoneSpec.ZoneName)
}
log.V(2).Info("successfully created private DNS zone", "private dns zone", zoneSpec.ZoneName)

for _, linkSpec := range zoneSpec.Links {
// If the virtual network link is not managed by capz, skip its reconciliation
isVnetLinkManaged, err := s.isVnetLinkManaged(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName, linkSpec.LinkName)
if err != nil && !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "could not get vnet link state of %s in resource group %s", zoneSpec.ZoneName, s.Scope.ResourceGroup())
}
// If resource is not found, it means it should be created and hence setting isVnetLinkManaged to true
// will allow the reconciliation to continue
if err != nil && azure.ResourceNotFound(err) {
isVnetLinkManaged = true
}
if !isVnetLinkManaged {
log.V(2).Info("Skipping vnet link reconciliation for unmanaged vnet link", "vnet link", linkSpec.LinkName, "private dns zone", zoneSpec.ZoneName)
continue
}
// Link each virtual network.
log.V(2).Info("creating a virtual network link", "virtual network", linkSpec.VNetName, "private dns zone", zoneSpec.ZoneName)
link := privatedns.VirtualNetworkLink{
Expand All @@ -74,14 +113,18 @@ func (s *Service) Reconcile(ctx context.Context) error {
RegistrationEnabled: to.BoolPtr(false),
},
Location: to.StringPtr(azure.Global),
Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{
ClusterName: s.Scope.ClusterName(),
Lifecycle: infrav1.ResourceLifecycleOwned,
Additional: s.Scope.AdditionalTags(),
})),
}
err = s.client.CreateOrUpdateLink(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName, linkSpec.LinkName, link)
if err != nil {
return errors.Wrapf(err, "failed to create virtual network link %s", linkSpec.LinkName)
}
log.V(2).Info("successfully created virtual network link", "virtual network", linkSpec.VNetName, "private dns zone", zoneSpec.ZoneName)
}

// Create the record(s).
for _, record := range zoneSpec.Records {
log.V(2).Info("creating record set", "private dns zone", zoneSpec.ZoneName, "record", record.Hostname)
Expand Down Expand Up @@ -110,25 +153,41 @@ func (s *Service) Reconcile(ctx context.Context) error {
return nil
}

// Delete deletes the private zone.
// Delete deletes the private zone and vnet links.
func (s *Service) Delete(ctx context.Context) error {
ctx, log, done := tele.StartSpanWithLogger(ctx, "privatedns.Service.Delete")
defer done()

zoneSpec := s.Scope.PrivateDNSSpec()
if zoneSpec != nil {
for _, linkSpec := range zoneSpec.Links {
// Remove each virtual network link.
// If the virtual network link is not managed by capz, skip its removal
isVnetLinkManaged, err := s.isVnetLinkManaged(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName, linkSpec.LinkName)
if err != nil && !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "could not get vnet link state of %s in resource group %s", zoneSpec.ZoneName, s.Scope.ResourceGroup())
}
if !isVnetLinkManaged {
log.V(2).Info("Skipping vnet link deletion for unmanaged vnet link", "vnet link", linkSpec.LinkName, "private dns zone", zoneSpec.ZoneName)
continue
}
log.V(2).Info("removing virtual network link", "virtual network", linkSpec.VNetName, "private dns zone", zoneSpec.ZoneName)
err := s.client.DeleteLink(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName, linkSpec.LinkName)
err = s.client.DeleteLink(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName, linkSpec.LinkName)
if err != nil && !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete virtual network link %s with zone %s in resource group %s", linkSpec.VNetName, zoneSpec.ZoneName, s.Scope.ResourceGroup())
}
}

// Skip the deletion of private DNS zone which is not managed by capz.
isManaged, err := s.isPrivateDNSManaged(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName)
if err != nil && !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "could not get private DNS zone state of %s in resource group %s", zoneSpec.ZoneName, s.Scope.ResourceGroup())
}
if !isManaged {
log.V(1).Info("Skipping private DNS zone deletion for unmanaged private DNS zone", "private DNS", zoneSpec.ZoneName)
return nil
}
// Delete the private DNS zone, which also deletes all records.
log.V(2).Info("deleting private dns zone", "private dns zone", zoneSpec.ZoneName)
err := s.client.DeleteZone(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName)
err = s.client.DeleteZone(ctx, s.Scope.ResourceGroup(), zoneSpec.ZoneName)
if err != nil && azure.ResourceNotFound(err) {
// already deleted
return nil
Expand All @@ -140,3 +199,25 @@ func (s *Service) Delete(ctx context.Context) error {
}
return nil
}

// isPrivateDNSManaged returns true if the private DNS has an owned tag with the cluster name as value,
// meaning that the DNS lifecycle is managed.
func (s *Service) isPrivateDNSManaged(ctx context.Context, resourceGroup, zoneName string) (bool, error) {
zone, err := s.client.GetZone(ctx, resourceGroup, zoneName)
if err != nil {
return false, err
}
tags := converters.MapToTags(zone.Tags)
return tags.HasOwned(s.Scope.ClusterName()), nil
}

// isVnetLinkManaged returns true if the vnet link has an owned tag with the cluster name as value,
// meaning that the vnet link lifecycle is managed.
func (s *Service) isVnetLinkManaged(ctx context.Context, resourceGroupName, zoneName, vnetLinkName string) (bool, error) {
zone, err := s.client.GetLink(ctx, resourceGroupName, zoneName, vnetLinkName)
if err != nil {
return false, err
}
tags := converters.MapToTags(zone.Tags)
return tags.HasOwned(s.Scope.ClusterName()), nil
}
Loading

0 comments on commit 92dcd55

Please sign in to comment.