From 2235653584aec0c1173675cd531dc987fa3b45e8 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 13 Mar 2020 08:26:57 +0100 Subject: gitaly-ssh: Refactor `run()` to use a command argument struct The `run()` function gained quite some of arguments and is going to be extended by another one to set up feature flags. This makes invocations of that function hard to read, especially as three of the current arguments are strings that may be confused quite easily. So let's refactor this to make use of a new struct `gitalySshCommand` and implement `run()` as a method on that struct. --- cmd/gitaly-ssh/main.go | 36 ++++++++++++++++++++++++++---------- cmd/gitaly-ssh/main_test.go | 9 ++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) (limited to 'cmd') diff --git a/cmd/gitaly-ssh/main.go b/cmd/gitaly-ssh/main.go index 4049cc47b..a6f0b984d 100644 --- a/cmd/gitaly-ssh/main.go +++ b/cmd/gitaly-ssh/main.go @@ -17,10 +17,23 @@ import ( type packFn func(_ context.Context, _ *grpc.ClientConn, _ string) (int32, error) +type gitalySSHCommand struct { + // The git packer that shall be executed. One of receivePack, + // uploadPack or uploadArchive + packer packFn + // Working directory to execute the packer in + workingDir string + // Address of the server we want to post the request to + address string + // Marshalled gRPC payload to pass to the remote server + payload string +} + // GITALY_ADDRESS="tcp://1.2.3.4:9999" or "unix:/var/run/gitaly.sock" // GITALY_TOKEN="foobar1234" // GITALY_PAYLOAD="{repo...}" // GITALY_WD="/path/to/working-directory" +// GITALY_FEATUREFLAGS="upload_pack_filter,hooks_rpc" // gitaly-ssh upload-pack func main() { // < 4 since git throws on 2x garbage here @@ -42,11 +55,14 @@ func main() { log.Fatalf("invalid pack command: %q", command) } - gitalyWorkingDir := os.Getenv("GITALY_WD") - gitalyAddress := os.Getenv("GITALY_ADDRESS") - gitalyPayload := os.Getenv("GITALY_PAYLOAD") + cmd := gitalySSHCommand{ + packer: packer, + workingDir: os.Getenv("GITALY_WD"), + address: os.Getenv("GITALY_ADDRESS"), + payload: os.Getenv("GITALY_PAYLOAD"), + } - code, err := run(packer, gitalyWorkingDir, gitalyAddress, gitalyPayload) + code, err := cmd.run() if err != nil { log.Printf("%s: %v", command, err) } @@ -54,7 +70,7 @@ func main() { os.Exit(code) } -func run(packer packFn, gitalyWorkingDir string, gitalyAddress string, gitalyPayload string) (int, error) { +func (cmd gitalySSHCommand) run() (int, error) { // Configure distributed tracing closer := tracing.Initialize(tracing.WithServiceName("gitaly-ssh")) defer closer.Close() @@ -62,19 +78,19 @@ func run(packer packFn, gitalyWorkingDir string, gitalyAddress string, gitalyPay ctx, finished := tracing.ExtractFromEnv(context.Background()) defer finished() - if gitalyWorkingDir != "" { - if err := os.Chdir(gitalyWorkingDir); err != nil { - return 1, fmt.Errorf("unable to chdir to %v", gitalyWorkingDir) + if cmd.workingDir != "" { + if err := os.Chdir(cmd.workingDir); err != nil { + return 1, fmt.Errorf("unable to chdir to %v", cmd.workingDir) } } - conn, err := getConnection(gitalyAddress) + conn, err := getConnection(cmd.address) if err != nil { return 1, err } defer conn.Close() - code, err := packer(ctx, conn, gitalyPayload) + code, err := cmd.packer(ctx, conn, cmd.payload) if err != nil { return 1, err } diff --git a/cmd/gitaly-ssh/main_test.go b/cmd/gitaly-ssh/main_test.go index d66a57a0f..7256a7e02 100644 --- a/cmd/gitaly-ssh/main_test.go +++ b/cmd/gitaly-ssh/main_test.go @@ -89,7 +89,14 @@ func TestRun(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotCode, err := run(tt.packer, tt.workingDir, tt.gitalyAddress, "{}") + cmd := gitalySSHCommand{ + packer: tt.packer, + workingDir: tt.workingDir, + address: tt.gitalyAddress, + payload: "{}", + } + + gotCode, err := cmd.run() if tt.wantErr { assert.Error(t, err) } else { -- cgit v1.2.3 From 2ddd6da2de2963cfc5414d07ea8fa8a77d75a2d5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 13 Mar 2020 09:31:39 +0100 Subject: gitaly-ssh: Allow passing feature flags via the environment It is currently not possible to set up any feature flags when using gitaly-ssh, which we require to test partial clones in our SSH upload-pack tests. In order to allow for this, let's implement support for another environment variable "GITALY_FEATUREFLAGS", which is simply a comma-separated list of flags that gitaly-ssh should set up for the requesting client. Thus, setting up a specific feature flag is as simple as doing: GITALY_FEATUREFLAG=upload_pack_filter gitaly-ssh upload-pack ... --- cmd/gitaly-ssh/main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'cmd') diff --git a/cmd/gitaly-ssh/main.go b/cmd/gitaly-ssh/main.go index a6f0b984d..5c70de194 100644 --- a/cmd/gitaly-ssh/main.go +++ b/cmd/gitaly-ssh/main.go @@ -5,10 +5,12 @@ import ( "fmt" "log" "os" + "strings" grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" "gitlab.com/gitlab-org/gitaly/client" + "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" grpccorrelation "gitlab.com/gitlab-org/labkit/correlation/grpc" "gitlab.com/gitlab-org/labkit/tracing" grpctracing "gitlab.com/gitlab-org/labkit/tracing/grpc" @@ -27,6 +29,9 @@ type gitalySSHCommand struct { address string // Marshalled gRPC payload to pass to the remote server payload string + // Comma separated list of feature flags that shall be enabled on the + // remote server + featureFlags string } // GITALY_ADDRESS="tcp://1.2.3.4:9999" or "unix:/var/run/gitaly.sock" @@ -56,10 +61,11 @@ func main() { } cmd := gitalySSHCommand{ - packer: packer, - workingDir: os.Getenv("GITALY_WD"), - address: os.Getenv("GITALY_ADDRESS"), - payload: os.Getenv("GITALY_PAYLOAD"), + packer: packer, + workingDir: os.Getenv("GITALY_WD"), + address: os.Getenv("GITALY_ADDRESS"), + payload: os.Getenv("GITALY_PAYLOAD"), + featureFlags: os.Getenv("GITALY_FEATUREFLAGS"), } code, err := cmd.run() @@ -78,6 +84,12 @@ func (cmd gitalySSHCommand) run() (int, error) { ctx, finished := tracing.ExtractFromEnv(context.Background()) defer finished() + if cmd.featureFlags != "" { + for _, flag := range strings.Split(cmd.featureFlags, ",") { + ctx = featureflag.OutgoingCtxWithFeatureFlag(ctx, flag) + } + } + if cmd.workingDir != "" { if err := os.Chdir(cmd.workingDir); err != nil { return 1, fmt.Errorf("unable to chdir to %v", cmd.workingDir) -- cgit v1.2.3