Skip to content

Commit

Permalink
Add track2 API Client to Cluster struct (loadbalancer and interfaces) (
Browse files Browse the repository at this point in the history
  • Loading branch information
bitoku authored May 16, 2024
1 parent 24f43d8 commit cceb396
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/Azure/ARO-RP/pkg/metrics"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
"github.com/Azure/ARO-RP/pkg/operator/deploy"
"github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/armnetwork"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/authorization"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/compute"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/features"
Expand Down Expand Up @@ -69,9 +70,11 @@ type manager struct {
spGraphClient *utilgraph.GraphServiceClient
disks compute.DisksClient
virtualMachines compute.VirtualMachinesClient
interfaces network.InterfacesClient
interfaces network.InterfacesClient // TODO: use armInterfaces instead.
armInterfaces armnetwork.InterfacesClient
publicIPAddresses network.PublicIPAddressesClient
loadBalancers network.LoadBalancersClient
loadBalancers network.LoadBalancersClient // TODO: use armLoadBalancers instead.
armLoadBalancers armnetwork.LoadBalancersClient
privateEndpoints network.PrivateEndpointsClient
securityGroups network.SecurityGroupsClient
deployments features.DeploymentsClient
Expand Down Expand Up @@ -128,11 +131,17 @@ func New(ctx context.Context, log *logrus.Entry, _env env.Interface, db database
return nil, err
}

// TODO: Delete once the replace to track2 is done
fpAuthorizer, err := refreshable.NewAuthorizer(_env, subscriptionDoc.Subscription.Properties.TenantID)
if err != nil {
return nil, err
}

fpCredential, err := _env.FPNewClientCertificateCredential(_env.TenantID())
if err != nil {
return nil, err
}

msiAuthorizer, err := _env.NewMSIAuthorizer(_env.Environment().ResourceManagerScope)
if err != nil {
return nil, err
Expand All @@ -150,6 +159,16 @@ func New(ctx context.Context, log *logrus.Entry, _env env.Interface, db database
return nil, err
}

armLoadBalancersClient, err := armnetwork.NewLoadBalancersClient(_env.Environment(), r.SubscriptionID, fpCredential)
if err != nil {
return nil, err
}

armInterfacesClient, err := armnetwork.NewInterfacesClient(_env.Environment(), r.SubscriptionID, fpCredential)
if err != nil {
return nil, err
}

return &manager{
log: log,
env: _env,
Expand All @@ -165,8 +184,10 @@ func New(ctx context.Context, log *logrus.Entry, _env env.Interface, db database
disks: compute.NewDisksClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
virtualMachines: compute.NewVirtualMachinesClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
interfaces: network.NewInterfacesClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
armInterfaces: armInterfacesClient,
publicIPAddresses: network.NewPublicIPAddressesClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
loadBalancers: network.NewLoadBalancersClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
armLoadBalancers: armLoadBalancersClient,
privateEndpoints: network.NewPrivateEndpointsClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
securityGroups: network.NewSecurityGroupsClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
deployments: features.NewDeploymentsClient(_env.Environment(), r.SubscriptionID, fpAuthorizer),
Expand Down
1 change: 1 addition & 0 deletions pkg/env/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (d *dev) Listen() (net.Listener, error) {
return net.Listen("tcp", "localhost:8443")
}

// TODO: Delete FPAuthorizer once the replace from track1 to track2 is done.
func (d *dev) FPAuthorizer(tenantID string, scopes ...string) (autorest.Authorizer, error) {
fpTokenCredential, err := d.FPNewClientCertificateCredential(tenantID)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Interface interface {
ClusterKeyvault() keyvault.Manager
Domain() string
FeatureIsSet(Feature) bool
// TODO: Delete FPAuthorizer once the replace from track1 to track2 is done.
FPAuthorizer(string, ...string) (autorest.Authorizer, error)
FPNewClientCertificateCredential(string) (*azidentity.ClientCertificateCredential, error)
FPClientID() string
Expand Down
1 change: 1 addition & 0 deletions pkg/env/prod.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func (p *prod) FeatureIsSet(f Feature) bool {
return p.features[f]
}

// TODO: Delete FPAuthorizer once the replace from track1 to track2 is done.
func (p *prod) FPAuthorizer(tenantID string, scopes ...string) (autorest.Authorizer, error) {
fpTokenCredential, err := p.FPNewClientCertificateCredential(tenantID)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/azureclient/azuresdk/armnetwork/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package armnetwork

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

//go:generate rm -rf ../../../../util/mocks/$GOPACKAGE
//go:generate go run ../../../../../vendor/github.com/golang/mock/mockgen -destination=../../../../util/mocks/azureclient/azuresdk/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/$GOPACKAGE InterfacesClient,LoadBalancersClient,LoadBalancerBackendAddressPoolsClient
//go:generate go run ../../../../../vendor/golang.org/x/tools/cmd/goimports -local=github.com/Azure/ARO-RP -e -w ../../../../util/mocks/azureclient/azuresdk/$GOPACKAGE/$GOPACKAGE.go
41 changes: 41 additions & 0 deletions pkg/util/azureclient/azuresdk/armnetwork/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package armnetwork

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"

"github.com/Azure/ARO-RP/pkg/util/azureclient"
)

// InterfacesClient is a minimal interface for azure InterfacesClient
type InterfacesClient interface {
InterfacesClientAddons
Get(ctx context.Context, resourceGroupName string, networkInterfaceName string, options *armnetwork.InterfacesClientGetOptions) (result armnetwork.InterfacesClientGetResponse, err error)
}

type interfacesClient struct {
*armnetwork.InterfacesClient
}

var _ InterfacesClient = &interfacesClient{}

// NewInterfacesClient creates a new InterfacesClient
func NewInterfacesClient(environment *azureclient.AROEnvironment, subscriptionID string, credential azcore.TokenCredential) (InterfacesClient, error) {
options := arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: environment.Cloud,
},
}
clientFactory, err := armnetwork.NewClientFactory(subscriptionID, credential, &options)
if err != nil {
return nil, err
}

return &interfacesClient{InterfacesClient: clientFactory.NewInterfacesClient()}, nil
}
34 changes: 34 additions & 0 deletions pkg/util/azureclient/azuresdk/armnetwork/interfaces_addons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package armnetwork

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"
)

// InterfacesClientAddons contains addons for InterfacesClient
type InterfacesClientAddons interface {
CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, networkInterfaceName string, parameters armnetwork.Interface, options *armnetwork.InterfacesClientBeginCreateOrUpdateOptions) (err error)
DeleteAndWait(ctx context.Context, resourceGroupName string, networkInterfaceName string, options *armnetwork.InterfacesClientBeginDeleteOptions) (err error)
}

func (c *interfacesClient) CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, networkInterfaceName string, parameters armnetwork.Interface, options *armnetwork.InterfacesClientBeginCreateOrUpdateOptions) error {
poller, err := c.InterfacesClient.BeginCreateOrUpdate(ctx, resourceGroupName, networkInterfaceName, parameters, options)
if err != nil {
return err
}
_, err = poller.PollUntilDone(ctx, nil)
return err
}

func (c *interfacesClient) DeleteAndWait(ctx context.Context, resourceGroupName string, networkInterfaceName string, options *armnetwork.InterfacesClientBeginDeleteOptions) error {
poller, err := c.InterfacesClient.BeginDelete(ctx, resourceGroupName, networkInterfaceName, options)
if err != nil {
return err
}
_, err = poller.PollUntilDone(ctx, nil)
return err
}
64 changes: 64 additions & 0 deletions pkg/util/azureclient/azuresdk/armnetwork/loadbalancers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package armnetwork

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"

"github.com/Azure/ARO-RP/pkg/util/azureclient"
)

// LoadBalancersClient is a minimal interface for Azure LoadBalancersClient
type LoadBalancersClient interface {
Get(ctx context.Context, resourceGroupName string, loadBalancerName string, options *armnetwork.LoadBalancersClientGetOptions) (result armnetwork.LoadBalancersClientGetResponse, err error)
LoadBalancersClientAddons
}

type loadBalancersClient struct {
*armnetwork.LoadBalancersClient
}

var _ LoadBalancersClient = &loadBalancersClient{}

// NewLoadBalancersClient creates a new LoadBalancersClient
func NewLoadBalancersClient(environment *azureclient.AROEnvironment, subscriptionID string, credential azcore.TokenCredential) (LoadBalancersClient, error) {
options := arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: environment.Cloud,
},
}
clientFactory, err := armnetwork.NewClientFactory(subscriptionID, credential, &options)
if err != nil {
return nil, err
}
return &loadBalancersClient{LoadBalancersClient: clientFactory.NewLoadBalancersClient()}, nil
}

