Skip to content

Commit

Permalink
Merge branch 'master' into feat/win_multi_app_run
Browse files Browse the repository at this point in the history
  • Loading branch information
mukundansundar authored Aug 8, 2023
2 parents f189d61 + 60f50e3 commit 759e46b
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 1 deletion.
38 changes: 38 additions & 0 deletions pkg/standalone/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ func (config *RunConfig) setDefaultFromSchemaRecursive(schema reflect.Value) {

func (config *RunConfig) getEnv() []string {
env := []string{}

// Handle values from config that have an "env" tag.
schema := reflect.ValueOf(*config)
for i := 0; i < schema.NumField(); i++ {
valueField := schema.Field(i).Interface()
Expand All @@ -365,12 +367,48 @@ func (config *RunConfig) getEnv() []string {
value := fmt.Sprintf("%v", reflect.ValueOf(valueField))
env = append(env, fmt.Sprintf("%s=%v", key, value))
}

// Handle APP_PROTOCOL separately since that requires some additional processing.
appProtocol := config.getAppProtocol()
if appProtocol != "" {
env = append(env, "APP_PROTOCOL="+appProtocol)
}

// Add user-defined env vars.
for k, v := range config.Env {
env = append(env, fmt.Sprintf("%s=%v", k, v))
}

return env
}

func (config *RunConfig) getAppProtocol() string {
appProtocol := strings.ToLower(config.AppProtocol)

switch appProtocol {
case string("grpcs"), string("https"), string("h2c"):
return appProtocol
case string("http"):
// For backwards compatibility, when protocol is HTTP and --app-ssl is set, use "https".
if config.AppSSL {
return "https"
} else {
return "http"
}
case string("grpc"):
// For backwards compatibility, when protocol is GRPC and --app-ssl is set, use "grpcs".
if config.AppSSL {
return string("grpcs")
} else {
return string("grpc")
}
case "":
return string("http")
default:
return ""
}
}

func GetDaprCommand(config *RunConfig) (*exec.Cmd, error) {
daprCMD, err := lookupBinaryFilePath(config.DaprdInstallPath, "daprd")
if err != nil {
Expand Down
141 changes: 141 additions & 0 deletions pkg/standalone/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright 2021 The Dapr 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 standalone

import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetEnv(t *testing.T) {
config := &RunConfig{
SharedRunConfig: SharedRunConfig{},
AppID: "testapp",
AppChannelAddress: "localhost",
AppPort: 1234,
HTTPPort: 2345,
GRPCPort: 3456,
ProfilePort: 4567, // This is not included in env.
MetricsPort: 5678,
}

t.Run("no explicit app-protocol", func(t *testing.T) {
expect := []string{
"APP_ID=testapp",
"APP_CHANNEL_ADDRESS=localhost",
"APP_PORT=1234",
"APP_PROTOCOL=http",
"DAPR_HTTP_PORT=2345",
"DAPR_GRPC_PORT=3456",
"DAPR_METRICS_PORT=5678",
}

got := config.getEnv()

sort.Strings(expect)
sort.Strings(got)

assert.Equal(t, expect, got)
})

t.Run("app-protocol grpcs", func(t *testing.T) {
config.AppProtocol = "grpcs"
config.AppSSL = false

expect := []string{
"APP_ID=testapp",
"APP_CHANNEL_ADDRESS=localhost",
"APP_PORT=1234",
"APP_PROTOCOL=grpcs",
"DAPR_HTTP_PORT=2345",
"DAPR_GRPC_PORT=3456",
"DAPR_METRICS_PORT=5678",
}

got := config.getEnv()

sort.Strings(expect)
sort.Strings(got)

assert.Equal(t, expect, got)
})

t.Run("app-protocol http", func(t *testing.T) {
config.AppProtocol = "http"
config.AppSSL = false

expect := []string{
"APP_ID=testapp",
"APP_CHANNEL_ADDRESS=localhost",
"APP_PORT=1234",
"APP_PROTOCOL=http",
"DAPR_HTTP_PORT=2345",
"DAPR_GRPC_PORT=3456",
"DAPR_METRICS_PORT=5678",
}

got := config.getEnv()

sort.Strings(expect)
sort.Strings(got)

assert.Equal(t, expect, got)
})

t.Run("app-protocol http with app-ssl", func(t *testing.T) {
config.AppProtocol = "http"
config.AppSSL = true

expect := []string{
"APP_ID=testapp",
"APP_CHANNEL_ADDRESS=localhost",
"APP_PORT=1234",
"APP_PROTOCOL=https",
"DAPR_HTTP_PORT=2345",
"DAPR_GRPC_PORT=3456",
"DAPR_METRICS_PORT=5678",
}

got := config.getEnv()

sort.Strings(expect)
sort.Strings(got)

assert.Equal(t, expect, got)
})

t.Run("app-protocol grpc with app-ssl", func(t *testing.T) {
config.AppProtocol = "grpc"
config.AppSSL = true

expect := []string{
"APP_ID=testapp",
"APP_CHANNEL_ADDRESS=localhost",
"APP_PORT=1234",
"APP_PROTOCOL=grpcs",
"DAPR_HTTP_PORT=2345",
"DAPR_GRPC_PORT=3456",
"DAPR_METRICS_PORT=5678",
}

got := config.getEnv()

sort.Strings(expect)
sort.Strings(got)

assert.Equal(t, expect, got)
})
}
8 changes: 7 additions & 1 deletion pkg/standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const (
DaprZipkinContainerName = "dapr_zipkin"

errInstallTemplate = "please run `dapr uninstall` first before running `dapr init`"

healthPort = 58080
metricPort = 59090
)

var (
Expand Down Expand Up @@ -520,7 +523,10 @@ func runPlacementService(wg *sync.WaitGroup, errorChan chan<- error, info initIn
}

args = append(args,
"-p", fmt.Sprintf("%v:50005", osPort))
"-p", fmt.Sprintf("%v:50005", osPort),
"-p", fmt.Sprintf("%v:8080", healthPort),
"-p", fmt.Sprintf("%v:9090", metricPort),
)
}

args = append(args, image)
Expand Down

0 comments on commit 759e46b

Please sign in to comment.