-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from 90poe/issue-12/connectors-status-cmd
feat: Connectors status cmd
- Loading branch information
Showing
8 changed files
with
290 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## connectctl connectors status | ||
|
||
Status of connectors | ||
|
||
### Synopsis | ||
|
||
|
||
Display status of selected connectors. | ||
If some tasks or connectors are failing, command will exit with code 1. | ||
|
||
|
||
``` | ||
connectctl connectors status [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for add | ||
-c, --clusterURL the url of the kafka connect cluster | ||
-n, --connectors the names of the connectors. Multiple connector names | ||
can be specified either by comma separating conn1,conn2 | ||
or by repeating the flag --n conn1 --n conn2. If no name is | ||
supplied status of ALL connectors will be displayed. | ||
-o, --output specify the output format (valid options: json, table) (default "json") | ||
-q, --quiet disable output logging | ||
``` | ||
### Options inherited from parent commands | ||
|
||
``` | ||
-l, --loglevel loglevel Specify the loglevel for the program (default info) | ||
--logfile Specify a file to output logs to | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [connectctl connectors](connectctl_connectors.md) - Manage connectors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package connectors | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/90poe/connectctl/internal/ctl" | ||
"github.com/90poe/connectctl/internal/version" | ||
"github.com/90poe/connectctl/pkg/client/connect" | ||
"github.com/90poe/connectctl/pkg/manager" | ||
"github.com/jedib0t/go-pretty/table" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type connectorsStatusCmdParams struct { | ||
ClusterURL string | ||
Connectors []string | ||
Output string | ||
Quiet bool | ||
} | ||
|
||
func connectorsStatusCmd() *cobra.Command { | ||
params := &connectorsStatusCmdParams{} | ||
|
||
statusCmd := &cobra.Command{ | ||
Use: "status", | ||
Short: "Get status for connectors in a cluster", | ||
Long: "", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
return doConnectorsStatus(cmd, params) | ||
}, | ||
} | ||
|
||
ctl.AddCommonConnectorsFlags(statusCmd, ¶ms.ClusterURL) | ||
ctl.AddConnectorNamesFlags(statusCmd, ¶ms.Connectors) | ||
ctl.AddOutputFlags(statusCmd, ¶ms.Output) | ||
ctl.AddQuietFlag(statusCmd, ¶ms.Quiet) | ||
|
||
return statusCmd | ||
} | ||
|
||
func doConnectorsStatus(_ *cobra.Command, params *connectorsStatusCmdParams) error { | ||
config := &manager.Config{ | ||
ClusterURL: params.ClusterURL, | ||
Version: version.Version, | ||
} | ||
|
||
userAgent := fmt.Sprintf("90poe.io/connectctl/%s", version.Version) | ||
|
||
client, err := connect.NewClient(params.ClusterURL, connect.WithUserAgent(userAgent)) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating connect client") | ||
} | ||
|
||
mngr, err := manager.NewConnectorsManager(client, config) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating connectors manager") | ||
} | ||
|
||
statusList, err := mngr.Status(params.Connectors) | ||
if err != nil { | ||
return errors.Wrap(err, "error getting connectors status") | ||
} | ||
|
||
if !params.Quiet { | ||
switch params.Output { | ||
case "json": | ||
if err = printAsJSON(statusList); err != nil { | ||
return errors.Wrap(err, "error printing connectors status as JSON") | ||
} | ||
|
||
case "table": | ||
printAsTable(statusList) | ||
|
||
default: | ||
return fmt.Errorf("invalid output format specified: %s", params.Output) | ||
} | ||
} | ||
|
||
failingConnectors, failingTasks := countFailing(statusList) | ||
|
||
if failingConnectors != 0 || failingTasks != 0 { | ||
return fmt.Errorf("%d connectors are failng, %d tasks are failing", failingConnectors, failingTasks) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func countFailing(statusList []*connect.ConnectorStatus) (int, int) { | ||
connectorCount := 0 | ||
taskCount := 0 | ||
|
||
for _, status := range statusList { | ||
if status.Connector.State == "FAILED" { | ||
connectorCount++ | ||
} | ||
|
||
taskCount += countFailingTasks(&status.Tasks) | ||
} | ||
|
||
return connectorCount, taskCount | ||
} | ||
|
||
func countFailingTasks(tasks *[]connect.TaskState) int { | ||
count := 0 | ||
|
||
for _, task := range *tasks { | ||
if task.State == "FAILED" { | ||
count++ | ||
} | ||
} | ||
|
||
return count | ||
} | ||
|
||
func printAsJSON(statusList []*connect.ConnectorStatus) error { | ||
b, err := json.MarshalIndent(statusList, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
os.Stdout.Write(b) | ||
return nil | ||
} | ||
|
||
func printAsTable(statusList []*connect.ConnectorStatus) { | ||
t := table.NewWriter() | ||
t.SetOutputMirror(os.Stdout) | ||
t.AppendHeader(table.Row{"Name", "State", "WorkerId", "Tasks"}) | ||
|
||
for _, status := range statusList { | ||
tasks := "" | ||
for _, task := range status.Tasks { | ||
tasks += fmt.Sprintf("%d(%s): %s\n", task.ID, task.WorkerID, task.State) | ||
} | ||
|
||
t.AppendRow(table.Row{ | ||
status.Name, | ||
status.Connector.State, | ||
status.Connector.WorkerID, | ||
tasks, | ||
}) | ||
} | ||
|
||
t.Render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package connectors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/90poe/connectctl/pkg/client/connect" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCountFailing(t *testing.T) { | ||
statusList := []*connect.ConnectorStatus{ | ||
{ | ||
Connector: connect.ConnectorState{ | ||
State: "RUNNING", | ||
}, | ||
Tasks: []connect.TaskState{ | ||
{ | ||
State: "RUNNING", | ||
}, | ||
{ | ||
State: "FAILED", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Connector: connect.ConnectorState{ | ||
State: "FAILED", | ||
}, | ||
Tasks: []connect.TaskState{ | ||
{ | ||
State: "FAILED", | ||
}, | ||
{ | ||
State: "FAILED", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
connectorsFailing, tasksFailing := countFailing(statusList) | ||
|
||
require.Equal(t, 1, connectorsFailing) | ||
require.Equal(t, 3, tasksFailing) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package manager | ||
|
||
import ( | ||
"github.com/90poe/connectctl/pkg/client/connect" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Status - gets status of specified (or all) connectors | ||
func (c *ConnectorManager) Status(connectors []string) ([]*connect.ConnectorStatus, error) { | ||
if len(connectors) == 0 { | ||
return c.allConnectorsStatus() | ||
} | ||
|
||
return c.specifiedConnectorsStatus(connectors) | ||
} | ||
|
||
func (c *ConnectorManager) allConnectorsStatus() ([]*connect.ConnectorStatus, error) { | ||
existing, _, err := c.client.ListConnectors() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error listing connectors") | ||
} | ||
|
||
return c.specifiedConnectorsStatus(existing) | ||
} | ||
|
||
func (c *ConnectorManager) specifiedConnectorsStatus(connectors []string) ([]*connect.ConnectorStatus, error) { | ||
statusList := make([]*connect.ConnectorStatus, len(connectors)) | ||
|
||
for idx, connectorName := range connectors { | ||
status, _, err := c.client.GetConnectorStatus(connectorName) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "error getting connector status for %s", connectorName) | ||
} | ||
|
||
statusList[idx] = status | ||
} | ||
|
||
return statusList, nil | ||
} |