Skip to content

Commit

Permalink
update private dns client for async reconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
shysank committed Jan 28, 2022
1 parent 86d5931 commit 8324433
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 161 deletions.
164 changes: 5 additions & 159 deletions azure/services/privatedns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,167 +18,13 @@ package privatedns

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
"github.com/Azure/go-autorest/autorest"

"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)

// 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
DeleteRecordSet(context.Context, string, string, privatedns.RecordType, string) error
}

// AzureClient contains the Azure go-sdk Client.
type azureClient struct {
privatezones privatedns.PrivateZonesClient
vnetlinks privatedns.VirtualNetworkLinksClient
recordsets privatedns.RecordSetsClient
}

var _ client = (*azureClient)(nil)

// newClient creates a new VM client from subscription ID.
func newClient(auth azure.Authorizer) *azureClient {
c := newPrivateZonesClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
v := newVirtualNetworkLinksClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
r := newRecordSetsClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
return &azureClient{c, v, r}
}

// newPrivateZonesClient creates a new private zones client from subscription ID.
func newPrivateZonesClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) privatedns.PrivateZonesClient {
zonesClient := privatedns.NewPrivateZonesClientWithBaseURI(baseURI, subscriptionID)
azure.SetAutoRestClientDefaults(&zonesClient.Client, authorizer)
return zonesClient
}

// newVirtualNetworkLinksClient creates a new virtual networks link client from subscription ID.
func newVirtualNetworkLinksClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) privatedns.VirtualNetworkLinksClient {
linksClient := privatedns.NewVirtualNetworkLinksClientWithBaseURI(baseURI, subscriptionID)
azure.SetAutoRestClientDefaults(&linksClient.Client, authorizer)
return linksClient
}

// newRecordSetsClient creates a new record sets client from subscription ID.
func newRecordSetsClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) privatedns.RecordSetsClient {
recordsClient := privatedns.NewRecordSetsClientWithBaseURI(baseURI, subscriptionID)
azure.SetAutoRestClientDefaults(&recordsClient.Client, authorizer)
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
}
err = future.WaitForCompletionRef(ctx, ac.privatezones.Client)
if err != nil {
return err
}
_, err = future.Result(ac.privatezones)
return err
// Getter is an interface that can get a private dns resource.
type Getter interface {
Get(ctx context.Context, spec azure.ResourceSpecGetter) (result interface{}, err error)
}

// DeleteZone deletes the private zone.
func (ac *azureClient) DeleteZone(ctx context.Context, resourceGroupName, name string) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.DeleteZone")
defer done()

future, err := ac.privatezones.Delete(ctx, resourceGroupName, name, "")
if err != nil {
return err
}
err = future.WaitForCompletionRef(ctx, ac.privatezones.Client)
if err != nil {
return err
}
_, err = future.Result(ac.privatezones)
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")
defer done()

future, err := ac.vnetlinks.CreateOrUpdate(ctx, resourceGroupName, privateZoneName, name, link, "", "")
if err != nil {
return err
}
err = future.WaitForCompletionRef(ctx, ac.vnetlinks.Client)
if err != nil {
return err
}
_, err = future.Result(ac.vnetlinks)
return err
}

// DeleteLink deletes a virtual network link to the specified Private DNS zone.
func (ac *azureClient) DeleteLink(ctx context.Context, resourceGroupName, privateZoneName, name string) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.DeleteLink")
defer done()

future, err := ac.vnetlinks.Delete(ctx, resourceGroupName, privateZoneName, name, "")
if err != nil {
return err
}
err = future.WaitForCompletionRef(ctx, ac.vnetlinks.Client)
if err != nil {
return err
}
_, err = future.Result(ac.vnetlinks)
return err
}

// CreateOrUpdateRecordSet creates or updates a record set within the specified Private DNS zone.
func (ac *azureClient) CreateOrUpdateRecordSet(ctx context.Context, resourceGroupName string, privateZoneName string, recordType privatedns.RecordType, name string, set privatedns.RecordSet) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.CreateOrUpdateRecordSet")
defer done()

