diff --git a/README.md b/README.md index 90a067e..fe58ff9 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/main.go b/main.go index 819f201..ecd63b7 100644 --- a/main.go +++ b/main.go @@ -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(), diff --git a/yaml/compiler/convert.go b/yaml/compiler/convert.go index 6c2ae9c..e593ab2 100644 --- a/yaml/compiler/convert.go +++ b/yaml/compiler/convert.go @@ -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), } } diff --git a/yaml/compiler/transform/limits.go b/yaml/compiler/transform/limits.go index a012298..52a6d26 100644 --- a/yaml/compiler/transform/limits.go +++ b/yaml/compiler/transform/limits.go @@ -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 } } } diff --git a/yaml/compiler/transform/limits_test.go b/yaml/compiler/transform/limits_test.go index 7430d7c..22eb53a 100644 --- a/yaml/compiler/transform/limits_test.go +++ b/yaml/compiler/transform/limits_test.go @@ -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) } } diff --git a/yaml/pipeline.go b/yaml/pipeline.go index 483c8ce..7a2865a 100644 --- a/yaml/pipeline.go +++ b/yaml/pipeline.go @@ -97,7 +97,7 @@ type ( // ResourceObject describes compute resource // requirements. ResourceObject struct { - CPU MilliSize `json:"cpu"` + CPU string `json:"cpu"` Memory BytesSize `json:"memory"` } diff --git a/yaml/pretty/testdata/pipeline_resources.yml b/yaml/pretty/testdata/pipeline_resources.yml index 1233c25..3e2cd3d 100644 --- a/yaml/pretty/testdata/pipeline_resources.yml +++ b/yaml/pretty/testdata/pipeline_resources.yml @@ -8,7 +8,7 @@ steps: - go build resources: limits: - cpu: 2 + cpu: 200m memory: '100Mi' requests: cpu: 100m diff --git a/yaml/pretty/testdata/pipeline_resources.yml.golden b/yaml/pretty/testdata/pipeline_resources.yml.golden index c592200..52bdac3 100644 --- a/yaml/pretty/testdata/pipeline_resources.yml.golden +++ b/yaml/pretty/testdata/pipeline_resources.yml.golden @@ -13,10 +13,10 @@ steps: - go build resources: limits: - cpu: 2000 + cpu: 200m memory: 100MiB requests: - cpu: 100 + cpu: 100m memory: 50MiB ... diff --git a/yaml/pretty/util.go b/yaml/pretty/util.go index 9441f87..aee7448 100644 --- a/yaml/pretty/util.go +++ b/yaml/pretty/util.go @@ -22,8 +22,6 @@ func isPrimative(v interface{}) bool { return true case yaml.BytesSize: return true - case yaml.MilliSize: - return true default: return false } diff --git a/yaml/pretty/writer.go b/yaml/pretty/writer.go index 11cc8ff..a137db9 100644 --- a/yaml/pretty/writer.go +++ b/yaml/pretty/writer.go @@ -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()) } } diff --git a/yaml/unit.go b/yaml/unit.go index 84b43c6..16fb9d5 100644 --- a/yaml/unit.go +++ b/yaml/unit.go @@ -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) -} diff --git a/yaml/unit_test.go b/yaml/unit_test.go index 0e600aa..aa944ab 100644 --- a/yaml/unit_test.go +++ b/yaml/unit_test.go @@ -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) - } - } -}