Add name template (#1274)

* add ability to specify a name for a run template

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix test template

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* move list e2e tests to stop template subtest

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix template name

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* Update cmd/run.go

Co-authored-by: Pravin Pushkar <ppushkar@microsoft.com>
Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

---------

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
Co-authored-by: Pravin Pushkar <ppushkar@microsoft.com>
This commit is contained in:
Mukundan Sundararajan 2023-04-25 15:40:22 +05:30 committed by GitHub
parent 81fd09b7d1
commit d3f54cf94b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 3 deletions

View File

@ -460,7 +460,7 @@ func init() {
RootCmd.AddCommand(RunCmd)
}
func executeRun(runFilePath string, apps []runfileconfig.App) (bool, error) {
func executeRun(runTemplateName, runFilePath string, apps []runfileconfig.App) (bool, error) {
var exitWithError bool
// setup shutdown notify channel.
@ -537,6 +537,9 @@ func executeRun(runFilePath string, apps []runfileconfig.App) (bool, error) {
// Update extended metadata with run file path.
putRunFilePathInMeta(runState, runFilePath)
// Update extended metadata with run file path.
putRunTemplateNameInMeta(runState, runTemplateName)
if runState.AppCMD.Command != nil {
putAppCommandInMeta(runConfig, runState)
@ -645,7 +648,7 @@ func executeRunWithAppsConfigFile(runFilePath string) {
print.StatusEvent(os.Stdout, print.LogFailure, "No apps to run")
os.Exit(1)
}
exitWithError, closeErr := executeRun(runFilePath, apps)
exitWithError, closeErr := executeRun(config.Name, runFilePath, apps)
if exitWithError {
if closeErr != nil {
print.StatusEvent(os.Stdout, print.LogFailure, "Error closing resources: %s", closeErr)
@ -977,7 +980,15 @@ func putRunFilePathInMeta(runE *runExec.RunExec, runFilePath string) {
}
err = metadata.Put(runE.DaprHTTPPort, "runTemplatePath", runFilePath, runE.AppID, unixDomainSocket)
if err != nil {
print.StatusEvent(runE.DaprCMD.OutputWriter, print.LogWarning, "Could not update sidecar metadata for runFile: %s", err.Error())
print.StatusEvent(runE.DaprCMD.OutputWriter, print.LogWarning, "Could not update sidecar metadata for run file path: %s", err.Error())
}
}
// putRunTemplateNameInMeta puts the name of the run file in metadata so that it can be used by the CLI to stop all apps started by this run file.
func putRunTemplateNameInMeta(runE *runExec.RunExec, runTemplateName string) {
err := metadata.Put(runE.DaprHTTPPort, "runTemplateName", runTemplateName, runE.AppID, unixDomainSocket)
if err != nil {
print.StatusEvent(runE.DaprCMD.OutputWriter, print.LogWarning, "Could not update sidecar metadata for run template name: %s", err.Error())
}
}

View File

@ -44,6 +44,7 @@ type ListOutput struct {
MaxRequestBodySize int `csv:"-" json:"maxRequestBodySize" yaml:"maxRequestBodySize"` // Additional field, not displayed in table.
HTTPReadBufferSize int `csv:"-" json:"httpReadBufferSize" yaml:"httpReadBufferSize"` // Additional field, not displayed in table.
RunTemplatePath string `csv:"RUN_TEMPLATE_PATH" json:"runTemplatePath" yaml:"runTemplatePath"`
RunTemplateName string `json:"runTemplateName" yaml:"runTemplateName"` // specifically omitted in csv output.
}
func (d *daprProcess) List() ([]ListOutput, error) {
@ -111,6 +112,7 @@ func List() ([]ListOutput, error) {
appPIDString := ""
cliPIDString := ""
runTemplatePath := ""
runTemplateName := ""
socket := argumentsMap["--unix-domain-socket"]
appMetadata, err := metadata.Get(httpPort, appID, socket)
if err == nil {
@ -118,6 +120,7 @@ func List() ([]ListOutput, error) {
appPIDString = appMetadata.Extended["appPID"]
cliPIDString = appMetadata.Extended["cliPID"]
runTemplatePath = appMetadata.Extended["runTemplatePath"]
runTemplateName = appMetadata.Extended["runTemplateName"]
}
appPID, err := strconv.Atoi(appPIDString)
@ -155,6 +158,7 @@ func List() ([]ListOutput, error) {
MaxRequestBodySize: maxRequestBodySize,
HTTPReadBufferSize: httpReadBufferSize,
RunTemplatePath: runTemplatePath,
RunTemplateName: runTemplateName,
}
// filter only dashboard instance.

View File

@ -44,6 +44,7 @@ type RunFileConfig struct {
Common Common `yaml:"common"`
Apps []App `yaml:"apps"`
Version int `yaml:"version"`
Name string `yaml:"name,omitempty"`
}
// App represents the configuration options for the apps in the run file.

View File

@ -44,6 +44,8 @@ func TestStopAppsStartedWithRunTemplate(t *testing.T) {
go ensureAllAppsStartedWithRunTemplate(t)
time.Sleep(10 * time.Second)
cliPID := getCLIPID(t)
// Assert dapr list contains template name
assertTemplateListOutput(t, "test_dapr_template")
output, err := cmdStopWithRunTemplate("../testdata/run-template-files/dapr.yaml")
assert.NoError(t, err, "failed to stop apps started with run template")
assert.Contains(t, output, "Dapr and app processes stopped successfully")
@ -115,3 +117,17 @@ func verifyCLIPIDNotExist(t *testing.T, pid string) {
require.NoError(t, err, "failed to list apps")
assert.NotContains(t, output, pid)
}
func assertTemplateListOutput(t *testing.T, name string) {
output, err := cmdList("json")
t.Log(output)
require.NoError(t, err, "dapr list failed")
var result []map[string]interface{}
err = json.Unmarshal([]byte(output), &result)
assert.NoError(t, err, "output was not valid JSON")
assert.Len(t, result, 2, "expected two apps to be running")
assert.Equal(t, name, result[0]["runTemplateName"], "expected run template name to be %s", name)
}

View File

@ -1,4 +1,5 @@
version: 1
name: test_dapr_template
apps:
- appDirPath: ../../../apps/processor/
appPort: 9081