Skip to content

Commit

Permalink
Refactor Azure support (#800)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke authored Feb 15, 2024
1 parent ebb7a0e commit 61007e8
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 139 deletions.
68 changes: 51 additions & 17 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 }}`,
Expand All @@ -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,
Expand Down
115 changes: 93 additions & 22 deletions cmd/cloud_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/mgutz/ansi"

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cloud_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 11 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 61007e8

Please sign in to comment.