Skip to content

Commit

Permalink
fix: check attr is nil prior going copy (#74)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Sep 1, 2023
1 parent f96c282 commit fed1855
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go/manifest/v2beta2/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (g *Group) Validate(helper *validateManifestGroupsHelper) error {
func (g *Group) checkAgainstGSpec(gspec *groupSpec) error {
for _, svc := range g.Services {
if err := svc.checkAgainstGSpec(gspec); err != nil {
return fmt.Errorf("%s: group %q: %w", ErrManifestCrossValidation, g.Name, err)
return fmt.Errorf("%w: group %q: %w", ErrManifestCrossValidation, g.Name, err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions go/manifest/v2beta2/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (groups Groups) Validate() error {
}

func (groups Groups) CheckAgainstGSpecs(gspecs dtypes.GroupSpecs) error {
gspecs = gspecs.Dup()

if err := groups.Validate(); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go/manifest/v2beta2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (s *Service) checkAgainstGSpec(gspec *groupSpec) error {
for idx := range gspec.gs.Resources {
if s.Resources.ID == gspec.gs.Resources[idx].ID {
gRes = &gspec.gs.Resources[idx]
break
}
}

Expand Down
20 changes: 20 additions & 0 deletions go/node/deployment/v1beta3/groupspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ var _ ResourceGroup = (*GroupSpec)(nil)

type GroupSpecs []*GroupSpec

func (gspecs GroupSpecs) Dup() GroupSpecs {
res := make(GroupSpecs, 0, len(gspecs))

for _, gspec := range gspecs {
gs := gspec.Dup()
res = append(res, &gs)
}
return res
}

func (g GroupSpec) Dup() GroupSpec {
res := GroupSpec{
Name: g.Name,
Requirements: g.Requirements.Dup(),
Resources: g.Resources,
}

return res
}

// ValidateBasic asserts non-zero values
func (g GroupSpec) ValidateBasic() error {
return g.validate()
Expand Down
8 changes: 8 additions & 0 deletions go/node/deployment/v1beta3/resourceunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ func (r *ResourceUnit) FullPrice() sdk.DecCoin {
return sdk.NewDecCoinFromDec(r.Price.Denom, r.Price.Amount.MulInt64(int64(r.Count)))
}

func (r *ResourceUnit) Dup() ResourceUnit {
return ResourceUnit{
Resources: r.Resources.Dup(),
Count: r.Count,
Price: r.GetPrice(),
}
}

func (r *ResourceUnit) validate() error {
if r.Count > uint32(validationConfig.MaxUnitCount) || r.Count < uint32(validationConfig.MinUnitCount) {
return fmt.Errorf("error: invalid unit count (%v > %v > %v fails)",
Expand Down
10 changes: 10 additions & 0 deletions go/node/deployment/v1beta3/resourceunits.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func (s ResourceUnits) Less(i, j int) bool {
return s[i].ID < s[j].ID
}

func (s ResourceUnits) Dup() ResourceUnits {
res := make(ResourceUnits, 0, len(s))

for _, ru := range s {
res = append(res, ru.Dup())
}

return s
}

func (s ResourceUnits) Validate() error {
if count := len(s); count > validationConfig.MaxGroupUnits {
return fmt.Errorf("too many units (%v > %v)", count, validationConfig.MaxGroupUnits)
Expand Down
11 changes: 11 additions & 0 deletions go/node/types/v1beta3/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func (val attributeValue) AsString() (string, bool) {
return val.value, true
}

func (m PlacementRequirements) Dup() PlacementRequirements {
return PlacementRequirements{
SignedBy: m.SignedBy,
Attributes: m.Attributes.Dup(),
}
}

func NewStringAttribute(key, val string) Attribute {
return Attribute{
Key: key,
Expand Down Expand Up @@ -141,6 +148,10 @@ func (attr Attributes) ValidateWithRegex(r *regexp.Regexp) error {
}

func (attr Attributes) Dup() Attributes {
if attr == nil {
return nil
}

res := make(Attributes, 0, len(attr))

for _, pair := range attr {
Expand Down

0 comments on commit fed1855

Please sign in to comment.