Skip to content

Commit

Permalink
Refactor towards tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bigkevmcd committed Oct 19, 2023
1 parent d187b05 commit da292f7
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions pkg/providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,45 @@ import (

corev1 "k8s.io/api/core/v1"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice"
acs "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/weaveworks/cluster-reflector-controller/pkg/providers"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// AKSclusterLister implementations query for AKS clusters.
type AKSClusterClient interface {
NewListPager(options *acs.ManagedClustersClientListOptions) *runtime.Pager[acs.ManagedClustersClientListResponse]
ListClusterAdminCredentials(ctx context.Context, resourceGroupName string, resourceName string, options *acs.ManagedClustersClientListClusterAdminCredentialsOptions) (acs.ManagedClustersClientListClusterAdminCredentialsResponse, error)
}

// AzureProvider queries all AKS clusters for the provided SubscriptionID and
// returns the clusters and kubeconfigs for the clusters.
type AzureProvider struct {
SubscriptionID string
ClientFactory func(string) (AKSClusterClient, error)
}

// NewAzureProvider creates and returns an AzureProvider ready for use.
func NewAzureProvider(subscriptionID string) *AzureProvider {
return &AzureProvider{
SubscriptionID: subscriptionID,
ClientFactory: clientFactory,
}
}

func (p *AzureProvider) ListClusters(ctx context.Context) ([]*providers.ProviderCluster, error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, fmt.Errorf("failed to obtain a credential: %v", err)
}
client, err := armcontainerservice.NewManagedClustersClient(p.SubscriptionID, cred, nil)
client, err := p.ClientFactory(p.SubscriptionID)
if err != nil {
return nil, fmt.Errorf("failed to create client: %v", err)
}

clusters := []*providers.ProviderCluster{}
pager := client.NewListPager(nil)
clusters := []*providers.ProviderCluster{}
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
Expand Down Expand Up @@ -85,7 +90,7 @@ func keysFromConfigMap(configMap *corev1.ConfigMap, keys ...string) []string {
return values
}

func getKubeconfigForCluster(ctx context.Context, client *armcontainerservice.ManagedClustersClient, aksCluster *armcontainerservice.ManagedCluster) (*clientcmdapi.Config, error) {
func getKubeconfigForCluster(ctx context.Context, client AKSClusterClient, aksCluster *acs.ManagedCluster) (*clientcmdapi.Config, error) {

resourceGroup, err := aksClusterResourceGroup(*aksCluster.ID)
if err != nil {
Expand All @@ -95,7 +100,7 @@ func getKubeconfigForCluster(ctx context.Context, client *armcontainerservice.Ma
credentialsResponse, err := client.ListClusterAdminCredentials(ctx,
resourceGroup,
*aksCluster.Name,
&armcontainerservice.ManagedClustersClientListClusterAdminCredentialsOptions{ServerFqdn: nil},
&acs.ManagedClustersClientListClusterAdminCredentialsOptions{ServerFqdn: nil},
)

if err != nil {
Expand All @@ -121,3 +126,18 @@ func aksClusterResourceGroup(clusterID string) (string, error) {
}
return resource.ResourceGroup, nil
}

// this is the default client factory which just creates a set of
// AzureCredentials and creates a client from it.
func clientFactory(subscriptionID string) (AKSClusterClient, error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, fmt.Errorf("failed to obtain a credential: %v", err)
}
client, err := acs.NewManagedClustersClient(subscriptionID, cred, nil)
if err != nil {
return nil, fmt.Errorf("failed to create client: %v", err)
}

return client, nil
}

0 comments on commit da292f7

Please sign in to comment.