Skip to content

Commit

Permalink
Add size reservations cmds and add reservations to partition capacity. (
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jan 9, 2024
1 parent 3484393 commit c8dd79b
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 97 deletions.
112 changes: 60 additions & 52 deletions cmd/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,16 @@ func Test_PartitionCapacityCmd(t *testing.T) {
Name: "partition-1",
Servers: []*models.V1ServerCapacity{
{
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Reservations: pointer.Pointer(int32(3)),
Usedreservations: pointer.Pointer(int32(1)),
},
},
},
Expand All @@ -289,37 +291,39 @@ func Test_PartitionCapacityCmd(t *testing.T) {
Name: "partition-1",
Servers: []*models.V1ServerCapacity{
{
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Reservations: pointer.Pointer(int32(3)),
Usedreservations: pointer.Pointer(int32(1)),
},
},
},
},
wantTable: pointer.Pointer(`
PARTITION SIZE TOTAL FREE ALLOCATED OTHER FAULTY
1 size-1 5 3 1 4 2
Total 5 3 1 4 2
PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY
1 size-1 5 3 1 1/3 4 2
Total 5 3 1 1/3 4 2
`),
wantWideTable: pointer.Pointer(`
PARTITION SIZE TOTAL FREE ALLOCATED OTHER FAULTY
1 size-1 5 3 1 def abc
Total 5 3 1 4 2
PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY
1 size-1 5 3 1 1/3 def abc
Total 5 3 1 1/3 4 2
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
wantTemplate: pointer.Pointer(`
1 partition-1
`),
wantMarkdown: pointer.Pointer(`
| PARTITION | SIZE | TOTAL | FREE | ALLOCATED | OTHER | FAULTY |
|-----------|--------|-------|------|-----------|-------|--------|
| 1 | size-1 | 5 | 3 | 1 | 4 | 2 |
| Total | | 5 | 3 | 1 | 4 | 2 |
| PARTITION | SIZE | TOTAL | FREE | ALLOCATED | RESERVED | OTHER | FAULTY |
|-----------|--------|-------|------|-----------|----------|-------|--------|
| 1 | size-1 | 5 | 3 | 1 | 1/3 | 4 | 2 |
| Total | | 5 | 3 | 1 | 1/3 | 4 | 2 |
`),
},
{
Expand All @@ -342,14 +346,16 @@ Total 5 3 1 4 2
Name: "partition-1",
Servers: []*models.V1ServerCapacity{
{
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Reservations: pointer.Pointer(int32(3)),
Usedreservations: pointer.Pointer(int32(1)),
},
},
},
Expand All @@ -364,37 +370,39 @@ Total 5 3 1 4 2
Name: "partition-1",
Servers: []*models.V1ServerCapacity{
{
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Allocated: pointer.Pointer(int32(1)),
Faulty: pointer.Pointer(int32(2)),
Faultymachines: []string{"abc"},
Free: pointer.Pointer(int32(3)),
Other: pointer.Pointer(int32(4)),
Othermachines: []string{"def"},
Size: pointer.Pointer("size-1"),
Total: pointer.Pointer(int32(5)),
Reservations: pointer.Pointer(int32(3)),
Usedreservations: pointer.Pointer(int32(1)),
},
},
},
},
wantTable: pointer.Pointer(`
PARTITION SIZE TOTAL FREE ALLOCATED OTHER FAULTY
1 size-1 5 3 1 4 2
Total 5 3 1 4 2
PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY
1 size-1 5 3 1 1/3 4 2
Total 5 3 1 1/3 4 2
`),
wantWideTable: pointer.Pointer(`
PARTITION SIZE TOTAL FREE ALLOCATED OTHER FAULTY
1 size-1 5 3 1 def abc
Total 5 3 1 4 2
PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY
1 size-1 5 3 1 1/3 def abc
Total 5 3 1 1/3 4 2
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
wantTemplate: pointer.Pointer(`
1 partition-1
`),
wantMarkdown: pointer.Pointer(`
| PARTITION | SIZE | TOTAL | FREE | ALLOCATED | OTHER | FAULTY |
|-----------|--------|-------|------|-----------|-------|--------|
| 1 | size-1 | 5 | 3 | 1 | 4 | 2 |
| Total | | 5 | 3 | 1 | 4 | 2 |
| PARTITION | SIZE | TOTAL | FREE | ALLOCATED | RESERVED | OTHER | FAULTY |
|-----------|--------|-------|------|-----------|----------|-------|--------|
| 1 | size-1 | 5 | 3 | 1 | 1/3 | 4 | 2 |
| Total | | 5 | 3 | 1 | 1/3 | 4 | 2 |
`),
},
}
Expand Down
47 changes: 42 additions & 5 deletions cmd/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,28 @@ func newSizeCmd(c *config) *cobra.Command {
tryCmd.Flags().StringP("memory", "M", "", "Memory of the hardware to try, can be given in bytes or any human readable size spec")
tryCmd.Flags().StringP("storagesize", "S", "", "Total storagesize of the hardware to try, can be given in bytes or any human readable size spec")

return genericcli.NewCmds(cmdsConfig, newSizeImageConstraintCmd(c), tryCmd)
reservationsCmd := &cobra.Command{
Use: "reservations",
Short: "manage size reservations",
RunE: func(cmd *cobra.Command, args []string) error {
return w.listReverations()
},
}

listReservationsCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "list size reservations",
RunE: func(cmd *cobra.Command, args []string) error {
return w.listReverations()
},
}

