diff --git a/cmd/cloud.go b/cmd/cloud.go index fd4243e3..d29e6297 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -147,27 +147,45 @@ func getClusters(provider, regionID string, num int) ([]cloud.ClusterInfo, error } case 4: fmt.Println("⛅ Selected: Azure") - clientID, clientSecret := checkEnvForSecret(4) - SubscriptionID, sub := os.LookupEnv("AZURE_SUBSCRIPTION_ID") - ObjectID, obj := os.LookupEnv("AZURE_OBJECT_ID") - TenantID, ten := os.LookupEnv("AZURE_TENANT_ID") - if !sub || !obj || !ten { - SubscriptionID = PromptUI("Azure Subscription ID", "") - ObjectID = PromptUI("Azure Object ID", "") - TenantID = PromptUI("Azure Tenant ID", "") - } + authModes := []string{"Default (SDK Auth)", "Service Principal"} + authMode := selectOption(nil, authModes, "Select Auth Type") azure := cloud.Azure{ - ClientID: clientID, - ClientSecret: clientSecret, - SubscriptionID: SubscriptionID, - ObjectID: ObjectID, - TenantID: TenantID, + AuthMode: cloud.AzureAuth(authMode), + TenantID: os.Getenv("AZURE_TENANT_ID"), + SubscriptionID: os.Getenv("AZURE_SUBSCRIPTION_ID"), + } + + if azure.TenantID == "" { + azure.TenantID = PromptUI("Azure Tenant ID", "") } - clusters, err = azure.ListCluster() + + if azure.AuthMode == cloud.AuthModeServicePrincipal { + azure.ClientID, azure.ClientSecret = checkEnvForSecret(4) + azure.ObjectID = os.Getenv("AZURE_OBJECT_ID") + + if azure.ObjectID == "" { + azure.ObjectID = PromptUI("Azure Object ID", "") + } + } + + subscriptionList, err := azure.ListSubscriptions() if err != nil { return nil, err } + + for _, subscription := range subscriptionList { + if azure.SubscriptionID != "" && azure.SubscriptionID != subscription.ID { + continue + } + + subscriptionClusters, err := azure.ListCluster(subscription) + if err != nil { + return nil, err + } + clusters = append(clusters, subscriptionClusters...) + } } + return clusters, err } @@ -262,6 +280,9 @@ func selectCluster(clouds []cloud.ClusterInfo, label string) int { Details: ` --------- Info ---------- {{ "Name:" | faint }} {{ .Name }} +{{- with .Account }} +{{ "Account:" | faint }} {{ . }} +{{- end }} {{ "RegionID:" | faint }} {{ .RegionID }} {{ "Version:" | faint }} {{ .K8sVersion }} {{ "ID:" | faint }} {{ .ID }}`, @@ -286,15 +307,28 @@ func selectRegion(regionList []string, label string) int { Inactive: " {{ . | cyan }}", Selected: "\U0001F30D Selected: {{ . | green }}", } + + return selectOption(templates, regionList, label) +} + +func selectOption(templates *promptui.SelectTemplates, options []string, label string) int { + if templates == nil { + templates = &promptui.SelectTemplates{ + Label: "{{ . }}", + Inactive: " {{ . | cyan }}", + Selected: "\U0001F30D Selected: {{ . | green }}", + } + } + searcher := func(input string, index int) bool { - pepper := regionList[index] + pepper := options[index] name := strings.Replace(strings.ToLower(pepper), " ", "", -1) input = strings.Replace(strings.ToLower(input), " ", "", -1) return strings.Contains(name, input) } prompt := promptui.Select{ Label: label, - Items: regionList, + Items: options, Templates: templates, Size: uiSize, Searcher: searcher, diff --git a/cmd/cloud_add.go b/cmd/cloud_add.go index 64346dbc..e76d7406 100644 --- a/cmd/cloud_add.go +++ b/cmd/cloud_add.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "strings" "github.com/mgutz/ansi" @@ -227,32 +228,50 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error ansi.Color(" please install the AWS CLI before normal use.", "white+h")) case 4: fmt.Println("⛅ Selected: Azure") - clientID, clientSecret := checkEnvForSecret(4) - SubscriptionID, sub := os.LookupEnv("AZURE_SUBSCRIPTION_ID") - ObjectID, obj := os.LookupEnv("AZURE_OBJECT_ID") - TenantID, ten := os.LookupEnv("AZURE_TENANT_ID") - if !sub || !obj || !ten { - SubscriptionID = PromptUI("Azure Subscription ID", "") - ObjectID = PromptUI("Azure Object ID", "") - TenantID = PromptUI("Azure Tenant ID", "") - } + authModes := []string{"Default (SDK Auth)", "Service Principal"} + authMode := selectOption(nil, authModes, "Select Auth Type") azure := cloud.Azure{ - ClientID: clientID, - ClientSecret: clientSecret, - SubscriptionID: SubscriptionID, - ObjectID: ObjectID, - TenantID: TenantID, + AuthMode: cloud.AzureAuth(authMode), + TenantID: os.Getenv("AZURE_TENANT_ID"), + SubscriptionID: os.Getenv("AZURE_SUBSCRIPTION_ID"), } - if clusterID == "" { - clusters, err := azure.ListCluster() - if err != nil { - return err + + if azure.TenantID == "" { + azure.TenantID = PromptUI("Azure Tenant ID", "") + } + + if azure.AuthMode == cloud.AuthModeServicePrincipal { + azure.ClientID, azure.ClientSecret = checkEnvForSecret(4) + azure.ObjectID = os.Getenv("AZURE_OBJECT_ID") + + if azure.ObjectID == "" { + azure.ObjectID = PromptUI("Azure Object ID", "") } - if len(clusters) == 0 { - return errors.New("no clusters found") + } + + if clusterID != "" { + clusterIDParts := strings.Split(clusterID, "/") + if len(clusterIDParts) != 9 { + return fmt.Errorf("invalid id %s", clusterID) + } + azure.SubscriptionID = clusterIDParts[2] + resourceGroup := clusterIDParts[4] + clusterName := clusterIDParts[8] + + var ( + kubeConfig []byte + err error + ) + kubeConfigType := selectOption(nil, []string{"User Config", "Admin Config"}, "Select Config Type") + switch kubeConfigType { + case 0: + kubeConfig, err = azure.GetKubeConfig(clusterName, resourceGroup) + case 1: + kubeConfig, err = azure.GetAdminKubeConfig(clusterName, resourceGroup) + default: + return fmt.Errorf("invalid config type %d", kubeConfigType) } - clusterNum := selectCluster(clusters, "Select Cluster") - kubeConfig, err := azure.GetKubeConfig(clusters[clusterNum].Name, clusters[clusterNum].ID) + if err != nil { return err } @@ -263,6 +282,58 @@ func (ca *CloudAddCommand) runCloudAdd(cmd *cobra.Command, args []string) error return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover) } + subscriptionList, err := azure.ListSubscriptions() + if err != nil { + return err + } + + var clusters []cloud.ClusterInfo + + for _, subscription := range subscriptionList { + if azure.SubscriptionID != "" && azure.SubscriptionID != subscription.ID { + continue + } + + subscriptionClusters, err := azure.ListCluster(subscription) + if err != nil { + return err + } + clusters = append(clusters, subscriptionClusters...) + } + + if len(clusters) == 0 { + return errors.New("no clusters found") + } + + clusterNum := selectCluster(clusters, "Select Cluster") + cluster := clusters[clusterNum] + + if azure.SubscriptionID == "" { + azure.SubscriptionID = strings.Split(cluster.ID, "/")[2] + } + + resourceGroup := strings.Split(cluster.ID, "/")[4] + + var kubeConfig []byte + kubeConfigType := selectOption(nil, []string{"User Config", "Admin Config"}, "Select Config Type") + switch kubeConfigType { + case 0: + kubeConfig, err = azure.GetKubeConfig(cluster.Name, resourceGroup) + case 1: + kubeConfig, err = azure.GetAdminKubeConfig(cluster.Name, resourceGroup) + default: + return fmt.Errorf("invalid config type %d", kubeConfigType) + } + + if err != nil { + return err + } + newConfig, err := clientcmd.Load(kubeConfig) + if err != nil { + return err + } + return AddToLocal(newConfig, fmt.Sprintf("azure-%s", clusterID), "", cover) + } return nil } diff --git a/cmd/cloud_list.go b/cmd/cloud_list.go index 3aace494..73638944 100644 --- a/cmd/cloud_list.go +++ b/cmd/cloud_list.go @@ -50,13 +50,13 @@ func (cl *CloudListCommand) runCloudList(cmd *cobra.Command, args []string) erro func printListTable(clusters []cloud.ClusterInfo) error { var table [][]string for _, k := range clusters { - conTmp := []string{k.ID, k.Name, k.RegionID, k.K8sVersion, k.ConsoleURL} + conTmp := []string{k.ID, k.Account, k.Name, k.RegionID, k.K8sVersion, k.ConsoleURL} table = append(table, conTmp) } if table != nil { tabulate := gotabulate.Create(table) - tabulate.SetHeaders([]string{"ID", "NAME", "REGION ID", "VERSION", "CONSOLE URL"}) + tabulate.SetHeaders([]string{"ID", "ACCOUNT", "NAME", "REGION ID", "VERSION", "CONSOLE URL"}) // Turn On String Wrapping tabulate.SetWrapStrings(false) // Render the table diff --git a/go.mod b/go.mod index 1d3ff3c1..641ea6c9 100644 --- a/go.mod +++ b/go.mod @@ -29,9 +29,10 @@ require ( ) require ( - github.com/Azure/azure-sdk-for-go v68.0.0+incompatible - github.com/Azure/go-autorest/autorest v0.11.24 - github.com/Azure/go-autorest/autorest/azure/auth v0.5.12 + github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2 + github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 + github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4 v4.7.0 + github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.2.0 github.com/aws/aws-sdk-go v1.50.5 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/stretchr/testify v1.8.4 @@ -41,14 +42,8 @@ require ( atomicgo.dev/cursor v0.2.0 // indirect atomicgo.dev/keyboard v0.2.9 // indirect atomicgo.dev/schedule v0.1.0 // indirect - github.com/Azure/go-autorest v14.2.0+incompatible // indirect - github.com/Azure/go-autorest/autorest/adal v0.9.18 // indirect - github.com/Azure/go-autorest/autorest/azure/cli v0.4.5 // indirect - github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect - github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect - github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect - github.com/Azure/go-autorest/logger v0.2.1 // indirect - github.com/Azure/go-autorest/tracing v0.6.0 // indirect + github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect + github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 // indirect github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4 // indirect github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68 // indirect github.com/alibabacloud-go/endpoint-util v1.1.0 // indirect @@ -61,7 +56,6 @@ require ( github.com/containerd/console v1.0.3 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/dimchansky/utfbom v1.1.1 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/evanphx/json-patch v4.12.0+incompatible // indirect github.com/ghodss/yaml v1.0.0 // indirect @@ -70,12 +64,12 @@ require ( github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang-jwt/jwt/v4 v4.4.2 // indirect + github.com/golang-jwt/jwt/v5 v5.2.0 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/gnostic-models v0.6.8 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/gookit/color v1.5.4 // indirect github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc // indirect github.com/gorilla/websocket v1.5.0 // indirect @@ -85,13 +79,13 @@ require ( github.com/json-iterator/go v1.1.12 // indirect github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect + github.com/kylelemons/godebug v1.1.0 // indirect github.com/lithammer/fuzzysearch v1.1.8 // indirect github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect @@ -105,9 +99,9 @@ require ( github.com/tjfoc/gmsm v1.3.2 // indirect github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - golang.org/x/crypto v0.17.0 // indirect + golang.org/x/crypto v0.18.0 // indirect golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect - golang.org/x/net v0.19.0 // indirect + golang.org/x/net v0.20.0 // indirect golang.org/x/oauth2 v0.10.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/term v0.16.0 // indirect diff --git a/go.sum b/go.sum index 9d3a265f..0990228d 100644 --- a/go.sum +++ b/go.sum @@ -10,38 +10,32 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= github.com/360EntSecGroup-Skylar/excelize v1.4.1/go.mod h1:vnax29X2usfl7HHkBrX5EvSCJcmH3dT9luvxzu8iGAE= -github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU= -github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2 h1:c4k2FIYIh4xtwqrQwV0Ct1v5+ehlNXj5NI/MWVsiTkQ= +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2/go.mod h1:5FDJtLEO/GxwNgUxbwrY3LP0pEoThTQJtk2oysdXHxM= +github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 h1:sO0/P7g68FrryJzljemN+6GTssUXdANk6aJ7T1ZxnsQ= +github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1/go.mod h1:h8hyGFDsU5HMivxiS2iYFZsgDbU9OnnJ163x5UGVKYo= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 h1:LqbJ/WzJUwBf8UiaSzgX7aMclParm9/5Vgp+TY51uBQ= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2/go.mod h1:yInRyqWXAuaPrgI7p70+lDDgh3mlBohis29jGMISnmc= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4 v4.7.0 h1:g65N4m1sAjm0BkjIJYtp5qnJlkoFtd6oqfa27KO9fI4= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4 v4.7.0/go.mod h1:noQIdW75SiQFB3mSFJBr4iRRH83S9skaFiBv4C0uEs0= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0 h1:PTFGRSlMKCQelWwxUyYVEUqseBJVemLyqWJjvMyt0do= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0/go.mod h1:LRr2FzBTQlONPPa5HREE5+RjSCTXl7BwOvYOaWTqCaI= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1 h1:7CBQ+Ei8SP2c6ydQTGCCrS35bDxgTMfoP2miAwK++OU= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1/go.mod h1:c/wcGeGx5FUPbM/JltUYHZcKmigwyVLJlDq+4HdtXaw= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.1.0 h1:pYhaMoTHP/zYIJGDA1sWsfyTDjdglaoYjIFMOEcL+/U= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.1.0/go.mod h1:iLq8GwpQhj09gpI4EdELwifR9kHrb/Q0LThq6iQq9yY= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.2.0 h1:UrGzkHueDwAWDdjQxC+QaXHd4tVCkISYE9j7fSSXF8k= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.2.0/go.mod h1:qskvSQeW+cxEE2bcKYyKimB1/KiQ9xpJ99bcHY0BX6c= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= -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= github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI= -github.com/Azure/go-autorest/autorest v0.11.24 h1:1fIGgHKqVm54KIPT+q8Zmd1QlVsmHqeUGso5qm2BqqE= -github.com/Azure/go-autorest/autorest v0.11.24/go.mod h1:G6kyRlFnTuSbEYkQGawPfsCswgme4iYf6rfSKUDzbCc= github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0= -github.com/Azure/go-autorest/autorest/adal v0.9.18 h1:kLnPsRjzZZUF3K5REu/Kc+qMQrvuza2bwSnNdhmzLfQ= -github.com/Azure/go-autorest/autorest/adal v0.9.18/go.mod h1:XVVeme+LZwABT8K5Lc3hA4nAe8LDBVle26gTrguhhPQ= -github.com/Azure/go-autorest/autorest/azure/auth v0.5.12 h1:wkAZRgT/pn8HhFyzfe9UnqOjJYqlembgCTi72Bm/xKk= -github.com/Azure/go-autorest/autorest/azure/auth v0.5.12/go.mod h1:84w/uV8E37feW2NCJ08uT9VBfjfUHpgLVnG2InYD6cg= -github.com/Azure/go-autorest/autorest/azure/cli v0.4.5 h1:0W/yGmFdTIT77fvdlGZ0LMISoLHFJ7Tx4U0yeB+uFs4= -github.com/Azure/go-autorest/autorest/azure/cli v0.4.5/go.mod h1:ADQAXrkgm7acgWVUNamOgh8YNrv4p27l3Wc55oVfpzg= github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA= -github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw= -github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74= github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0= github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0= -github.com/Azure/go-autorest/autorest/mocks v0.4.1 h1:K0laFcLE6VLTOwNgSxaGbUcLPuGXlNkbVvq4cW4nIHk= -github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k= -github.com/Azure/go-autorest/autorest/to v0.4.0 h1:oXVqrxakqqV1UZdSazDOPOLvOIz+XA683u8EctwboHk= -github.com/Azure/go-autorest/autorest/to v0.4.0/go.mod h1:fE8iZBn7LQR7zH/9XU2NcPR4o9jEImooCeWJcYV/zLE= -github.com/Azure/go-autorest/autorest/validation v0.3.1 h1:AgyqjAd94fwNAoTjl/WQXg4VvFeRFpO+UhNyRXqF1ac= -github.com/Azure/go-autorest/autorest/validation v0.3.1/go.mod h1:yhLgjC0Wda5DYXl6JAsWyUe4KVNffhoDhG0zVzUMo3E= github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc= -github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+ZtXWSmf4Tg= -github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8= github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= -github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo= -github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU= +github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 h1:DzHpqpoJVaCgOUdVHxE8QB52S6NiVdDQvGlny1qvPqA= +github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E= @@ -161,8 +155,8 @@ github.com/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U= -github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE= +github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI= +github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ= github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= @@ -266,10 +260,8 @@ github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5 github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= -github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= -github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs= -github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= +github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -326,8 +318,8 @@ github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLe github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= github.com/googleapis/gnostic v0.1.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= @@ -414,6 +406,8 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc= github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4= @@ -446,7 +440,6 @@ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= @@ -666,10 +659,9 @@ golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190312203227-4b39c73a6495/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -719,13 +711,12 @@ golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -774,7 +765,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -803,7 +793,6 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= diff --git a/pkg/cloud/aws.go b/pkg/cloud/aws.go index 7c9cc5a2..7a2d45ad 100644 --- a/pkg/cloud/aws.go +++ b/pkg/cloud/aws.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "fmt" + "github.com/aws/aws-sdk-go/service/sts" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "github.com/aws/aws-sdk-go/aws/credentials" @@ -37,7 +38,7 @@ func GetRegionID() ([]string, error) { var regionList []string for _, p := range partitions { for id := range p.Regions() { - //fmt.Printf("%s\n", id) + // fmt.Printf("%s\n", id) regionList = append(regionList, id) } } @@ -75,6 +76,13 @@ func (a *AWS) getClusterInfo(clusterName string) (clusterInfo ClusterInfo, err e if err != nil { return ClusterInfo{}, err } + + svcSts := sts.New(sess) + callerIdentity, err := svcSts.GetCallerIdentity(&sts.GetCallerIdentityInput{}) + if err != nil { + return ClusterInfo{}, err + } + svc := eks.New(sess) input := &eks.DescribeClusterInput{ Name: &clusterName, @@ -85,6 +93,7 @@ func (a *AWS) getClusterInfo(clusterName string) (clusterInfo ClusterInfo, err e } return ClusterInfo{ ID: *cluster.Cluster.Name, + Account: *callerIdentity.Account, Name: *cluster.Cluster.Name, RegionID: a.RegionID, K8sVersion: *cluster.Cluster.Version, diff --git a/pkg/cloud/azure.go b/pkg/cloud/azure.go index 95e58929..66917583 100644 --- a/pkg/cloud/azure.go +++ b/pkg/cloud/azure.go @@ -3,88 +3,176 @@ package cloud import ( "context" "fmt" - "strings" - "github.com/Azure/azure-sdk-for-go/profiles/latest/containerservice/mgmt/containerservice" - "github.com/Azure/go-autorest/autorest" - "github.com/Azure/go-autorest/autorest/azure/auth" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4" ) // Azure struct of azure cloud type Azure struct { + AuthMode AzureAuth ClientID string ClientSecret string SubscriptionID string TenantID string ObjectID string + + client azcore.TokenCredential +} +type AzureAuth int + +type AzureSubscription struct { + ID string + DisplayName string +} + +const ( + AuthModeDefault AzureAuth = iota + AuthModeServicePrincipal +) + +func (a *Azure) getAzureClient() (azcore.TokenCredential, error) { + if a.client != nil { + return a.client, nil + } + + var ( + err error + client azcore.TokenCredential + ) + + switch a.AuthMode { + case AuthModeDefault: + client, err = azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{ + TenantID: a.TenantID, + }) + case AuthModeServicePrincipal: + client, err = azidentity.NewClientSecretCredential(a.TenantID, a.ClientID, a.ClientSecret, nil) + default: + return nil, fmt.Errorf("invalid auth mode: %d", a.AuthMode) + } + + a.client = client + + return client, err } -func getAzureClient(clientID, clientSecret, TenantID string) (autorest.Authorizer, error) { - credConfig := auth.NewClientCredentialsConfig(clientID, clientSecret, TenantID) - return credConfig.Authorizer() +// ListSubscriptions list subscriptions +func (a *Azure) ListSubscriptions() (subscription []AzureSubscription, err error) { + client, err := a.getAzureClient() + if err != nil { + return nil, err + } + + subscriptionClient, err := armsubscription.NewSubscriptionsClient(client, nil) + if err != nil { + return nil, err + } + + var subscriptionList []AzureSubscription + + pager := subscriptionClient.NewListPager(nil) + + for pager.More() { + page, err := pager.NextPage(context.Background()) + if err != nil { + return nil, err + } + for _, subscription := range page.Value { + subscriptionList = append(subscriptionList, AzureSubscription{ + DisplayName: *subscription.DisplayName, + ID: *subscription.SubscriptionID, + }) + } + } + + return subscriptionList, err } // ListCluster list cluster info -func (a *Azure) ListCluster() (clusters []ClusterInfo, err error) { - authorizer, err := getAzureClient(a.ClientID, a.ClientSecret, a.TenantID) +func (a *Azure) ListCluster(subscription AzureSubscription) (clusters []ClusterInfo, err error) { + client, err := a.getAzureClient() if err != nil { return nil, err } - aksClient := containerservice.NewManagedClustersClient(a.SubscriptionID) - aksClient.Authorizer = authorizer - list, err := aksClient.List(context.Background()) + aksClient, err := armcontainerservice.NewManagedClustersClient(subscription.ID, client, nil) if err != nil { return nil, err } + var clusterList []ClusterInfo - for _, cluster := range list.Values() { - clusterList = append(clusterList, ClusterInfo{ - Name: *cluster.Name, - ID: getResourceGroupName(*cluster.ID), - RegionID: *cluster.Location, - K8sVersion: *cluster.ManagedClusterProperties.KubernetesVersion, - ConsoleURL: "https://portal.azure.com", - }) + + pager := aksClient.NewListPager(nil) + for pager.More() { + page, err := pager.NextPage(context.Background()) + if err != nil { + return nil, err + } + for _, cluster := range page.Value { + clusterList = append(clusterList, ClusterInfo{ + Name: *cluster.Name, + Account: subscription.DisplayName, + ID: *cluster.ID, + RegionID: *cluster.Location, + K8sVersion: *cluster.Properties.KubernetesVersion, + ConsoleURL: "https://portal.azure.com", + // Too long to CLI + // ConsoleURL: fmt.Sprintf("https://portal.azure.com/#resource%s/overview", *cluster.ID), + }) + } } + return clusterList, err } // GetKubeConfig get kubeConfig file func (a *Azure) GetKubeConfig(clusterName, resourceGroupName string) ([]byte, error) { - authorizer, err := getAzureClient(a.ClientID, a.ClientSecret, a.TenantID) + client, err := a.getAzureClient() + if err != nil { + return nil, err + } + + aksClient, err := armcontainerservice.NewManagedClustersClient(a.SubscriptionID, client, nil) if err != nil { return nil, err } - aksClient := containerservice.NewManagedClustersClient(a.SubscriptionID) - aksClient.Authorizer = authorizer - res, err := aksClient.ListClusterAdminCredentials(context.Background(), resourceGroupName, clusterName, "") + res, err := aksClient.ListClusterUserCredentials(context.Background(), resourceGroupName, clusterName, nil) if err != nil { return nil, err } - kubeconfig := *(res.Kubeconfigs) + + kubeconfig := res.Kubeconfigs for _, v := range kubeconfig { - return (*(v.Value)), nil + return v.Value, nil } return nil, nil } -func getResourceGroupName(id string) string { - segments := strings.Split(id, "/") - resourceGroupIndex := -1 +// GetAdminKubeConfig get kubeConfig file +func (a *Azure) GetAdminKubeConfig(clusterName, resourceGroupName string) ([]byte, error) { + client, err := a.getAzureClient() + if err != nil { + return nil, err + } - for i, segment := range segments { - if segment == "resourcegroups" { - resourceGroupIndex = i - break - } + aksClient, err := armcontainerservice.NewManagedClustersClient(a.SubscriptionID, client, nil) + if err != nil { + return nil, err } - if resourceGroupIndex != -1 && resourceGroupIndex+1 < len(segments) { - resourceGroupName := segments[resourceGroupIndex+1] - return resourceGroupName + res, err := aksClient.ListClusterAdminCredentials(context.Background(), resourceGroupName, clusterName, nil) + if err != nil { + return nil, err + } + + kubeconfig := res.Kubeconfigs + for _, v := range kubeconfig { + return v.Value, nil } - fmt.Println("can not found resourcegroups") - return "" + return nil, nil } diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 649c7302..52073d7c 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -10,6 +10,7 @@ type Cluster interface { // ClusterInfo ack cluster info type ClusterInfo struct { Name string + Account string ID string RegionID string K8sVersion string