Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provider Rancher #331

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ var Clouds = []CloudInfo{
HomePage: "https://console.cloud.tencent.com/tke",
Service: "TKE",
},
{
Name: "Rancher",
Alias: []string{"rancher"},
HomePage: "https://rancher.com",
Service: "Rancher",
},
}

// Init AddCommand
Expand Down Expand Up @@ -174,6 +180,48 @@ func (cc *CloudCommand) runCloud(cmd *cobra.Command, args []string) error {
return err
}
}
case 2:
fmt.Println("⛅ Selected: Rancher")
serverURL, apiKey := checkEnvForSecret(2)
rancher := cloud.Rancher{
ServerURL: serverURL,
APIKey: apiKey,
}
if clusterID == "" {
clusters, err := rancher.ListCluster()
if err != nil {
return err
}
if len(clusters) == 0 {
return errors.New("no clusters found")
}
clusterNum := selectCluster(clusters, "Select Cluster")
kubeconfig, err := rancher.GetKubeConfig(clusters[clusterNum].ID)
if err != nil {
return err
}
newConfig, err := clientcmd.Load([]byte(kubeconfig))
if err != nil {
return err
}
err = AddToLocal(newConfig, clusters[clusterNum].Name, cover)
if err != nil {
return err
}
} else {
kubeconfig, err := rancher.GetKubeConfig(clusterID)
if err != nil {
return err
}
newConfig, err := clientcmd.Load([]byte(kubeconfig))
if err != nil {
return err
}
err = AddToLocal(newConfig, fmt.Sprintf("rancher-%s", clusterID), cover)
if err != nil {
return err
}
}
}
return nil
}
Expand All @@ -196,6 +244,14 @@ func checkEnvForSecret(num int) (string, string) {
secretKey = PromptUI("TencentCloud API secretKey", "")
}
return secretID, secretKey
case 2:
serverURL, su := os.LookupEnv("RANCHER_SERVER_URL")
apiKey, key := os.LookupEnv("RANCHER_API_KEY")
if !su || !key {
serverURL = PromptUI("Rancher API serverURL", "")
apiKey = PromptUI("Rancher API key", "")
}
return serverURL, apiKey
}
return "", ""
}
Expand Down Expand Up @@ -302,6 +358,9 @@ export ACCESS_KEY_SECRET=xxx
# Set env Tencent secret key
export TENCENTCLOUD_SECRET_ID=xxx
export TENCENTCLOUD_SECRET_KEY=xxx
# Set env Rancher secret key
export RANCHER_SERVER_URL=https://xxx
export RANCHER_API_KEY=xxx
# Interaction: select kubeconfig from the cloud
kubecm add cloud
# Add kubeconfig from cloud
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ require (
github.com/imdario/mergo v0.3.12
github.com/manifoldco/promptui v0.3.2
github.com/pterm/pterm v0.12.41
github.com/rancher/norman v0.0.0-20200820172041-261460ee9088
github.com/rancher/rancher/pkg/client v0.0.0-20211110212758-cc2b8beb1473
github.com/spf13/cobra v1.4.0
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.370
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.370
Expand Down Expand Up @@ -40,6 +42,7 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -48,17 +51,22 @@ require (
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gookit/color v1.5.0 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
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/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rancher/wrangler v0.6.2-0.20200820173016-2068de651106 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tjfoc/gmsm v1.3.2 // indirect
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 // indirect
Expand Down
Loading