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

optim egctl get httpserver to show port and https #1248

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
5 changes: 4 additions & 1 deletion cmd/client/commandv2/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func getAllResources(cmd *cobra.Command) error {

// non-default namespace is not supported for members and custom data kinds.
if getFlags.Namespace != "" && getFlags.Namespace != resources.DefaultNamespace {
return nil
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}

funcs := []func(*cobra.Command, *general.ArgInfo) error{
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/resources/customdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func getCustomDataBatchYaml(args *general.ArgInfo) (string, error) {
return fmt.Sprintf("name: %s\nkind: CustomData\n\n%s", args.Name, yamlStr), nil
}

func editCustomDataItem(cmd *cobra.Command, args *general.ArgInfo) error {
func editCustomDataItem(_ *cobra.Command, args *general.ArgInfo) error {
getErr := func(err error) error {
return general.ErrorMsg(general.EditCmd, err, fmt.Sprintf("%s %s %s", CustomData().Kind, args.Name, args.Other))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/resources/customdatakind.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ func DeleteCustomDataKind(cmd *cobra.Command, names []string, all bool) error {

// ApplyCustomDataKind applies the custom data kind.
func ApplyCustomDataKind(cmd *cobra.Command, s *general.Spec) error {
checkKindExist := func(cmd *cobra.Command, name string) bool {
checkKindExist := func(_ *cobra.Command, name string) bool {
_, err := httpGetCustomDataKind(name)
return err == nil
}

createOrUpdate := func(cmd *cobra.Command, yamlDoc []byte, exist bool) error {
createOrUpdate := func(_ *cobra.Command, yamlDoc []byte, exist bool) error {
if exist {
_, err := handleReq(http.MethodPut, makePath(general.CustomDataKindURL), yamlDoc)
return err
Expand Down
190 changes: 128 additions & 62 deletions cmd/client/resources/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/megaease/easegress/v2/pkg/api"
Expand All @@ -45,6 +44,42 @@ type ObjectNamespaceFlags struct {

var globalAPIResources []*api.APIResource

func generateTableMap(objectSpecs []ObjectSpec, namespace string) map[string]Table {
tables := map[string]Table{}

wrapRow := func(row TableRow) TableRow {
if namespace != "" {
return row.WithNamespace(namespace)
}
return row
}

for _, spec := range objectSpecs {
tableType, row := spec.TableRow()

if val, ok := tables[tableType]; ok {
tables[tableType] = append(val, wrapRow(row))
} else {
_, header := spec.TableHeader()
tables[tableType] = Table{wrapRow(header), wrapRow(row)}
}
}
return tables
}

func tableMapToArray(tableMap map[string]Table) []Table {
res := make([]Table, len(tableMap))
keys := make([]string, 0, len(tableMap))
for k := range tableMap {
keys = append(keys, k)
}
sort.Strings(keys)
for i, k := range keys {
res[i] = tableMap[k]
}
return res
}

// ObjectAPIResources returns the object api resources.
func ObjectAPIResources() ([]*api.APIResource, error) {
if globalAPIResources != nil {
Expand Down Expand Up @@ -105,7 +140,7 @@ func GetAllObject(cmd *cobra.Command, flags *ObjectNamespaceFlags) error {
}

if flags.AllNamespace {
err := unmarshalPrintNamespaceMetaSpec(body, nil)
err := unmarshalPrintNamespaceObjectSpec(body, nil)
if err != nil {
return getErr(err)
}
Expand Down Expand Up @@ -217,117 +252,137 @@ func GetObject(cmd *cobra.Command, args *general.ArgInfo, kind string, flags *Ob
}

if flags.AllNamespace {
err := unmarshalPrintNamespaceMetaSpec(body, func(m *supervisor.MetaSpec) bool {
return m.Kind == kind
err := unmarshalPrintNamespaceObjectSpec(body, func(s ObjectSpec) bool {
return s.GetKind() == kind
})
if err != nil {
return getErr(err)
}
return nil
}

err = unmarshalPrintMetaSpec(body, !args.ContainName(), func(m *supervisor.MetaSpec) bool {
return m.Kind == kind
err = unmarshalPrintMetaSpec(body, !args.ContainName(), func(o ObjectSpec) bool {
return o.GetKind() == kind
})
if err != nil {
return getErr(err)
}
return nil
}

func unmarshalPrintMetaSpec(body []byte, list bool, filter func(*supervisor.MetaSpec) bool) error {
metas, err := unmarshalMetaSpec(body, list)
func unmarshalPrintMetaSpec(body []byte, list bool, filter func(ObjectSpec) bool) error {
specs, err := unmarshalObjectSpec(body, list)
if err != nil {
return err
}
if filter != nil {
metas = general.Filter(metas, filter)
specs = general.Filter(specs, filter)
}
sort.Slice(metas, func(i, j int) bool {
return metas[i].Name < metas[j].Name
sort.Slice(specs, func(i, j int) bool {
return specs[i].GetName() < specs[j].GetName()
})
printMetaSpec(metas)
printObjectSpec(specs)
return nil
}

func unmarshalPrintNamespaceMetaSpec(body []byte, filter func(*supervisor.MetaSpec) bool) error {
allMetas, err := unmarshalNamespaceMetaSpec(body)
func unmarshalPrintNamespaceObjectSpec(body []byte, filter func(ObjectSpec) bool) error {
allObjectSpecs, err := unmarshalNamespaceMetaSpec(body)
if err != nil {
return err
}
if filter != nil {
for k, v := range allMetas {
allMetas[k] = general.Filter(v, filter)
for k, v := range allObjectSpecs {
allObjectSpecs[k] = general.Filter(v, filter)
}
}
for k, v := range allMetas {
for k, v := range allObjectSpecs {
if len(v) > 0 {
sort.Slice(v, func(i, j int) bool {
return v[i].Name < v[j].Name
return v[i].GetName() < v[j].GetName()
})
allMetas[k] = v
allObjectSpecs[k] = v
}
}
printNamespaceMetaSpec(allMetas)
printNamespaceObjectSpec(allObjectSpecs)
return nil
}

func unmarshalMetaSpec(body []byte, listBody bool) ([]*supervisor.MetaSpec, error) {
if listBody {
metas := []*supervisor.MetaSpec{}
err := codectool.Unmarshal(body, &metas)
return metas, err
func unmarshalObjectSpec(body []byte, listBody bool) ([]ObjectSpec, error) {
res, err := general.UnmarshalMapInterface(body, listBody)
if err != nil {
return nil, err
}
meta := &supervisor.MetaSpec{}
err := codectool.Unmarshal(body, meta)
return []*supervisor.MetaSpec{meta}, err
}

func unmarshalNamespaceMetaSpec(body []byte) (map[string][]*supervisor.MetaSpec, error) {
res := map[string][]*supervisor.MetaSpec{}
err := codectool.Unmarshal(body, &res)
return res, err
specs := []ObjectSpec{}
for _, m := range res {
data, err := codectool.MarshalJSON(m)
if err != nil {
return nil, err
}
spec, err := GetObjectSpec(m["kind"].(string), data)
if err != nil {
return nil, err
}
specs = append(specs, spec)
}
return specs, nil
}

func getAgeFromMetaSpec(meta *supervisor.MetaSpec) string {
createdAt, err := time.Parse(time.RFC3339, meta.CreatedAt)
func unmarshalNamespaceMetaSpec(body []byte) (map[string][]ObjectSpec, error) {
raw := map[string][]map[string]interface{}{}
err := codectool.Unmarshal(body, &raw)
if err != nil {
return "unknown"
return nil, err
}
return general.DurationMostSignificantUnit(time.Since(createdAt))
res := map[string][]ObjectSpec{}
for ns, v := range raw {
for _, s := range v {
data, err := codectool.MarshalJSON(s)
if err != nil {
return nil, err
}
spec, err := GetObjectSpec(s["kind"].(string), data)
if err != nil {
return nil, err
}
res[ns] = append(res[ns], spec)
}
}
return res, nil
}

func printNamespaceMetaSpec(metas map[string][]*supervisor.MetaSpec) {
// Output:
// NAME KIND NAMESPACE AGE
// ...
table := [][]string{}
table = append(table, []string{"NAME", "KIND", "NAMESPACE", "AGE"})
func printNamespaceObjectSpec(metas map[string][]ObjectSpec) {
defaults := metas[DefaultNamespace]
for _, meta := range defaults {
table = append(table, []string{meta.Name, meta.Kind, DefaultNamespace, getAgeFromMetaSpec(meta)})
}
res := generateTableMap(defaults, DefaultNamespace)

for namespace, metas := range metas {
if namespace == DefaultNamespace {
continue
}
for _, meta := range metas {
table = append(table, []string{meta.Name, meta.Kind, namespace, getAgeFromMetaSpec(meta)})
tableMap := generateTableMap(metas, namespace)
for k, v := range tableMap {
if _, ok := res[k]; ok {
// remove table header
res[k] = append(res[k], v[1:]...)
} else {
res[k] = v
}
}
}
general.PrintTable(table)

tables := tableMapToArray(res)
for _, table := range tables {
general.PrintTable(table)
fmt.Println("")
}
}

func printMetaSpec(metas []*supervisor.MetaSpec) {
// Output:
// NAME KIND AGE
// ...
table := [][]string{}
table = append(table, []string{"NAME", "KIND", "AGE"})
for _, meta := range metas {
table = append(table, []string{meta.Name, meta.Kind, getAgeFromMetaSpec(meta)})
func printObjectSpec(metas []ObjectSpec) {
tableMap := generateTableMap(metas, "")
tables := tableMapToArray(tableMap)
for _, table := range tables {
general.PrintTable(table)
fmt.Println("")
}
general.PrintTable(table)
}

// DescribeObject describes an object.
Expand Down Expand Up @@ -460,6 +515,17 @@ func CreateObject(cmd *cobra.Command, s *general.Spec) error {
return nil
}

func unmarshalMetaSpec(body []byte, listBody bool) ([]*MetaSpec, error) {
if listBody {
metas := []*MetaSpec{}
err := codectool.Unmarshal(body, &metas)
return metas, err
}
meta := &MetaSpec{}
err := codectool.Unmarshal(body, meta)
return []*MetaSpec{meta}, err
}

// DeleteObject deletes an object.
func DeleteObject(cmd *cobra.Command, kind string, names []string, all bool) error {
// define error msg
Expand All @@ -480,7 +546,7 @@ func DeleteObject(cmd *cobra.Command, kind string, names []string, all bool) err
if err != nil {
return getErr(err)
}
metas = general.Filter(metas, func(m *supervisor.MetaSpec) bool {
metas = general.Filter(metas, func(m *MetaSpec) bool {
return m.Kind == kind
})

Expand Down Expand Up @@ -514,12 +580,12 @@ func DeleteObject(cmd *cobra.Command, kind string, names []string, all bool) err

// ApplyObject applies an object.
func ApplyObject(cmd *cobra.Command, s *general.Spec) error {
checkObjExist := func(cmd *cobra.Command, name string) bool {
checkObjExist := func(_ *cobra.Command, name string) bool {
_, err := httpGetObject(name, nil)
return err == nil
}

createOrUpdate := func(cmd *cobra.Command, s *general.Spec, exist bool) error {
createOrUpdate := func(_ *cobra.Command, s *general.Spec, exist bool) error {
if exist {
_, err := handleReq(http.MethodPut, makePath(general.ObjectItemURL, s.Name), []byte(s.Doc()))
return err
Expand Down
Loading
Loading