-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
size image constraints types and validation (#232)
- Loading branch information
Showing
20 changed files
with
1,426 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
FROM metalstack/builder:latest as builder | ||
|
||
FROM alpine:3.13 | ||
FROM alpine:3.15 | ||
RUN apk -U add ca-certificates | ||
COPY --from=builder /work/bin/metal-api /metal-api | ||
CMD ["/metal-api"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package datastore | ||
|
||
import "github.com/metal-stack/metal-api/cmd/metal-api/internal/metal" | ||
|
||
// FindSizeImageConstraint return a SizeImageConstraint for a given size. | ||
func (rs *RethinkStore) FindSizeImageConstraint(sizeID string) (*metal.SizeImageConstraint, error) { | ||
var ic metal.SizeImageConstraint | ||
err := rs.findEntityByID(rs.sizeImageConstraintTable(), &ic, sizeID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ic, nil | ||
} | ||
|
||
// ListSizeImageConstraints returns all SizeImageConstraints. | ||
func (rs *RethinkStore) ListSizeImageConstraints() (metal.SizeImageConstraints, error) { | ||
fls := make(metal.SizeImageConstraints, 0) | ||
err := rs.listEntities(rs.sizeImageConstraintTable(), &fls) | ||
return fls, err | ||
} | ||
|
||
// CreateSizeImageConstraint creates a new SizeImageConstraint. | ||
func (rs *RethinkStore) CreateSizeImageConstraint(ic *metal.SizeImageConstraint) error { | ||
return rs.createEntity(rs.sizeImageConstraintTable(), ic) | ||
} | ||
|
||
// DeleteSizeImageConstraint deletes a SizeImageConstraint. | ||
func (rs *RethinkStore) DeleteSizeImageConstraint(ic *metal.SizeImageConstraint) error { | ||
return rs.deleteEntity(rs.sizeImageConstraintTable(), ic) | ||
} | ||
|
||
// UpdateSizeImageConstraint updates a SizeImageConstraint. | ||
func (rs *RethinkStore) UpdateSizeImageConstraint(oldSizeImageConstraint *metal.SizeImageConstraint, newSizeImageConstraint *metal.SizeImageConstraint) error { | ||
return rs.updateEntity(rs.sizeImageConstraintTable(), newSizeImageConstraint, oldSizeImageConstraint) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package metal | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
) | ||
|
||
// SizeImageConstraint expresses optional restrictions for specific size to image combinations | ||
// this might be required if the support for a specific hardware in a given size is only supported | ||
// with a newer version of the image. | ||
// | ||
// If the size in question is not found, no restrictions apply. | ||
// If the image in question is not found, no restrictions apply as well. | ||
// If the image in question is found, but does not match the given expression, machine creation must be forbidden. | ||
type SizeImageConstraint struct { | ||
Base | ||
// Images a map from imageID to semver compatible matcher string | ||
// example: | ||
// images: | ||
// ubuntu: ">= 20.04.20211011" | ||
// debian: ">= 10.0.20210101" | ||
Images map[string]string `rethinkdb:"images" json:"images"` | ||
} | ||
|
||
// SizeImageConstraints is a slice of ImageConstraint | ||
type SizeImageConstraints []SizeImageConstraint | ||
|
||
func (scs *SizeImageConstraints) Validate() error { | ||
for _, c := range *scs { | ||
err := c.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (sc *SizeImageConstraint) Validate() error { | ||
for os, vc := range sc.Images { | ||
// no pure wildcard in images | ||
if os == "*" { | ||
return fmt.Errorf("just '*' is not allowed as image os constraint") | ||
} | ||
// a single "*" is possible | ||
if strings.TrimSpace(vc) == "*" { | ||
continue | ||
} | ||
_, _, err := convertToOpAndVersion(vc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (scs *SizeImageConstraints) Matches(size Size, image Image) error { | ||
for _, sc := range *scs { | ||
if sc.ID == size.ID { | ||
return sc.Matches(size, image) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (sc *SizeImageConstraint) Matches(size Size, image Image) error { | ||
if sc.ID != size.ID { | ||
return nil | ||
} | ||
for os, versionconstraint := range sc.Images { | ||
if os != image.OS { | ||
continue | ||
} | ||
version, err := semver.NewVersion(image.Version) | ||
if err != nil { | ||
return fmt.Errorf("version of image is invalid %w", err) | ||
} | ||
|
||
// FIXME is this a valid assumption | ||
if version.Patch() == 0 { | ||
return fmt.Errorf("no patch version given") | ||
} | ||
c, err := semver.NewConstraint(versionconstraint) | ||
if err != nil { | ||
return fmt.Errorf("versionconstraint %s is invalid %w", versionconstraint, err) | ||
} | ||
if !c.Check(version) { | ||
return fmt.Errorf("given size:%s with image:%s does violate image constraint:%s %s", size.ID, image.OS+"-"+image.Version, os, c.String()) | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.