revert pull request #17

This commit is contained in:
Brad Rydzewski 2019-03-17 18:37:29 -07:00
parent b32d023dca
commit a16b72d961
12 changed files with 11 additions and 140 deletions

View File

@ -29,5 +29,5 @@ $ drone-yaml verify 642909eb4c3d47e33999235c0598353c samples/simple.yml
Compile the yaml file:
```text
$ drone-yaml compile samples/simple.yml > samples/simple.json
$ drone-yaml compile samples/simple.yml samples/simple.json
```

View File

@ -201,7 +201,6 @@ var (
password = compile.Flag("netrc-password", "netrc password").PlaceHolder("x-oauth-basic").String()
machine = compile.Flag("netrc-machine", "netrc machine").PlaceHolder("github.com").String()
memlimit = compile.Flag("mem-limit", "memory limit").PlaceHolder("1GB").Bytes()
cpulimit = compile.Flag("cpu-limit", "cpu limit").PlaceHolder("2").Int64()
)
func runCompile() error {
@ -269,7 +268,7 @@ func runCompile() error {
transform.WithEnviron(*environ),
transform.WithEnviron(defaultEnvs()),
transform.WithLables(*labels),
transform.WithLimits(int64(*memlimit), int64(*cpulimit)),
transform.WithLimits(int64(*memlimit), 0),
transform.WithNetrc(*machine, *username, *password),
transform.WithNetworks(*network),
transform.WithProxy(),

View File

@ -84,7 +84,7 @@ func toResourceObject(from *yaml.ResourceObject) *engine.ResourceObject {
return nil
}
return &engine.ResourceObject{
CPU: int64(from.CPU),
// TODO(bradrydzewski) set the CPU resource limit.
Memory: int64(from.Memory),
}
}

View File

@ -19,7 +19,7 @@ import "github.com/drone/drone-runtime/engine"
// WithLimits is a transform function that applies
// resource limits to the container processes.
func WithLimits(memlimit int64, cpulimit int64) func(*engine.Spec) {
func WithLimits(memlimit, cpulimit int64) func(*engine.Spec) {
return func(spec *engine.Spec) {
// if no limits are defined exit immediately.
if memlimit == 0 && cpulimit == 0 {
@ -34,12 +34,8 @@ func WithLimits(memlimit int64, cpulimit int64) func(*engine.Spec) {
if step.Resources.Limits == nil {
step.Resources.Limits = &engine.ResourceObject{}
}
if memlimit != 0 {
step.Resources.Limits.Memory = memlimit
}
if cpulimit != 0 {
step.Resources.Limits.CPU = cpulimit * 1000
}
step.Resources.Limits.Memory = memlimit
step.Resources.Limits.CPU = cpulimit
}
}
}

View File

@ -1,4 +1,3 @@
// Copyright Jesse Haka.
// Copyright the Drone Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -36,47 +35,7 @@ func TestWithLimits(t *testing.T) {
if got, want := step.Resources.Limits.Memory, int64(1); got != want {
t.Errorf("Want memory limit %v, got %v", want, got)
}
if got, want := step.Resources.Limits.CPU, int64(2000); got != want {
t.Errorf("Want cpu limit %v, got %v", want, got)
}
}
func TestWithMemory(t *testing.T) {
step := &engine.Step{
Metadata: engine.Metadata{
UID: "1",
Name: "build",
},
Docker: &engine.DockerStep{},
}
spec := &engine.Spec{
Steps: []*engine.Step{step},
}
WithLimits(1, 0)(spec)
if got, want := step.Resources.Limits.Memory, int64(1); got != want {
t.Errorf("Want memory limit %v, got %v", want, got)
}
if got, want := step.Resources.Limits.CPU, int64(0); got != want {
t.Errorf("Want cpu limit %v, got %v", want, got)
}
}
func TestWithCPU(t *testing.T) {
step := &engine.Step{
Metadata: engine.Metadata{
UID: "1",
Name: "build",
},
Docker: &engine.DockerStep{},
}
spec := &engine.Spec{
Steps: []*engine.Step{step},
}
WithLimits(0, 3)(spec)
if got, want := step.Resources.Limits.Memory, int64(0); got != want {
t.Errorf("Want memory limit %v, got %v", want, got)
}
if got, want := step.Resources.Limits.CPU, int64(3000); got != want {
if got, want := step.Resources.Limits.CPU, int64(2); got != want {
t.Errorf("Want cpu limit %v, got %v", want, got)
}
}

View File

@ -97,7 +97,7 @@ type (
// ResourceObject describes compute resource
// requirements.
ResourceObject struct {
CPU MilliSize `json:"cpu"`
CPU string `json:"cpu"`
Memory BytesSize `json:"memory"`
}

View File

@ -8,7 +8,7 @@ steps:
- go build
resources:
limits:
cpu: 2
cpu: 200m
memory: '100Mi'
requests:
cpu: 100m

View File

@ -13,10 +13,10 @@ steps:
- go build
resources:
limits:
cpu: 2000
cpu: 200m
memory: 100MiB
requests:
cpu: 100
cpu: 100m
memory: 50MiB
...

View File

@ -22,8 +22,6 @@ func isPrimative(v interface{}) bool {
return true
case yaml.BytesSize:
return true
case yaml.MilliSize:
return true
default:
return false
}

View File

@ -216,8 +216,6 @@ func writeValue(w writer, v interface{}) {
writeMappingStr(w, v)
case yaml.BytesSize:
writeValue(w, v.String())
case yaml.MilliSize:
writeValue(w, v.String())
}
}

View File

@ -1,4 +1,3 @@
// Copyright Jesse Haka.
// Copyright the Drone Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -16,9 +15,6 @@
package yaml
import (
"strconv"
"strings"
"github.com/docker/go-units"
)
@ -53,39 +49,3 @@ func (b *BytesSize) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (b BytesSize) String() string {
return units.BytesSize(float64(b))
}
// MilliSize will convert cpus to millicpus as int64.
// for instance "1" will be converted to 1000 and "100m" to 100
type MilliSize int64
// UnmarshalYAML implements yaml unmarshalling.
func (m *MilliSize) UnmarshalYAML(unmarshal func(interface{}) error) error {
var intType int64
if err := unmarshal(&intType); err == nil {
*m = MilliSize(intType * 1000)
return nil
}
var stringType string
if err := unmarshal(&stringType); err != nil {
return err
}
if len(stringType) > 0 {
lastChar := string(stringType[len(stringType)-1:])
if lastChar == "m" {
// convert to int64
i, err := strconv.ParseInt(strings.TrimSuffix(stringType, "m"), 10, 64)
if err != nil {
return err
}
*m = MilliSize(i)
}
}
return nil
}
// String returns a human-readable cpu millis,
// (eg. "1000", "10").
func (m MilliSize) String() string {
return strconv.FormatInt(int64(m), 10)
}

View File

@ -59,42 +59,3 @@ func TestBytesSize(t *testing.T) {
}
}
}
func TestMilliSize(t *testing.T) {
tests := []struct {
yaml string
size int64
text string
}{
{
yaml: "100m",
size: 100,
text: "100",
},
{
yaml: "1",
size: 1000,
text: "1000",
},
{
yaml: "0m",
size: 0,
text: "0",
},
}
for _, test := range tests {
in := []byte(test.yaml)
out := MilliSize(0)
err := yaml.Unmarshal(in, &out)
if err != nil {
t.Error(err)
return
}
if got, want := int64(out), test.size; got != want {
t.Errorf("Want millis %d, got %d", want, got)
}
if got, want := out.String(), test.text; got != want {
t.Errorf("Want text %s, got %s", want, got)
}
}
}