-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agentctl): Add config.resync subcommand (with resync) (#1642)
* Improve json encoding/decoding and txn summary format - TxnType and ResyncType are now marshalled as strings (unmarshalling works for both) - Added tests for json encoding/decoding of TxnType and ResyncType Signed-off-by: Ondrej Fabry <[email protected]> * Fix comments Signed-off-by: Ondrej Fabry <[email protected]> * Ensure proto is encoded to json properly Signed-off-by: Ondrej Fabry <[email protected]> * Improve recorded transaction formatting for json Signed-off-by: Ondrej Fabry <[email protected]> * Log content length for HTTP client Signed-off-by: Ondrej Fabry <[email protected]> * Add config subcommand with resync Signed-off-by: Ondrej Fabry <[email protected]>
- Loading branch information
1 parent
ec2549c
commit 83d2423
Showing
12 changed files
with
450 additions
and
53 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
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
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,87 @@ | ||
// Copyright (c) 2019 Cisco and/or its affiliates. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types" | ||
agentcli "go.ligato.io/vpp-agent/v3/cmd/agentctl/cli" | ||
) | ||
|
||
func NewConfigCommand(cli agentcli.Cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "config", | ||
Short: "Manage agent configuration", | ||
} | ||
cmd.AddCommand( | ||
newConfigResyncCommand(cli), | ||
) | ||
return cmd | ||
} | ||
|
||
func newConfigResyncCommand(cli agentcli.Cli) *cobra.Command { | ||
var ( | ||
opts ConfigResyncOptions | ||
) | ||
cmd := &cobra.Command{ | ||
Use: "resync", | ||
Short: "Run config resync in agent", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runConfigResync(cli, opts) | ||
}, | ||
} | ||
flags := cmd.Flags() | ||
flags.StringVarP(&opts.Format, "format", "f", "", "Format output") | ||
flags.BoolVar(&opts.Verbose, "verbose", false, "Run resync in verbose mode") | ||
flags.BoolVar(&opts.Retry, "retry", false, "Run resync with retries") | ||
return cmd | ||
} | ||
|
||
type ConfigResyncOptions struct { | ||
Format string | ||
Verbose bool | ||
Retry bool | ||
} | ||
|
||
func runConfigResync(cli agentcli.Cli, opts ConfigResyncOptions) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
rectxn, err := cli.Client().SchedulerResync(ctx, types.SchedulerResyncOptions{ | ||
Retry: opts.Retry, | ||
Verbose: opts.Verbose, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
format := opts.Format | ||
if len(format) == 0 { | ||
format = defaultFormatConfigResync | ||
} | ||
|
||
if err := formatAsTemplate(cli.Out(), format, rectxn); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// TODO: define default format with go template | ||
const defaultFormatConfigResync = `json` |
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,136 @@ | ||
// Copyright (c) 2020 Cisco and/or its affiliates. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" | ||
) | ||
|
||
func TestTxnTypeEncode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
|
||
txntype api.TxnType | ||
|
||
expectOut string | ||
expectErr error | ||
}{ | ||
{"SBNotification", api.SBNotification, `"SBNotification"`, nil}, | ||
{"NBTransaction", api.NBTransaction, `"NBTransaction"`, nil}, | ||
{"RetryFailedOps", api.RetryFailedOps, `"RetryFailedOps"`, nil}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
b, err := json.Marshal(test.txntype) | ||
if err != test.expectErr { | ||
t.Fatalf("expected error: %v, got %v", test.expectErr, err) | ||
} | ||
out := string(b) | ||
if out != test.expectOut { | ||
t.Fatalf("expected output: %q, got %q", test.expectOut, out) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTxnTypeDecode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
|
||
input string | ||
|
||
expectTxnType api.TxnType | ||
expectErr error | ||
}{ | ||
{"RetryFailedOps", `"RetryFailedOps"`, api.RetryFailedOps, nil}, | ||
{"NBTransaction", `"NBTransaction"`, api.NBTransaction, nil}, | ||
{"1 (NBTransaction)", `1`, api.NBTransaction, nil}, | ||
{"0 (SBNotification)", `0`, api.SBNotification, nil}, | ||
{"invalid", `"INVALID"`, api.TxnType(-1), nil}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var txntype api.TxnType | ||
err := json.Unmarshal([]byte(test.input), &txntype) | ||
if err != test.expectErr { | ||
t.Fatalf("expected error: %v, got %v", test.expectErr, err) | ||
} | ||
if txntype != test.expectTxnType { | ||
t.Fatalf("expected TxnType: %v, got %v", test.expectTxnType, txntype) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestResyncTypeEncode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
|
||
resynctype api.ResyncType | ||
|
||
expectOut string | ||
expectErr error | ||
}{ | ||
{"FullResync", api.FullResync, `"FullResync"`, nil}, | ||
{"UpstreamResync", api.UpstreamResync, `"UpstreamResync"`, nil}, | ||
{"DownstreamResync", api.DownstreamResync, `"DownstreamResync"`, nil}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
b, err := json.Marshal(test.resynctype) | ||
if err != test.expectErr { | ||
t.Fatalf("expected error: %v, got %v", test.expectErr, err) | ||
} | ||
out := string(b) | ||
if out != test.expectOut { | ||
t.Fatalf("expected output: %q, got %q", test.expectOut, out) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestResyncTypeDecode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
|
||
input string | ||
|
||
expectResyncType api.ResyncType | ||
expectErr error | ||
}{ | ||
{"FullResync", `"FullResync"`, api.FullResync, nil}, | ||
{"UpstreamResync", `"UpstreamResync"`, api.UpstreamResync, nil}, | ||
{"DownstreamResync", `"DownstreamResync"`, api.DownstreamResync, nil}, | ||
{"1 (FullResync)", `1`, api.FullResync, nil}, | ||
{"2 (UpstreamResync)", `2`, api.UpstreamResync, nil}, | ||
{"3 (DownstreamResync)", `3`, api.DownstreamResync, nil}, | ||
{"invalid", `"INVALID"`, api.ResyncType(0), nil}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var resyncType api.ResyncType | ||
err := json.Unmarshal([]byte(test.input), &resyncType) | ||
if err != test.expectErr { | ||
t.Fatalf("expected error: %v, got %v", test.expectErr, err) | ||
} | ||
if resyncType != test.expectResyncType { | ||
t.Fatalf("expected ResyncType: %v, got %v", test.expectResyncType, resyncType) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.