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

[Server] add version to agent list #5250

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 13 additions & 21 deletions cli/ctl/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import (
"strings"

"github.com/bitly/go-simplejson"
agentpb "github.com/deepflowio/deepflow/message/trident"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/deepflowio/deepflow/cli/ctl/common"
"github.com/deepflowio/deepflow/cli/ctl/common/jsonparser"
"github.com/deepflowio/deepflow/cli/ctl/common/printutil"
"github.com/deepflowio/deepflow/cli/ctl/common/table"
"github.com/deepflowio/deepflow/cli/ctl/example"
agentpb "github.com/deepflowio/deepflow/message/trident"
)

type RebalanceType string
Expand Down Expand Up @@ -213,21 +214,10 @@ func listAgent(cmd *cobra.Command, args []string, output string) {
dataYaml, _ := yaml.JSONToYAML(dataJson)
fmt.Printf(string(dataYaml))
} else {
nameMaxSize, groupMaxSize := 0, 0
for i := range response.Get("DATA").MustArray() {
vtap := response.Get("DATA").GetIndex(i)
nameSize := len(vtap.Get("NAME").MustString())
if nameSize > nameMaxSize {
nameMaxSize = nameSize
}
groupSize := len(vtap.Get("VTAP_GROUP_NAME").MustString())
if groupSize > groupMaxSize {
groupMaxSize = groupSize
}
}
t := table.New()
t.SetHeader([]string{"ID", "NAME", "TYPE", "CTRL_IP", "CTRL_MAC", "STATE", "GROUP", "EXCEPTIONS", "REVISION", "UPGRADE_REVISION"})

cmdFormat := "%-8s %-*s %-10s %-18s %-18s %-8s %-*s %s\n"
fmt.Printf(cmdFormat, "ID", nameMaxSize, "NAME", "TYPE", "CTRL_IP", "CTRL_MAC", "STATE", groupMaxSize, "GROUP", "EXCEPTIONS")
tableItems := [][]string{}
for i := range response.Get("DATA").MustArray() {
vtap := response.Get("DATA").GetIndex(i)

Expand All @@ -242,19 +232,21 @@ func listAgent(cmd *cobra.Command, args []string, output string) {
}
}

fmt.Printf(cmdFormat,
tableItems = append(tableItems, []string{
strconv.Itoa(vtap.Get("ID").MustInt()),
nameMaxSize,
vtap.Get("NAME").MustString(),
common.VtapType(vtap.Get("TYPE").MustInt()),
fmt.Sprintf("%v", common.VtapType(vtap.Get("TYPE").MustInt())),
vtap.Get("CTRL_IP").MustString(),
vtap.Get("CTRL_MAC").MustString(),
common.VtapState(vtap.Get("STATE").MustInt()),
groupMaxSize,
fmt.Sprintf("%v", common.VtapState(vtap.Get("STATE").MustInt())),
vtap.Get("VTAP_GROUP_NAME").MustString(),
strings.Join(exceptionStrings, ","),
)
vtap.Get("REVISION").MustString(),
vtap.Get("UPGRADE_REVISION").MustString(),
})
}
t.AppendBulk(tableItems)
t.Render()
}
}

Expand Down
7 changes: 5 additions & 2 deletions cli/ctl/common/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func New() *Table {
}
}

const indent = 2

