Skip to content

Commit

Permalink
Merge branch 'main' into PMM-9407
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhat1595 authored Jan 8, 2024
2 parents 1fae843 + 5b1eb53 commit 275020b
Show file tree
Hide file tree
Showing 208 changed files with 1,549 additions and 513 deletions.
5 changes: 2 additions & 3 deletions .devcontainer/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def setup():
# Having fsync off in dev environment is fine.
"sed -i -e \"s/#fsync = on/fsync = off/\" /srv/postgres14/postgresql.conf",
"echo 'host all all 0.0.0.0/0 trust' >> /srv/postgres14/pg_hba.conf",
"supervisorctl restart postgresql",
# "supervisorctl restart postgresql",
])


Expand All @@ -99,8 +99,7 @@ def main():
make_init()

# do basic setup
# TODO: fix the setup and revert
# setup()
setup()


MARKER = "/tmp/devcontainer-setup-done"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
fail_on_error: true
cache: false
golangci_lint_flags: "-c=.golangci.yml"
golangci_lint_version: v1.51.2 # Version should match specified in Makefile
golangci_lint_version: v1.55.2 # Version should match specified in Makefile

- name: Run go-consistent
env:
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ linters:
- revive
- paralleltest
- gocognit
- tagalign

# ENDTODO

Expand Down
2 changes: 1 addition & 1 deletion Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ init: ## Install tools
cd tools && go generate -x -tags=tools

# Install golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin v1.51.2 # Version should match specified in CI
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin v1.55.2 # Version should match specified in CI

release: ## Build release versions of all components
make -C agent release
Expand Down
2 changes: 1 addition & 1 deletion admin/commands/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestReadFile(t *testing.T) {
err = os.Remove(cert.Name())
assert.NoError(t, err)
}()
_, err = cert.Write([]byte("cert"))
_, err = cert.WriteString("cert")
require.NoError(t, err)

certificate, err := ReadFile(cert.Name())
Expand Down
37 changes: 23 additions & 14 deletions admin/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type ListCommand struct {
NodeID string `help:"Node ID (default is autodetected)"`
}

