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

Enhance egctl #1020

Merged
merged 31 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a755223
finish get command and refactor code
suchen-sci Jun 16, 2023
a118ed2
finish describe object
suchen-sci Jun 16, 2023
4c96e30
finish all describe
suchen-sci Jun 19, 2023
84ab4c6
finish create, delete, update
suchen-sci Jun 20, 2023
5f9815a
finish other command
suchen-sci Jun 21, 2023
6a807fe
add example framework
suchen-sci Jun 21, 2023
587e948
add more examples, add other missing commands
suchen-sci Jun 25, 2023
75219f6
add command for object status
suchen-sci Jun 26, 2023
9a74cf9
udpate egctl command
suchen-sci Jun 26, 2023
7d1f484
update
suchen-sci Jun 26, 2023
9f75742
add comments
suchen-sci Jun 26, 2023
7bc5634
add more comments, add egctl cheat sheet
suchen-sci Jun 26, 2023
9d8063a
update egctl cheat sheet
suchen-sci Jun 26, 2023
a496d83
fix random test bug
suchen-sci Jun 26, 2023
86baec0
update based on comment
suchen-sci Jun 26, 2023
599cd37
rewrite apply create get command
suchen-sci Jun 29, 2023
bf84ef6
finish describe, delete and refactor code
suchen-sci Jun 30, 2023
66d0089
update examples
suchen-sci Jul 3, 2023
e3169b9
Merge branch 'main' into enhance-egctl
suchen-sci Jul 3, 2023
f4a1e84
fix typo
suchen-sci Jul 3, 2023
d62f266
add security for egctl
suchen-sci Jul 5, 2023
d65c077
update doc
suchen-sci Jul 5, 2023
cb58b43
refactor code structure
suchen-sci Jul 6, 2023
f69ec13
update based on code review comments
suchen-sci Jul 6, 2023
14ebb75
update based on github action
suchen-sci Jul 6, 2023
7dfd1cc
update based on github action
suchen-sci Jul 6, 2023
83df91b
update based on github action
suchen-sci Jul 6, 2023
92f54a8
update based on github action
suchen-sci Jul 6, 2023
b8593fc
update based on github action
suchen-sci Jul 6, 2023
c4e92c4
update based on github action
suchen-sci Jul 6, 2023
6e81f67
support egctl create/apply -f -
suchen-sci Jul 7, 2023
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
Prev Previous commit
Next Next commit
update based on github action
  • Loading branch information
suchen-sci committed Jul 6, 2023
commit 7dfd1ccfd8da91609c303c858a5754a65cd10789
1 change: 1 addition & 0 deletions cmd/client/commandv2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/spf13/cobra"
)

