Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/26738.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Fixed nomad run web ui link for namespaces
```
6 changes: 3 additions & 3 deletions command/job_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines -311 to +313
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heya @nipunn1313, thanks for the PR!

I believe this resolves the issue for jobs that have namespace defined within the job specification, but it's also possible to specify the namespace on the CLI:

nomad job run -namespace='some-other-ns' job.nomad.hcl

which is what c.Meta.namespace reflects, so I think this change breaks that case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I'm realizing we probably want to prioritize the CLI and then if CLI is missing - use the namespace within the job specification.

}

// Check if we should enter monitor mode
Expand Down
60 changes: 60 additions & 0 deletions command/job_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is actually writing to nomad - I don't think this is what we want - going to remove it.

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")
}