Skip to content

Commit

Permalink
Merge pull request #75 from Lwxiang/dev_wesley_client_feat
Browse files Browse the repository at this point in the history
feature: client list command support --all-namespaces options, issue #66
  • Loading branch information
DeveloperJim authored Jul 1, 2019
2 parents 5ac657f + 58c2dd5 commit 4d11417
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bcs-services/bcs-client/cmd/list/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func listAgent(c *utils.ClientContext) error {
}

condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

ipList := utils.GetIPList(c.String(utils.OptionIP))

Expand Down
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

commonTypes "bk-bcs/bcs-common/common/types"
"bk-bcs/bcs-services/bcs-client/cmd/utils"
Expand All @@ -27,15 +28,25 @@ func listApplication(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListApplication(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list application: %v", err)
}

sort.Sort(list)
return printListApplication(list)
}

Expand Down
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
Expand All @@ -26,15 +27,25 @@ func listConfigMap(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListConfigMap(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list configmap: %v", err)
}

sort.Sort(list)
return printListConfigMap(list)
}

Expand Down
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
Expand All @@ -26,15 +27,25 @@ func listDeployment(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListDeployment(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list deployment: %v", err)
}

sort.Sort(list)
return printListDeployment(list)
}

Expand Down
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
Expand All @@ -26,15 +27,25 @@ func listEndpoint(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListEndpoint(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list endpoint: %v", err)
}

sort.Sort(list)
return printListEndpoint(list)
}

Expand Down
8 changes: 8 additions & 0 deletions bcs-services/bcs-client/cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func NewListCommand() cli.Command {
Usage: "Namespace",
Value: "",
},
cli.BoolFlag{
Name: "all-namespaces, an",
Usage: "list resources with all namespaces",
},
cli.StringFlag{
Name: "ip",
Usage: "The ip of taskgroup. Split by ,",
Expand Down Expand Up @@ -85,3 +89,7 @@ func list(c *utils.ClientContext) error {
return fmt.Errorf("invalid type: %s", resourceType)
}
}

const (
FilterNamespaceTag = "namespace"
)
15 changes: 15 additions & 0 deletions bcs-services/bcs-client/cmd/list/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
"fmt"
"net/url"
)

func listNamespace(c *utils.ClientContext) error {
Expand Down Expand Up @@ -45,3 +46,17 @@ func printListNamespace(list []string) error {
}
return nil
}

func getNamespaceFilter(storage v1.Storage, clusterID string) (url.Values, error) {
ns, err := storage.ListNamespace(clusterID, nil)
if err != nil {
return nil, fmt.Errorf("failed to list all namespace: %v", err)
}

data := url.Values{}
for _, item := range ns {
data.Add(FilterNamespaceTag, item)
}

return data, nil
}
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

commonTypes "bk-bcs/bcs-common/common/types"
"bk-bcs/bcs-services/bcs-client/cmd/utils"
Expand All @@ -27,15 +28,25 @@ func listProcess(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListProcess(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list process: %v", err)
}

sort.Sort(list)
return printListProcess(list)
}

Expand Down
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
Expand All @@ -26,15 +27,25 @@ func listSecret(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListSecret(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list secret: %v", err)
}

sort.Sort(list)
return printListSecret(list)
}

Expand Down
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
Expand All @@ -26,15 +27,25 @@ func listService(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListService(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list service: %v", err)
}

sort.Sort(list)
return printListService(list)
}

Expand Down
15 changes: 13 additions & 2 deletions bcs-services/bcs-client/cmd/list/taskgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package list
import (
"fmt"
"net/url"
"sort"

"bk-bcs/bcs-services/bcs-client/cmd/utils"
"bk-bcs/bcs-services/bcs-client/pkg/storage/v1"
Expand All @@ -26,20 +27,30 @@ func listTaskGroup(c *utils.ClientContext) error {
return err
}

storage := v1.NewBcsStorage(utils.GetClientOption())

// get namespace
condition := url.Values{}
condition.Add("namespace", c.Namespace())
condition.Add(FilterNamespaceTag, c.Namespace())

if c.IsAllNamespace() {
var err error
if condition, err = getNamespaceFilter(storage, c.ClusterID()); err != nil {
return err
}
}

ip := c.String(utils.OptionIP)
if ip != "" {
condition.Add("hostIp", ip)
}

storage := v1.NewBcsStorage(utils.GetClientOption())
list, err := storage.ListTaskGroup(c.ClusterID(), condition)
if err != nil {
return fmt.Errorf("failed to list taskgroup: %v", err)
}

sort.Sort(list)
return printListTaskGroup(list)
}

Expand Down
1 change: 1 addition & 0 deletions bcs-services/bcs-client/cmd/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package utils
const (
OptionClusterID = "clusterid"
OptionNamespace = "namespace"
OptionAllNamespace = "all-namespaces"
OptionName = "name"
OptionTaskGroupName = "tgname"
OptionType = "type"
Expand Down
15 changes: 12 additions & 3 deletions bcs-services/bcs-client/cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ func DebugPrintf(format string, a ...interface{}) {
type ClientContext struct {
*cli.Context

clusterID string
namespace string
clusterID string
namespace string
allNamespace bool
}

func NewClientContext(c *cli.Context) *ClientContext {
Expand All @@ -185,7 +186,7 @@ func (cc *ClientContext) MustSpecified(key ...string) error {
continue
}
if k == OptionNamespace {
if cc.namespace == "" {
if cc.namespace == "" && !cc.allNamespace {
return fmt.Errorf("namespace must be specified, options or env")
}
continue
Expand All @@ -209,6 +210,10 @@ func (cc *ClientContext) initEnv() {
if cc.IsSet(OptionNamespace) && cc.String(OptionNamespace) != "" {
cc.namespace = cc.String(OptionNamespace)
}

if cc.IsSet(OptionAllNamespace) {
cc.allNamespace = cc.Bool(OptionAllNamespace)
}
}

func (cc *ClientContext) ClusterID() string {
Expand All @@ -219,6 +224,10 @@ func (cc *ClientContext) Namespace() string {
return cc.namespace
}

func (cc *ClientContext) IsAllNamespace() bool {
return cc.allNamespace
}

func (cc *ClientContext) FileData() ([]byte, error) {
if err := cc.MustSpecified(OptionFile); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 4d11417

Please sign in to comment.