-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ParseResourceLimits utility and fixed mounted file encodi…
…ng bug (#70) Signed-off-by: Ben Meier <[email protected]>
- Loading branch information
1 parent
2c11108
commit eb4256a
Showing
5 changed files
with
147 additions
and
31 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 |
---|---|---|
|
@@ -14,6 +14,12 @@ | |
|
||
package types | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
//go:generate go run github.com/atombender/[email protected] -v --schema-output=https://score.dev/schemas/score=types.gen.go --schema-package=https://score.dev/schemas/score=types --schema-root-type=https://score.dev/schemas/score=Workload ../schema/files/score-v1b1.json.modified | ||
|
||
func (m *ResourceMetadata) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
|
@@ -33,3 +39,52 @@ func (m *WorkloadMetadata) UnmarshalYAML(unmarshal func(interface{}) error) erro | |
*m = WorkloadMetadata(out) | ||
return nil | ||
} | ||
|
||
// findIPowerSuffix is used in ParseResourceLimits to parse memory units. | ||
func findIPowerSuffix(raw string, suffi []string, base int64) (suffix string, m int64) { | ||
m = 1 | ||
for _, s := range suffi { | ||
m *= base | ||
if strings.HasSuffix(raw, s) { | ||
return s, m | ||
} | ||
} | ||
return "", 0 | ||
} | ||
|
||
// ParseResourceLimits parses a resource limits definition into milli-cpus and memory bytes if present. | ||
// For example, 500m cpus = 500 millicpus, while 2 cpus = 2000 cpus. 1M == 1000000 bytes of memory, while 1Ki = 1024. | ||
func ParseResourceLimits(rl ResourcesLimits) (milliCpus *int, memoryBytes *int64, err error) { | ||
if rl.Cpu != nil { | ||
isMilli := strings.HasSuffix(*rl.Cpu, "m") | ||
c := strings.TrimSuffix(*rl.Cpu, "m") | ||
v, err := strconv.ParseFloat(c, 64) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to parse cpus '%s' as a number", c) | ||
} | ||
if !isMilli { | ||
v *= 1000 | ||
} | ||
iv := int(v) | ||
milliCpus = &iv | ||
} | ||
if rl.Memory != nil { | ||
// https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/#memory-units | ||
raw := *rl.Memory | ||
var multiplier int64 = 1 | ||
if s, m := findIPowerSuffix(raw, []string{"K", "M", "G", "T"}, 1000); m > 0 { | ||
raw = strings.TrimSuffix(raw, s) | ||
multiplier = m | ||
} else if s, m = findIPowerSuffix(raw, []string{"Ki", "Mi", "Gi", "Ti"}, 1024); m > 0 { | ||
raw = strings.TrimSuffix(raw, s) | ||
multiplier = m | ||
} | ||
if v, err := strconv.ParseInt(raw, 10, 64); err != nil { | ||
return nil, nil, fmt.Errorf("failed to parse memory '%s' as a number", raw) | ||
} else { | ||
v *= multiplier | ||
memoryBytes = &v | ||
} | ||
} | ||
return | ||
} |
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,46 @@ | ||
// Copyright 2020 Humanitec | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Ref[k any](in k) *k { | ||
return &in | ||
} | ||
|
||
func DerefOr[k any](in *k, def k) k { | ||
if in == nil { | ||
return def | ||
} | ||
return *in | ||
} | ||
|
||
func parseAndFormatResourceLimits(rl ResourcesLimits) string { | ||
c, m, err := ParseResourceLimits(rl) | ||
return fmt.Sprintf("%d %d %v", DerefOr(c, -1), DerefOr(m, -1), err) | ||
} | ||
|
||
func TestParseResourceLimits(t *testing.T) { | ||
assert.Equal(t, "-1 -1 <nil>", parseAndFormatResourceLimits(ResourcesLimits{})) | ||
assert.Equal(t, "1000 1000000 <nil>", parseAndFormatResourceLimits(ResourcesLimits{Cpu: Ref("1"), Memory: Ref("1M")})) | ||
assert.Equal(t, "-1 -1 failed to parse cpus 'banana' as a number", parseAndFormatResourceLimits(ResourcesLimits{Cpu: Ref("banana"), Memory: nil})) | ||
assert.Equal(t, "-1 -1 failed to parse memory 'banana' as a number", parseAndFormatResourceLimits(ResourcesLimits{Cpu: nil, Memory: Ref("banana")})) | ||
assert.Equal(t, "200 128974848 <nil>", parseAndFormatResourceLimits(ResourcesLimits{Cpu: Ref("200m"), Memory: Ref("123Mi")})) | ||
} |