Update use of Quantity in other classes

This commit is contained in:
Clayton Coleman
2016-05-17 00:36:56 -04:00
parent b2a01d4d94
commit 5e4308f91d
37 changed files with 204 additions and 242 deletions

View File

@@ -58,16 +58,7 @@ var Semantic = conversion.EqualitiesOrDie(
// TODO: if we decide it's important, it should be safe to start comparing the format.
//
// Uninitialized quantities are equivalent to 0 quantities.
if a.Amount == nil && b.MilliValue() == 0 {
return true
}
if b.Amount == nil && a.MilliValue() == 0 {
return true
}
if a.Amount == nil || b.Amount == nil {
return false
}
return a.Amount.Cmp(b.Amount) == 0
return a.Cmp(b) == 0
},
func(a, b unversioned.Time) bool {
return a.UTC() == b.UTC()

View File

@@ -23,8 +23,6 @@ import (
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/labels"
inf "gopkg.in/inf.v0"
)
func TestConversionError(t *testing.T) {
@@ -56,8 +54,8 @@ func TestSemantic(t *testing.T) {
{resource.Quantity{}, resource.MustParse("0"), true},
{resource.Quantity{}, resource.MustParse("1m"), false},
{
resource.Quantity{Amount: inf.NewDec(5, 0), Format: resource.BinarySI},
resource.Quantity{Amount: inf.NewDec(5, 0), Format: resource.DecimalSI},
resource.NewQuantity(5, resource.BinarySI),
resource.NewQuantity(5, resource.DecimalSI),
true,
},
{resource.MustParse("2m"), resource.MustParse("1m"), false},

View File

@@ -151,15 +151,17 @@ func PodRequestsAndLimits(pod *Pod) (reqs map[ResourceName]resource.Quantity, li
for name, quantity := range container.Resources.Requests {
if value, ok := reqs[name]; !ok {
reqs[name] = *quantity.Copy()
} else if err = value.Add(quantity); err != nil {
return nil, nil, err
} else {
value.Add(quantity)
reqs[name] = value
}
}
for name, quantity := range container.Resources.Limits {
if value, ok := limits[name]; !ok {
limits[name] = *quantity.Copy()
} else if err = value.Add(quantity); err != nil {
return nil, nil, err
} else {
value.Add(quantity)
limits[name] = value
}
}
}

View File

@@ -32,10 +32,10 @@ func TestResourceHelpers(t *testing.T) {
"kube.io/storage": memoryLimit,
},
}
if res := resourceSpec.Limits.Cpu(); *res != cpuLimit {
if res := resourceSpec.Limits.Cpu(); res.Cmp(cpuLimit) != 0 {
t.Errorf("expected cpulimit %v, got %v", cpuLimit, res)
}
if res := resourceSpec.Limits.Memory(); *res != memoryLimit {
if res := resourceSpec.Limits.Memory(); res.Cmp(memoryLimit) != 0 {
t.Errorf("expected memorylimit %v, got %v", memoryLimit, res)
}
resourceSpec = ResourceRequirements{
@@ -47,7 +47,7 @@ func TestResourceHelpers(t *testing.T) {
if res := resourceSpec.Limits.Cpu(); res.Value() != 0 {
t.Errorf("expected cpulimit %v, got %v", 0, res)
}
if res := resourceSpec.Limits.Memory(); *res != memoryLimit {
if res := resourceSpec.Limits.Memory(); res.Cmp(memoryLimit) != 0 {
t.Errorf("expected memorylimit %v, got %v", memoryLimit, res)
}
}

View File

@@ -25,14 +25,13 @@ import (
"errors"
"fmt"
codec1978 "github.com/ugorji/go/codec"
pkg4_inf_v0 "gopkg.in/inf.v0"
pkg3_resource "k8s.io/kubernetes/pkg/api/resource"
pkg2_unversioned "k8s.io/kubernetes/pkg/api/unversioned"
pkg7_fields "k8s.io/kubernetes/pkg/fields"
pkg6_labels "k8s.io/kubernetes/pkg/labels"
pkg8_runtime "k8s.io/kubernetes/pkg/runtime"
pkg6_fields "k8s.io/kubernetes/pkg/fields"
pkg5_labels "k8s.io/kubernetes/pkg/labels"
pkg7_runtime "k8s.io/kubernetes/pkg/runtime"
pkg1_types "k8s.io/kubernetes/pkg/types"
pkg5_intstr "k8s.io/kubernetes/pkg/util/intstr"
pkg4_intstr "k8s.io/kubernetes/pkg/util/intstr"
"reflect"
"runtime"
time "time"
@@ -68,16 +67,15 @@ func init() {
panic(err)
}
if false { // reference the types, but skip this branch at build/run time
var v0 pkg4_inf_v0.Dec
var v1 pkg3_resource.Quantity
var v2 pkg2_unversioned.Time
var v3 pkg7_fields.Selector
var v4 pkg6_labels.Selector
var v5 pkg8_runtime.Object
var v6 pkg1_types.UID
var v7 pkg5_intstr.IntOrString
var v8 time.Time
_, _, _, _, _, _, _, _, _ = v0, v1, v2, v3, v4, v5, v6, v7, v8
var v0 pkg3_resource.Quantity
var v1 pkg2_unversioned.Time
var v2 pkg6_fields.Selector
var v3 pkg5_labels.Selector
var v4 pkg7_runtime.Object
var v5 pkg1_types.UID
var v6 pkg4_intstr.IntOrString
var v7 time.Time
_, _, _, _, _, _, _, _ = v0, v1, v2, v3, v4, v5, v6, v7
}
}
@@ -16227,7 +16225,7 @@ func (x *HTTPGetAction) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
}
case "port":
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv5 := &x.Port
yym6 := z.DecBinary()
@@ -16306,7 +16304,7 @@ func (x *HTTPGetAction) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
}
z.DecSendContainerState(codecSelfer_containerArrayElem1234)
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv13 := &x.Port
yym14 := z.DecBinary()
@@ -16542,7 +16540,7 @@ func (x *TCPSocketAction) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
switch yys3 {
case "port":
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv4 := &x.Port
yym5 := z.DecBinary()
@@ -16581,7 +16579,7 @@ func (x *TCPSocketAction) codecDecodeSelfFromArray(l int, d *codec1978.Decoder)
}
z.DecSendContainerState(codecSelfer_containerArrayElem1234)
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv7 := &x.Port
yym8 := z.DecBinary()
@@ -31535,7 +31533,7 @@ func (x *ServicePort) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
}
case "targetPort":
if r.TryDecodeAsNil() {
x.TargetPort = pkg5_intstr.IntOrString{}
x.TargetPort = pkg4_intstr.IntOrString{}
} else {
yyv7 := &x.TargetPort
yym8 := z.DecBinary()
@@ -31628,7 +31626,7 @@ func (x *ServicePort) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
}
z.DecSendContainerState(codecSelfer_containerArrayElem1234)
if r.TryDecodeAsNil() {
x.TargetPort = pkg5_intstr.IntOrString{}
x.TargetPort = pkg4_intstr.IntOrString{}
} else {
yyv14 := &x.TargetPort
yym15 := z.DecBinary()
@@ -45477,7 +45475,7 @@ func (x *List) CodecEncodeSelf(e *codec1978.Encoder) {
_ = yym9
if false {
} else {
h.encSliceruntime_Object(([]pkg8_runtime.Object)(x.Items), e)
h.encSliceruntime_Object(([]pkg7_runtime.Object)(x.Items), e)
}
}
} else {
@@ -45491,7 +45489,7 @@ func (x *List) CodecEncodeSelf(e *codec1978.Encoder) {
_ = yym10
if false {
} else {
h.encSliceruntime_Object(([]pkg8_runtime.Object)(x.Items), e)
h.encSliceruntime_Object(([]pkg7_runtime.Object)(x.Items), e)
}
}
}
@@ -45628,7 +45626,7 @@ func (x *List) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
_ = yym7
if false {
} else {
h.decSliceruntime_Object((*[]pkg8_runtime.Object)(yyv6), d)
h.decSliceruntime_Object((*[]pkg7_runtime.Object)(yyv6), d)
}
}
case "kind":
@@ -45699,7 +45697,7 @@ func (x *List) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
_ = yym14
if false {
} else {
h.decSliceruntime_Object((*[]pkg8_runtime.Object)(yyv13), d)
h.decSliceruntime_Object((*[]pkg7_runtime.Object)(yyv13), d)
}
}
yyj10++
@@ -56094,7 +56092,7 @@ func (x codecSelfer1234) decResourceList(v *ResourceList, d *codec1978.Decoder)
yyl1 := r.ReadMapStart()
yybh1 := z.DecBasicHandle()
if yyv1 == nil {
yyrl1, _ := z.DecInferLen(yyl1, yybh1.MaxInitLen, 40)
yyrl1, _ := z.DecInferLen(yyl1, yybh1.MaxInitLen, 80)
yyv1 = make(map[ResourceName]pkg3_resource.Quantity, yyrl1)
*v = yyv1
}
@@ -56643,7 +56641,7 @@ func (x codecSelfer1234) decSliceEvent(v *[]Event, d *codec1978.Decoder) {
}
}
func (x codecSelfer1234) encSliceruntime_Object(v []pkg8_runtime.Object, e *codec1978.Encoder) {
func (x codecSelfer1234) encSliceruntime_Object(v []pkg7_runtime.Object, e *codec1978.Encoder) {
var h codecSelfer1234
z, r := codec1978.GenHelperEncoder(e)
_, _, _ = h, z, r
@@ -56665,7 +56663,7 @@ func (x codecSelfer1234) encSliceruntime_Object(v []pkg8_runtime.Object, e *code
z.EncSendContainerState(codecSelfer_containerArrayEnd1234)
}
func (x codecSelfer1234) decSliceruntime_Object(v *[]pkg8_runtime.Object, d *codec1978.Decoder) {
func (x codecSelfer1234) decSliceruntime_Object(v *[]pkg7_runtime.Object, d *codec1978.Decoder) {
var h codecSelfer1234
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
@@ -56676,7 +56674,7 @@ func (x codecSelfer1234) decSliceruntime_Object(v *[]pkg8_runtime.Object, d *cod
_ = yyc1
if yyl1 == 0 {
if yyv1 == nil {
yyv1 = []pkg8_runtime.Object{}
yyv1 = []pkg7_runtime.Object{}
yyc1 = true
} else if len(yyv1) != 0 {
yyv1 = yyv1[:0]
@@ -56696,10 +56694,10 @@ func (x codecSelfer1234) decSliceruntime_Object(v *[]pkg8_runtime.Object, d *cod
if yyrl1 <= cap(yyv1) {
yyv1 = yyv1[:yyrl1]
} else {
yyv1 = make([]pkg8_runtime.Object, yyrl1)
yyv1 = make([]pkg7_runtime.Object, yyrl1)
}
} else {
yyv1 = make([]pkg8_runtime.Object, yyrl1)
yyv1 = make([]pkg7_runtime.Object, yyrl1)
}
yyc1 = true
yyrr1 = len(yyv1)
@@ -56752,7 +56750,7 @@ func (x codecSelfer1234) decSliceruntime_Object(v *[]pkg8_runtime.Object, d *cod
for ; !r.CheckBreak(); yyj1++ {
if yyj1 >= len(yyv1) {
yyv1 = append(yyv1, nil) // var yyz1 pkg8_runtime.Object
yyv1 = append(yyv1, nil) // var yyz1 pkg7_runtime.Object
yyc1 = true
}
yyh1.ElemContainerState(yyj1)
@@ -56779,7 +56777,7 @@ func (x codecSelfer1234) decSliceruntime_Object(v *[]pkg8_runtime.Object, d *cod
yyv1 = yyv1[:yyj1]
yyc1 = true
} else if yyj1 == 0 && yyv1 == nil {
yyv1 = []pkg8_runtime.Object{}
yyv1 = []pkg7_runtime.Object{}
yyc1 = true
}
}

View File

@@ -20,8 +20,6 @@ import (
"encoding/json"
"fmt"
inf "gopkg.in/inf.v0"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime"
@@ -670,8 +668,8 @@ func Convert_v1_ResourceList_To_api_ResourceList(in *ResourceList, out *api.Reso
// TODO(#18538): We round up resource values to milli scale to maintain API compatibility.
// In the future, we should instead reject values that need rounding.
const milliScale = 3
value.Amount.Round(value.Amount, milliScale, inf.RoundUp)
const milliScale = -3
value.RoundUp(milliScale)
converted[api.ResourceName(key)] = *value
}

View File

@@ -25,12 +25,11 @@ import (
"errors"
"fmt"
codec1978 "github.com/ugorji/go/codec"
pkg4_inf_v0 "gopkg.in/inf.v0"
pkg3_resource "k8s.io/kubernetes/pkg/api/resource"
pkg2_unversioned "k8s.io/kubernetes/pkg/api/unversioned"
pkg6_runtime "k8s.io/kubernetes/pkg/runtime"
pkg5_runtime "k8s.io/kubernetes/pkg/runtime"
pkg1_types "k8s.io/kubernetes/pkg/types"
pkg5_intstr "k8s.io/kubernetes/pkg/util/intstr"
pkg4_intstr "k8s.io/kubernetes/pkg/util/intstr"
"reflect"
"runtime"
time "time"
@@ -66,14 +65,13 @@ func init() {
panic(err)
}
if false { // reference the types, but skip this branch at build/run time
var v0 pkg4_inf_v0.Dec
var v1 pkg3_resource.Quantity
var v2 pkg2_unversioned.Time
var v3 pkg6_runtime.RawExtension
var v4 pkg1_types.UID
var v5 pkg5_intstr.IntOrString
var v6 time.Time
_, _, _, _, _, _, _ = v0, v1, v2, v3, v4, v5, v6
var v0 pkg3_resource.Quantity
var v1 pkg2_unversioned.Time
var v2 pkg5_runtime.RawExtension
var v3 pkg1_types.UID
var v4 pkg4_intstr.IntOrString
var v5 time.Time
_, _, _, _, _, _ = v0, v1, v2, v3, v4, v5
}
}
@@ -15844,7 +15842,7 @@ func (x *HTTPGetAction) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
}
case "port":
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv5 := &x.Port
yym6 := z.DecBinary()
@@ -15923,7 +15921,7 @@ func (x *HTTPGetAction) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
}
z.DecSendContainerState(codecSelfer_containerArrayElem1234)
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv13 := &x.Port
yym14 := z.DecBinary()
@@ -16152,7 +16150,7 @@ func (x *TCPSocketAction) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
switch yys3 {
case "port":
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv4 := &x.Port
yym5 := z.DecBinary()
@@ -16191,7 +16189,7 @@ func (x *TCPSocketAction) codecDecodeSelfFromArray(l int, d *codec1978.Decoder)
}
z.DecSendContainerState(codecSelfer_containerArrayElem1234)
if r.TryDecodeAsNil() {
x.Port = pkg5_intstr.IntOrString{}
x.Port = pkg4_intstr.IntOrString{}
} else {
yyv7 := &x.Port
yym8 := z.DecBinary()
@@ -31024,7 +31022,7 @@ func (x *ServicePort) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
}
case "targetPort":
if r.TryDecodeAsNil() {
x.TargetPort = pkg5_intstr.IntOrString{}
x.TargetPort = pkg4_intstr.IntOrString{}
} else {
yyv7 := &x.TargetPort
yym8 := z.DecBinary()
@@ -31117,7 +31115,7 @@ func (x *ServicePort) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
}
z.DecSendContainerState(codecSelfer_containerArrayElem1234)
if r.TryDecodeAsNil() {
x.TargetPort = pkg5_intstr.IntOrString{}
x.TargetPort = pkg4_intstr.IntOrString{}
} else {
yyv14 := &x.TargetPort
yym15 := z.DecBinary()
@@ -45289,7 +45287,7 @@ func (x *List) CodecEncodeSelf(e *codec1978.Encoder) {
_ = yym9
if false {
} else {
h.encSliceruntime_RawExtension(([]pkg6_runtime.RawExtension)(x.Items), e)
h.encSliceruntime_RawExtension(([]pkg5_runtime.RawExtension)(x.Items), e)
}
}
} else {
@@ -45303,7 +45301,7 @@ func (x *List) CodecEncodeSelf(e *codec1978.Encoder) {
_ = yym10
if false {
} else {
h.encSliceruntime_RawExtension(([]pkg6_runtime.RawExtension)(x.Items), e)
h.encSliceruntime_RawExtension(([]pkg5_runtime.RawExtension)(x.Items), e)
}
}
}
@@ -45440,7 +45438,7 @@ func (x *List) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
_ = yym7
if false {
} else {
h.decSliceruntime_RawExtension((*[]pkg6_runtime.RawExtension)(yyv6), d)
h.decSliceruntime_RawExtension((*[]pkg5_runtime.RawExtension)(yyv6), d)
}
}
case "kind":
@@ -45511,7 +45509,7 @@ func (x *List) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
_ = yym14
if false {
} else {
h.decSliceruntime_RawExtension((*[]pkg6_runtime.RawExtension)(yyv13), d)
h.decSliceruntime_RawExtension((*[]pkg5_runtime.RawExtension)(yyv13), d)
}
}
yyj10++
@@ -56147,7 +56145,7 @@ func (x codecSelfer1234) decResourceList(v *ResourceList, d *codec1978.Decoder)
yyl1 := r.ReadMapStart()
yybh1 := z.DecBasicHandle()
if yyv1 == nil {
yyrl1, _ := z.DecInferLen(yyl1, yybh1.MaxInitLen, 40)
yyrl1, _ := z.DecInferLen(yyl1, yybh1.MaxInitLen, 80)
yyv1 = make(map[ResourceName]pkg3_resource.Quantity, yyrl1)
*v = yyv1
}
@@ -56696,7 +56694,7 @@ func (x codecSelfer1234) decSliceEvent(v *[]Event, d *codec1978.Decoder) {
}
}
func (x codecSelfer1234) encSliceruntime_RawExtension(v []pkg6_runtime.RawExtension, e *codec1978.Encoder) {
func (x codecSelfer1234) encSliceruntime_RawExtension(v []pkg5_runtime.RawExtension, e *codec1978.Encoder) {
var h codecSelfer1234
z, r := codec1978.GenHelperEncoder(e)
_, _, _ = h, z, r
@@ -56717,7 +56715,7 @@ func (x codecSelfer1234) encSliceruntime_RawExtension(v []pkg6_runtime.RawExtens
z.EncSendContainerState(codecSelfer_containerArrayEnd1234)
}
func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg6_runtime.RawExtension, d *codec1978.Decoder) {
func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg5_runtime.RawExtension, d *codec1978.Decoder) {
var h codecSelfer1234
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
@@ -56728,7 +56726,7 @@ func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg6_runtime.RawExten
_ = yyc1
if yyl1 == 0 {
if yyv1 == nil {
yyv1 = []pkg6_runtime.RawExtension{}
yyv1 = []pkg5_runtime.RawExtension{}
yyc1 = true
} else if len(yyv1) != 0 {
yyv1 = yyv1[:0]
@@ -56748,10 +56746,10 @@ func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg6_runtime.RawExten
if yyrl1 <= cap(yyv1) {
yyv1 = yyv1[:yyrl1]
} else {
yyv1 = make([]pkg6_runtime.RawExtension, yyrl1)
yyv1 = make([]pkg5_runtime.RawExtension, yyrl1)
}
} else {
yyv1 = make([]pkg6_runtime.RawExtension, yyrl1)
yyv1 = make([]pkg5_runtime.RawExtension, yyrl1)
}
yyc1 = true
yyrr1 = len(yyv1)
@@ -56766,7 +56764,7 @@ func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg6_runtime.RawExten
for ; yyj1 < yyrr1; yyj1++ {
yyh1.ElemContainerState(yyj1)
if r.TryDecodeAsNil() {
yyv1[yyj1] = pkg6_runtime.RawExtension{}
yyv1[yyj1] = pkg5_runtime.RawExtension{}
} else {
yyv2 := &yyv1[yyj1]
yym3 := z.DecBinary()
@@ -56783,10 +56781,10 @@ func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg6_runtime.RawExten
}
if yyrt1 {
for ; yyj1 < yyl1; yyj1++ {
yyv1 = append(yyv1, pkg6_runtime.RawExtension{})
yyv1 = append(yyv1, pkg5_runtime.RawExtension{})
yyh1.ElemContainerState(yyj1)
if r.TryDecodeAsNil() {
yyv1[yyj1] = pkg6_runtime.RawExtension{}
yyv1[yyj1] = pkg5_runtime.RawExtension{}
} else {
yyv4 := &yyv1[yyj1]
yym5 := z.DecBinary()
@@ -56808,13 +56806,13 @@ func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg6_runtime.RawExten
for ; !r.CheckBreak(); yyj1++ {
if yyj1 >= len(yyv1) {
yyv1 = append(yyv1, pkg6_runtime.RawExtension{}) // var yyz1 pkg6_runtime.RawExtension
yyv1 = append(yyv1, pkg5_runtime.RawExtension{}) // var yyz1 pkg5_runtime.RawExtension
yyc1 = true
}
yyh1.ElemContainerState(yyj1)
if yyj1 < len(yyv1) {
if r.TryDecodeAsNil() {
yyv1[yyj1] = pkg6_runtime.RawExtension{}
yyv1[yyj1] = pkg5_runtime.RawExtension{}
} else {
yyv6 := &yyv1[yyj1]
yym7 := z.DecBinary()
@@ -56837,7 +56835,7 @@ func (x codecSelfer1234) decSliceruntime_RawExtension(v *[]pkg6_runtime.RawExten
yyv1 = yyv1[:yyj1]
yyc1 = true
} else if yyj1 == 0 && yyv1 == nil {
yyv1 = []pkg6_runtime.RawExtension{}
yyv1 = []pkg5_runtime.RawExtension{}
yyc1 = true
}
}