From c3f08b067999655ffb871f94160b96192c47e0dc Mon Sep 17 00:00:00 2001 From: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> Date: Wed, 31 May 2023 17:51:02 +0530 Subject: [PATCH] add app channel address flag for run (#1283) Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> --- cmd/run.go | 27 ++++++++++++------- pkg/runexec/runexec_test.go | 20 +++++++------- pkg/standalone/run.go | 21 ++++++++------- tests/e2e/standalone/invoke_test.go | 42 ++++++++++++++++++++++++----- utils/utils.go | 3 +++ 5 files changed, 78 insertions(+), 35 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 10cda7619..f664a7eef 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -63,6 +63,7 @@ var ( enableAPILogging bool apiListenAddresses string runFilePath string + appChannelAddress string ) const ( @@ -92,6 +93,10 @@ dapr run --app-id myapp # Run a gRPC application written in Go (listening on port 3000) dapr run --app-id myapp --app-port 3000 --app-protocol grpc -- go run main.go +# Run a gRPC application written in Go (listening on port 3000) with a different app channel address +dapr run --app-id myapp --app-port 3000 --app-channel-address localhost --app-protocol grpc -- go run main.go + + # Run sidecar only specifying dapr runtime installation directory dapr run --app-id myapp --runtime-path /usr/local/dapr @@ -174,16 +179,17 @@ dapr run --run-file /path/to/directory DaprdInstallPath: daprRuntimePath, } output, err := runExec.NewOutput(&standalone.RunConfig{ - AppID: appID, - AppPort: appPort, - HTTPPort: port, - GRPCPort: grpcPort, - ProfilePort: profilePort, - Command: args, - MetricsPort: metricsPort, - UnixDomainSocket: unixDomainSocket, - InternalGRPCPort: internalGRPCPort, - SharedRunConfig: *sharedRunConfig, + AppID: appID, + AppChannelAddress: appChannelAddress, + AppPort: appPort, + HTTPPort: port, + GRPCPort: grpcPort, + ProfilePort: profilePort, + Command: args, + MetricsPort: metricsPort, + UnixDomainSocket: unixDomainSocket, + InternalGRPCPort: internalGRPCPort, + SharedRunConfig: *sharedRunConfig, }) if err != nil { print.FailureStatusEvent(os.Stderr, err.Error()) @@ -457,6 +463,7 @@ func init() { RunCmd.Flags().BoolVar(&enableAPILogging, "enable-api-logging", false, "Log API calls at INFO verbosity. Valid values are: true or false") RunCmd.Flags().StringVar(&apiListenAddresses, "dapr-listen-addresses", "", "Comma separated list of IP addresses that sidecar will listen to") RunCmd.Flags().StringVarP(&runFilePath, "run-file", "f", "", "Path to the run template file for the list of apps to run") + RunCmd.Flags().StringVarP(&appChannelAddress, "app-channel-address", "", utils.DefaultAppChannelAddress, "The network address the application listens on") RootCmd.AddCommand(RunCmd) } diff --git a/pkg/runexec/runexec_test.go b/pkg/runexec/runexec_test.go index 6dd7f886f..377124e62 100644 --- a/pkg/runexec/runexec_test.go +++ b/pkg/runexec/runexec_test.go @@ -186,15 +186,16 @@ func TestRun(t *testing.T) { APIListenAddresses: "127.0.0.1", } basicConfig := &standalone.RunConfig{ - AppID: "MyID", - AppPort: 3000, - HTTPPort: 8000, - GRPCPort: 50001, - Command: []string{"MyCommand", "--my-arg"}, - ProfilePort: 9090, - MetricsPort: 9001, - InternalGRPCPort: 5050, - SharedRunConfig: *sharedRunConfig, + AppID: "MyID", + AppPort: 3000, + HTTPPort: 8000, + GRPCPort: 50001, + Command: []string{"MyCommand", "--my-arg"}, + ProfilePort: 9090, + MetricsPort: 9001, + InternalGRPCPort: 5050, + AppChannelAddress: "localhost", + SharedRunConfig: *sharedRunConfig, } t.Run("run happy http", func(t *testing.T) { @@ -204,6 +205,7 @@ func TestRun(t *testing.T) { assertCommonArgs(t, basicConfig, output) assert.Equal(t, "MyCommand", output.AppCMD.Args[0]) assert.Equal(t, "--my-arg", output.AppCMD.Args[1]) + assertArgumentEqual(t, "app-channel-address", "localhost", output.DaprCMD.Args) assertAppEnv(t, basicConfig, output) }) diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index 8b353f670..2e6d1ccd3 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -46,16 +46,17 @@ const ( // RunConfig represents the application configuration parameters. type RunConfig struct { - SharedRunConfig `yaml:",inline"` - AppID string `env:"APP_ID" arg:"app-id" yaml:"appID"` - AppPort int `env:"APP_PORT" arg:"app-port" yaml:"appPort" default:"-1"` - HTTPPort int `env:"DAPR_HTTP_PORT" arg:"dapr-http-port" yaml:"daprHTTPPort" default:"-1"` - GRPCPort int `env:"DAPR_GRPC_PORT" arg:"dapr-grpc-port" yaml:"daprGRPCPort" default:"-1"` - ProfilePort int `arg:"profile-port" yaml:"profilePort" default:"-1"` - Command []string `yaml:"command"` - MetricsPort int `env:"DAPR_METRICS_PORT" arg:"metrics-port" yaml:"metricsPort" default:"-1"` - UnixDomainSocket string `arg:"unix-domain-socket" yaml:"unixDomainSocket"` - InternalGRPCPort int `arg:"dapr-internal-grpc-port" yaml:"daprInternalGRPCPort" default:"-1"` + SharedRunConfig `yaml:",inline"` + AppID string `env:"APP_ID" arg:"app-id" yaml:"appID"` + AppChannelAddress string `env:"APP_CHANNEL_ADDRESS" arg:"app-channel-address" ifneq:"127.0.0.1" yaml:"appChannelAddress"` + AppPort int `env:"APP_PORT" arg:"app-port" yaml:"appPort" default:"-1"` + HTTPPort int `env:"DAPR_HTTP_PORT" arg:"dapr-http-port" yaml:"daprHTTPPort" default:"-1"` + GRPCPort int `env:"DAPR_GRPC_PORT" arg:"dapr-grpc-port" yaml:"daprGRPCPort" default:"-1"` + ProfilePort int `arg:"profile-port" yaml:"profilePort" default:"-1"` + Command []string `yaml:"command"` + MetricsPort int `env:"DAPR_METRICS_PORT" arg:"metrics-port" yaml:"metricsPort" default:"-1"` + UnixDomainSocket string `arg:"unix-domain-socket" yaml:"unixDomainSocket"` + InternalGRPCPort int `arg:"dapr-internal-grpc-port" yaml:"daprInternalGRPCPort" default:"-1"` } // SharedRunConfig represents the application configuration parameters, which can be shared across many apps. diff --git a/tests/e2e/standalone/invoke_test.go b/tests/e2e/standalone/invoke_test.go index 5c829e40d..812d0ea4b 100644 --- a/tests/e2e/standalone/invoke_test.go +++ b/tests/e2e/standalone/invoke_test.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "net/http" + "strconv" "testing" "github.com/dapr/go-sdk/service/common" @@ -28,9 +29,8 @@ import ( "github.com/stretchr/testify/require" ) -func TestStandaloneInvoke(t *testing.T) { - ensureDaprInstallation(t) - s := daprHttp.NewService(":9987") +func StartTestService(t *testing.T, port int) common.Service { + s := daprHttp.NewService(":" + strconv.Itoa(port)) err := s.AddServiceInvocationHandler("/test", func(ctx context.Context, e *common.InvocationEvent) (*common.Content, error) { val := &common.Content{ @@ -43,7 +43,6 @@ func TestStandaloneInvoke(t *testing.T) { assert.NoError(t, err, "unable to AddTopicEventHandler") - defer s.Stop() go func() { err = s.Start() @@ -52,8 +51,16 @@ func TestStandaloneInvoke(t *testing.T) { err = nil } - assert.NoError(t, err, "unable to listen on :9987") + assert.NoError(t, err, "unable to listen on :%d", port) }() + return s +} + +func TestStandaloneInvoke(t *testing.T) { + port := 9987 + ensureDaprInstallation(t) + s := StartTestService(t, port) + defer s.Stop() for _, path := range getSocketCases() { executeAgainstRunningDapr(t, func() { @@ -105,6 +112,29 @@ func TestStandaloneInvoke(t *testing.T) { t.Log(output) require.NoError(t, err, "dapr stop failed") assert.Contains(t, output, "app stopped successfully: invoke_e2e") - }, "run", "--app-id", "invoke_e2e", "--app-port", "9987", "--unix-domain-socket", path) + }, "run", "--app-id", "invoke_e2e", "--app-port", strconv.Itoa(port), "--unix-domain-socket", path) } } + +func TestStandaloneInvokeWithAppChannel(t *testing.T) { + port := 9988 + ensureDaprInstallation(t) + s := StartTestService(t, port) + defer s.Stop() + + executeAgainstRunningDapr(t, func() { + t.Run(fmt.Sprintf("data from file with app channel address set to localhost"), func(t *testing.T) { + // empty unix domain socket path + output, err := cmdInvoke("invoke_e2e_app_channel", "test", "", "--data-file", "../testdata/message.json") + t.Log(output) + assert.NoError(t, err, "unable to invoke with --data-file") + assert.Contains(t, output, "App invoked successfully") + assert.Contains(t, output, "{\"dapr\": \"is_great\"}") + }) + + output, err := cmdStopWithAppID("invoke_e2e_app_channel") + t.Log(output) + require.NoError(t, err, "dapr stop failed") + assert.Contains(t, output, "app stopped successfully: invoke_e2e_app_channel") + }, "run", "--app-id", "invoke_e2e_app_channel", "--app-port", strconv.Itoa(port), "--app-channel-address", "localhost") +} diff --git a/utils/utils.go b/utils/utils.go index 262c3f674..7230257cc 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -48,6 +48,9 @@ const ( windowsOsType = "windows" homeDirPrefix = "~/" + + // DefaultAppChannelAddress is the default local network address that user application listen on. + DefaultAppChannelAddress = "127.0.0.1" ) // IsValidContainerRuntime checks if the input is a valid container runtime.