genericcli.AddSortFlag(listReservationsCmd, sorters.SizeReservationsSorter())

reservationsCmd.AddCommand(listReservationsCmd)

return genericcli.NewCmds(cmdsConfig, newSizeImageConstraintCmd(c), tryCmd, reservationsCmd)
}

func (c sizeCmd) Get(id string) (*models.V1SizeResponse, error) {
Expand Down Expand Up @@ -157,10 +178,12 @@ func sizeResponseToUpdate(r *models.V1SizeResponse) *models.V1SizeUpdateRequest
})
}
return &models.V1SizeUpdateRequest{
Constraints: constraints,
Description: r.Description,
ID: r.ID,
Name: r.Name,
Constraints: constraints,
Description: r.Description,
ID: r.ID,
Name: r.Name,
Labels: r.Labels,
Reservations: r.Reservations,
}
}

Expand Down Expand Up @@ -202,3 +225,17 @@ func (c *sizeCmd) try() error {

return c.listPrinter.Print(resp.Payload)
}

func (c sizeCmd) listReverations() error {
resp, err := c.client.Size().ListSizeReservations(size.NewListSizeReservationsParams(), nil)
if err != nil {
return err
}

err = sorters.SizeReservationsSorter().SortBy(resp.Payload)
if err != nil {
return err
}

return c.listPrinter.Print(resp.Payload)
}
113 changes: 96 additions & 17 deletions cmd/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ var (
Type: pointer.Pointer("cores"),
},
},
Reservations: []*models.V1SizeReservation{
{
Amount: pointer.Pointer(int32(5)),
Description: "for testing",
Partitionids: []string{*partition1.ID},
Projectid: pointer.Pointer(project1.Meta.ID),
},
{
Amount: pointer.Pointer(int32(2)),
Description: "for testing",
Partitionids: []string{*partition2.ID},
Projectid: pointer.Pointer(project2.Meta.ID),
},
},
Labels: map[string]string{
"size.metal-stack.io/cpu-description": "1x Intel(R) Xeon(R) D-2141I CPU @ 2.20GHz",
"size.metal-stack.io/drive-description": "960GB NVMe",
},
Description: "size 1",
ID: pointer.Pointer("1"),
Name: "size-1",
Expand Down Expand Up @@ -84,25 +102,26 @@ func Test_SizeCmd_MultiResult(t *testing.T) {
size2,
},
wantTable: pointer.Pointer(`
ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE
1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B
2 size-2 size 2 5 - 6 3 B - 4 B 1 B - 2 B
ID NAME DESCRIPTION RESERVATIONS CPU RANGE MEMORY RANGE STORAGE RANGE
1 size-1 size 1 7 5 - 6 3 B - 4 B 1 B - 2 B
2 size-2 size 2 0 5 - 6 3 B - 4 B 1 B - 2 B
`),
wantWideTable: pointer.Pointer(`
ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE
1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B
2 size-2 size 2 5 - 6 3 B - 4 B 1 B - 2 B
ID NAME DESCRIPTION RESERVATIONS CPU RANGE MEMORY RANGE STORAGE RANGE LABELS
1 size-1 size 1 7 5 - 6 3 B - 4 B 1 B - 2 B size.metal-stack.io/cpu-description=1x Intel(R) Xeon(R) D-2141I CPU @ 2.20GHz
size.metal-stack.io/drive-description=960GB NVMe
2 size-2 size 2 0 5 - 6 3 B - 4 B 1 B - 2 B
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
wantTemplate: pointer.Pointer(`
1 size-1
2 size-2
`),
wantMarkdown: pointer.Pointer(`
| ID | NAME | DESCRIPTION | CPU RANGE | MEMORY RANGE | STORAGE RANGE |
|----|--------|-------------|-----------|--------------|---------------|
| 1 | size-1 | size 1 | 5 - 6 | 3 B - 4 B | 1 B - 2 B |
| 2 | size-2 | size 2 | 5 - 6 | 3 B - 4 B | 1 B - 2 B |
| ID | NAME | DESCRIPTION | RESERVATIONS | CPU RANGE | MEMORY RANGE | STORAGE RANGE |
|----|--------|-------------|--------------|-----------|--------------|---------------|
| 1 | size-1 | size 1 | 7 | 5 - 6 | 3 B - 4 B | 1 B - 2 B |
| 2 | size-2 | size 2 | 0 | 5 - 6 | 3 B - 4 B | 1 B - 2 B |
`),
},
{
Expand Down Expand Up @@ -208,21 +227,22 @@ func Test_SizeCmd_SingleResult(t *testing.T) {
},
want: size1,
wantTable: pointer.Pointer(`
ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE
1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B
ID NAME DESCRIPTION RESERVATIONS CPU RANGE MEMORY RANGE STORAGE RANGE
1 size-1 size 1 7 5 - 6 3 B - 4 B 1 B - 2 B
`),
wantWideTable: pointer.Pointer(`
ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE
1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B
ID NAME DESCRIPTION RESERVATIONS CPU RANGE MEMORY RANGE STORAGE RANGE LABELS
1 size-1 size 1 7 5 - 6 3 B - 4 B 1 B - 2 B size.metal-stack.io/cpu-description=1x Intel(R) Xeon(R) D-2141I CPU @ 2.20GHz
size.metal-stack.io/drive-description=960GB NVMe
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
wantTemplate: pointer.Pointer(`
1 size-1
`),
wantMarkdown: pointer.Pointer(`
| ID | NAME | DESCRIPTION | CPU RANGE | MEMORY RANGE | STORAGE RANGE |
|----|--------|-------------|-----------|--------------|---------------|
| 1 | size-1 | size 1 | 5 - 6 | 3 B - 4 B | 1 B - 2 B |
| ID | NAME | DESCRIPTION | RESERVATIONS | CPU RANGE | MEMORY RANGE | STORAGE RANGE |
|----|--------|-------------|--------------|-----------|--------------|---------------|
| 1 | size-1 | size 1 | 7 | 5 - 6 | 3 B - 4 B | 1 B - 2 B |
`),
},
{
Expand Down Expand Up @@ -275,3 +295,62 @@ ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE
tt.testCmd(t)
}
}