func (cmd *ListCommand) RunCmd() (Result, error) { //nolint:cyclop,maintidx
func (cmd *ListCommand) RunCmd() (Result, error) {
if cmd.NodeID == "" {
status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo)
if err != nil {
Expand All @@ -142,6 +142,20 @@ func (cmd *ListCommand) RunCmd() (Result, error) { //nolint:cyclop,maintidx
return nil, err
}

agentsRes, err := client.Default.Agents.ListAgents(&agents.ListAgentsParams{
Context: Ctx,
})
if err != nil {
return nil, err
}

return &listResult{
Services: servicesList(servicesRes),
Agents: agentsList(agentsRes, cmd.NodeID),
}, nil
}

func servicesList(servicesRes *services.ListServicesOK) []listResultService {
getSocketOrHost := func(socket, address string, port int64) string {
if socket != "" {
return socket
Expand All @@ -152,6 +166,7 @@ func (cmd *ListCommand) RunCmd() (Result, error) { //nolint:cyclop,maintidx
l := len(servicesRes.Payload.Mysql) + len(servicesRes.Payload.Mongodb) + len(servicesRes.Payload.Postgresql)
l += len(servicesRes.Payload.Proxysql) + len(servicesRes.Payload.Haproxy) + len(servicesRes.Payload.External)
servicesList := make([]listResultService, 0, l)

for _, s := range servicesRes.Payload.Mysql {
servicesList = append(servicesList, listResultService{
ServiceType: types.ServiceTypeMySQLService,
Expand Down Expand Up @@ -200,13 +215,10 @@ func (cmd *ListCommand) RunCmd() (Result, error) { //nolint:cyclop,maintidx
})
}

agentsRes, err := client.Default.Agents.ListAgents(&agents.ListAgentsParams{
Context: Ctx,
})
if err != nil {
return nil, err
}
return servicesList
}

func agentsList(agentsRes *agents.ListAgentsOK, nodeID string) []listResultAgent { //nolint:cyclop
getStatus := func(s *string) string {
res := pointer.GetString(s)
if res == "" {
Expand All @@ -222,9 +234,9 @@ func (cmd *ListCommand) RunCmd() (Result, error) { //nolint:cyclop,maintidx
return "pull"
}
pmmAgentIDs := make(map[string]struct{})
var agentsList []listResultAgent
agentsList := []listResultAgent{}
for _, a := range agentsRes.Payload.PMMAgent {
if a.RunsOnNodeID == cmd.NodeID {
if a.RunsOnNodeID == nodeID {
pmmAgentIDs[a.AgentID] = struct{}{}

status := "disconnected"
Expand Down Expand Up @@ -370,7 +382,7 @@ func (cmd *ListCommand) RunCmd() (Result, error) { //nolint:cyclop,maintidx
}
}
for _, a := range agentsRes.Payload.ExternalExporter {
if a.RunsOnNodeID == cmd.NodeID {
if a.RunsOnNodeID == nodeID {
agentsList = append(agentsList, listResultAgent{
AgentType: types.AgentTypeExternalExporter,
AgentID: a.AgentID,
Expand All @@ -394,8 +406,5 @@ func (cmd *ListCommand) RunCmd() (Result, error) { //nolint:cyclop,maintidx
}
}

return &listResult{
Services: servicesList,
Agents: agentsList,
}, nil
return agentsList
}
101 changes: 101 additions & 0 deletions admin/commands/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
package commands

import (
"encoding/json"
"strings"
"testing"

"github.com/AlekSi/pointer"
"github.com/stretchr/testify/assert"

"github.com/percona/pmm/api/inventorypb/json/client/agents"
"github.com/percona/pmm/api/inventorypb/json/client/services"
"github.com/percona/pmm/api/inventorypb/types"
)

Expand Down Expand Up @@ -119,3 +123,100 @@ func TestNiceAgentStatus(t *testing.T) {
})
}
}

func TestListJSONOutput(t *testing.T) {
t.Parallel()
t.Run("basic", func(t *testing.T) {
t.Parallel()
services := &services.ListServicesOK{
Payload: &services.ListServicesOKBody{
Mysql: []*services.ListServicesOKBodyMysqlItems0{
{
ServiceID: "/service_id/4ff49c41-80a1-4030-bc02-cd76e3b0b84a",
ServiceName: "mysql-service",
Address: "127.0.0.1",
Port: 3306,
},
},
},
}
agents := &agents.ListAgentsOK{
Payload: &agents.ListAgentsOKBody{
PMMAgent: []*agents.ListAgentsOKBodyPMMAgentItems0{
{
AgentID: "/agent_id/8b732ac3-8256-40b0-a98b-0fd5fa9a1140",
RunsOnNodeID: "/node_id/8b732ac3-8256-40b0-a98b-0fd5fa9a1140",
Connected: true,
},
},
MysqldExporter: []*agents.ListAgentsOKBodyMysqldExporterItems0{
{
AgentID: "/agent_id/8b732ac3-8256-40b0-a98b-0fd5fa9a1198",
PMMAgentID: "/agent_id/8b732ac3-8256-40b0-a98b-0fd5fa9a1140",
ServiceID: "/service_id/4ff49c41-80a1-4030-bc02-cd76e3b0b84a",
Status: pointer.ToString("RUNNING"),
PushMetricsEnabled: false,
ListenPort: 3306,
},
},
},
}
result := listResult{
Services: servicesList(services),
Agents: agentsList(agents, "/node_id/8b732ac3-8256-40b0-a98b-0fd5fa9a1140"),
}

res, err := json.Marshal(result)
assert.NoError(t, err)
expected := `
{
"service": [
{
"service_type": "MYSQL_SERVICE",
"service_id": "/service_id/4ff49c41-80a1-4030-bc02-cd76e3b0b84a",
"service_name": "mysql-service",
"address_port": "127.0.0.1:3306",
"external_group": ""
}
],
"agent": [
{
"agent_type": "PMM_AGENT",
"agent_id": "/agent_id/8b732ac3-8256-40b0-a98b-0fd5fa9a1140",
"service_id": "",
"status": "CONNECTED",
"disabled": false,
"push_metrics_enabled": ""
},
{
"agent_type": "MYSQLD_EXPORTER",
"agent_id": "/agent_id/8b732ac3-8256-40b0-a98b-0fd5fa9a1198",
"service_id": "/service_id/4ff49c41-80a1-4030-bc02-cd76e3b0b84a",
"status": "RUNNING",
"disabled": false,
"push_metrics_enabled": "pull",
"port": 3306
}
]
}
`
expected = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(expected, "\t", ""), "\n", ""), " ", "")
assert.Equal(t, expected, string(res))
})
t.Run("empty", func(t *testing.T) {
t.Parallel()
result := listResult{
Services: servicesList(&services.ListServicesOK{
Payload: &services.ListServicesOKBody{},
}),
Agents: agentsList(&agents.ListAgentsOK{
Payload: &agents.ListAgentsOKBody{},
}, ""),
}

res, err := json.Marshal(result)
assert.NoError(t, err)
expected := `{"service":[],"agent":[]}`
assert.Equal(t, expected, string(res))
})
}
Loading

0 comments on commit 275020b

Please sign in to comment.