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

add egctl edit, egctl logs, update egctl create cmd #1067

Merged
merged 15 commits into from
Aug 31, 2023
Prev Previous commit
Next Next commit
udpate doc, add examples
  • Loading branch information
suchen-sci committed Aug 25, 2023
commit 4b04d195808725aa9415c1880287e08af64f531f
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ filters:
The pipeline means it will forward traffic to 3 backend endpoints, using the
`roundRobin` load balance policy.

You can also create them using `egctl create httpproxy` command.
```bash
egctl create httpproxy demo --port 10080 \
--rule="/pipeline=http:https://127.0.0.1:9095,http:https://127.0.0.1:9096,http:https://127.0.0.1:9097"
```
this command will create `HTTPServer` `demo-server` and `Pipeline` `demo-pipeline-0` which work exactly same to `server-demo` and `pipeline-demo`. See more about [`egctl create httpproxy`](./doc/egctl-cheat-sheet.md#create-httpproxy).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propose not adding -server suffix to the name, user may get confused what he/she has created.
for the pipeline names, also propose not using the pipeline-x suffixes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea. I will update that!

Additionally, we provide a [dashboard](https://cloud.megaease.com) that
streamlines the aforementioned steps, this intuitive tool can help you create,
manage HTTPServers, Pipelines and other Easegress configuration.
Expand Down
38 changes: 35 additions & 3 deletions cmd/client/commandv2/create/createhttpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"github.com/spf13/cobra"
)

// CreateHTTPProxyOptions are the options to create a HTTPProxy.
type CreateHTTPProxyOptions struct {

Check warning on line 40 in cmd/client/commandv2/create/createhttpproxy.go

View workflow job for this annotation

GitHub Actions / analysis

type name will be used as create.CreateHTTPProxyOptions by other packages, and that stutters; consider calling this HTTPProxyOptions
Name string
Port int
Rules []string
Expand All @@ -55,13 +56,38 @@

var createHTTPProxyOptions = &CreateHTTPProxyOptions{}

var createHTTPProxyExamples = `# General case
egctl create httpproxy NAME --port PORT \
--rule HOST/PATH=ENDPOINT1,ENDPOINT2 \
[--rule HOST/PATH=ENDPOINT1,ENDPOINT2] \
[--tls] \
[--auto-cert] \
[--ca-cert-file CA_CERT_FILE] \
[--cert-file CERT_FILE] \
[--key-file KEY_FILE]

# Create a HTTPServer (with port 10080) and corresponding Pipelines to direct
# request with path "/bar" to "http:https://127.0.0.1:8080" and "http:https://127.0.0.1:8081" and
# request with path "/foo" to "http:https://127.0.0.1:8082".
egctl create httpproxy demo --port 10080 \
--rule="/bar=http:https://127.0.0.1:8080,http:https://127.0.0.1:8081" \
--rule="/foo=http:https://127.0.0.1:8082"

# Create a HTTPServer (with port 10081) and corresponding Pipelines to direct request
# with path prefix "foo.com/prefix" to "http:https://127.0.0.1:8083".
egctl create httpproxy demo2 --port 10081 \
--rule="foo.com/prefix*=http:https://127.0.0.1:8083"
`

// CreateHTTPProxyCmd returns create command of HTTPProxy.
func CreateHTTPProxyCmd() *cobra.Command {

Check warning on line 83 in cmd/client/commandv2/create/createhttpproxy.go

View workflow job for this annotation

GitHub Actions / analysis

func name will be used as create.CreateHTTPProxyCmd by other packages, and that stutters; consider calling this HTTPProxyCmd
o := createHTTPProxyOptions

cmd := &cobra.Command{
Use: "httpproxy NAME",
Short: "Create a HTTPServer and corresponding Pipelines with a specific name",
Args: createHTTPProxyArgs,
Use: "httpproxy NAME",
Short: "Create a HTTPServer and corresponding Pipelines with a specific name",
Args: createHTTPProxyArgs,
Example: general.CreateMultiLineExample(createHTTPProxyExamples),
Run: func(cmd *cobra.Command, args []string) {
err := createHTTPProxyRun(cmd, args)
if err != nil {
Expand Down Expand Up @@ -121,24 +147,28 @@
return nil
}

// HTTPServerSpec is the spec of HTTPServer.
type HTTPServerSpec struct {
Name string `json:"name"`
Kind string `json:"kind"`

httpserver.Spec `json:",inline"`
}

// PipelineSpec is the spec of Pipeline.
type PipelineSpec struct {
Name string `json:"name"`
Kind string `json:"kind"`

pipeline.Spec `json:",inline"`
}

// Complete completes all the required options.
func (o *CreateHTTPProxyOptions) Complete(args []string) {
o.Name = args[0]
}

// Parse parses all the optional options.
func (o *CreateHTTPProxyOptions) Parse() error {
// parse rules
rules := []*CreateHTTPProxyRule{}
Expand Down Expand Up @@ -192,6 +222,7 @@
return fmt.Sprintf("%s-pipeline-%d", o.Name, id)
}

// Translate translates CreateHTTPProxyOptions to HTTPServerSpec and PipelineSpec.
func (o *CreateHTTPProxyOptions) Translate() (*HTTPServerSpec, []*PipelineSpec) {
hs := &HTTPServerSpec{
Name: o.getServerName(),
Expand Down Expand Up @@ -343,7 +374,8 @@
}, nil
}

// CreateHTTPProxyRule is the rule of HTTPProxy.
type CreateHTTPProxyRule struct {

Check warning on line 378 in cmd/client/commandv2/create/createhttpproxy.go

View workflow job for this annotation

GitHub Actions / analysis

type name will be used as create.CreateHTTPProxyRule by other packages, and that stutters; consider calling this HTTPProxyRule
Host string
Path string
PathPrefix string
Expand Down
13 changes: 10 additions & 3 deletions cmd/client/commandv2/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ import (
func LogsCmd() *cobra.Command {
var n int
var follow bool
examples := []general.Example{
{Desc: "Print the most recent 500 logs by default.", Command: "egctl logs"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not print all logs like other logs by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Here are my concerns.

  1. kubectl logs will print help information. If print logs kubectl logs <pod-name>. And actually egctl create, egctl apply will print help information. So, based on this, user may think egctl logs will print help information. They may think there are commands like egctl logs std or egctl logs http-access etc. So, by default, I choose egctl logs not print all logs, just a few.
  2. easegress logs can be really really long. So print all logs may be not necessary...

Just my naive opinions, I am ok to change it to print all logs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know another command line to do like this, does this usage have a precedent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I am not sure these cases are precedent. But for really long logs, these tools not print them all by default.

  • git log only show a screen of logs, and allow scroll it.
  • journalctl, systemctl status is similar.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git log | cat will output all of them with cat to dismantle the pager effect. It just uses a pager to display logs, essentially it prints all of the logs. But egctl logs | cat outputs the last 500 lines. Well, I just provided another thought as I don't strongly insist on my point. You can decide on the design.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I am just a little afraid that in practice, when user run egctl logs, and terminal starts to print logs, then they press CTRL+C... (It happens to me all the time).
I am more prefer current design that provides a user-friendly default setting...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it is.

{Desc: "Print the most recent 100 logs.", Command: "egctl logs --tail 100"},
{Desc: "Print all logs.", Command: "egctl logs --tail -1"},
{Desc: "Print the most recent 500 logs and streaming the log.", Command: "egctl logs -f"},
}

cmd := &cobra.Command{
Use: "logs",
Short: "Print the logs of Easegress server",
Args: cobra.NoArgs,
Use: "logs",
Short: "Print the logs of Easegress server",
Args: cobra.NoArgs,
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
query := fmt.Sprintf("?tail=%d&follow=%v", n, follow)
p := general.LogsURL + query
Expand Down
9 changes: 9 additions & 0 deletions cmd/client/general/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ func CreateMultiExample(examples []Example) string {
return output
}

// CreateMultiLineExample creates cobra example by using multiple lines.
func CreateMultiLineExample(example string) string {
lines := strings.Split(example, "\n")
for i, line := range lines {
lines[i] = " " + line
}
return strings.Join(lines, "\n")
}

// GenerateExampleFromChild generates cobra example from child commands.
func GenerateExampleFromChild(cmd *cobra.Command) {
if len(cmd.Commands()) == 0 {
Expand Down
82 changes: 75 additions & 7 deletions doc/egctl-cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Easegress manifests are defined using YAML. They can be identified by the file extensions `.yaml` or `.yml`. You can create resources using either the `egctl create` or `egctl apply` commands. To view all available resources along with their supported actions, use the `egctl api-resources` command.

```
```bash
cat globalfilter.yaml | egctl create -f - # create GlobalFilter resource from stdin
cat httpserver-new.yaml | egctl apply -f - # create HTTPServer resource from stdin

Expand All @@ -16,9 +16,73 @@ egctl apply -f ./cdk-demo.yaml # create CustomDataKind resource
egctl create -f ./custom-data-demo.yaml # create CustomData resource
```

## Viewing and finding resources
## Create HTTPProxy
`egctl create httpproxy` is used to create `HTTPServer` and corresponding `Pipelines` quickly.

```bash
egctl create httpproxy NAME --port PORT \
--rule HOST/PATH=ENDPOINT1,ENDPOINT2 \
[--rule HOST/PATH=ENDPOINT1,ENDPOINT2] \
[--tls] \
[--auto-cert] \
[--ca-cert-file CA_CERT_FILE] \
[--cert-file CERT_FILE] \
[--key-file KEY_FILE]
```

For example:

```bash
# Create a HTTPServer (with port 10080) and corresponding Pipelines to direct
# request with path "/bar" to "http:https://127.0.0.1:8080" and "http:https://127.0.0.1:8081" and
# request with path "/foo" to "http:https://127.0.0.1:8082".
egctl create httpproxy demo --port 10080 \
--rule="/bar=http:https://127.0.0.1:8080,http:https://127.0.0.1:8081" \
--rule="/foo=http:https://127.0.0.1:8082"
```

this equals to
```yaml
kind: HTTPServer
name: demo-server
port: 10080
https: false
rules:
- paths:
- path: /bar
backend: demo-pipeline-0
- path: /foo
backend: demo-pipeline-1

---
name: demo-pipeline-0
kind: Pipeline
filters:
- name: proxy
kind: Proxy
pools:
- servers:
- url: http:https://127.0.0.1:8080
- url: http:https://127.0.0.1:8081
loadBalance:
policy: roundRobin

---
name: demo-pipeline-1
kind: Pipeline
filters:
- name: proxy
kind: Proxy
pools:
- servers:
- url: http:https://127.0.0.1:8082
loadBalance:
policy: roundRobin
```

## Viewing and finding resources

```bash
egctl get all # view all resources
egctl get httpserver httpserver-demo # find HTTPServer resources with name "httpserver-demo"

Expand All @@ -33,28 +97,32 @@ egctl describe pipeline pipeline-demo # describe Pipeline resource with name "p
```

## Updating resources
```
```bash
egctl apply -f httpserver-demo-version2.yaml # update HTTPServer resource
egctl apply -f cdk-demo2.yaml # udpate CustomDataKind resource
```

## Editing resources
```
```bash
egctl edit httpserver httpserver-demo # edit httpserver with name httpserver-demo
egctl edit customdata cdk-demo # batch edit custom data with kind cdk-demo
egctl edit customdata cdk-demo data1 # edit custom data data1 of kind cdk-demo
```
The default editor for `egctl edit` is `vi`. To change it, update the `EGCTL_EDITOR` environment variable.

## Deleting resources
```
```bash
egctl delete httpserver httpserver-demo # delete HTTPServer resource with name "httpserver-demo"
egctl delete httpserver --all # delete all HTTPServer resources
egctl delete customdatakind cdk-demo cdk-kind # delete CustomDataKind resources named "cdk-demo" and "cdk-kind"
```

## Other commands
```
```bash
egctl logs # print easegress-server logs
egctl logs --tail 100 # print most recent 100 logs
egctl logs -f # print logs as stream

egctl api-resources # view all available resources
egctl completion zsh # generate completion script for zsh
egctl health # check easegress health
Expand Down Expand Up @@ -131,7 +199,7 @@ cluster:
client-ca-file: "/tmp/certs/ca.crt"
```

```
```bash
egctl config current-context # display the current context in use by egctl
egctl config get-contexts # view all available contexts
egctl config use-context <name> # update the current-context field in the .egctlrc file to <name>
Expand Down
Loading