// ConfigCmd defines config command.
func ConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/commandv2/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func describeCmdArgs(cmd *cobra.Command, args []string) (err error) {
return fmt.Errorf("no resource specified")
}
if len(args) == 1 {
if general.InApiResource(args[0], resources.CustomData()) {
if general.InAPIResource(args[0], resources.CustomData()) {
return fmt.Errorf("no custom data kind specified")
}
return nil
Expand All @@ -95,7 +95,7 @@ func describeCmdArgs(cmd *cobra.Command, args []string) (err error) {
if len(args) == 2 {
return nil
}
if len(args) == 3 && general.InApiResource(args[0], resources.CustomData()) {
if len(args) == 3 && general.InAPIResource(args[0], resources.CustomData()) {
return nil
}
return fmt.Errorf("invalid args")
Expand Down
1 change: 1 addition & 0 deletions cmd/client/commandv2/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func APIsCmd() *cobra.Command {
return cmd
}

// APIResourcesCmd returns api-resources command.
func APIResourcesCmd() *cobra.Command {
cd := resources.CustomData()
cdk := resources.CustomDataKind()
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/commandv2/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func getCmdArgs(cmd *cobra.Command, args []string) (err error) {
return fmt.Errorf("no resource specified")
}
if len(args) == 1 {
if general.InApiResource(args[0], resources.CustomData()) {
if general.InAPIResource(args[0], resources.CustomData()) {
return fmt.Errorf("no custom data kind specified")
}
return nil
Expand All @@ -136,7 +136,7 @@ func getCmdArgs(cmd *cobra.Command, args []string) (err error) {
if len(args) == 2 {
return nil
}
if len(args) == 3 && general.InApiResource(args[0], resources.CustomData()) {
if len(args) == 3 && general.InAPIResource(args[0], resources.CustomData()) {
return nil
}
return fmt.Errorf("invalid args")
Expand Down
8 changes: 4 additions & 4 deletions cmd/client/general/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ func GetConfig() (*Config, error) {

// GetRedactedConfig returns the config with sensitive data redacted.
func GetRedactedConfig(c *Config) *Config {
copy := *c
config := *c
redacted, _ := base64.StdEncoding.DecodeString(string("REDACTED"))
copy.Clusters = Map(copy.Clusters, func(c NamedCluster) NamedCluster {
config.Clusters = Map(config.Clusters, func(c NamedCluster) NamedCluster {
if len(c.Cluster.CertificateAuthorityData) > 0 {
c.Cluster.CertificateAuthorityData = redacted
}
return c
})
copy.AuthInfos = Map(copy.AuthInfos, func(u NamedAuthInfo) NamedAuthInfo {
config.AuthInfos = Map(config.AuthInfos, func(u NamedAuthInfo) NamedAuthInfo {
if len(u.AuthInfo.ClientCertificateData) > 0 {
u.AuthInfo.ClientCertificateData = redacted
}
Expand All @@ -276,7 +276,7 @@ func GetRedactedConfig(c *Config) *Config {
}
return u
})
return &copy
return &config
}

var globalCurrentConfig *CurrentConfig
Expand Down
2 changes: 2 additions & 0 deletions cmd/client/general/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"strings"
)

// SuccessMsg returns the success message.
func SuccessMsg(action CmdType, values ...string) string {
return fmt.Sprintf("%s %s successfully", action, strings.Join(values, " "))
}

// ErrorMsg returns the error message.
func ErrorMsg(action CmdType, err error, values ...string) error {
return fmt.Errorf("%s %s failed, %v", action, strings.Join(values, " "), err)
}
4 changes: 2 additions & 2 deletions cmd/client/general/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func PrintMapInterface(maps []map[string]interface{}, fronts []string, backs []s
}
}

print := func(spec map[string]interface{}) {
printFn := func(spec map[string]interface{}) {
spec = func(s map[string]interface{}) map[string]interface{} {
res := map[string]interface{}{}
for k, v := range s {
Expand Down Expand Up @@ -212,7 +212,7 @@ func PrintMapInterface(maps []map[string]interface{}, fronts []string, backs []s
}

for i, m := range maps {
print(m)
printFn(m)
if len(maps) > 1 && i != len(maps)-1 {
fmt.Print("\n\n---\n\n")
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/client/general/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/megaease/easegress/pkg/util/stringtool"
)

// MakeURL is used to make URL for given path.
func MakeURL(path string) (string, error) {
// server priority: flag > config > default
server := defaultServer
Expand Down Expand Up @@ -63,6 +64,7 @@ func MakePath(urlTemplate string, a ...interface{}) string {
return fmt.Sprintf(urlTemplate, a...)
}

// GetHTTPClient is used to get HTTP client.
func GetHTTPClient() (*http.Client, error) {
config, err := GetCurrentConfig()
if err != nil {
Expand Down Expand Up @@ -116,6 +118,7 @@ func GetHTTPClient() (*http.Client, error) {
return client, nil
}

// SuccessfulStatusCode returns true if the status code is successful.
func SuccessfulStatusCode(code int) bool {
return code >= 200 && code < 300
}
Expand Down Expand Up @@ -188,24 +191,29 @@ func doRequest(httpMethod string, url string, jsonBody []byte, client *http.Clie
return resp, body, nil
}

// ArgInfo is used to store the information of arguments.
type ArgInfo struct {
Resource string
Name string
Other string
}

// ContainResource returns true if the arguments contain resource.
func (a *ArgInfo) ContainResource() bool {
return a.Resource != ""
}

// ContainName returns true if the arguments contain name.
func (a *ArgInfo) ContainName() bool {
return a.Name != ""
}

// ContainOther returns true if the arguments contain other.
func (a *ArgInfo) ContainOther() bool {
return a.Other != ""
}

// ParseArgs parses the arguments and returns the ArgInfo.
func ParseArgs(args []string) *ArgInfo {
if len(args) == 0 {
return nil
Expand All @@ -221,6 +229,7 @@ func ParseArgs(args []string) *ArgInfo {
return argInfo
}

func InApiResource(arg string, r *api.ApiResource) bool {
// InAPIResource returns true if the arg is in the api resource.
func InAPIResource(arg string, r *api.ApiResource) bool {
return arg == r.Name || arg == r.Kind || stringtool.StrInSlice(arg, r.Aliases)
}
26 changes: 18 additions & 8 deletions cmd/client/general/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ package general
type CmdType string

const (
GetCmd CmdType = "get"
CreateCmd CmdType = "create"
ApplyCmd CmdType = "apply"
DeleteCmd CmdType = "delete"
// GetCmd is the get command.
GetCmd CmdType = "get"
// CreateCmd is the create command.
CreateCmd CmdType = "create"
// ApplyCmd is the apply command.
ApplyCmd CmdType = "apply"
// DeleteCmd is the delete command.
DeleteCmd CmdType = "delete"
// DescribeCmd is the describe command.
DescribeCmd CmdType = "describe"
StartCmd CmdType = "start"
StopCmd CmdType = "stop"
// StartCmd is the start command.
StartCmd CmdType = "start"
// StopCmd is the stop command.
StopCmd CmdType = "stop"
)

const (
JSONFormat string = "json"
YamlFormat string = "yaml"
// JSONFormat is the json format.
JSONFormat string = "json"
// YamlFormat is the yaml format.
YamlFormat string = "yaml"
// DefaultFormat is the default format.
DefaultFormat string = "default"
)
3 changes: 3 additions & 0 deletions cmd/client/general/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (v *yamlVisitor) Close() {
}
}

// Spec is the spec of a resource
type Spec struct {
Kind string
Name string
Expand Down Expand Up @@ -124,6 +125,7 @@ func (v *specVisitor) Close() {
v.v.Close()
}

// BuildYAMLVisitor builds a YAMLVisitor
func BuildYAMLVisitor(yamlFile string, cmd *cobra.Command) YAMLVisitor {
var r io.ReadCloser
if yamlFile == "" {
Expand All @@ -136,6 +138,7 @@ func BuildYAMLVisitor(yamlFile string, cmd *cobra.Command) YAMLVisitor {
return &yamlVisitor{reader: r}
}

// BuildSpecVisitor builds a SpecVisitor
func BuildSpecVisitor(yamlFile string, cmd *cobra.Command) SpecVisitor {
v := BuildYAMLVisitor(yamlFile, cmd)
return &specVisitor{v: v}
Expand Down
8 changes: 4 additions & 4 deletions cmd/client/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
"github.com/megaease/easegress/cmd/client/general"
)

func GetResourceKind(arg string) (string, error) {

Check warning on line 27 in cmd/client/resources/resources.go

View workflow job for this annotation

GitHub Actions / analysis

exported function GetResourceKind should have comment or be unexported
if general.InApiResource(arg, CustomData()) {
if general.InAPIResource(arg, CustomData()) {
return CustomData().Kind, nil
}
if general.InApiResource(arg, CustomDataKind()) {
if general.InAPIResource(arg, CustomDataKind()) {
return CustomDataKind().Kind, nil
}
if general.InApiResource(arg, Member()) {
if general.InAPIResource(arg, Member()) {
return Member().Kind, nil
}
objects, err := ObjectApiResources()
if err != nil {
return "", err
}
for _, object := range objects {
if general.InApiResource(arg, object) {
if general.InAPIResource(arg, object) {
return object.Kind, nil
}
}
Expand Down
18 changes: 9 additions & 9 deletions doc/egctl-cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,26 @@ kind: Config
# current used context.
current-context: context-default

# `contexts` section contains `user` and `cluster` information, which informs egctl about which `user` should be used to access a specific `cluster`.
# "contexts" section contains "user" and "cluster" information, which informs egctl about which "user" should be used to access a specific "cluster".
contexts:
- context:
cluster: cluster-default
user: user-default
name: context-default

# `clusters` section contains information about the `cluster`.
# `server` specifies the host address that egctl should access.
# `certificate-authority` or `certificate-authority-data` contain the root certificate authority that the client uses to verify server certificates.
# "clusters" section contains information about the "cluster".
# "server" specifies the host address that egctl should access.
# "certificate-authority" or "certificate-authority-data" contain the root certificate authority that the client uses to verify server certificates.
clusters:
- cluster:
server: localhost:2381
certificate-authority: "/tmp/certs/ca.crt"
certificate-authority-data: "xxxx" # base64
certificate-authority: "/tmp/certs/ca.crt"
certificate-authority-data: "xxxx"
name: cluster-default

# `users` section contains `user` information.
# `username` and `password` are used for basic authentication.
# either the pair (`client-key`, `client-certificate`) or the pair (`client-key-data`, `client-certificate-data`) contains the client certificate
# "users" section contains "user" information.
# "username" and "password" are used for basic authentication.
# either the pair ("client-key", "client-certificate") or the pair ("client-key-data", "client-certificate-data") contains the client certificate.
users:
- name: user-default
user:
Expand Down
Loading