-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[vtctldclient] Add legacy shim #8284
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
22bec26
Move duplicated vtctld cert/tls flags to a common package
ea4926b
Don't show `help requested` errors as errors
1928fac
Add LegacyVtctlCommand shim to vtctldclient CLI
732a088
[vtctldclient] Update help text for legacy shim
c3fb79c
Correct copyright year and comment typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,104 @@ | ||
| /* | ||
| Copyright 2021 The Vitess Authors. | ||
|
|
||
| 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 command | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "vitess.io/vitess/go/cmd/vtctldclient/cli" | ||
| "vitess.io/vitess/go/vt/log" | ||
| "vitess.io/vitess/go/vt/logutil" | ||
| "vitess.io/vitess/go/vt/vtctl/vtctlclient" | ||
|
|
||
| logutilpb "vitess.io/vitess/go/vt/proto/logutil" | ||
| ) | ||
|
|
||
| var ( | ||
| // LegacyVtctlCommand provides a shim to make legacy ExecuteVtctlCommand | ||
| // RPCs. This allows users to use a single binary to make RPCs against both | ||
| // the new and old vtctld gRPC APIs. | ||
| LegacyVtctlCommand = &cobra.Command{ | ||
| Use: "LegacyVtctlCommand -- <command> [flags ...] [args ...]", | ||
| Short: "Invoke a legacy vtctlclient command. Flag parsing is best effort.", | ||
| Args: cobra.ArbitraryArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| cli.FinishedParsing(cmd) | ||
| return runLegacyCommand(args) | ||
| }, | ||
| Long: strings.TrimSpace(` | ||
| LegacyVtctlCommand uses the legacy vtctl grpc client to make an ExecuteVtctlCommand | ||
| rpc to a vtctld. | ||
|
|
||
| This command exists to support a smooth transition of any scripts that relied on | ||
| vtctlclient during the migration to the new vtctldclient, and will be removed, | ||
| following the Vitess project's standard deprecation cycle, once all commands | ||
| have been migrated to the new VtctldServer api. | ||
|
|
||
| To see the list of available legacy commands, run "LegacyVtctlCommand -- help". | ||
| Note that, as with the old client, this requires a running server, as the flag | ||
| parsing and help/usage text generation, is done server-side. | ||
|
|
||
| Also note that, in order to defer that flag parsing to the server side, you must | ||
| use the double-dash ("--") after the LegacyVtctlCommand subcommand string, or | ||
| the client-side flag parsing library we are using will attempt to parse those | ||
| flags (and fail). | ||
| `), | ||
| Example: strings.TrimSpace(` | ||
| LegacyVtctlCommand help # displays this help message | ||
| LegacyVtctlCommand -- help # displays help for supported legacy vtctl commands | ||
|
|
||
| # When using legacy command that take arguments, a double dash must be used | ||
| # before the first flag argument, like in the first example. The double dash may | ||
| # be used, however, at any point after the "LegacyVtctlCommand" string, as in | ||
| # the second example. | ||
| LegacyVtctlCommand AddCellInfo -- -server_address "localhost:1234" -root "/vitess/cell1" | ||
| LegacyVtctlCommand -- AddCellInfo -server_address "localhost:5678" -root "/vitess/cell1"`), | ||
| } | ||
| ) | ||
|
|
||
| func runLegacyCommand(args []string) error { | ||
| // Duplicated (mostly) from go/cmd/vtctlclient/main.go. | ||
| logger := logutil.NewConsoleLogger() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), actionTimeout) | ||
| defer cancel() | ||
|
|
||
| err := vtctlclient.RunCommandAndWait(ctx, server, args, func(e *logutilpb.Event) { | ||
| logutil.LogEvent(logger, e) | ||
| }) | ||
| if err != nil { | ||
| if strings.Contains(err.Error(), "flag: help requested") { | ||
| // Help is caught by SetHelpFunc, so we don't want to indicate this as an error. | ||
| return nil | ||
| } | ||
|
|
||
| errStr := strings.Replace(err.Error(), "remote error: ", "", -1) | ||
| fmt.Printf("%s Error: %s\n", flag.Arg(0), errStr) | ||
| log.Error(err) | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func init() { | ||
| Root.AddCommand(LegacyVtctlCommand) | ||
| } |
This file contains hidden or 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 hidden or 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,23 @@ | ||
| /* | ||
| Copyright 2021 The Vitess Authors. | ||
|
|
||
| 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 main | ||
|
|
||
| // Imports and registers the gRPC vtctl client. | ||
|
|
||
| import ( | ||
| _ "vitess.io/vitess/go/vt/vtctl/grpcvtctlclient" | ||
| ) |
This file contains hidden or 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,41 @@ | ||
| /* | ||
| Copyright 2021 The Vitess Authors. | ||
|
|
||
| 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 grpcclientcommon defines the flags shared by both grpcvtctlclient and | ||
| // grpcvtctldclient. | ||
| package grpcclientcommon | ||
|
|
||
| import ( | ||
| "flag" | ||
|
|
||
| "google.golang.org/grpc" | ||
|
|
||
| "vitess.io/vitess/go/vt/grpcclient" | ||
| ) | ||
|
|
||
| var ( | ||
| cert = flag.String("vtctld_grpc_cert", "", "the cert to use to connect") | ||
| key = flag.String("vtctld_grpc_key", "", "the key to use to connect") | ||
| ca = flag.String("vtctld_grpc_ca", "", "the server ca to use to validate servers when connecting") | ||
| name = flag.String("vtctld_grpc_server_name", "", "the server name to use to validate server certificate") | ||
| ) | ||
|
|
||
| // SecureDialOption returns a grpc.DialOption configured to use TLS (or | ||
| // insecure if no flags were set) based on the vtctld_grpc_* flags declared by | ||
| // this package. | ||
| func SecureDialOption() (grpc.DialOption, error) { | ||
| return grpcclient.SecureDialOption(*cert, *key, *ca, *name) | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This turns:
Into