From 5eb70c10348b97449583c4347413d8d265ff9c94 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sun, 17 Mar 2019 20:03:27 -0700 Subject: [PATCH] implement basic cpu resource limits --- yaml/compiler/convert.go | 2 +- yaml/pipeline.go | 2 +- yaml/pretty/testdata/pipeline_resources.yml | 4 ++-- yaml/pretty/testdata/pipeline_resources.yml.golden | 4 ++-- yaml/pretty/util.go | 2 +- yaml/pretty/writer.go | 10 +++++++++- 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/yaml/compiler/convert.go b/yaml/compiler/convert.go index e593ab2..a182255 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{ - // TODO(bradrydzewski) set the CPU resource limit. + CPU: from.CPU, Memory: int64(from.Memory), } } diff --git a/yaml/pipeline.go b/yaml/pipeline.go index 7a2865a..e4e9e6a 100644 --- a/yaml/pipeline.go +++ b/yaml/pipeline.go @@ -97,7 +97,7 @@ type ( // ResourceObject describes compute resource // requirements. ResourceObject struct { - CPU string `json:"cpu"` + CPU int64 `json:"cpu" yaml:"cpu"` Memory BytesSize `json:"memory"` } diff --git a/yaml/pretty/testdata/pipeline_resources.yml b/yaml/pretty/testdata/pipeline_resources.yml index 3e2cd3d..9ac81a3 100644 --- a/yaml/pretty/testdata/pipeline_resources.yml +++ b/yaml/pretty/testdata/pipeline_resources.yml @@ -8,8 +8,8 @@ steps: - go build resources: limits: - cpu: 200m + cpu: 2 memory: '100Mi' requests: - cpu: 100m + cpu: 1 memory: '50Mi' diff --git a/yaml/pretty/testdata/pipeline_resources.yml.golden b/yaml/pretty/testdata/pipeline_resources.yml.golden index 52bdac3..635f867 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: 200m + cpu: 2 memory: 100MiB requests: - cpu: 100m + cpu: 1 memory: 50MiB ... diff --git a/yaml/pretty/util.go b/yaml/pretty/util.go index aee7448..21b9ce7 100644 --- a/yaml/pretty/util.go +++ b/yaml/pretty/util.go @@ -18,7 +18,7 @@ import "github.com/drone/drone-yaml/yaml" func isPrimative(v interface{}) bool { switch v.(type) { - case bool, string, int, float64: + case bool, string, int, int64, float64: return true case yaml.BytesSize: return true diff --git a/yaml/pretty/writer.go b/yaml/pretty/writer.go index a137db9..8de89d2 100644 --- a/yaml/pretty/writer.go +++ b/yaml/pretty/writer.go @@ -183,6 +183,12 @@ func writeInt(w writer, v int) { ) } +func writeInt64(w writer, v int64) { + w.WriteString( + strconv.FormatInt(v, 10), + ) +} + func writeEncode(w writer, v string) { if len(v) == 0 { w.WriteByte('"') @@ -204,7 +210,7 @@ func writeValue(w writer, v interface{}) { return } switch v := v.(type) { - case bool, int, float64, string: + case bool, int, int64, float64, string: writeScalar(w, v) case []interface{}: writeSequence(w, v) @@ -225,6 +231,8 @@ func writeScalar(w writer, v interface{}) { writeBool(w, v) case int: writeInt(w, v) + case int64: + writeInt64(w, v) case float64: writeFloat(w, v) case string: