From d3f54cf94b068d1cf1f56a264bb7cd594736dbd5 Mon Sep 17 00:00:00 2001 From: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> Date: Tue, 25 Apr 2023 15:40:22 +0530 Subject: [PATCH] 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 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 --- cmd/run.go | 17 ++++++++++++++--- pkg/standalone/list.go | 4 ++++ pkg/standalone/runfileconfig/run_file_config.go | 1 + .../standalone/stop_with_run_template_test.go | 16 ++++++++++++++++ tests/e2e/testdata/run-template-files/dapr.yaml | 1 + 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 2e6b1f3f..41388c03 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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()) } } diff --git a/pkg/standalone/list.go b/pkg/standalone/list.go index 102e9b97..63a15fce 100644 --- a/pkg/standalone/list.go +++ b/pkg/standalone/list.go @@ -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. diff --git a/pkg/standalone/runfileconfig/run_file_config.go b/pkg/standalone/runfileconfig/run_file_config.go index a11d3775..dba54101 100644 --- a/pkg/standalone/runfileconfig/run_file_config.go +++ b/pkg/standalone/runfileconfig/run_file_config.go @@ -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. diff --git a/tests/e2e/standalone/stop_with_run_template_test.go b/tests/e2e/standalone/stop_with_run_template_test.go index 79c3da60..920d916d 100644 --- a/tests/e2e/standalone/stop_with_run_template_test.go +++ b/tests/e2e/standalone/stop_with_run_template_test.go @@ -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) +} diff --git a/tests/e2e/testdata/run-template-files/dapr.yaml b/tests/e2e/testdata/run-template-files/dapr.yaml index d7f25281..87f531ec 100644 --- a/tests/e2e/testdata/run-template-files/dapr.yaml +++ b/tests/e2e/testdata/run-template-files/dapr.yaml @@ -1,4 +1,5 @@ version: 1 +name: test_dapr_template apps: - appDirPath: ../../../apps/processor/ appPort: 9081