Skip to content

Commit d295a05

Browse files
authored
Remove size/fromHardware endpoint (#524)
1 parent 9aa6fc7 commit d295a05

File tree

8 files changed

+14
-186
lines changed

8 files changed

+14
-186
lines changed

cmd/metal-api/internal/datastore/size.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ func (rs *RethinkStore) UpdateSize(oldSize *metal.Size, newSize *metal.Size) err
3939
}
4040

4141
// FromHardware tries to find a size which matches the given hardware specs.
42-
func (rs *RethinkStore) FromHardware(hw metal.MachineHardware) (*metal.Size, []*metal.SizeMatchingLog, error) {
42+
func (rs *RethinkStore) FromHardware(hw metal.MachineHardware) (*metal.Size, error) {
4343
sz, err := rs.ListSizes()
4444
if err != nil {
45-
return nil, nil, err
45+
return nil, err
4646
}
4747
if len(sz) < 1 {
4848
// this should not happen, so we do not return a notfound
49-
return nil, nil, errors.New("no sizes found in database")
49+
return nil, errors.New("no sizes found in database")
5050
}
5151
var sizes metal.Sizes
5252
for _, s := range sz {

cmd/metal-api/internal/datastore/size_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func TestRethinkStore_FromHardware(t *testing.T) {
196196
for i := range tests {
197197
tt := tests[i]
198198
t.Run(tt.name, func(t *testing.T) {
199-
got, _, err := tt.rs.FromHardware(tt.hw)
199+
got, err := tt.rs.FromHardware(tt.hw)
200200
if (err != nil) != tt.wantErr {
201201
t.Errorf("RethinkStore.FromHardware() error = %v, wantErr %v", err, tt.wantErr)
202202
return

cmd/metal-api/internal/grpc/boot-service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (b *BootService) Register(ctx context.Context, req *v1.BootServiceRegisterR
151151
MetalGPUs: gpus,
152152
}
153153

154-
size, _, err := b.ds.FromHardware(machineHardware)
154+
size, err := b.ds.FromHardware(machineHardware)
155155
if err != nil {
156156
size = metal.UnknownSize()
157157
b.log.Error("no size found for hardware, defaulting to unknown size", "hardware", machineHardware, "error", err)

cmd/metal-api/internal/metal/size.go

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,68 +74,53 @@ func UnknownSize() *Size {
7474

7575
// Matches returns true if the given machine hardware is inside the min/max values of the
7676
// constraint.
77-
func (c *Constraint) Matches(hw MachineHardware) (ConstraintMatchingLog, bool) {
78-
logentryFmt := fmt.Sprintf("%%d >= %d && %%d <= %d", c.Min, c.Max)
79-
cml := ConstraintMatchingLog{Constraint: *c, Log: fmt.Sprintf("no constraint matching %q", c.Type)}
77+
func (c *Constraint) Matches(hw MachineHardware) bool {
8078
res := false
8179
switch c.Type {
8280
case CoreConstraint:
8381
res = uint64(hw.CPUCores) >= c.Min && uint64(hw.CPUCores) <= c.Max
84-
cml.Log = fmt.Sprintf(logentryFmt, hw.CPUCores, hw.CPUCores)
8582
case MemoryConstraint:
8683
res = hw.Memory >= c.Min && hw.Memory <= c.Max
87-
cml.Log = fmt.Sprintf(logentryFmt, hw.Memory, hw.Memory)
8884
case StorageConstraint:
8985
res = hw.DiskCapacity() >= c.Min && hw.DiskCapacity() <= c.Max
90-
cml.Log = fmt.Sprintf(logentryFmt, hw.DiskCapacity(), hw.DiskCapacity())
9186
case GPUConstraint:
9287
for model, count := range hw.GPUModels() {
9388
idMatches, err := filepath.Match(c.Identifier, model)
9489
if err != nil {
95-
cml.Log = fmt.Sprintf("cannot match gpu model:%v", err)
96-
return cml, false
90+
return false
9791
}
9892
res = count >= c.Min && count <= c.Max && idMatches
9993
if res {
10094
break
10195
}
10296
}
10397

104-
cml.Log = fmt.Sprintf("existing gpus:%#v required gpus:%s count %d-%d", hw.MetalGPUs, c.Identifier, c.Min, c.Max)
10598
}
106-
cml.Match = res
107-
return cml, res
99+
return res
108100
}
109101

110102
// FromHardware searches a Size for given hardware specs. It will search
111103
// for a size where the constraints matches the given hardware.
112-
func (sz Sizes) FromHardware(hardware MachineHardware) (*Size, []*SizeMatchingLog, error) {
104+
func (sz Sizes) FromHardware(hardware MachineHardware) (*Size, error) {
113105
var found []Size
114-
matchlog := make([]*SizeMatchingLog, 0)
115-
var matchedlog *SizeMatchingLog
116106
nextsize:
117107
for _, s := range sz {
118-
ml := &SizeMatchingLog{Name: s.ID, Match: false}
119-
matchlog = append(matchlog, ml)
120108
for _, c := range s.Constraints {
121-
lg, match := c.Matches(hardware)
122-
ml.Constraints = append(ml.Constraints, lg)
109+
match := c.Matches(hardware)
123110
if !match {
124111
continue nextsize
125112
}
126113
}
127-
ml.Match = true
128-
matchedlog = ml
129114
found = append(found, s)
130115
}
131116

132117
if len(found) == 0 {
133-
return nil, matchlog, NotFound("no size found for hardware (%s)", hardware.ReadableSpec())
118+
return nil, NotFound("no size found for hardware (%s)", hardware.ReadableSpec())
134119
}
135120
if len(found) > 1 {
136-
return nil, matchlog, fmt.Errorf("%d sizes found for hardware (%s)", len(found), hardware.ReadableSpec())
121+
return nil, fmt.Errorf("%d sizes found for hardware (%s)", len(found), hardware.ReadableSpec())
137122
}
138-
return &found[0], []*SizeMatchingLog{matchedlog}, nil
123+
return &found[0], nil
139124
}
140125

141126
func (s *Size) overlaps(so *Size) bool {
@@ -293,19 +278,3 @@ func (rs *Reservations) Validate(partitions PartitionMap, projects map[string]*m
293278

294279
return nil
295280
}
296-
297-
// A ConstraintMatchingLog is used do return a log message to the caller
298-
// beside the constraint itself.
299-
type ConstraintMatchingLog struct {
300-
Constraint Constraint
301-
Match bool
302-
Log string
303-
}
304-
305-
// A SizeMatchingLog returns information about a list of constraints.
306-
type SizeMatchingLog struct {
307-
Name string
308-
Log string
309-
Match bool
310-
Constraints []ConstraintMatchingLog
311-
}

cmd/metal-api/internal/metal/size_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func TestSizes_FromHardware(t *testing.T) {
415415
for i := range tests {
416416
tt := tests[i]
417417
t.Run(tt.name, func(t *testing.T) {
418-
got, _, err := tt.sz.FromHardware(tt.args.hardware)
418+
got, err := tt.sz.FromHardware(tt.args.hardware)
419419
if (err != nil) != tt.wantErr {
420420
t.Errorf("Sizes.FromHardware() error = %v, wantErr %v", err, tt.wantErr)
421421
return

cmd/metal-api/internal/service/size-service.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package service
22

33
import (
4-
"errors"
54
"fmt"
65
"log/slog"
76
"net/http"
@@ -115,16 +114,6 @@ func (r *sizeResource) webService() *restful.WebService {
115114
Returns(http.StatusConflict, "Conflict", httperrors.HTTPErrorResponse{}).
116115
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))
117116

118-
ws.Route(ws.POST("/from-hardware").
119-
To(r.fromHardware).
120-
Operation("fromHardware").
121-
Doc("Searches all sizes for one to match the given hardwarespecs. If nothing is found, a list of entries is returned which describe the constraint which did not match").
122-
Metadata(restfulspec.KeyOpenAPITags, tags).
123-
Metadata(auditing.Exclude, true).
124-
Reads(v1.MachineHardware{}).
125-
Returns(http.StatusOK, "OK", v1.SizeMatchingLog{}).
126-
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))
127-
128117
return ws
129118
}
130119

@@ -427,29 +416,6 @@ func (r *sizeResource) updateSize(request *restful.Request, response *restful.Re
427416
r.send(request, response, http.StatusOK, v1.NewSizeResponse(&newSize))
428417
}
429418

430-
func (r *sizeResource) fromHardware(request *restful.Request, response *restful.Response) {
431-
var requestPayload v1.MachineHardware
432-
err := request.ReadEntity(&requestPayload)
433-
if err != nil {
434-
r.sendError(request, response, httperrors.BadRequest(err))
435-
return
436-
}
437-
438-
hw := v1.NewMetalMachineHardware(&requestPayload)
439-
_, lg, err := r.ds.FromHardware(hw)
440-
if err != nil {
441-
r.sendError(request, response, defaultError(err))
442-
return
443-
}
444-
445-
if len(lg) < 1 {
446-
r.sendError(request, response, httperrors.UnprocessableEntity(errors.New("size matching log is empty")))
447-
return
448-
}
449-
450-
r.send(request, response, http.StatusOK, v1.NewSizeMatchingLog(lg[0]))
451-
}
452-
453419
func (r *sizeResource) listSizeReservations(request *restful.Request, response *restful.Response) {
454420
ss, err := r.ds.ListSizes()
455421
if err != nil {

cmd/metal-api/internal/service/v1/size.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,6 @@ type SizeMatchingLog struct {
6868
Constraints []SizeConstraintMatchingLog `json:"constraints"`
6969
}
7070

71-
func NewSizeMatchingLog(m *metal.SizeMatchingLog) *SizeMatchingLog {
72-
constraints := []SizeConstraintMatchingLog{}
73-
for i := range m.Constraints {
74-
constraint := SizeConstraintMatchingLog{
75-
Constraint: SizeConstraint{
76-
Type: m.Constraints[i].Constraint.Type,
77-
Min: m.Constraints[i].Constraint.Min,
78-
Max: m.Constraints[i].Constraint.Max,
79-
},
80-
Match: m.Constraints[i].Match,
81-
Log: m.Constraints[i].Log,
82-
}
83-
constraints = append(constraints, constraint)
84-
}
85-
return &SizeMatchingLog{
86-
Name: m.Name,
87-
Match: m.Match,
88-
Log: m.Log,
89-
Constraints: constraints,
90-
}
91-
}
92-
9371
func NewSizeResponse(s *metal.Size) *SizeResponse {
9472
if s == nil {
9573
return nil

spec/metal-api.json

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4503,27 +4503,6 @@
45034503
"type"
45044504
]
45054505
},
4506-
"v1.SizeConstraintMatchingLog": {
4507-
"properties": {
4508-
"constraint": {
4509-
"$ref": "#/definitions/v1.SizeConstraint",
4510-
"description": "the size constraint to which this log relates to"
4511-
},
4512-
"log": {
4513-
"description": "a string representation of the matching condition",
4514-
"type": "string"
4515-
},
4516-
"match": {
4517-
"description": "indicates whether the constraint matched or not",
4518-
"type": "boolean"
4519-
}
4520-
},
4521-
"required": [
4522-
"constraint",
4523-
"log",
4524-
"match"
4525-
]
4526-
},
45274506
"v1.SizeCreateRequest": {
45284507
"properties": {
45294508
"constraints": {
@@ -4663,31 +4642,6 @@
46634642
"id"
46644643
]
46654644
},
4666-
"v1.SizeMatchingLog": {
4667-
"properties": {
4668-
"constraints": {
4669-
"items": {
4670-
"$ref": "#/definitions/v1.SizeConstraintMatchingLog"
4671-
},
4672-
"type": "array"
4673-
},
4674-
"log": {
4675-
"type": "string"
4676-
},
4677-
"match": {
4678-
"type": "boolean"
4679-
},
4680-
"name": {
4681-
"type": "string"
4682-
}
4683-
},
4684-
"required": [
4685-
"constraints",
4686-
"log",
4687-
"match",
4688-
"name"
4689-
]
4690-
},
46914645
"v1.SizeReservation": {
46924646
"properties": {
46934647
"amount": {
@@ -9077,45 +9031,6 @@
90779031
]
90789032
}
90799033
},
9080-
"/v1/size/from-hardware": {
9081-
"post": {
9082-
"consumes": [
9083-
"application/json"
9084-
],
9085-
"operationId": "fromHardware",
9086-
"parameters": [
9087-
{
9088-
"in": "body",
9089-
"name": "body",
9090-
"required": true,
9091-
"schema": {
9092-
"$ref": "#/definitions/v1.MachineHardware"
9093-
}
9094-
}
9095-
],
9096-
"produces": [
9097-
"application/json"
9098-
],
9099-
"responses": {
9100-
"200": {
9101-
"description": "OK",
9102-
"schema": {
9103-
"$ref": "#/definitions/v1.SizeMatchingLog"
9104-
}
9105-
},
9106-
"default": {
9107-
"description": "Error",
9108-
"schema": {
9109-
"$ref": "#/definitions/httperrors.HTTPErrorResponse"
9110-
}
9111-
}
9112-
},
9113-
"summary": "Searches all sizes for one to match the given hardwarespecs. If nothing is found, a list of entries is returned which describe the constraint which did not match",
9114-
"tags": [
9115-
"size"
9116-
]
9117-
}
9118-
},
91199034
"/v1/size/reservations": {
91209035
"post": {
91219036
"consumes": [

0 commit comments

Comments
 (0)