func (t *Table) SetHeader(keys []string) {
t.colSize = len(keys)
t.colSize = len(keys) + indent
for i, v := range keys {
t.parseDimension(v, i)
}
Expand All @@ -58,7 +60,8 @@ func (t *Table) SetHeader(keys []string) {

func (t *Table) parseDimension(str string, colKey int) {
maxWidth := 0
if w := displayWidth(str); w > maxWidth {
w := displayWidth(str) + indent
if w > maxWidth {
maxWidth = w
}

Expand Down
40 changes: 16 additions & 24 deletions cli/ctl/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/deepflowio/deepflow/cli/ctl/common"
"github.com/deepflowio/deepflow/cli/ctl/common/table"
"github.com/deepflowio/deepflow/cli/ctl/example"
)

Expand Down Expand Up @@ -125,25 +126,10 @@ func listDomain(cmd *cobra.Command, args []string, output string) {
yData, _ := yaml.JSONToYAML(jData)
fmt.Printf(string(yData))
} else {
nameMaxSize := len("NAME")
idMaxSize := len("ID")
for i := range response.Get("DATA").MustArray() {
d := response.Get("DATA").GetIndex(i)
nameSize := len(d.Get("NAME").MustString())
if nameSize > nameMaxSize {
nameMaxSize = nameSize
}
idSize := len(d.Get("CLUSTER_ID").MustString())
if idSize > idMaxSize {
idMaxSize = idSize
}
}
format := "%-*s %-*s %-37s %-17s %-15s %-22s %-22s %-8s %-10s %s\n"
header := fmt.Sprintf(
format, nameMaxSize, "NAME", idMaxSize, "ID", "LCUUID", "TYPE", "CONTROLLER_IP",
"CREATED_AT", "SYNCED_AT", "ENABLED", "STATE", "AGENT_WATCH_K8S", // TODO translate state to readable word
)
fmt.Fprint(os.Stderr, header)
t := table.New()
t.SetHeader([]string{"NAME", "ID", "LCUUID", "TYPE", "CONTROLLER_IP", "CREATED_AT", "SYNCED_AT", "ENABLED", "STATE", "AGENT_WATCH_K8S"})
tableItems := [][]string{}

for i := range response.Get("DATA").MustArray() {
d := response.Get("DATA").GetIndex(i)
name := d.Get("NAME").MustString()
Expand All @@ -153,14 +139,20 @@ func listDomain(cmd *cobra.Command, args []string, output string) {
nameChineseCount += 1
}
}
fmt.Printf(
format, nameMaxSize-nameChineseCount, name, idMaxSize, d.Get("CLUSTER_ID").MustString(), d.Get("LCUUID").MustString(),
common.DomainType(d.Get("TYPE").MustInt()), d.Get("CONTROLLER_IP").MustString(),
tableItems = append(tableItems, []string{
name,
d.Get("CLUSTER_ID").MustString(),
d.Get("LCUUID").MustString(),
fmt.Sprintf("%v", common.DomainType(d.Get("TYPE").MustInt())),
d.Get("CONTROLLER_IP").MustString(),
d.Get("CREATED_AT").MustString(), d.Get("SYNCED_AT").MustString(),
common.DomainEnabled(d.Get("ENABLED").MustInt()), common.DomainState(d.Get("STATE").MustInt()),
fmt.Sprintf("%v", common.DomainEnabled(d.Get("ENABLED").MustInt())),
fmt.Sprintf("%v", common.DomainState(d.Get("STATE").MustInt())),
d.Get("VTAP_NAME").MustString(),
)
})
}
t.AppendBulk(tableItems)
t.Render()
}
}

Expand Down
32 changes: 28 additions & 4 deletions server/controller/http/service/vtap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func GetVtaps(filter map[string]interface{}) (resp []model.Vtap, err error) {
var vtapGroups []mysql.VTapGroup
var regions []mysql.Region
var azs []mysql.AZ
var vtapRepos []mysql.VTapRepo

Db := mysql.Db
for _, param := range []string{
Expand All @@ -65,10 +66,21 @@ func GetVtaps(filter map[string]interface{}) (resp []model.Vtap, err error) {
Db = Db.Where("name IN (?)", filter["names"].([]string))
}
}
Db.Find(&vtaps)
mysql.Db.Find(&vtapGroups)
mysql.Db.Find(&regions)
mysql.Db.Find(&azs)
if err := Db.Find(&vtaps).Error; err != nil {
return nil, err
}
if err := mysql.Db.Find(&vtapGroups).Error; err != nil {
return nil, err
}
if err := mysql.Db.Find(&regions).Error; err != nil {
return nil, err
}
if err := mysql.Db.Find(&azs).Error; err != nil {
return nil, err
}
if err := mysql.Db.Select("name", "branch", "rev_count").Find(&vtapRepos).Error; err != nil {
return nil, err
}

lcuuidToRegion := make(map[string]string)
for _, region := range regions {
Expand All @@ -87,6 +99,11 @@ func GetVtaps(filter map[string]interface{}) (resp []model.Vtap, err error) {
lcuuidToGroup[group.Lcuuid] = group.Name
}

vtapRepoNameToRevision := make(map[string]string, len(vtapRepos))
for _, item := range vtapRepos {
vtapRepoNameToRevision[item.Name] = fmt.Sprintf("%s %d", item.Branch, item.RevCount)
}

for _, vtap := range vtaps {
vtapResp := model.Vtap{
ID: vtap.ID,
Expand Down Expand Up @@ -130,6 +147,13 @@ func GetVtaps(filter map[string]interface{}) (resp []model.Vtap, err error) {
}
vtapResp.Revision = revision
vtapResp.CompleteRevision = completeRevision
if vtap.UpgradePackage != "" {
if upgradeRevision, ok := vtapRepoNameToRevision[vtap.UpgradePackage]; ok {
vtapResp.UpgradeRevision = upgradeRevision
} else {
log.Errorf("vtap upgrade package(%v) cannot assoicated with vtap repo", vtap.UpgradePackage)
}
}
// exceptions
exceptions := vtap.Exceptions
bitNum := 0
Expand Down
1 change: 1 addition & 0 deletions server/controller/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type Vtap struct {
SyncedAnalyzerAt string `json:"SYNCED_ANALYZER_AT"`
BootTime int `json:"BOOT_TIME"`
Revision string `json:"REVISION"`
UpgradeRevision string `json:"UPGRADE_REVISION"`
CompleteRevision string `json:"COMPLETE_REVISION"`
Exceptions []int64 `json:"EXCEPTIONS"`
VtapGroupLcuuid string `json:"VTAP_GROUP_LCUUID"`
Expand Down
Loading