Skip to content

Commit

Permalink
Add support for a unique ID from clusters.
Browse files Browse the repository at this point in the history
  • Loading branch information
bigkevmcd committed Oct 19, 2023
1 parent 6410230 commit d187b05
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 7 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module github.com/weaveworks/cluster-reflector-controller
go 1.20

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4 v4.3.0
github.com/Azure/go-autorest/autorest v0.11.29
github.com/fluxcd/pkg/apis/meta v1.1.2
github.com/google/go-cmp v0.5.9
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
github.com/weaveworks/cluster-controller v1.5.2
github.com/weaveworks/weave-gitops v0.33.0
k8s.io/api v0.27.5
Expand All @@ -20,7 +23,6 @@ require (
)

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.22 // indirect
Expand All @@ -47,7 +49,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 h1:sXr+ck84g/ZlZUOZiNELInm
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0 h1:figxyQZXzZQIcP3njhC68bYUiTw45J8/SsHaLW8Ax0M=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0/go.mod h1:TmlMW4W5OvXOmOyKNnor8nlMMiO1ctIyzmHme/VHsrA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4 v4.3.0 h1:U73ZEM5QTwb7x/VrXLTi+sb6Aw9DqFJxOpWuj+pDPfk=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4 v4.3.0/go.mod h1:WpiaNrHqgIy+P5gTYbOA/JuMmxq7uq8onUvVBybjIlI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2 h1:mLY+pNLjCUeKhgnAJWAKhEUQM+RJQo2H1fuGSw1Ky1E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.0.0 h1:ECsQtyERDVz3NP3kvDOTLvbQhqWp/x9EsGKtb4ogUr8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
Expand Down Expand Up @@ -218,8 +222,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/weaveworks/cluster-controller v1.5.2 h1:HEgiovJSvrLsdU7Y4mswfCzq8ca2hzpnygBVIzbQQW8=
github.com/weaveworks/cluster-controller v1.5.2/go.mod h1:rYBv/mUMvXOP6NdSSUvvpT6ptpOPE9cqSl7WMM4LkcY=
github.com/weaveworks/weave-gitops v0.33.0 h1:LL3KO3nP+nluIYdXhfGbw9y4JmDk7PRDbgCbo/jG68c=
Expand Down
27 changes: 27 additions & 0 deletions pkg/providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package azure
import (
"context"
"fmt"
"strings"

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

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice"
"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"
)

// AzureProvider queries all AKS clusters for the provided SubscriptionID and
Expand Down Expand Up @@ -58,6 +62,29 @@ func (p *AzureProvider) ListClusters(ctx context.Context) ([]*providers.Provider
return clusters, nil
}

func (p *AzureProvider) ClusterID(ctx context.Context, kubeClient client.Reader) (string, error) {
configMap := &corev1.ConfigMap{}
if err := kubeClient.Get(ctx, client.ObjectKey{Name: "extension-manager-config", Namespace: "kube-system"}, configMap); err != nil {
return "", client.IgnoreNotFound(err)
}

keys := keysFromConfigMap(configMap, "AZURE_RESOURCE_GROUP", "AZURE_RESOURCE_NAME", "AZURE_SUBSCRIPTION_ID")

return strings.Join(keys, "/"), nil
}

func keysFromConfigMap(configMap *corev1.ConfigMap, keys ...string) []string {
values := []string{}
for _, k := range keys {
v, ok := configMap.Data[k]
if ok {
values = append(values, v)
}
}

return values
}

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

resourceGroup, err := aksClusterResourceGroup(*aksCluster.ID)
Expand Down
123 changes: 121 additions & 2 deletions pkg/providers/azure/azure_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,123 @@
package azure

// TODO: Add tests
// The next release of the Azure SDK will include a mock client for testing.
import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
acs "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/google/go-cmp/cmp"

"github.com/weaveworks/cluster-reflector-controller/pkg/providers"
)

const testSubscriptionID = "ace37984-3d07-4051-fe01-d5a52c0cb79d"

var _ providers.Provider = (*AzureProvider)(nil)

func TestAzureProvider_ClusterID(t *testing.T) {
clusterIDTests := []struct {
name string
objs []client.Object
want string
}{
{
name: "ConfigMap exists",
objs: []client.Object{
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "extension-manager-config",
Namespace: "kube-system",
},
Data: map[string]string{
"AZURE_RESOURCE_GROUP": "team-pesto-use1",
"AZURE_RESOURCE_NAME": "pestomarketplacetest",
"AZURE_SUBSCRIPTION_ID": testSubscriptionID,
},
},
},
want: "team-pesto-use1/pestomarketplacetest/" + testSubscriptionID,
},
{
// Ths is ok because it should not match an ID provided by the
// ListClusters method.
name: "ConfigMap does not exist",
objs: []client.Object{},
want: "",
},
}

for _, tt := range clusterIDTests {
t.Run(tt.name, func(t *testing.T) {
fc := fake.NewClientBuilder().WithObjects(tt.objs...).Build()

provider := NewAzureProvider(testSubscriptionID)

clusterID, err := provider.ClusterID(context.TODO(), fc)
if err != nil {
t.Fatal(err)
}

if clusterID != tt.want {
t.Fatalf("ClusterID() got %s, want %s", clusterID, tt.want)
}
})
}
}

func TestClusterProvider_ListClusters(t *testing.T) {
stubClient := stubManagedClustersClient{
[]*acs.ManagedCluster{
{
ID: to.Ptr("/subscriptions/ace37984-3d07-4051-9002-d5a52c0ae14b/resourcegroups/demo-team/providers/Microsoft.ContainerService/managedClusters/cluster-1"),
Name: to.Ptr("cluster-1"),
},
},
}

provider := NewAzureProvider("test-subscription")
provider.ClientFactory = func(s string) (AKSClusterClient, error) {
return stubClient, nil
}

provided, err := provider.ListClusters(context.TODO())
if err != nil {
t.Fatal(err)
}

want := []*providers.ProviderCluster{
{Name: "cluster-1"},
}
if diff := cmp.Diff(want, provided); diff != "" {
t.Fatalf("failed to list clusters:\n%s", diff)
}
}

type stubManagedClustersClient struct {
Clusters []*acs.ManagedCluster
}

func (m stubManagedClustersClient) NewListPager(_ *acs.ManagedClustersClientListOptions) *runtime.Pager[acs.ManagedClustersClientListResponse] {
return runtime.NewPager(runtime.PagingHandler[acs.ManagedClustersClientListResponse]{
More: func(_ acs.ManagedClustersClientListResponse) bool {
return false
},
Fetcher: func(_ context.Context, _ *acs.ManagedClustersClientListResponse) (acs.ManagedClustersClientListResponse, error) {
return acs.ManagedClustersClientListResponse{
ManagedClusterListResult: acs.ManagedClusterListResult{
Value: m.Clusters,
},
}, nil
},
})
}

func (m stubManagedClustersClient) ListClusterAdminCredentials(ctx context.Context, resourceGroupName string, resourceName string, options *acs.ManagedClustersClientListClusterAdminCredentialsOptions) (acs.ManagedClustersClientListClusterAdminCredentialsResponse, error) {
return acs.ManagedClustersClientListClusterAdminCredentialsResponse{}, nil
}

0 comments on commit d187b05

Please sign in to comment.