Skip to content

Commit

Permalink
Change for multiple resources paths (#1276)
Browse files Browse the repository at this point in the history
* Initial change for multiple resources paths for normal run

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* multi app run changes for multiple resource paths

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* correct file paths for windows

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* remove checking duplicates

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* addressed review comments

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* Fix tests

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

---------

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>
  • Loading branch information
pravinpushkar authored May 25, 2023
1 parent a59826c commit af9030a
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 60 deletions.
8 changes: 4 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
logLevel string
protocol string
componentsPath string
resourcesPath string
resourcesPaths []string
appSSL bool
metricsPort int
maxRequestBodySize int
Expand Down Expand Up @@ -160,7 +160,7 @@ dapr run --run-file /path/to/directory
AppProtocol: protocol,
PlacementHostAddr: viper.GetString("placement-host-address"),
ComponentsPath: componentsPath,
ResourcesPath: resourcesPath,
ResourcesPaths: resourcesPaths,
AppSSL: appSSL,
MaxRequestBodySize: maxRequestBodySize,
HTTPReadBufferSize: readBufferSize,
Expand Down Expand Up @@ -435,8 +435,8 @@ func init() {
RunCmd.Flags().StringVarP(&logLevel, "log-level", "", "info", "The log verbosity. Valid values are: debug, info, warn, error, fatal, or panic")
RunCmd.Flags().IntVarP(&maxConcurrency, "app-max-concurrency", "", -1, "The concurrency level of the application, otherwise is unlimited")
RunCmd.Flags().StringVarP(&protocol, "app-protocol", "P", "http", "The protocol (grpc, grpcs, http, https, h2c) Dapr uses to talk to the application")
RunCmd.Flags().StringVarP(&componentsPath, "components-path", "d", "", "The path for components directory")
RunCmd.Flags().StringVarP(&resourcesPath, "resources-path", "", "", "The path for resources directory")
RunCmd.Flags().StringVarP(&componentsPath, "components-path", "d", "", "The path for components directory. Default is $HOME/.dapr/components or %USERPROFILE%\\.dapr\\components")
RunCmd.Flags().StringSliceVarP(&resourcesPaths, "resources-path", "", []string{}, "The path for resources directory")
// TODO: Remove below line once the flag is removed in the future releases.
// By marking this as deprecated, the flag will be hidden from the help menu, but will continue to work. It will show a warning message when used.
RunCmd.Flags().MarkDeprecated("components-path", "This flag is deprecated and will be removed in the future releases. Use \"resources-path\" flag instead")
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Azure/go-autorest/autorest/adal v0.9.22 // indirect
github.com/Pallinder/sillyname-go v0.0.0-20130730142914-97aeae9e6ba1
github.com/briandowns/spinner v1.19.0
github.com/dapr/dapr v1.11.0-rc.1
github.com/dapr/dapr v1.11.0-rc.4
github.com/dapr/go-sdk v1.6.0
github.com/docker/docker v20.10.21+incompatible
github.com/fatih/color v1.15.0
Expand Down Expand Up @@ -72,7 +72,7 @@ require (
github.com/containerd/containerd v1.6.18 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/dapr/components-contrib v1.11.0-rc.4 // indirect
github.com/dapr/components-contrib v1.11.0-rc.6 // indirect
github.com/dapr/kit v0.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
Expand Down Expand Up @@ -147,7 +147,7 @@ require (
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/microsoft/durabletask-go v0.2.1 // indirect
github.com/microsoft/durabletask-go v0.2.3 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1S
github.com/d2g/dhcp4client v1.0.0/go.mod h1:j0hNfjhrt2SxUOw55nL0ATM/z4Yt3t2Kd1mW34z5W5s=
github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5/go.mod h1:Eo87+Kg/IX2hfWJfwxMzLyuSZyxSoAug2nGa1G2QAi8=
github.com/d2g/hardwareaddr v0.0.0-20190221164911-e7d9fbe030e4/go.mod h1:bMl4RjIciD2oAxI7DmWRx6gbeqrkoLqv3MV0vzNad+I=
github.com/dapr/components-contrib v1.11.0-rc.4 h1:+MexV5pqeBxqCWcmAMwHCENl8DosAZhND9UDiLBK15A=
github.com/dapr/components-contrib v1.11.0-rc.4/go.mod h1:AwbfpZkJttIxPGGtg4tDyXz7izzjca0xfHbzixazLmw=
github.com/dapr/dapr v1.11.0-rc.1 h1:GlQI3xwwMdfbICltsnMjSwPeSn9IXhERwoQwoWYu5Po=
github.com/dapr/dapr v1.11.0-rc.1/go.mod h1:fBzcrk4+o6pdGOHaFp0fsEM7gL/swfH78NYw2hCvsbM=
github.com/dapr/components-contrib v1.11.0-rc.6 h1:sJiILsy/UVVeDeYp7tfG41Jw/kut3B98yWPKnxFO0tI=
github.com/dapr/components-contrib v1.11.0-rc.6/go.mod h1:esIqaJSvemBfMVoO9PVNCKZBEX38SVKRd22RI7XLWOA=
github.com/dapr/dapr v1.11.0-rc.4 h1:orrtDE0QSLGy5JbKvnZpAjuLTc3iERVKzHRLePpZW4o=
github.com/dapr/dapr v1.11.0-rc.4/go.mod h1:BZI1EBRrjVUgF9be++d92AnX3RzPrzuaMpEF6YoqveI=
github.com/dapr/go-sdk v1.6.0 h1:jg5A2khSCHF8bGZsig5RWN/gD0jjitszc2V6Uq2pPdY=
github.com/dapr/go-sdk v1.6.0/go.mod h1:KLQBltoD9K0w5hKTihdcyg9Epob9gypwL5dYcQzPro4=
github.com/dapr/kit v0.0.5 h1:BbjO6LksdXAv6iuTVZiHJNkXj9Ii1gS2wA1u785Ypsg=
Expand Down Expand Up @@ -822,8 +822,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182aff
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
github.com/microsoft/durabletask-go v0.2.1 h1:ZSEJi43KOEALwWOlj0oJI2LezEgwQeUsITyTUbH2DYQ=
github.com/microsoft/durabletask-go v0.2.1/go.mod h1:UtJXHmKalksdccRiN9Y16cHJYYtZN0bqmqOSiy56V8g=
github.com/microsoft/durabletask-go v0.2.3 h1:Qrwij52Y1qf2jVPEETBZEOoRK69ZMZl5VqwVNKEJh7w=
github.com/microsoft/durabletask-go v0.2.3/go.mod h1:UtJXHmKalksdccRiN9Y16cHJYYtZN0bqmqOSiy56V8g=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
Expand Down
36 changes: 22 additions & 14 deletions pkg/standalone/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type SharedRunConfig struct {
LogLevel string `arg:"log-level" yaml:"logLevel"`
MaxConcurrency int `arg:"app-max-concurrency" yaml:"appMaxConcurrency" default:"-1"`
PlacementHostAddr string `arg:"placement-host-address" yaml:"placementHostAddress"`
ComponentsPath string `arg:"components-path"`
ResourcesPath string `arg:"resources-path" yaml:"resourcesPath"`
ComponentsPath string `arg:"components-path"` // Deprecated in run template file: use ResourcesPaths instead.
ResourcesPath string `yaml:"resourcesPath"` // Deprecated in run template file: use ResourcesPaths instead.
ResourcesPaths []string `arg:"resources-path" yaml:"resourcesPaths"`
AppSSL bool `arg:"app-ssl" yaml:"appSSL"`
MaxRequestBodySize int `arg:"dapr-http-max-request-size" yaml:"daprHTTPMaxRequestSize" default:"-1"`
HTTPReadBufferSize int `arg:"dapr-http-read-buffer-size" yaml:"daprHTTPReadBufferSize" default:"-1"`
Expand All @@ -83,17 +84,18 @@ func (meta *DaprMeta) newAppID() string {
}
}

func (config *RunConfig) validateResourcesPath() error {
dirPath := config.ResourcesPath
if dirPath == "" {
dirPath = config.ComponentsPath
func (config *RunConfig) validateResourcesPaths() error {
dirPath := config.ResourcesPaths
if len(dirPath) == 0 {
dirPath = []string{config.ComponentsPath}
}
_, err := os.Stat(dirPath)
if err != nil {
return fmt.Errorf("error validating resources path %q : %w", dirPath, err)
for _, path := range dirPath {
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("error validating resources path %q : %w", dirPath, err)
}
}
componentsLoader := components.NewLocalComponents(dirPath)
_, err = componentsLoader.LoadComponents()
componentsLoader := components.NewLocalComponents(dirPath...)
_, err := componentsLoader.LoadComponents()
if err != nil {
return fmt.Errorf("error validating components in resources path %q : %w", dirPath, err)
}
Expand Down Expand Up @@ -142,12 +144,12 @@ func (config *RunConfig) Validate() error {
config.AppID = meta.newAppID()
}

err = config.validateResourcesPath()
err = config.validateResourcesPaths()
if err != nil {
return err
}

if config.ResourcesPath != "" {
if len(config.ResourcesPaths) > 0 {
config.ComponentsPath = ""
}

Expand Down Expand Up @@ -286,11 +288,17 @@ func getArgsFromSchema(schema reflect.Value, args []string) []string {

ifneq, hasIfneq := typeField.Tag.Lookup("ifneq")

switch valueField.(type) {
switch vType := valueField.(type) {
case bool:
if valueField == true {
args = append(args, key)
}
case []string:
if len(vType) > 0 {
for _, val := range vType {
args = append(args, key, val)
}
}
default:
value := fmt.Sprintf("%v", reflect.ValueOf(valueField))
if len(value) != 0 && (!hasIfneq || value != ifneq) {
Expand Down
39 changes: 33 additions & 6 deletions pkg/standalone/runfileconfig/run_file_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func (a *RunFileConfig) validateRunConfig(runFilePath string) error {
if err != nil {
return err
}

// Resolves common's section ResourcesPaths to absolute paths and validates them.
for i := range a.Common.ResourcesPaths {
err := a.resolvePathToAbsAndValidate(baseDir, &a.Common.ResourcesPaths[i])
if err != nil {
return err
}
}

// Merge common's section ResourcesPaths and ResourcePath. ResourcesPaths will be single source of truth for resources to be loaded.
if len(strings.TrimSpace(a.Common.ResourcesPath)) > 0 {
a.Common.ResourcesPaths = append(a.Common.ResourcesPaths, a.Common.ResourcesPath)
}

for i := 0; i < len(a.Apps); i++ {
if a.Apps[i].AppDirPath == "" {
return errors.New("required field 'appDirPath' not found in the provided app config file")
Expand All @@ -70,6 +84,19 @@ func (a *RunFileConfig) validateRunConfig(runFilePath string) error {
if err != nil {
return err
}

// Resolves ResourcesPaths to absolute paths and validates them.
for j := range a.Apps[i].ResourcesPaths {
err := a.resolvePathToAbsAndValidate(a.Apps[i].AppDirPath, &a.Apps[i].ResourcesPaths[j])
if err != nil {
return err
}
}

// Merge app's section ResourcesPaths and ResourcePath. ResourcesPaths will be single source of truth for resources to be loaded.
if len(strings.TrimSpace(a.Apps[i].ResourcesPath)) > 0 {
a.Apps[i].ResourcesPaths = append(a.Apps[i].ResourcesPaths, a.Apps[i].ResourcesPath)
}
}
return nil
}
Expand Down Expand Up @@ -231,22 +258,22 @@ func (a *RunFileConfig) mergeCommonAndAppsEnv() {
}

// resolveResourcesFilePath resolves the resources path for the app.
// Precedence order for resourcesPath -> apps[i].resourcesPath > apps[i].appDirPath/.dapr/resources > common.resourcesPath > dapr default resources path.
// Precedence order for resourcesPaths -> apps[i].resourcesPaths > apps[i].appDirPath/.dapr/resources > common.resourcesPaths > dapr default resources path.
func (a *RunFileConfig) resolveResourcesFilePath(app *App) error {
if app.ResourcesPath != "" {
if len(app.ResourcesPaths) > 0 {
return nil
}
localResourcesDir := filepath.Join(app.AppDirPath, standalone.DefaultDaprDirName, standalone.DefaultResourcesDirName)
if err := utils.ValidateFilePath(localResourcesDir); err == nil {
app.ResourcesPath = localResourcesDir
} else if len(strings.TrimSpace(a.Common.ResourcesPath)) > 0 {
app.ResourcesPath = a.Common.ResourcesPath
app.ResourcesPaths = []string{localResourcesDir}
} else if len(a.Common.ResourcesPaths) > 0 {
app.ResourcesPaths = append(app.ResourcesPaths, a.Common.ResourcesPaths...)
} else {
daprDirPath, err := standalone.GetDaprRuntimePath(app.DaprdInstallPath)
if err != nil {
return fmt.Errorf("error getting dapr install path: %w", err)
}
app.ResourcesPath = standalone.GetDaprComponentsPath(daprDirPath)
app.ResourcesPaths = []string{standalone.GetDaprComponentsPath(daprDirPath)}
}
return nil
}
Expand Down
Loading

0 comments on commit af9030a

Please sign in to comment.