Skip to content

Commit

Permalink
support egctl get to display httpserver and pipelines in different na…
Browse files Browse the repository at this point in the history
…mespace
  • Loading branch information
suchen-sci committed Jan 19, 2024
1 parent 2aa5400 commit 87668e0
Show file tree
Hide file tree
Showing 11 changed files with 613 additions and 34 deletions.
162 changes: 162 additions & 0 deletions build/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,3 +1312,165 @@ filters:
// get this body means our pipeline is working.
assert.Equal("body from response builder", string(data))
}

func TestEgctlNamespace(t *testing.T) {
assert := assert.New(t)
mockNamespace := `
name: mockNamespace
kind: MockNamespacer
namespace: mockNamespace
httpservers:
- kind: HTTPServer
name: mock-httpserver
port: 10222
https: false
rules:
- paths:
- path: /pipeline
backend: mock-pipeline
pipelines:
- name: mock-pipeline
kind: Pipeline
flow:
- filter: proxy
filters:
- name: proxy
kind: Proxy
pools:
- servers:
- url: http://127.0.0.1:9095
- url: http://127.0.0.1:9096
loadBalance:
policy: roundRobin
`
err := createResource(mockNamespace)
assert.Nil(err)
defer func() {
err := deleteResource("MockNamespacer", "mockNamespace")
assert.Nil(err)
}()

httpserver := `
name: httpserver-test
kind: HTTPServer
port: 10181
https: false
keepAlive: true
keepAliveTimeout: 75s
maxConnection: 10240
cacheSize: 0
rules:
- paths:
- backend: pipeline-test
`
err = createResource(httpserver)
assert.Nil(err)
defer func() {
err := deleteResource("HTTPServer", "httpserver-test")
assert.Nil(err)
}()

pipeline := `
name: pipeline-test
kind: Pipeline
flow:
- filter: proxy
filters:
- name: proxy
kind: Proxy
pools:
- servers:
- url: http://127.0.0.1:8888
`
err = createResource(pipeline)
assert.Nil(err)
defer func() {
err := deleteResource("Pipeline", "pipeline-test")
assert.Nil(err)
}()

// egctl get all
{
// by default, list resources in "default" namespace
cmd := egctlCmd("get", "all")
output, stderr, err := runCmd(cmd)
assert.Nil(err)
assert.Empty(stderr)
assert.Contains(output, "httpserver-test")
assert.Contains(output, "pipeline-test")
assert.NotContains(output, "mock-httpserver")
assert.NotContains(output, "mock-pipeline")
}

// egctl get all --all-namespaces
{
cmd := egctlCmd("get", "all", "--all-namespaces")
output, stderr, err := runCmd(cmd)
assert.Nil(err)
assert.Empty(stderr)
assert.Contains(output, "httpserver-test")
assert.Contains(output, "pipeline-test")
assert.Contains(output, "mock-httpserver")
assert.Contains(output, "mock-pipeline")
}

// egctl get all --namespace mockNamespace
{
cmd := egctlCmd("get", "all", "--namespace", "mockNamespace")
output, stderr, err := runCmd(cmd)
assert.Nil(err)
assert.Empty(stderr)
assert.NotContains(output, "httpserver-test")
assert.NotContains(output, "pipeline-test")
assert.Contains(output, "mock-httpserver")
assert.Contains(output, "mock-pipeline")
}

// egctl get all --namespace default
{
cmd := egctlCmd("get", "all", "--namespace", "default")
output, stderr, err := runCmd(cmd)
assert.Nil(err)
assert.Empty(stderr)
assert.Contains(output, "httpserver-test")
assert.Contains(output, "pipeline-test")
assert.NotContains(output, "mock-httpserver")
assert.NotContains(output, "mock-pipeline")
}

// egctl get hs --namespace mockNamespace
{
cmd := egctlCmd("get", "hs", "--namespace", "mockNamespace")
output, stderr, err := runCmd(cmd)
assert.Nil(err)
assert.Empty(stderr)
assert.NotContains(output, "httpserver-test")
assert.NotContains(output, "pipeline-test")
assert.Contains(output, "mock-httpserver")
assert.NotContains(output, "mock-pipeline")
}

// egctl get hs --all-namespaces
{
cmd := egctlCmd("get", "hs", "--all-namespaces")
output, stderr, err := runCmd(cmd)
assert.Nil(err)
assert.Empty(stderr)
assert.Contains(output, "httpserver-test")
assert.NotContains(output, "pipeline-test")
assert.Contains(output, "mock-httpserver")
assert.NotContains(output, "mock-pipeline")
}

// egctl get hs mock-httpserver --namespace mockNamespace -o yaml
{
cmd := egctlCmd("get", "hs", "mock-httpserver", "--namespace", "mockNamespace", "-o", "yaml")
output, stderr, err := runCmd(cmd)
assert.Nil(err)
assert.Empty(stderr)
assert.NotContains(output, "httpserver-test")
assert.NotContains(output, "pipeline-test")
assert.Contains(output, "mock-httpserver")
assert.Contains(output, "port: 10222")
}
}
17 changes: 15 additions & 2 deletions cmd/client/commandv2/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/spf13/cobra"
)

var getFlags resources.GetObjectFlags

// GetCmd returns get command.
func GetCmd() *cobra.Command {
examples := []general.Example{
Expand All @@ -41,6 +43,8 @@ func GetCmd() *cobra.Command {
{Desc: "Get a customdata kind", Command: "egctl get customdatakind <name>"},
{Desc: "Get a customdata of given kind", Command: "egctl get customdata <kind> <name>"},
{Desc: "Check all possible api resources", Command: "egctl api-resources"},
{Desc: "Check all possible api resources from all namespaces, including httpservers and pipelines created by IngressController, MeshController and GatewayController", Command: "egctl get all --all-namespaces"},
{Desc: "Check all possible api resources from certain namespace", Command: "egctl get all --namespace <namespace>"},
}
cmd := &cobra.Command{
Use: "get",
Expand All @@ -49,6 +53,11 @@ func GetCmd() *cobra.Command {
Example: createMultiExample(examples),
Run: getCmdRun,
}
cmd.Flags().StringVar(&getFlags.Namespace, "namespace", "",
"namespace is used to get httpservers and pipelines created by IngressController, MeshController or GatewayController"+
"(these objects create httpservers and pipelines in an independent namespace)")
cmd.Flags().BoolVar(&getFlags.AllNamespace, "all-namespaces", false,
"get all resources in all namespaces (including the ones created by IngressController, MeshController and GatewayController that are in an independent namespace)")
return cmd
}

Expand All @@ -59,13 +68,17 @@ func getAllResources(cmd *cobra.Command) error {
errs = append(errs, err.Error())
}
}
err := resources.GetAllObject(cmd)
err := resources.GetAllObject(cmd, &getFlags)
if err != nil {
appendErr(err)
} else {
fmt.Printf("\n")
}

if getFlags.Namespace != "" && getFlags.Namespace != resources.DefaultNamespace {
return nil
}

funcs := []func(*cobra.Command, *general.ArgInfo) error{
resources.GetMember, resources.GetCustomDataKind,
}
Expand Down Expand Up @@ -109,7 +122,7 @@ func getCmdRun(cmd *cobra.Command, args []string) {
case resources.Member().Kind:
err = resources.GetMember(cmd, a)
default:
err = resources.GetObject(cmd, a, kind)
err = resources.GetObject(cmd, a, kind, &getFlags)
}
}

Expand Down
Loading

0 comments on commit 87668e0

Please sign in to comment.