type LoadBalancerBackendAddressPoolsClient interface {
Get(ctx context.Context, resourceGroupName string, loadBalancerName string, backendAddressPoolName string, options *armnetwork.LoadBalancerBackendAddressPoolsClientGetOptions) (result armnetwork.LoadBalancerBackendAddressPoolsClientGetResponse, err error)
}

type loadBalancerBackendAddressPoolsClient struct {
*armnetwork.LoadBalancerBackendAddressPoolsClient
}

var _ LoadBalancerBackendAddressPoolsClient = &loadBalancerBackendAddressPoolsClient{}

// NewLoadBalancerBackendAddressPoolsClient creates a new NewLoadBalancerBackendAddressPoolsClient
func NewLoadBalancerBackendAddressPoolsClient(environment *azureclient.AROEnvironment, subscriptionID string, credential azcore.TokenCredential) (LoadBalancerBackendAddressPoolsClient, error) {
options := arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: environment.Cloud,
},
}
clientFactory, err := armnetwork.NewClientFactory(subscriptionID, credential, &options)
if err != nil {
return nil, err
}
return &loadBalancerBackendAddressPoolsClient{LoadBalancerBackendAddressPoolsClient: clientFactory.NewLoadBalancerBackendAddressPoolsClient()}, nil
}
24 changes: 24 additions & 0 deletions pkg/util/azureclient/azuresdk/armnetwork/loadbalancers_addons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package armnetwork

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"
)

// LoadBalancersClientAddons contains addons for Azure LoadBalancersClient
type LoadBalancersClientAddons interface {
CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, loadBalancerName string, parameters armnetwork.LoadBalancer, options *armnetwork.LoadBalancersClientBeginCreateOrUpdateOptions) error
}

func (c *loadBalancersClient) CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, loadBalancerName string, parameters armnetwork.LoadBalancer, options *armnetwork.LoadBalancersClientBeginCreateOrUpdateOptions) error {
poller, err := c.LoadBalancersClient.BeginCreateOrUpdate(ctx, resourceGroupName, loadBalancerName, parameters, options)
if err != nil {
return err
}
_, err = poller.PollUntilDone(ctx, nil)
return err
}
Loading

0 comments on commit cceb396

Please sign in to comment.