Skip to content

Commit

Permalink
chore: update lorry k8s client (apecloud#5761)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun authored Nov 6, 2023
1 parent 54364b0 commit eeb8579
Show file tree
Hide file tree
Showing 27 changed files with 758 additions and 558 deletions.
3 changes: 1 addition & 2 deletions cmd/lorry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"github.com/apecloud/kubeblocks/pkg/lorry/highavailability"
"github.com/apecloud/kubeblocks/pkg/lorry/httpserver"
opsregister "github.com/apecloud/kubeblocks/pkg/lorry/operations/register"
"github.com/apecloud/kubeblocks/pkg/lorry/util"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)

Expand Down Expand Up @@ -93,7 +92,7 @@ func main() {
characterType = viper.GetString(constant.KBEnvBuiltinHandler)
}
workloadType := viper.GetString(constant.KBEnvWorkloadType)
if util.IsHAAvailable(characterType, workloadType) {
if highavailability.IsHAAvailable(characterType, workloadType) {
ha := highavailability.NewHa()
if ha != nil {
defer ha.ShutdownWithWait()
Expand Down
106 changes: 7 additions & 99 deletions pkg/cli/cmd/accounts/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package accounts

import (
"context"
"encoding/json"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -34,8 +33,6 @@ import (
clusterutil "github.com/apecloud/kubeblocks/pkg/cli/cluster"
"github.com/apecloud/kubeblocks/pkg/cli/exec"
"github.com/apecloud/kubeblocks/pkg/cli/printer"
"github.com/apecloud/kubeblocks/pkg/cli/util"
"github.com/apecloud/kubeblocks/pkg/lorry/client"
lorryutil "github.com/apecloud/kubeblocks/pkg/lorry/util"
)

Expand All @@ -56,15 +53,13 @@ var (
errMissingUserName = fmt.Errorf("please specify username")
errMissingRoleName = fmt.Errorf("please specify at least ONE role name")
errInvalidRoleName = fmt.Errorf("invalid role name, should be one of [SUPERUSER, READWRITE, READONLY] ")
errInvalidOp = fmt.Errorf("invalid operation")
errCompNameOrInstName = fmt.Errorf("please specify either --component or --instance, they are exclusive")
errClusterNameorInstName = fmt.Errorf("specify either cluster name or --instance")
)

func NewAccountBaseOptions(f cmdutil.Factory, streams genericiooptions.IOStreams, op lorryutil.OperationKind) *AccountBaseOptions {
func NewAccountBaseOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *AccountBaseOptions {
return &AccountBaseOptions{
ExecOptions: exec.NewExecOptions(f, streams),
AccountOp: op,
}
}

Expand Down Expand Up @@ -147,60 +142,6 @@ func (o *AccountBaseOptions) Complete(f cmdutil.Factory) error {
return nil
}

func (o *AccountBaseOptions) Run(cmd *cobra.Command, f cmdutil.Factory, streams genericiooptions.IOStreams) error {
var err error
response, err := o.Do()
if err != nil {
if lorryutil.IsUnSupportedError(err) {
return fmt.Errorf("command `%s` on characterType `%s` (defined in cluster: %s, component: %s) is not supported yet", cmd.Use, o.CharType, o.ClusterName, o.ComponentName)
}
return err
}

switch o.AccountOp {
case
lorryutil.DeleteUserOp,
lorryutil.RevokeUserRoleOp,
lorryutil.GrantUserRoleOp:
o.printGeneralInfo(response)
err = nil
case lorryutil.CreateUserOp:
o.printGeneralInfo(response)
if response.Event == lorryutil.RespEveSucc {
printer.Alert(o.Out, "Please do REMEMBER the password for the new user! Once forgotten, it cannot be retrieved!\n")
}
err = nil
case lorryutil.DescribeUserOp:
err = o.printRoleInfo(response)
case lorryutil.ListUsersOp:
err = o.printUserInfo(response)
default:
err = errInvalidOp
}
if err != nil {
return err
}

if o.Verbose {
fmt.Fprintln(o.Out, "")
o.printMeta(response)
}
return err
}

func (o *AccountBaseOptions) Do() (lorryutil.SQLChannelResponse, error) {
klog.V(1).Info(fmt.Sprintf("connect to cluster %s, component %s, instance %s\n", o.ClusterName, o.ComponentName, o.PodName))
response := lorryutil.SQLChannelResponse{}
sqlClient, err := client.NewHTTPClientWithChannelPod(o.Pod, o.CharType)
if err != nil {
return response, err
}

request := lorryutil.SQLChannelRequest{Operation: (string)(o.AccountOp), Metadata: o.RequestMeta}
response, err = sqlClient.SendRequest(o.ExecOptions, request)
return response, err
}

func (o *AccountBaseOptions) newTblPrinterWithStyle(title string, header []interface{}) *printer.TablePrinter {
tblPrinter := printer.NewTablePrinter(o.Out)
tblPrinter.SetStyle(printer.TerminalStyle)
Expand All @@ -209,59 +150,26 @@ func (o *AccountBaseOptions) newTblPrinterWithStyle(title string, header []inter
return tblPrinter
}

func (o *AccountBaseOptions) printGeneralInfo(response lorryutil.SQLChannelResponse) {
func (o *AccountBaseOptions) printGeneralInfo(event, message string) {
tblPrinter := o.newTblPrinterWithStyle("QUERY RESULT", []interface{}{"RESULT", "MESSAGE"})
tblPrinter.AddRow(response.Event, response.Message)
tblPrinter.Print()
}

func (o *AccountBaseOptions) printMeta(response lorryutil.SQLChannelResponse) {
meta := response.Metadata
tblPrinter := o.newTblPrinterWithStyle("QUERY META", []interface{}{"START TIME", "END TIME", "OPERATION", "DATA"})
tblPrinter.SetStyle(printer.KubeCtlStyle)
tblPrinter.AddRow(util.TimeTimeFormat(meta.StartTime), util.TimeTimeFormat(meta.EndTime), meta.Operation, meta.Extra)
tblPrinter.AddRow(event, message)
tblPrinter.Print()
}

func (o *AccountBaseOptions) printUserInfo(response lorryutil.SQLChannelResponse) error {
if response.Event == lorryutil.RespEveFail {
o.printGeneralInfo(response)
return nil
}
// decode user info from metadata
users := []lorryutil.UserInfo{}
err := json.Unmarshal([]byte(response.Message), &users)
if err != nil {
return err
}

func (o *AccountBaseOptions) printUserInfo(users []map[string]any) {
// render user info with username and password expired boolean
tblPrinter := o.newTblPrinterWithStyle("USER INFO", []interface{}{"USERNAME", "EXPIRED"})
for _, user := range users {
tblPrinter.AddRow(user.UserName, user.Expired)
tblPrinter.AddRow(user["userName"], user["expired"])
}

tblPrinter.Print()
return nil
}

func (o *AccountBaseOptions) printRoleInfo(response lorryutil.SQLChannelResponse) error {
if response.Event == lorryutil.RespEveFail {
o.printGeneralInfo(response)
return nil
}

// decode role info from metadata
users := []lorryutil.UserInfo{}
err := json.Unmarshal([]byte(response.Message), &users)
if err != nil {
return err
}

func (o *AccountBaseOptions) printRoleInfo(users []map[string]any) {
tblPrinter := o.newTblPrinterWithStyle("USER INFO", []interface{}{"USERNAME", "ROLE"})
for _, user := range users {
tblPrinter.AddRow(user.UserName, user.RoleName)
tblPrinter.AddRow(user["userName"], user["roleName"])
}
tblPrinter.Print()
return nil
}
13 changes: 4 additions & 9 deletions pkg/cli/cmd/accounts/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

"github.com/apecloud/kubeblocks/pkg/cli/testing"
"github.com/apecloud/kubeblocks/pkg/cli/types"
lorryutil "github.com/apecloud/kubeblocks/pkg/lorry/util"
)

var _ = Describe("Base Account Options", func() {
Expand Down Expand Up @@ -81,16 +80,12 @@ var _ = Describe("Base Account Options", func() {

Context("new options", func() {
It("new option", func() {
for _, op := range []lorryutil.OperationKind{lorryutil.CreateUserOp, lorryutil.DeleteUserOp,
lorryutil.ListUsersOp, lorryutil.DescribeUserOp,
lorryutil.GrantUserRoleOp, lorryutil.RevokeUserRoleOp} {
o := NewAccountBaseOptions(tf, streams, op)
Expect(o).ShouldNot(BeNil())
}
o := NewAccountBaseOptions(tf, streams)
Expect(o).ShouldNot(BeNil())
})

It("validate options", func() {
o := NewAccountBaseOptions(tf, streams, lorryutil.CreateUserOp)
o := NewAccountBaseOptions(tf, streams)
Expect(o).ShouldNot(BeNil())
args := []string{}
Expect(o.Validate(args)).Should(MatchError(errClusterNameorInstName))
Expand All @@ -117,7 +112,7 @@ var _ = Describe("Base Account Options", func() {
})

It("complete option", func() {
o := NewAccountBaseOptions(tf, streams, lorryutil.CreateUserOp)
o := NewAccountBaseOptions(tf, streams)
Expect(o).ShouldNot(BeNil())
o.PodName = pods.Items[0].Name
o.ClusterName = clusterName
Expand Down
39 changes: 29 additions & 10 deletions pkg/cli/cmd/accounts/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package accounts

import (
"context"
"fmt"

"github.com/sethvargo/go-password/password"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

lorryutil "github.com/apecloud/kubeblocks/pkg/lorry/util"
"github.com/apecloud/kubeblocks/pkg/lorry/client"
)

type CreateUserOptions struct {
*AccountBaseOptions
info lorryutil.UserInfo
userName string
password string
}

func NewCreateUserOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *CreateUserOptions {
return &CreateUserOptions{
AccountBaseOptions: NewAccountBaseOptions(f, streams, lorryutil.CreateUserOp),
AccountBaseOptions: NewAccountBaseOptions(f, streams),
}
}

func (o *CreateUserOptions) AddFlags(cmd *cobra.Command) {
o.AccountBaseOptions.AddFlags(cmd)
cmd.Flags().StringVar(&o.info.UserName, "name", "", "Required. Specify the name of user, which must be unique.")
cmd.Flags().StringVarP(&o.info.Password, "password", "p", "", "Optional. Specify the password of user. The default value is empty, which means a random password will be generated.")
cmd.Flags().StringVar(&o.userName, "name", "", "Required. Specify the name of user, which must be unique.")
cmd.Flags().StringVarP(&o.password, "password", "p", "", "Optional. Specify the password of user. The default value is empty, which means a random password will be generated.")
_ = cmd.MarkFlagRequired("name")
// TODO:@shanshan add expire flag if needed
// cmd.Flags().DurationVar(&o.info.ExpireAt, "expire", 0, "Optional. Specify the expired time of password. The default value is 0, which means the user will never expire.")
Expand All @@ -52,7 +57,7 @@ func (o CreateUserOptions) Validate(args []string) error {
if err := o.AccountBaseOptions.Validate(args); err != nil {
return err
}
if len(o.info.UserName) == 0 {
if len(o.userName) == 0 {
return errMissingUserName
}
return nil
Expand All @@ -64,10 +69,24 @@ func (o *CreateUserOptions) Complete(f cmdutil.Factory) error {
return err
}
// complete other options
if len(o.info.Password) == 0 {
o.info.Password, _ = password.Generate(10, 2, 0, false, false)
if len(o.password) == 0 {
o.password, _ = password.Generate(10, 2, 0, false, false)
}
// encode user info to metadata
o.RequestMeta, err = struct2Map(o.info)
return err
}

func (o *CreateUserOptions) Run(cmd *cobra.Command, f cmdutil.Factory, streams genericiooptions.IOStreams) error {
klog.V(1).Info(fmt.Sprintf("connect to cluster %s, component %s, instance %s\n", o.ClusterName, o.ComponentName, o.PodName))
lorryClient, err := client.NewK8sExecClientWithPod(o.Pod)
if err != nil {
return err
}

err = lorryClient.CreateUser(context.Background(), o.userName, o.password)
if err != nil {
o.printGeneralInfo("fail", err.Error())
return err
}
o.printGeneralInfo("success", "")
return nil
}
12 changes: 5 additions & 7 deletions pkg/cli/cmd/accounts/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (

"github.com/apecloud/kubeblocks/pkg/cli/testing"
"github.com/apecloud/kubeblocks/pkg/cli/types"
lorryutil "github.com/apecloud/kubeblocks/pkg/lorry/util"
)

var _ = Describe("Create Account Options", func() {
Expand Down Expand Up @@ -85,7 +84,6 @@ var _ = Describe("Create Account Options", func() {
o := NewCreateUserOptions(tf, streams)
Expect(o).ShouldNot(BeNil())
Expect(o.AccountBaseOptions).ShouldNot(BeNil())
Expect(o.AccountBaseOptions.AccountOp).Should(Equal(lorryutil.CreateUserOp))
})

It("validate user name and password", func() {
Expand All @@ -105,10 +103,10 @@ var _ = Describe("Create Account Options", func() {
Expect(o.Validate(args)).Should(MatchError(errMissingUserName))

// set user name
o.info.UserName = "fooUser"
o.userName = "fooUser"
Expect(o.Validate(args)).Should(Succeed())
// set password
o.info.Password = "fooPwd"
o.password = "fooPwd"
Expect(o.Validate(args)).Should(Succeed())
})

Expand All @@ -117,11 +115,11 @@ var _ = Describe("Create Account Options", func() {
Expect(o).ShouldNot(BeNil())
o.PodName = pods.Items[0].Name
o.ClusterName = clusterName
o.info.UserName = "foo-user"
o.userName = "foo-user"

Expect(o.info.Password).Should(HaveLen(0))
Expect(o.password).Should(HaveLen(0))
Expect(o.Complete(tf)).Should(Succeed())
Expect(o.info.Password).ShouldNot(BeEmpty())
Expect(o.password).ShouldNot(BeEmpty())
})
})
})
Loading

0 comments on commit eeb8579

Please sign in to comment.