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

refactor: apiserver address to viper #111

Merged
merged 8 commits into from
Dec 14, 2023
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
20 changes: 10 additions & 10 deletions docs/user_docs/cli/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/user_docs/cli/kbcli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}, "")
Expand Down
20 changes: 11 additions & 9 deletions pkg/cmd/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import (
"io"
"net/http"

viper "github.com/apecloud/kubeblocks/pkg/viperx"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"

"github.com/apecloud/kbcli/pkg/cmd/auth/authorize"
"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 {
Expand Down Expand Up @@ -143,8 +145,8 @@ func (o *LoginOptions) loginWithOrg(ctx context.Context) error {
org := &organization.OrganizationOption{
Organization: &organization.CloudOrganization{
Token: token,
APIURL: organization.APIURL,
APIPath: organization.APIPath,
APIURL: viper.GetString(types.CfgKeyOpenAPIServer),
APIPath: utils.APIPathV1,
},
}
if ok, err := org.Organization.IsValidOrganization(o.OrgName); !ok {
Expand Down Expand Up @@ -219,8 +221,8 @@ func getFirstOrg(token string) string {
org := &organization.OrganizationOption{
Organization: &organization.CloudOrganization{
Token: token,
APIURL: organization.APIURL,
APIPath: organization.APIPath,
APIURL: viper.GetString(types.CfgKeyOpenAPIServer),
APIPath: utils.APIPathV1,
},
}
organizations, err := org.Organization.GetOrganizations()
Expand All @@ -238,8 +240,8 @@ func getFirstContext(token string, orgName string) string {
c := &cloudctx.CloudContext{
OrgName: orgName,
Token: token,
APIURL: organization.APIURL,
APIPath: organization.APIPath,
APIURL: viper.GetString(types.CfgKeyOpenAPIServer),
APIPath: utils.APIPathV1,
}
contexts, err := c.GetContexts()
if err != nil {
Expand Down Expand Up @@ -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,
}, "")
Expand All @@ -298,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
}
10 changes: 5 additions & 5 deletions pkg/cmd/auth/utils/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/cmd/auth/utils/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
package utils

const (
DefaultBaseURL = "https://apecloud.authing.cn/oidc"
OpenAPIHost = "cloudapi.apecloud.cn"
APIPathV1 = "api/v1"
APIPathV2 = "api/v2"
)
5 changes: 5 additions & 0 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ 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")
viper.SetDefault(types.CfgKeyAuthURL, "https://apecloud.authing.cn/oidc")
viper.SetDefault(types.CfgKeyClientID, "64e42ca02df49bffa50719a9")
}

func registerCompletionFuncForGlobalFlags(cmd *cobra.Command, f cmdutil.Factory) {
Expand Down
34 changes: 17 additions & 17 deletions pkg/cmd/context/cloud_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -252,37 +252,37 @@ 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
}

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
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/context/cloud_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
}
})

Expand Down
Loading