Skip to content

Commit

Permalink
initial stub at updating loadbalancers service to SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
nawazkh committed Sep 7, 2023
1 parent b082981 commit 959e28e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
8 changes: 4 additions & 4 deletions azure/converters/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ limitations under the License.
package converters

import (
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

// SKUtoSDK converts infrav1.SKU into a network.LoadBalancerSkuName.
func SKUtoSDK(src infrav1.SKU) network.LoadBalancerSkuName {
// SKUtoSDK converts infrav1.SKU into a armnetwork.LoadBalancerSKUName.
func SKUtoSDK(src infrav1.SKU) armnetwork.LoadBalancerSKUName {
if src == infrav1.SKUStandard {
return network.LoadBalancerSkuNameStandard
return armnetwork.LoadBalancerSKUNameStandard
}
return ""
}
30 changes: 20 additions & 10 deletions azure/services/loadbalancers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"context"
"encoding/json"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
"github.com/Azure/go-autorest/autorest"
azureautorest "github.com/Azure/go-autorest/autorest/azure"
"github.com/pkg/errors"
Expand All @@ -32,19 +34,27 @@ import (

// azureClient contains the Azure go-sdk Client.
type azureClient struct {
loadbalancers network.LoadBalancersClient
loadbalancers armnetwork.LoadBalancersClient
}

// newClient creates a new load balancer client from subscription ID.
func newClient(auth azure.Authorizer) *azureClient {
c := newLoadBalancersClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
return &azureClient{c}
func newClient(auth azure.Authorizer) (*azureClient, error) {
c, err := newLoadBalancersClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer(), auth.Token())
if err != nil {
return nil, err
}
return &azureClient{c}, nil
}

// newLoadbalancersClient creates a new load balancer client from subscription ID.
func newLoadBalancersClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) network.LoadBalancersClient {
loadBalancersClient := network.NewLoadBalancersClientWithBaseURI(baseURI, subscriptionID)
azure.SetAutoRestClientDefaults(&loadBalancersClient.Client, authorizer)
func newLoadBalancersClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer, credential azcore.TokenCredential) (armnetwork.LoadBalancersClient, error) {
// loadBalancersClient := network.NewLoadBalancersClientWithBaseURI(baseURI, subscriptionID)
loadBalancersClient, err := armnetwork.NewLoadBalancersClient(subscriptionID, credential, &policy.ClientOptions{})
if err != nil {
return armnetwork.LoadBalancersClient{}, errors.Wrap(err, "failed to create load balancers client")
}

azure.SetAutoRestClientDefaults(&loadBalancersClient, authorizer)
return loadBalancersClient
}

Expand All @@ -63,7 +73,7 @@ func (ac *azureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.Resou
ctx, _, done := tele.StartSpanWithLogger(ctx, "loadbalancers.azureClient.CreateOrUpdate")
defer done()

loadBalancer, ok := parameters.(network.LoadBalancer)
loadBalancer, ok := parameters.(armnetwork.LoadBalancer)
if !ok {
return nil, nil, errors.Errorf("%T is not a network.LoadBalancer", parameters)
}
Expand Down Expand Up @@ -155,7 +165,7 @@ func (ac *azureClient) Result(ctx context.Context, future azureautorest.FutureAP
// Marshal and Unmarshal the future to put it into the correct future type so we can access the Result function.
// Unfortunately the FutureAPI can't be casted directly to LoadBalancersCreateOrUpdateFuture because it is a azureautorest.Future, which doesn't implement the Result function. See PR #1686 for discussion on alternatives.
// It was converted back to a generic azureautorest.Future from the CAPZ infrav1.Future type stored in Status: https://github.com/kubernetes-sigs/cluster-api-provider-azure/blob/main/azure/converters/futures.go#L49.
var createFuture *network.LoadBalancersCreateOrUpdateFuture
var createFuture *armnetwork.LoadBalancersCreateOrUpdateFuture
jsonData, err := future.MarshalJSON()
if err != nil {
return nil, errors.Wrap(err, "failed to marshal future")
Expand Down
6 changes: 3 additions & 3 deletions azure/services/loadbalancers/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/async"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/asyncpoller"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
Expand All @@ -44,15 +44,15 @@ type LBScope interface {
// Service provides operations on Azure resources.
type Service struct {
Scope LBScope
async.Reconciler
asyncpoller.Reconciler
}

// New creates a new service.
func New(scope LBScope) *Service {
client := newClient(scope)
return &Service{
Scope: scope,
Reconciler: async.New(scope, client, client),
Reconciler: asyncpoller.New(scope, client, client),
}
}

Expand Down

0 comments on commit 959e28e

Please sign in to comment.