From 0544709f9fb12073e7d02c2b0ac7fadb6d3cef09 Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 14 Dec 2023 17:05:31 +0800 Subject: [PATCH 1/8] refactor apiserver address to viper --- .../auth/authorize/authenticator/pkce_authenticator.go | 4 +++- pkg/cmd/auth/login.go | 10 ++++++---- pkg/cmd/auth/utils/const.go | 1 - pkg/cmd/cli.go | 3 +++ pkg/cmd/context/context.go | 4 +++- pkg/cmd/organization/organization.go | 5 +++-- pkg/types/config.go | 1 + 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/cmd/auth/authorize/authenticator/pkce_authenticator.go b/pkg/cmd/auth/authorize/authenticator/pkce_authenticator.go index a7363a0d9..95cbd549e 100644 --- a/pkg/cmd/auth/authorize/authenticator/pkce_authenticator.go +++ b/pkg/cmd/auth/authorize/authenticator/pkce_authenticator.go @@ -31,11 +31,13 @@ import ( "path" "strings" + viper "github.com/apecloud/kubeblocks/pkg/viperx" "github.com/benbjohnson/clock" "github.com/hashicorp/go-cleanhttp" "github.com/pkg/errors" "github.com/apecloud/kbcli/pkg/cmd/auth/utils" + "github.com/apecloud/kbcli/pkg/types" ) type OIDCWellKnownEndpoints struct { @@ -195,7 +197,7 @@ func (p *PKCEAuthenticator) GetToken(ctx context.Context, authorization interfac } func (p *PKCEAuthenticator) GetUserInfo(ctx context.Context, token string) (*UserInfoResponse, error) { - URL := fmt.Sprintf("https://%s/api/v1/user", utils.OpenAPIHost) + URL := fmt.Sprintf("%s/api/v1/user", viper.GetString(types.CfgKeyOpenAPIServer)) req, err := utils.NewFullRequest(ctx, URL, http.MethodGet, map[string]string{ "Authorization": "Bearer " + token, }, "") diff --git a/pkg/cmd/auth/login.go b/pkg/cmd/auth/login.go index 09d840b52..990b307cd 100644 --- a/pkg/cmd/auth/login.go +++ b/pkg/cmd/auth/login.go @@ -26,6 +26,7 @@ import ( "io" "net/http" + viper "github.com/apecloud/kubeblocks/pkg/viperx" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericiooptions" @@ -33,6 +34,7 @@ import ( "github.com/apecloud/kbcli/pkg/cmd/auth/utils" cloudctx "github.com/apecloud/kbcli/pkg/cmd/context" "github.com/apecloud/kbcli/pkg/cmd/organization" + "github.com/apecloud/kbcli/pkg/types" ) type LoginOptions struct { @@ -143,7 +145,7 @@ func (o *LoginOptions) loginWithOrg(ctx context.Context) error { org := &organization.OrganizationOption{ Organization: &organization.CloudOrganization{ Token: token, - APIURL: organization.APIURL, + APIURL: viper.GetString(types.CfgKeyOpenAPIServer), APIPath: organization.APIPath, }, } @@ -219,7 +221,7 @@ func getFirstOrg(token string) string { org := &organization.OrganizationOption{ Organization: &organization.CloudOrganization{ Token: token, - APIURL: organization.APIURL, + APIURL: viper.GetString(types.CfgKeyOpenAPIServer), APIPath: organization.APIPath, }, } @@ -238,7 +240,7 @@ func getFirstContext(token string, orgName string) string { c := &cloudctx.CloudContext{ OrgName: orgName, Token: token, - APIURL: organization.APIURL, + APIURL: viper.GetString(types.CfgKeyOpenAPIServer), APIPath: organization.APIPath, } contexts, err := c.GetContexts() @@ -271,7 +273,7 @@ func IsLoggedIn() bool { // CheckTokenAvailable Check whether the token is available by getting user info. func checkTokenAvailable(token string) bool { - URL := fmt.Sprintf("https://%s/api/v1/user", utils.OpenAPIHost) + URL := fmt.Sprintf("%s/api/v1/user", viper.GetString(types.CfgKeyOpenAPIServer)) req, err := utils.NewFullRequest(context.TODO(), URL, http.MethodGet, map[string]string{ "Authorization": "Bearer " + token, }, "") diff --git a/pkg/cmd/auth/utils/const.go b/pkg/cmd/auth/utils/const.go index d8ba17585..fd9f8fde2 100644 --- a/pkg/cmd/auth/utils/const.go +++ b/pkg/cmd/auth/utils/const.go @@ -21,5 +21,4 @@ package utils const ( DefaultBaseURL = "https://apecloud.authing.cn/oidc" - OpenAPIHost = "cloudapi.apecloud.cn" ) diff --git a/pkg/cmd/cli.go b/pkg/cmd/cli.go index 93f3b2d2c..794a25288 100644 --- a/pkg/cmd/cli.go +++ b/pkg/cmd/cli.go @@ -255,6 +255,9 @@ func initConfig() { if err := viper.ReadInConfig(); err == nil { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } + + //cloud + viper.SetDefault(types.CfgKeyOpenAPIServer, "https://cloudapi.apecloud.cn") } func registerCompletionFuncForGlobalFlags(cmd *cobra.Command, f cmdutil.Factory) { diff --git a/pkg/cmd/context/context.go b/pkg/cmd/context/context.go index 14e907577..6f60f76c0 100644 --- a/pkg/cmd/context/context.go +++ b/pkg/cmd/context/context.go @@ -20,6 +20,7 @@ along with this program. If not, see . package context import ( + viper "github.com/apecloud/kubeblocks/pkg/viperx" "github.com/pkg/errors" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericiooptions" @@ -27,6 +28,7 @@ import ( "k8s.io/kubectl/pkg/util/templates" "github.com/apecloud/kbcli/pkg/cmd/organization" + "github.com/apecloud/kbcli/pkg/types" ) var contextExample = templates.Examples(` @@ -171,7 +173,7 @@ func (o *ContextOptions) complete(args []string) error { Token: token, OrgName: currentOrgAndContext.CurrentOrganization, IOStreams: o.IOStreams, - APIURL: organization.APIURL, + APIURL: viper.GetString(types.CfgKeyOpenAPIServer), APIPath: organization.APIPath, OutputFormat: o.OutputFormat, } diff --git a/pkg/cmd/organization/organization.go b/pkg/cmd/organization/organization.go index a4a759546..8d86d1bf0 100644 --- a/pkg/cmd/organization/organization.go +++ b/pkg/cmd/organization/organization.go @@ -24,6 +24,7 @@ import ( "fmt" "strings" + viper "github.com/apecloud/kubeblocks/pkg/viperx" "github.com/jedib0t/go-pretty/v6/table" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -33,6 +34,7 @@ import ( "k8s.io/kubectl/pkg/util/templates" "github.com/apecloud/kbcli/pkg/printer" + "github.com/apecloud/kbcli/pkg/types" ) var organizationExample = templates.Examples(` @@ -47,7 +49,6 @@ var organizationExample = templates.Examples(` `) const ( - APIURL = "https://cloudapi.apecloud.cn" APIPath = "api/v1" ) @@ -198,7 +199,7 @@ func (o *OrganizationOption) complete(args []string) error { if o.Organization == nil { o.Organization = &CloudOrganization{ Token: token, - APIURL: APIURL, + APIURL: viper.GetString(types.CfgKeyOpenAPIServer), APIPath: APIPath, } } diff --git a/pkg/types/config.go b/pkg/types/config.go index 4686c1e4b..d1f342f7a 100644 --- a/pkg/types/config.go +++ b/pkg/types/config.go @@ -27,4 +27,5 @@ const ( CfgKeyClusterDefaultMemory = "CLUSTER_DEFAULT_MEMORY" CfgKeyHelmRepoURL = "HELM_REPO_URL" CfgKeyImageRegistry = "IMAGE_REGISTRY" + CfgKeyOpenAPIServer = "OPENAPI_SERVER" ) From 639ea919373e1291d101e684984f3a992c28f519 Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 14 Dec 2023 17:11:24 +0800 Subject: [PATCH 2/8] mv api path to const --- pkg/cmd/auth/login.go | 6 +++--- pkg/cmd/auth/utils/const.go | 2 ++ pkg/cmd/context/cloud_context_test.go | 3 ++- pkg/cmd/context/context.go | 3 ++- pkg/cmd/organization/cloud_organization_test.go | 4 +++- pkg/cmd/organization/organization.go | 7 ++----- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/auth/login.go b/pkg/cmd/auth/login.go index 990b307cd..8b5b2dd12 100644 --- a/pkg/cmd/auth/login.go +++ b/pkg/cmd/auth/login.go @@ -146,7 +146,7 @@ func (o *LoginOptions) loginWithOrg(ctx context.Context) error { Organization: &organization.CloudOrganization{ Token: token, APIURL: viper.GetString(types.CfgKeyOpenAPIServer), - APIPath: organization.APIPath, + APIPath: utils.APIPathV1, }, } if ok, err := org.Organization.IsValidOrganization(o.OrgName); !ok { @@ -222,7 +222,7 @@ func getFirstOrg(token string) string { Organization: &organization.CloudOrganization{ Token: token, APIURL: viper.GetString(types.CfgKeyOpenAPIServer), - APIPath: organization.APIPath, + APIPath: utils.APIPathV1, }, } organizations, err := org.Organization.GetOrganizations() @@ -241,7 +241,7 @@ func getFirstContext(token string, orgName string) string { OrgName: orgName, Token: token, APIURL: viper.GetString(types.CfgKeyOpenAPIServer), - APIPath: organization.APIPath, + APIPath: utils.APIPathV1, } contexts, err := c.GetContexts() if err != nil { diff --git a/pkg/cmd/auth/utils/const.go b/pkg/cmd/auth/utils/const.go index fd9f8fde2..1357e84e6 100644 --- a/pkg/cmd/auth/utils/const.go +++ b/pkg/cmd/auth/utils/const.go @@ -21,4 +21,6 @@ package utils const ( DefaultBaseURL = "https://apecloud.authing.cn/oidc" + APIPathV1 = "api/v1" + APIPathV2 = "api/v2" ) diff --git a/pkg/cmd/context/cloud_context_test.go b/pkg/cmd/context/cloud_context_test.go index a9c6c44d0..288daabed 100644 --- a/pkg/cmd/context/cloud_context_test.go +++ b/pkg/cmd/context/cloud_context_test.go @@ -27,6 +27,7 @@ import ( "net/http" "net/http/httptest" + "github.com/apecloud/kbcli/pkg/cmd/auth/utils" "github.com/apecloud/kbcli/pkg/cmd/organization" ) @@ -84,7 +85,7 @@ var _ = ginkgo_context.Describe("Test Cloud Context", func() { ContextName: "test_context", OrgName: "test_org", APIURL: server.URL, - APIPath: organization.APIPath, + APIPath: utils.APIPathV1, } }) diff --git a/pkg/cmd/context/context.go b/pkg/cmd/context/context.go index 6f60f76c0..cb5d99538 100644 --- a/pkg/cmd/context/context.go +++ b/pkg/cmd/context/context.go @@ -27,6 +27,7 @@ import ( cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" + "github.com/apecloud/kbcli/pkg/cmd/auth/utils" "github.com/apecloud/kbcli/pkg/cmd/organization" "github.com/apecloud/kbcli/pkg/types" ) @@ -174,7 +175,7 @@ func (o *ContextOptions) complete(args []string) error { OrgName: currentOrgAndContext.CurrentOrganization, IOStreams: o.IOStreams, APIURL: viper.GetString(types.CfgKeyOpenAPIServer), - APIPath: organization.APIPath, + APIPath: utils.APIPathV1, OutputFormat: o.OutputFormat, } } diff --git a/pkg/cmd/organization/cloud_organization_test.go b/pkg/cmd/organization/cloud_organization_test.go index 08286d215..757fc51b4 100644 --- a/pkg/cmd/organization/cloud_organization_test.go +++ b/pkg/cmd/organization/cloud_organization_test.go @@ -27,6 +27,8 @@ import ( "net/http" "net/http/httptest" "os" + + "github.com/apecloud/kbcli/pkg/cmd/auth/utils" ) func mockServer() *httptest.Server { @@ -92,7 +94,7 @@ var _ = Describe("Test Cloud Organization", func() { ) BeforeEach(func() { server := mockServer() - o = &CloudOrganization{Token: "test_token", APIURL: server.URL, APIPath: APIPath} + o = &CloudOrganization{Token: "test_token", APIURL: server.URL, APIPath: utils.APIPathV1} os.Setenv("TEST_ENV", "true") }) diff --git a/pkg/cmd/organization/organization.go b/pkg/cmd/organization/organization.go index 8d86d1bf0..8e39be5df 100644 --- a/pkg/cmd/organization/organization.go +++ b/pkg/cmd/organization/organization.go @@ -33,6 +33,7 @@ import ( cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" + "github.com/apecloud/kbcli/pkg/cmd/auth/utils" "github.com/apecloud/kbcli/pkg/printer" "github.com/apecloud/kbcli/pkg/types" ) @@ -48,10 +49,6 @@ var organizationExample = templates.Examples(` kbcli org switch org2 `) -const ( - APIPath = "api/v1" -) - type Organizations struct { Items []OrgItem `json:"items"` } @@ -200,7 +197,7 @@ func (o *OrganizationOption) complete(args []string) error { o.Organization = &CloudOrganization{ Token: token, APIURL: viper.GetString(types.CfgKeyOpenAPIServer), - APIPath: APIPath, + APIPath: utils.APIPathV1, } } From 88bcef4b27b6f71687187c3049a7da710c40fa9c Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 14 Dec 2023 17:19:38 +0800 Subject: [PATCH 3/8] refactor auth url to viper --- pkg/cmd/auth/login.go | 4 ++-- pkg/cmd/auth/utils/const.go | 5 ++--- pkg/cmd/cli.go | 1 + pkg/types/config.go | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/auth/login.go b/pkg/cmd/auth/login.go index 8b5b2dd12..f4ace994e 100644 --- a/pkg/cmd/auth/login.go +++ b/pkg/cmd/auth/login.go @@ -300,9 +300,9 @@ func getAuthURL(region string) string { var authURL string switch region { case "jp": - authURL = utils.DefaultBaseURL + authURL = viper.GetString(types.CfgKeyAuthURL) default: - authURL = utils.DefaultBaseURL + authURL = viper.GetString(types.CfgKeyAuthURL) } return authURL } diff --git a/pkg/cmd/auth/utils/const.go b/pkg/cmd/auth/utils/const.go index 1357e84e6..b336e58f7 100644 --- a/pkg/cmd/auth/utils/const.go +++ b/pkg/cmd/auth/utils/const.go @@ -20,7 +20,6 @@ package utils const ( - DefaultBaseURL = "https://apecloud.authing.cn/oidc" - APIPathV1 = "api/v1" - APIPathV2 = "api/v2" + APIPathV1 = "api/v1" + APIPathV2 = "api/v2" ) diff --git a/pkg/cmd/cli.go b/pkg/cmd/cli.go index 794a25288..fba69c707 100644 --- a/pkg/cmd/cli.go +++ b/pkg/cmd/cli.go @@ -258,6 +258,7 @@ func initConfig() { //cloud viper.SetDefault(types.CfgKeyOpenAPIServer, "https://cloudapi.apecloud.cn") + viper.SetDefault(types.CfgKeyAuthURL, "https://apecloud.authing.cn/oidc") } func registerCompletionFuncForGlobalFlags(cmd *cobra.Command, f cmdutil.Factory) { diff --git a/pkg/types/config.go b/pkg/types/config.go index d1f342f7a..5bc0fbd7e 100644 --- a/pkg/types/config.go +++ b/pkg/types/config.go @@ -28,4 +28,5 @@ const ( CfgKeyHelmRepoURL = "HELM_REPO_URL" CfgKeyImageRegistry = "IMAGE_REGISTRY" CfgKeyOpenAPIServer = "OPENAPI_SERVER" + CfgKeyAuthURL = "AUTH_URL" ) From 2c6999df4e731be65eb9325cd48390e29bf4c229 Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 14 Dec 2023 17:37:16 +0800 Subject: [PATCH 4/8] refactor client id to viper --- pkg/cmd/auth/utils/bindata.go | 10 +++++----- pkg/cmd/cli.go | 1 + pkg/types/config.go | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/auth/utils/bindata.go b/pkg/cmd/auth/utils/bindata.go index 15522e7ad..16be6880c 100644 --- a/pkg/cmd/auth/utils/bindata.go +++ b/pkg/cmd/auth/utils/bindata.go @@ -29,6 +29,10 @@ import ( "path/filepath" "strings" "time" + + viper "github.com/apecloud/kubeblocks/pkg/viperx" + + "github.com/apecloud/kbcli/pkg/types" ) type asset struct { @@ -73,13 +77,9 @@ func (fi bindataFileInfo) Sys() interface{} { return nil } -const clientID = "64e42ca02df49bffa50719a9" - -var clientIDJSON = fmt.Sprintf(`{"client_id": "%s"}`, clientID) - func configConfigEnc() (*asset, error) { info := bindataFileInfo{name: "config/config.enc", size: 53, mode: os.FileMode(0420), modTime: time.Unix(1688544460, 0)} - a := &asset{bytes: []byte(clientIDJSON), info: info} + a := &asset{bytes: []byte(fmt.Sprintf(`{"client_id": "%s"}`, viper.GetString(types.CfgKeyClientID))), info: info} return a, nil } diff --git a/pkg/cmd/cli.go b/pkg/cmd/cli.go index fba69c707..097293781 100644 --- a/pkg/cmd/cli.go +++ b/pkg/cmd/cli.go @@ -259,6 +259,7 @@ func initConfig() { //cloud viper.SetDefault(types.CfgKeyOpenAPIServer, "https://cloudapi.apecloud.cn") viper.SetDefault(types.CfgKeyAuthURL, "https://apecloud.authing.cn/oidc") + viper.SetDefault(types.CfgKeyClientID, "64e42ca02df49bffa50719a9") } func registerCompletionFuncForGlobalFlags(cmd *cobra.Command, f cmdutil.Factory) { diff --git a/pkg/types/config.go b/pkg/types/config.go index 5bc0fbd7e..902d1832e 100644 --- a/pkg/types/config.go +++ b/pkg/types/config.go @@ -29,4 +29,5 @@ const ( CfgKeyImageRegistry = "IMAGE_REGISTRY" CfgKeyOpenAPIServer = "OPENAPI_SERVER" CfgKeyAuthURL = "AUTH_URL" + CfgKeyClientID = "CLIENT_ID" ) From 39d7c06903b9d5ed130064da098325f0eb074e40 Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 14 Dec 2023 17:52:12 +0800 Subject: [PATCH 5/8] rename context to env --- pkg/cmd/context/context.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/pkg/cmd/context/context.go b/pkg/cmd/context/context.go index cb5d99538..02920cb6d 100644 --- a/pkg/cmd/context/context.go +++ b/pkg/cmd/context/context.go @@ -33,14 +33,14 @@ import ( ) var contextExample = templates.Examples(` - // Get the context name currently used by the user. - kbcli context current - // List all contexts created by the current user. - kbcli context list - // Get the description information of context context1. - kbcli context describe context1 - // Switch to context context2. - kbcli context use context2 + // Get the environment name currently used by the user. + kbcli environment current + // List all environments created by the current user. + kbcli environment list + // Get the description information of environment environment1. + kbcli environment describe environment1 + // Switch to environment environment2. + kbcli environment use environment2 `) const ( @@ -65,9 +65,10 @@ type ContextOptions struct { func NewContextCmd(streams genericiooptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ - Use: "context", - Short: "kbcli context allows you to manage cloud context. This command is currently only applicable to cloud," + - " and currently does not support switching the context of the local k8s cluster.", + Use: "environment", + Aliases: []string{"env"}, + Short: "kbcli environment allows you to manage cloud environment. This command is currently only applicable to cloud," + + " and currently does not support switching the environment of the local k8s cluster.", Example: contextExample, } cmd.AddCommand( @@ -84,7 +85,7 @@ func newContextListCmd(streams genericiooptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "list", - Short: "List all created contexts.", + Short: "List all created environments.", Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.complete(args)) cmdutil.CheckErr(o.validate(cmd)) @@ -99,7 +100,7 @@ func newContextCurrentCmd(streams genericiooptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "current", - Short: "Get the currently used context.", + Short: "Get the currently used environment.", Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.complete(args)) cmdutil.CheckErr(o.validate(cmd)) @@ -114,7 +115,7 @@ func newContextDescribeCmd(streams genericiooptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "describe", - Short: "Get the description information of a context.", + Short: "Get the description information of a environment.", Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.complete(args)) cmdutil.CheckErr(o.validate(cmd)) @@ -132,7 +133,7 @@ func newContextUseCmd(streams genericiooptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "use", - Short: "Use another context that you have already created.", + Short: "Use another environment that you have already created.", Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.complete(args)) cmdutil.CheckErr(o.validate(cmd)) @@ -146,7 +147,7 @@ func newContextUseCmd(streams genericiooptions.IOStreams) *cobra.Command { func (o *ContextOptions) validate(cmd *cobra.Command) error { if cmd.Name() == "describe" || cmd.Name() == "use" { if o.ContextName == "" { - return errors.New("context name is required") + return errors.New("environment name is required") } } From c6043097804004718a9684903c232c65b225c4f4 Mon Sep 17 00:00:00 2001 From: lynnleelhl Date: Thu, 14 Dec 2023 09:55:15 +0000 Subject: [PATCH 6/8] chore: auto update cli doc changes --- docs/user_docs/cli/cli.md | 20 ++++++++++---------- docs/user_docs/cli/kbcli.md | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/user_docs/cli/cli.md b/docs/user_docs/cli/cli.md index eac91da81..5fc98deae 100644 --- a/docs/user_docs/cli/cli.md +++ b/docs/user_docs/cli/cli.md @@ -143,16 +143,6 @@ ClusterVersion command. * [kbcli clusterversion unset-default](kbcli_clusterversion_unset-default.md) - Unset the clusterversion if it's default. -## [context](kbcli_context.md) - -kbcli context allows you to manage cloud context. This command is currently only applicable to cloud, and currently does not support switching the context of the local k8s cluster. - -* [kbcli context current](kbcli_context_current.md) - Get the currently used context. -* [kbcli context describe](kbcli_context_describe.md) - Get the description information of a context. -* [kbcli context list](kbcli_context_list.md) - List all created contexts. -* [kbcli context use](kbcli_context_use.md) - Use another context that you have already created. - - ## [dashboard](kbcli_dashboard.md) List and open the KubeBlocks dashboards. @@ -174,6 +164,16 @@ Data protection command. * [kbcli dataprotection restore](kbcli_dataprotection_restore.md) - Restore a new cluster from backup +## [environment](kbcli_environment.md) + +kbcli environment allows you to manage cloud environment. This command is currently only applicable to cloud, and currently does not support switching the environment of the local k8s cluster. + +* [kbcli environment current](kbcli_environment_current.md) - Get the currently used environment. +* [kbcli environment describe](kbcli_environment_describe.md) - Get the description information of a environment. +* [kbcli environment list](kbcli_environment_list.md) - List all created environments. +* [kbcli environment use](kbcli_environment_use.md) - Use another environment that you have already created. + + ## [fault](kbcli_fault.md) Inject faults to pod. diff --git a/docs/user_docs/cli/kbcli.md b/docs/user_docs/cli/kbcli.md index 98d199fa5..dbdc3af31 100644 --- a/docs/user_docs/cli/kbcli.md +++ b/docs/user_docs/cli/kbcli.md @@ -63,9 +63,9 @@ kbcli [flags] * [kbcli cluster](kbcli_cluster.md) - Cluster command. * [kbcli clusterdefinition](kbcli_clusterdefinition.md) - ClusterDefinition command. * [kbcli clusterversion](kbcli_clusterversion.md) - ClusterVersion command. -* [kbcli context](kbcli_context.md) - kbcli context allows you to manage cloud context. This command is currently only applicable to cloud, and currently does not support switching the context of the local k8s cluster. * [kbcli dashboard](kbcli_dashboard.md) - List and open the KubeBlocks dashboards. * [kbcli dataprotection](kbcli_dataprotection.md) - Data protection command. +* [kbcli environment](kbcli_environment.md) - kbcli environment allows you to manage cloud environment. This command is currently only applicable to cloud, and currently does not support switching the environment of the local k8s cluster. * [kbcli fault](kbcli_fault.md) - Inject faults to pod. * [kbcli infra](kbcli_infra.md) - infra command * [kbcli kubeblocks](kbcli_kubeblocks.md) - KubeBlocks operation commands. From e23eb68080040fd087f76f1112f73d3f85fa7d4e Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 14 Dec 2023 18:02:59 +0800 Subject: [PATCH 7/8] context to environment in error log --- pkg/cmd/context/cloud_context.go | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/cmd/context/cloud_context.go b/pkg/cmd/context/cloud_context.go index 19962bd88..68ea616fa 100644 --- a/pkg/cmd/context/cloud_context.go +++ b/pkg/cmd/context/cloud_context.go @@ -138,7 +138,7 @@ type CloudContextResponse struct { func (c *CloudContext) showContext() error { cloudContext, err := c.GetContext() if err != nil { - return errors.Wrapf(err, "Failed to get context %s", c.ContextName) + return errors.Wrapf(err, "Failed to get environment %s", c.ContextName) } switch strings.ToLower(c.OutputFormat) { @@ -213,7 +213,7 @@ func (c *CloudContext) printTable(ctxRes *CloudContextResponse) error { func (c *CloudContext) showContexts() error { cloudContexts, err := c.GetContexts() if err != nil { - return errors.Wrapf(err, "Failed to get contexts, please check your organization name") + return errors.Wrapf(err, "Failed to get environments, please check your organization name") } tbl := printer.NewTablePrinter(c.Out) @@ -252,7 +252,7 @@ func (c *CloudContext) showContexts() error { tbl.Print() if ok := writeContexts(cloudContexts); ok != nil { - return errors.Wrapf(err, "Failed to write contexts.") + return errors.Wrapf(err, "Failed to write environments.") } return nil } @@ -260,29 +260,29 @@ func (c *CloudContext) showContexts() error { func (c *CloudContext) showCurrentContext() error { currentContext, err := c.getCurrentContext() if err != nil { - return errors.Wrapf(err, "Failed to get current context.") + return errors.Wrapf(err, "Failed to get current environment.") } - fmt.Fprintf(c.Out, "Current context: %s\n", currentContext) + fmt.Fprintf(c.Out, "Current environment: %s\n", currentContext) return nil } func (c *CloudContext) showUseContext() error { oldContextName, err := c.useContext(c.ContextName) if err != nil { - return errors.Wrapf(err, "Failed to switch context to %s.", c.ContextName) + return errors.Wrapf(err, "Failed to switch environment to %s.", c.ContextName) } - fmt.Fprintf(c.Out, "Successfully switched from %s to context %s.\n", oldContextName, c.ContextName) + fmt.Fprintf(c.Out, "Successfully switched from %s to environment %s.\n", oldContextName, c.ContextName) return nil } func (c *CloudContext) showRemoveContext() error { if err := c.removeContext(); err != nil { - return errors.Wrapf(err, "Failed to remove context %s.", c.ContextName) + return errors.Wrapf(err, "Failed to remove environment %s.", c.ContextName) } - fmt.Fprintf(c.Out, "Context %s removed.\n", c.ContextName) + fmt.Fprintf(c.Out, "Environment %s removed.\n", c.ContextName) return nil } @@ -296,7 +296,7 @@ func (c *CloudContext) GetContext() (*CloudContextResponse, error) { var context CloudContextResponse err = json.Unmarshal(response, &context) if err != nil { - return nil, errors.Wrapf(err, "Failed to unmarshal context %s.", c.ContextName) + return nil, errors.Wrapf(err, "Failed to unmarshal environment %s.", c.ContextName) } return &context, nil @@ -312,7 +312,7 @@ func (c *CloudContext) GetContexts() ([]CloudContextResponse, error) { var contexts []CloudContextResponse err = json.Unmarshal(response, &contexts) if err != nil { - return nil, errors.Wrap(err, "Failed to unmarshal contexts.") + return nil, errors.Wrap(err, "Failed to unmarshal environments.") } return contexts, nil @@ -321,7 +321,7 @@ func (c *CloudContext) GetContexts() ([]CloudContextResponse, error) { func (c *CloudContext) getCurrentContext() (string, error) { currentOrgAndContext, err := organization.GetCurrentOrgAndContext() if err != nil { - return "", errors.Wrap(err, "Failed to get current context.") + return "", errors.Wrap(err, "Failed to get current environment.") } if ok, err := c.isValidContext(currentOrgAndContext.CurrentContext); !ok { @@ -338,13 +338,13 @@ func (c *CloudContext) useContext(contextName string) (string, error) { currentOrgAndContext, err := organization.GetCurrentOrgAndContext() if err != nil { - return "", errors.Wrap(err, "Failed to get current context.") + return "", errors.Wrap(err, "Failed to get current environment.") } oldContextName := currentOrgAndContext.CurrentContext currentOrgAndContext.CurrentContext = contextName if err = organization.SetCurrentOrgAndContext(currentOrgAndContext); err != nil { - return "", errors.Wrapf(err, "Failed to switch context to %s.", contextName) + return "", errors.Wrapf(err, "Failed to switch environment to %s.", contextName) } return oldContextName, nil @@ -364,11 +364,11 @@ func (c *CloudContext) removeContext() error { func (c *CloudContext) isValidContext(contextName string) (bool, error) { cloudContexts, err := c.GetContexts() if err != nil { - return false, errors.Wrap(err, "Failed to get contexts.") + return false, errors.Wrap(err, "Failed to get environments.") } if len(cloudContexts) == 0 { - return false, errors.Wrap(err, "No context found, please create a context on cloud.") + return false, errors.Wrap(err, "No environment found, please create a environment on cloud.") } for _, item := range cloudContexts { @@ -377,7 +377,7 @@ func (c *CloudContext) isValidContext(contextName string) (bool, error) { } } - return false, errors.Errorf("Context %s does not exist.", contextName) + return false, errors.Errorf("Environment %s does not exist.", contextName) } func writeContexts(contexts []CloudContextResponse) error { From 86c5f4a04bfe5f841d231349b57ba8049a2ea598 Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 14 Dec 2023 18:17:51 +0800 Subject: [PATCH 8/8] pass lint --- pkg/cmd/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/cli.go b/pkg/cmd/cli.go index 097293781..7657630e3 100644 --- a/pkg/cmd/cli.go +++ b/pkg/cmd/cli.go @@ -256,7 +256,7 @@ func initConfig() { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } - //cloud + // cloud viper.SetDefault(types.CfgKeyOpenAPIServer, "https://cloudapi.apecloud.cn") viper.SetDefault(types.CfgKeyAuthURL, "https://apecloud.authing.cn/oidc") viper.SetDefault(types.CfgKeyClientID, "64e42ca02df49bffa50719a9")