func Test_SizeReservationsCmd_MultiResult(t *testing.T) {
reservations := []*models.V1SizeReservationResponse{
{
Partitionid: pointer.Pointer("a"),
Projectallocations: pointer.Pointer(int32(10)),
Projectid: pointer.Pointer("1"),
Projectname: pointer.Pointer("project-1"),
Reservations: pointer.Pointer(int32(5)),
Sizeid: pointer.Pointer("size-1"),
Tenant: pointer.Pointer("tenant-1"),
Usedreservations: pointer.Pointer(int32(5)),
},
{
Partitionid: pointer.Pointer("b"),
Projectallocations: pointer.Pointer(int32(1)),
Projectid: pointer.Pointer("2"),
Projectname: pointer.Pointer("project-2"),
Reservations: pointer.Pointer(int32(3)),
Sizeid: pointer.Pointer("size-2"),
Tenant: pointer.Pointer("tenant-2"),
Usedreservations: pointer.Pointer(int32(1)),
},
}

tests := []*test[[]*models.V1SizeReservationResponse]{
{
name: "reservation list",
cmd: func(want []*models.V1SizeReservationResponse) []string {
return []string{"size", "reservations", "list"}
},
mocks: &client.MetalMockFns{
Size: func(mock *mock.Mock) {
mock.On("ListSizeReservations", testcommon.MatchIgnoreContext(t, size.NewListSizeReservationsParams()), nil).Return(&size.ListSizeReservationsOK{Payload: reservations}, nil)
},
},
want: reservations,
wantTable: pointer.Pointer(`
PARTITION SIZE TENANT PROJECT PROJECT NAME USED/AMOUNT PROJECT ALLOCATIONS
a size-1 tenant-1 1 project-1 5/5 10
b size-2 tenant-2 2 project-2 1/3 1
`),
wantWideTable: pointer.Pointer(`
PARTITION SIZE TENANT PROJECT PROJECT NAME USED/AMOUNT PROJECT ALLOCATIONS
a size-1 tenant-1 1 project-1 5/5 10
b size-2 tenant-2 2 project-2 1/3 1
`),
wantMarkdown: pointer.Pointer(`
| PARTITION | SIZE | TENANT | PROJECT | PROJECT NAME | USED/AMOUNT | PROJECT ALLOCATIONS |
|-----------|--------|----------|---------|--------------|-------------|---------------------|
| a | size-1 | tenant-1 | 1 | project-1 | 5/5 | 10 |
| b | size-2 | tenant-2 | 2 | project-2 | 1/3 | 1 |
`),
},
}
for _, tt := range tests {
tt.testCmd(t)
}
}
Loading

0 comments on commit c8dd79b

Please sign in to comment.