From 1708d6572bf918e4492b0ad35561aae969066972 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Mon, 8 Sep 2025 14:13:23 -0700 Subject: [PATCH 1/3] Fix job_run to show appropriate namespace in web UI link --- command/job_run.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/command/job_run.go b/command/job_run.go index 21a3b16238f..458c85f2bd0 100644 --- a/command/job_run.go +++ b/command/job_run.go @@ -308,9 +308,9 @@ func (c *JobRunCommand) Run(args []string) int { evalID := resp.EvalID - jobNamespace := c.Meta.namespace - if jobNamespace == "" { - jobNamespace = "default" + jobNamespace := "default" + if job.Namespace != nil && *job.Namespace != "" { + jobNamespace = *job.Namespace } // Check if we should enter monitor mode From 27d9a1561b2798a033507fd0ad2b2df7efb33158 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Mon, 8 Sep 2025 14:20:51 -0700 Subject: [PATCH 2/3] Add changelog entry --- .changelog/26738.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/26738.txt diff --git a/.changelog/26738.txt b/.changelog/26738.txt new file mode 100644 index 00000000000..4def52d5d14 --- /dev/null +++ b/.changelog/26738.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: Fixed nomad run web ui link for namespaces +``` From 9154de04861936c9323631d07c6a7a3bb33dda47 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Mon, 8 Sep 2025 14:23:48 -0700 Subject: [PATCH 3/3] Add test --- command/job_run_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/command/job_run_test.go b/command/job_run_test.go index 95414fbf8dc..356fc52e6b7 100644 --- a/command/job_run_test.go +++ b/command/job_run_test.go @@ -13,7 +13,10 @@ import ( "time" "github.com/hashicorp/cli" + "github.com/hashicorp/nomad/api" "github.com/hashicorp/nomad/ci" + "github.com/hashicorp/nomad/command/agent" + "github.com/hashicorp/nomad/helper/pointer" "github.com/hashicorp/nomad/testutil" "github.com/shoenig/test/must" ) @@ -281,3 +284,60 @@ func TestRunCommand_JSON(t *testing.T) { must.Eq(t, "", stderr) must.NotEq(t, "", stdout) } + +// TestRunCommand_NamespaceInUILink tests that the UI link uses the job's namespace +// rather than the CLI namespace when they differ. +func TestRunCommand_NamespaceInUILink(t *testing.T) { + ci.Parallel(t) + + // Create a server with UI hints enabled + srv, client, url := testServer(t, true, func(c *agent.Config) { + c.UI.ShowCLIHints = pointer.Of(true) + }) + defer srv.Shutdown() + waitForNodes(t, client) + + // Create a job with a non-default namespace + _, err := client.Namespaces().Register(&api.Namespace{Name: "internal"}, nil) + must.NoError(t, err) + + // Create a job file with the "internal" namespace + jobFile := filepath.Join(t.TempDir(), "test.nomad") + jobSpec := ` +job "test-job" { + namespace = "internal" + type = "service" + datacenters = ["dc1"] + group "web" { + count = 1 + task "web" { + driver = "exec" + resources { + cpu = 100 + memory = 64 + } + } + } +}` + must.NoError(t, os.WriteFile(jobFile, []byte(jobSpec), 0644)) + + ui := cli.NewMockUi() + cmd := &JobRunCommand{ + Meta: Meta{ + Ui: ui, + flagAddress: url, + // Deliberately set CLI namespace to "default" to test that + // the job's namespace ("internal") takes precedence + namespace: "default", + }, + } + + // Run the job in detach mode so we get the UI hint + code := cmd.Run([]string{"-detach", jobFile}) + must.Zero(t, code) + + // Verify that the UI link uses the job's namespace ("internal") not the CLI namespace ("default") + output := ui.ErrorWriter.String() + must.StrContains(t, output, "/ui/jobs/test-job@internal") + must.StrNotContains(t, output, "/ui/jobs/test-job@default") +}