_, err := ac.recordsets.CreateOrUpdate(ctx, resourceGroupName, privateZoneName, recordType, name, set, "", "")
return err
}

// DeleteRecordSet deletes a record set within the specified Private DNS zone.
func (ac *azureClient) DeleteRecordSet(ctx context.Context, resourceGroupName string, privateZoneName string, recordType privatedns.RecordType, name string) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.AzureClient.DeleteRecordSet")
defer done()

_, err := ac.recordsets.Delete(ctx, resourceGroupName, privateZoneName, recordType, name, "")
return err
}
var _ Getter = (*azureZonesClient)(nil)
var _ Getter = (*azureVirtualNetworkLinksClient)(nil)
94 changes: 94 additions & 0 deletions azure/services/privatedns/client_records.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package privatedns

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
azureautorest "github.com/Azure/go-autorest/autorest/azure"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)

// azureRecordsClient contains the Azure go-sdk Client for record sets.
type azureRecordsClient struct {
recordsets privatedns.RecordSetsClient
}

// newRecordSetsClient creates a new record sets client from subscription ID.
func newRecordSetsClient(auth azure.Authorizer) *azureRecordsClient {
recordsClient := privatedns.NewRecordSetsClientWithBaseURI(auth.BaseURI(), auth.SubscriptionID())
azure.SetAutoRestClientDefaults(&recordsClient.Client, auth.Authorizer())
return &azureRecordsClient{
recordsets: recordsClient,
}
}

// CreateOrUpdateAsync creates or updates a record asynchronously.
// Well, I lied, CreateOrUpdateAsync creates a record synchronously. This is because azure api for records does not futures yet.
// TODO: make it async once azure api for records gets future support.
func (arc *azureRecordsClient) CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter, parameters interface{}) (result interface{}, future azureautorest.FutureAPI, err error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.azureRecordsClient.CreateOrUpdateAsync")
defer done()

set, ok := parameters.(privatedns.RecordSet)
if !ok {
return nil, nil, errors.Errorf("%T is not a privatedns.RecordSet", parameters)
}

// Determine record type.
var (
recordType privatedns.RecordType
aRecords = set.RecordSetProperties.ARecords
aaaRecords = set.RecordSetProperties.AaaaRecords
)
if aRecords != nil && len(*aRecords) > 0 && (*aRecords)[0].Ipv4Address != nil {
recordType = privatedns.A
} else if aaaRecords != nil && len(*aaaRecords) > 0 && (*aaaRecords)[0].Ipv6Address != nil {
recordType = privatedns.AAAA
}

recordSet, err := arc.recordsets.CreateOrUpdate(ctx, spec.ResourceGroupName(), spec.OwnerResourceName(), recordType, spec.ResourceName(), set, "", "")
if err != nil {
return nil, nil, err
}

return recordSet, nil, err
}

// Get gets the specified record set. Noop for records.
func (arc *azureRecordsClient) Get(ctx context.Context, spec azure.ResourceSpecGetter) (result interface{}, err error) {
return nil, nil
}

// DeleteAsync deletes a record asynchronously. Noop for records.
// TODO: implement Delete for records.
func (arc *azureRecordsClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecGetter) (future azureautorest.FutureAPI, err error) {
return nil, nil
}

// IsDone returns true if the long-running operation has completed. Noop for records.
func (arc *azureRecordsClient) IsDone(ctx context.Context, future azureautorest.FutureAPI) (isDone bool, err error) {
return true, nil
}

// Result fetches the result of a long-running operation future. Noop for records.
func (arc *azureRecordsClient) Result(ctx context.Context, future azureautorest.FutureAPI, futureType string) (result interface{}, err error) {
return nil, nil
}
Loading

0 comments on commit 8324433

Please sign in to comment.