mirror of
https://github.com/thegeeklab/drone-yaml.git
synced 2024-11-22 01:50:40 +00:00
revert pull request #17
This commit is contained in:
parent
b32d023dca
commit
a16b72d961
@ -29,5 +29,5 @@ $ drone-yaml verify 642909eb4c3d47e33999235c0598353c samples/simple.yml
|
|||||||
Compile the yaml file:
|
Compile the yaml file:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
$ drone-yaml compile samples/simple.yml > samples/simple.json
|
$ drone-yaml compile samples/simple.yml samples/simple.json
|
||||||
```
|
```
|
||||||
|
3
main.go
3
main.go
@ -201,7 +201,6 @@ var (
|
|||||||
password = compile.Flag("netrc-password", "netrc password").PlaceHolder("x-oauth-basic").String()
|
password = compile.Flag("netrc-password", "netrc password").PlaceHolder("x-oauth-basic").String()
|
||||||
machine = compile.Flag("netrc-machine", "netrc machine").PlaceHolder("github.com").String()
|
machine = compile.Flag("netrc-machine", "netrc machine").PlaceHolder("github.com").String()
|
||||||
memlimit = compile.Flag("mem-limit", "memory limit").PlaceHolder("1GB").Bytes()
|
memlimit = compile.Flag("mem-limit", "memory limit").PlaceHolder("1GB").Bytes()
|
||||||
cpulimit = compile.Flag("cpu-limit", "cpu limit").PlaceHolder("2").Int64()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func runCompile() error {
|
func runCompile() error {
|
||||||
@ -269,7 +268,7 @@ func runCompile() error {
|
|||||||
transform.WithEnviron(*environ),
|
transform.WithEnviron(*environ),
|
||||||
transform.WithEnviron(defaultEnvs()),
|
transform.WithEnviron(defaultEnvs()),
|
||||||
transform.WithLables(*labels),
|
transform.WithLables(*labels),
|
||||||
transform.WithLimits(int64(*memlimit), int64(*cpulimit)),
|
transform.WithLimits(int64(*memlimit), 0),
|
||||||
transform.WithNetrc(*machine, *username, *password),
|
transform.WithNetrc(*machine, *username, *password),
|
||||||
transform.WithNetworks(*network),
|
transform.WithNetworks(*network),
|
||||||
transform.WithProxy(),
|
transform.WithProxy(),
|
||||||
|
@ -84,7 +84,7 @@ func toResourceObject(from *yaml.ResourceObject) *engine.ResourceObject {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &engine.ResourceObject{
|
return &engine.ResourceObject{
|
||||||
CPU: int64(from.CPU),
|
// TODO(bradrydzewski) set the CPU resource limit.
|
||||||
Memory: int64(from.Memory),
|
Memory: int64(from.Memory),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ import "github.com/drone/drone-runtime/engine"
|
|||||||
|
|
||||||
// WithLimits is a transform function that applies
|
// WithLimits is a transform function that applies
|
||||||
// resource limits to the container processes.
|
// 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) {
|
return func(spec *engine.Spec) {
|
||||||
// if no limits are defined exit immediately.
|
// if no limits are defined exit immediately.
|
||||||
if memlimit == 0 && cpulimit == 0 {
|
if memlimit == 0 && cpulimit == 0 {
|
||||||
@ -34,12 +34,8 @@ func WithLimits(memlimit int64, cpulimit int64) func(*engine.Spec) {
|
|||||||
if step.Resources.Limits == nil {
|
if step.Resources.Limits == nil {
|
||||||
step.Resources.Limits = &engine.ResourceObject{}
|
step.Resources.Limits = &engine.ResourceObject{}
|
||||||
}
|
}
|
||||||
if memlimit != 0 {
|
|
||||||
step.Resources.Limits.Memory = memlimit
|
step.Resources.Limits.Memory = memlimit
|
||||||
}
|
step.Resources.Limits.CPU = cpulimit
|
||||||
if cpulimit != 0 {
|
|
||||||
step.Resources.Limits.CPU = cpulimit * 1000
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
// Copyright Jesse Haka.
|
|
||||||
// Copyright the Drone Authors.
|
// Copyright the Drone Authors.
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// 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 {
|
if got, want := step.Resources.Limits.Memory, int64(1); got != want {
|
||||||
t.Errorf("Want memory limit %v, got %v", want, got)
|
t.Errorf("Want memory limit %v, got %v", want, got)
|
||||||
}
|
}
|
||||||
if got, want := step.Resources.Limits.CPU, int64(2000); got != want {
|
if got, want := step.Resources.Limits.CPU, int64(2); 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 {
|
|
||||||
t.Errorf("Want cpu limit %v, got %v", want, got)
|
t.Errorf("Want cpu limit %v, got %v", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,7 +97,7 @@ type (
|
|||||||
// ResourceObject describes compute resource
|
// ResourceObject describes compute resource
|
||||||
// requirements.
|
// requirements.
|
||||||
ResourceObject struct {
|
ResourceObject struct {
|
||||||
CPU MilliSize `json:"cpu"`
|
CPU string `json:"cpu"`
|
||||||
Memory BytesSize `json:"memory"`
|
Memory BytesSize `json:"memory"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
yaml/pretty/testdata/pipeline_resources.yml
vendored
2
yaml/pretty/testdata/pipeline_resources.yml
vendored
@ -8,7 +8,7 @@ steps:
|
|||||||
- go build
|
- go build
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 2
|
cpu: 200m
|
||||||
memory: '100Mi'
|
memory: '100Mi'
|
||||||
requests:
|
requests:
|
||||||
cpu: 100m
|
cpu: 100m
|
||||||
|
@ -13,10 +13,10 @@ steps:
|
|||||||
- go build
|
- go build
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 2000
|
cpu: 200m
|
||||||
memory: 100MiB
|
memory: 100MiB
|
||||||
requests:
|
requests:
|
||||||
cpu: 100
|
cpu: 100m
|
||||||
memory: 50MiB
|
memory: 50MiB
|
||||||
|
|
||||||
...
|
...
|
||||||
|
@ -22,8 +22,6 @@ func isPrimative(v interface{}) bool {
|
|||||||
return true
|
return true
|
||||||
case yaml.BytesSize:
|
case yaml.BytesSize:
|
||||||
return true
|
return true
|
||||||
case yaml.MilliSize:
|
|
||||||
return true
|
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -216,8 +216,6 @@ func writeValue(w writer, v interface{}) {
|
|||||||
writeMappingStr(w, v)
|
writeMappingStr(w, v)
|
||||||
case yaml.BytesSize:
|
case yaml.BytesSize:
|
||||||
writeValue(w, v.String())
|
writeValue(w, v.String())
|
||||||
case yaml.MilliSize:
|
|
||||||
writeValue(w, v.String())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
40
yaml/unit.go
40
yaml/unit.go
@ -1,4 +1,3 @@
|
|||||||
// Copyright Jesse Haka.
|
|
||||||
// Copyright the Drone Authors.
|
// Copyright the Drone Authors.
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -16,9 +15,6 @@
|
|||||||
package yaml
|
package yaml
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -53,39 +49,3 @@ func (b *BytesSize) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
func (b BytesSize) String() string {
|
func (b BytesSize) String() string {
|
||||||
return units.BytesSize(float64(b))
|
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)
|
|
||||||
}
|
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user