diff --git a/pkg/providers/azure/azure.go b/pkg/providers/azure/azure.go index fb5ebf3..2705703 100644 --- a/pkg/providers/azure/azure.go +++ b/pkg/providers/azure/azure.go @@ -7,8 +7,9 @@ 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" @@ -16,31 +17,35 @@ import ( "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 { @@ -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 { @@ -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 { @@ -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 +}