Skip to content

Commit

Permalink
Merge pull request #37 from hashicorp/HCD-35-namespace-based-allocs
Browse files Browse the repository at this point in the history
Show only tasks for a given Namespace
  • Loading branch information
hcjulz committed Sep 13, 2022
2 parents 45c0c38 + a62bbc9 commit e79c4a3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions component/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
LabelTaskGroup,
LabelJobID,
LabelType,
LabelNamespace,
LabelNodeID,
LabelNodeName,
LabelDesiredStatus,
Expand Down Expand Up @@ -106,6 +107,7 @@ func (t *AllocationTable) renderRows() {
a.TaskGroup,
a.JobID,
a.JobType,
a.Namespace,
a.NodeID,
a.NodeName,
a.DesiredStatus,
Expand Down
7 changes: 5 additions & 2 deletions component/allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestAllocation_Happy(t *testing.T) {
TaskGroup: "tokio",
JobID: "japan",
JobType: "manga",
Namespace: "namespace",
NodeID: "node",
NodeName: "node",
DesiredStatus: "run",
Expand All @@ -38,6 +39,7 @@ func TestAllocation_Happy(t *testing.T) {
TaskGroup: "tokio",
JobID: "japan",
JobType: "manga",
Namespace: "namespace",
NodeID: "node",
NodeName: "node",
DesiredStatus: "stop",
Expand Down Expand Up @@ -77,8 +79,8 @@ func TestAllocation_Happy(t *testing.T) {

row1, index1, c1 := fakeTable.RenderRowArgsForCall(0)
row2, index2, c2 := fakeTable.RenderRowArgsForCall(1)
expectedRow1 := []string{"ichi", "tokio", "japan", "manga", "node", "node", "run"}
expectedRow2 := []string{"ni", "tokio", "japan", "manga", "node", "node", "stop"}
expectedRow1 := []string{"ichi", "tokio", "japan", "manga", "namespace", "node", "node", "run"}
expectedRow2 := []string{"ni", "tokio", "japan", "manga", "namespace", "node", "node", "stop"}

// It render the correct data for the rows
r.Equal(expectedRow1, row1)
Expand All @@ -105,6 +107,7 @@ func TestAllocation_Happy(t *testing.T) {
TaskGroup: "tokio",
JobID: "japan",
JobType: "manga",
Namespace: "namespace",
NodeID: "node",
NodeName: "node",
DesiredStatus: "run",
Expand Down
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type TaskGroupStatus struct {
type Alloc struct {
ID string
Name string
Namespace string
TaskGroup string
Tasks []AllocTask
TaskNames []string
Expand Down
1 change: 1 addition & 0 deletions nomad/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func toAllocs(list []*api.AllocationListStub) []*models.Alloc {
for _, el := range list {
alloc := &models.Alloc{
ID: el.ID,
Namespace: el.Namespace,
TaskGroup: el.TaskGroup,
JobID: el.JobID,
JobType: el.JobType,
Expand Down
4 changes: 4 additions & 0 deletions nomad/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestJobAllocs(t *testing.T) {
{
ID: "id-one",
TaskGroup: "the-group",
Namespace: "namespace",
JobID: "the-job",
JobType: "the-type",
NodeID: "node-id",
Expand All @@ -50,6 +51,7 @@ func TestJobAllocs(t *testing.T) {
{
ID: "id-two",
TaskGroup: "the-group",
Namespace: "namespace",
JobID: "the-job",
JobType: "the-type",
NodeID: "node-id",
Expand Down Expand Up @@ -84,6 +86,7 @@ func TestJobAllocs(t *testing.T) {
{
ID: "id-one",
TaskGroup: "the-group",
Namespace: "namespace",
JobID: "the-job",
JobType: "the-type",
NodeID: "node-id",
Expand All @@ -110,6 +113,7 @@ func TestJobAllocs(t *testing.T) {
{
ID: "id-two",
TaskGroup: "the-group",
Namespace: "namespace",
JobID: "the-job",
JobType: "the-type",
NodeID: "node-id",
Expand Down
13 changes: 13 additions & 0 deletions view/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (v *View) Allocations(jobID string) {

func (v *View) filterAllocs(jobID string) []*models.Alloc {
data := v.filterAllocsForJob(jobID)
data = v.namespaceFilterAllocs(data)
filter := v.state.Filter.Allocations
if filter != "" {
rx, _ := regexp.Compile(filter)
Expand Down Expand Up @@ -92,3 +93,15 @@ func (v *View) filterAllocsForJob(jobID string) []*models.Alloc {
}
return result
}

func (v *View) namespaceFilterAllocs(allocs []*models.Alloc) []*models.Alloc {
rx, _ := regexp.Compile(v.state.SelectedNamespace)
result := []*models.Alloc{}
for _, a := range allocs {
switch true {
case rx.MatchString(a.Namespace):
result = append(result, a)
}
}
return result
}

0 comments on commit e79c4a3

Please sign in to comment.