From 25ed8588b4f5926bde68ca0a05a4442dcde9ba17 Mon Sep 17 00:00:00 2001 From: Paulo Sousa Date: Wed, 6 Apr 2022 16:07:16 -0300 Subject: [PATCH] events: add new conditions flag to event block cmd --- go.mod | 2 +- go.sum | 7 +++++-- tsuru/admin/event.go | 30 +++++++++++++++++++++++------- tsuru/admin/event_test.go | 21 +++++++++++---------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index ce81c9c2a..05850b550 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa github.com/tsuru/go-tsuruclient v0.0.0-20211213213525-0d2868229cfd github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6 - github.com/tsuru/tsuru v0.0.0-20211213152908-6331694da402 + github.com/tsuru/tsuru v0.0.0-20220406184024-f7b0d1d0a8e7 golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 0f6ef9a5a..6220eb6c9 100644 --- a/go.sum +++ b/go.sum @@ -284,6 +284,7 @@ github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1S github.com/d2g/dhcp4client v1.0.0/go.mod h1:j0hNfjhrt2SxUOw55nL0ATM/z4Yt3t2Kd1mW34z5W5s= github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5/go.mod h1:Eo87+Kg/IX2hfWJfwxMzLyuSZyxSoAug2nGa1G2QAi8= github.com/d2g/hardwareaddr v0.0.0-20190221164911-e7d9fbe030e4/go.mod h1:bMl4RjIciD2oAxI7DmWRx6gbeqrkoLqv3MV0vzNad+I= +github.com/danieljoos/wincred v1.1.0/go.mod h1:XYlo+eRTsVA9aHGp7NGjFkPla4m+DCL7hqDjlFjiygg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -296,6 +297,7 @@ github.com/digitalocean/godo v0.0.0-20170404195252-dfa802149cae/go.mod h1:h6faOI github.com/digitalocean/godo v1.1.1 h1:v0A7yF3xmKLjjdJGIeBbINfMufcrrRhqZsxuVQMoT+U= github.com/digitalocean/godo v1.1.1/go.mod h1:h6faOIcZ8lWIwNQ+DN7b3CgX4Kwby5T+nbpNqkUIozU= github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E= +github.com/docker/cli v20.10.12+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY= github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= @@ -303,6 +305,7 @@ github.com/docker/docker v1.5.0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsvi github.com/docker/docker v20.10.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker v20.10.8+incompatible h1:RVqD337BgQicVCzYrrlhLDWhq6OAD2PJDUg2LsEUvKM= github.com/docker/docker v20.10.8+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/docker-credential-helpers v0.6.4/go.mod h1:ofX3UI0Gz1TteYBjtgs07O36Pyasyp66D2uKT7H8W1c= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-events v0.0.0-20170721190031-9461782956ad/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA= @@ -849,8 +852,8 @@ github.com/tsuru/tablecli v0.0.0-20180215113938-82de88f75181/go.mod h1:ztYpOhW+u github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6 h1:1XDdWFAjIbCSG1OjN9v9KdWhuM8UtYlFcfHe/Ldkchk= github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6/go.mod h1:ztYpOhW+u1k21FEqp7nZNgpWbr0dUKok5lgGCZi+1AQ= github.com/tsuru/tsuru v0.0.0-20180820205921-0e7f7f02eac5/go.mod h1:8EIA5MXZUPRxQR1lf8kQkcJVss0q+lv0L8f2nb23siQ= -github.com/tsuru/tsuru v0.0.0-20211213152908-6331694da402 h1:SUHSv8suT6m9l+tPaktoogOdTWu3svq8RBdjM8qMEWI= -github.com/tsuru/tsuru v0.0.0-20211213152908-6331694da402/go.mod h1:6z64Wfmx86iZps0qyPVcBC1DYWcS3Runfq8CiI0F9Lc= +github.com/tsuru/tsuru v0.0.0-20220406184024-f7b0d1d0a8e7 h1:e9zIv5TodVwfA2jYxTYjJ3BDFfwn1qP6ssroJ7h0rWc= +github.com/tsuru/tsuru v0.0.0-20220406184024-f7b0d1d0a8e7/go.mod h1:PnBG5PgupAP693OhFm8eU1N8F6ClVltTmCQqj1mkzms= github.com/uber/jaeger-client-go v2.25.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= github.com/uber/jaeger-client-go v2.25.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U= diff --git a/tsuru/admin/event.go b/tsuru/admin/event.go index 8f355faf5..b3c33a57b 100644 --- a/tsuru/admin/event.go +++ b/tsuru/admin/event.go @@ -63,7 +63,7 @@ func (c *EventBlockList) Run(context *cmd.Context, client *cmd.Client) error { } } tbl := tablecli.NewTable() - tbl.Headers = tablecli.Row{"ID", "Start (duration)", "Kind", "Owner", "Target (Type: Value)", "Reason"} + tbl.Headers = tablecli.Row{"ID", "Start (duration)", "Kind", "Owner", "Target (Type: Value)", "Conditions", "Reason"} for _, b := range blocks { var duration *time.Duration if !b.EndTime.IsZero() { @@ -75,7 +75,8 @@ func (c *EventBlockList) Run(context *cmd.Context, client *cmd.Client) error { owner := valueOrWildcard(b.OwnerName) targetType := valueOrWildcard(string(b.Target.Type)) targetValue := valueOrWildcard(b.Target.Value) - row := tablecli.Row{b.ID.Hex(), ts, kind, owner, fmt.Sprintf("%s: %s", targetType, targetValue), b.Reason} + conditions := mapValueOrWildcard(b.Conditions) + row := tablecli.Row{b.ID.Hex(), ts, kind, owner, fmt.Sprintf("%s: %s", targetType, targetValue), conditions, b.Reason} color := "yellow" if !b.Active { color = "white" @@ -96,18 +97,30 @@ func valueOrWildcard(str string) string { return str } +func mapValueOrWildcard(m map[string]string) string { + if len(m) == 0 { + return "all" + } + var s string + for k, v := range m { + s += fmt.Sprintf("%s=%s ", k, v) + } + return s +} + type EventBlockAdd struct { fs *gnuflag.FlagSet kind string owner string targetType string targetValue string + conditions cmd.MapFlag } func (c *EventBlockAdd) Info() *cmd.Info { return &cmd.Info{ Name: "event-block-add", - Usage: "event block add [-k/--kind kindName] [-o/--owner ownerName] [-t/--target targetType] [-v/--value targetValue]", + Usage: "event block add [-k/--kind kindName] [-o/--owner ownerName] [-t/--target targetType] [-v/--value targetValue] [-c/--conditions name=value]...", Desc: "Block events.", MinArgs: 1, } @@ -124,6 +137,8 @@ func (c *EventBlockAdd) Flags() *gnuflag.FlagSet { c.fs.StringVar(&c.targetType, "t", "", "Block events with this target type.") c.fs.StringVar(&c.targetValue, "value", "", "Block events with this target value.") c.fs.StringVar(&c.targetValue, "v", "", "Block events with this target value.") + c.fs.Var(&c.conditions, "conditions", "Conditions to apply on event kind to be blocked.") + c.fs.Var(&c.conditions, "c", "Conditions to apply on event kind to be blocked.") } return c.fs } @@ -144,10 +159,11 @@ func (c *EventBlockAdd) Run(context *cmd.Context, client *cmd.Client) error { } target.Value = c.targetValue block := event.Block{ - Reason: context.Args[0], - KindName: c.kind, - OwnerName: c.owner, - Target: target, + Reason: context.Args[0], + KindName: c.kind, + OwnerName: c.owner, + Target: target, + Conditions: c.conditions, } body, err := json.Marshal(block) diff --git a/tsuru/admin/event_test.go b/tsuru/admin/event_test.go index 7ab9587c5..beafeda3a 100644 --- a/tsuru/admin/event_test.go +++ b/tsuru/admin/event_test.go @@ -48,6 +48,7 @@ func (s *S) TestEventBlockList(c *check.C) { "Value": "" }, "Reason": "Maintenance.", + "Conditions": {"pool": "pool1", "cluster": "cluster1"}, "Active": false }]` trans := &cmdtest.ConditionalTransport{ @@ -60,12 +61,12 @@ func (s *S) TestEventBlockList(c *check.C) { command := EventBlockList{} err := command.Run(&context, client) c.Assert(err, check.IsNil) - expected := `+--------------------------+-----------------------------+------------+----------------+----------------------+--------------+ -| ID | Start (duration) | Kind | Owner | Target (Type: Value) | Reason | -+--------------------------+-----------------------------+------------+----------------+----------------------+--------------+ -| 58c6db0b0640fd2fec413cc6 | 13 Mar 17 12:46 CDT (…) | app.create | user@email.com | all: all | Problems | -| 58c1d29ac47369e95c5520c8 | 13 Mar 17 11:43 CDT (44:15) | app.deploy | all | all: all | Maintenance. | -+--------------------------+-----------------------------+------------+----------------+----------------------+--------------+ + expected := `+--------------------------+-----------------------------+------------+----------------+----------------------+------------------------------+--------------+ +| ID | Start (duration) | Kind | Owner | Target (Type: Value) | Conditions | Reason | ++--------------------------+-----------------------------+------------+----------------+----------------------+------------------------------+--------------+ +| 58c6db0b0640fd2fec413cc6 | 13 Mar 17 12:46 CDT (…) | app.create | user@email.com | all: all | all | Problems | +| 58c1d29ac47369e95c5520c8 | 13 Mar 17 11:43 CDT (44:15) | app.deploy | all | all: all | pool=pool1 cluster=cluster1 | Maintenance. | ++--------------------------+-----------------------------+------------+----------------+----------------------+------------------------------+--------------+ ` c.Assert(stdout.String(), check.Equals, expected) } @@ -89,10 +90,10 @@ func (s *S) TestEventBlockListNoEvents(c *check.C) { command := EventBlockList{} err := command.Run(&context, client) c.Assert(err, check.IsNil) - expected := `+----+------------------+------+-------+----------------------+--------+ -| ID | Start (duration) | Kind | Owner | Target (Type: Value) | Reason | -+----+------------------+------+-------+----------------------+--------+ -+----+------------------+------+-------+----------------------+--------+ + expected := `+----+------------------+------+-------+----------------------+------------+--------+ +| ID | Start (duration) | Kind | Owner | Target (Type: Value) | Conditions | Reason | ++----+------------------+------+-------+----------------------+------------+--------+ ++----+------------------+------+-------+----------------------+------------+--------+ ` c.Assert(stdout.String(), check.Equals, expected) }