Skip to content

Commit

Permalink
add app channel address flag for run (#1283)
Browse files Browse the repository at this point in the history
Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
  • Loading branch information
mukundansundar authored May 31, 2023
1 parent c65d816 commit c3f08b0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 35 deletions.
27 changes: 17 additions & 10 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var (
enableAPILogging bool
apiListenAddresses string
runFilePath string
appChannelAddress string
)

const (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}

Expand Down
20 changes: 11 additions & 9 deletions pkg/runexec/runexec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
})

Expand Down
21 changes: 11 additions & 10 deletions pkg/standalone/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 36 additions & 6 deletions tests/e2e/standalone/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"testing"

"github.com/dapr/go-sdk/service/common"
Expand All @@ -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{
Expand All @@ -43,7 +43,6 @@ func TestStandaloneInvoke(t *testing.T) {

assert.NoError(t, err, "unable to AddTopicEventHandler")

defer s.Stop()
go func() {
err = s.Start()

Expand All @@ -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() {
Expand Down Expand Up @@ -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")
}
3 changes: 3 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c3f08b0

Please sign in to comment.