From 74f368f50e9369ecaa29a85fe87f551a4aa6f4be Mon Sep 17 00:00:00 2001 From: derekwaynecarr Date: Thu, 22 Jan 2015 22:21:13 -0500 Subject: [PATCH] Modified LimitRangeItem.Kind to LimitRangeItem.Type, added example files --- cmd/kube-apiserver/plugins.go | 1 + examples/limitrange/invalid-pod.json | 18 ++++++++++++++ .../{limitrange.json => limit-range.json} | 4 ++-- examples/limitrange/valid-pod.json | 20 ++++++++++++++++ pkg/api/types.go | 11 +++++++-- pkg/api/v1beta1/conversion.go | 4 ++-- pkg/api/v1beta1/types.go | 11 +++++++-- pkg/api/v1beta2/conversion.go | 4 ++-- pkg/api/v1beta2/types.go | 11 +++++++-- pkg/api/v1beta3/types.go | 11 +++++++-- pkg/api/validation/validation_test.go | 6 ++--- pkg/client/limit_ranges_test.go | 8 +++---- pkg/kubectl/describe.go | 5 ++-- pkg/registry/limitrange/registry_test.go | 2 +- plugin/pkg/admission/limitranger/admission.go | 24 +++++++++---------- .../admission/limitranger/admission_test.go | 4 ++-- 16 files changed, 105 insertions(+), 39 deletions(-) create mode 100644 examples/limitrange/invalid-pod.json rename examples/limitrange/{limitrange.json => limit-range.json} (89%) create mode 100644 examples/limitrange/valid-pod.json diff --git a/cmd/kube-apiserver/plugins.go b/cmd/kube-apiserver/plugins.go index 366b211ecf6..de652600880 100644 --- a/cmd/kube-apiserver/plugins.go +++ b/cmd/kube-apiserver/plugins.go @@ -28,5 +28,6 @@ import ( _ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/admit" _ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/deny" + _ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/limitranger" _ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/resourcedefaults" ) diff --git a/examples/limitrange/invalid-pod.json b/examples/limitrange/invalid-pod.json new file mode 100644 index 00000000000..28a4d83e042 --- /dev/null +++ b/examples/limitrange/invalid-pod.json @@ -0,0 +1,18 @@ +{ + "id": "invalid-pod", + "kind": "Pod", + "apiVersion":"v1beta2", + "labels": { + "name": "invalid-pod" + }, + "desiredState": { + "manifest": { + "version": "v1beta1", + "id": "invalid-pod", + "containers": [{ + "name": "kubernetes-serve-hostname", + "image": "kubernetes/serve_hostname", + }] + } + }, +} \ No newline at end of file diff --git a/examples/limitrange/limitrange.json b/examples/limitrange/limit-range.json similarity index 89% rename from examples/limitrange/limitrange.json rename to examples/limitrange/limit-range.json index daef2adaa79..f3ade396f45 100644 --- a/examples/limitrange/limitrange.json +++ b/examples/limitrange/limit-range.json @@ -5,7 +5,7 @@ "spec": { "limits": [ { - "kind": "pods", + "type": "Pod", "max": { "memory": "1073741824", "cpu": "2", @@ -16,7 +16,7 @@ } }, { - "kind": "containers", + "type": "Container", "max": { "memory": "1073741824", "cpu": "2", diff --git a/examples/limitrange/valid-pod.json b/examples/limitrange/valid-pod.json new file mode 100644 index 00000000000..3f606840815 --- /dev/null +++ b/examples/limitrange/valid-pod.json @@ -0,0 +1,20 @@ +{ + "id": "valid-pod", + "kind": "Pod", + "apiVersion":"v1beta2", + "labels": { + "name": "valid-pod" + }, + "desiredState": { + "manifest": { + "version": "v1beta1", + "id": "invalid-pod", + "containers": [{ + "name": "kubernetes-serve-hostname", + "image": "kubernetes/serve_hostname", + "cpu": 1000, + "memory": 1048576, + }] + } + }, +} \ No newline at end of file diff --git a/pkg/api/types.go b/pkg/api/types.go index 84430119058..a9e6694fc7c 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -1139,10 +1139,17 @@ type List struct { Items []runtime.Object `json:"items"` } +const ( + // Limit that applies to all pods in a namespace + LimitTypePod string = "Pod" + // Limit that applies to all containers in a namespace + LimitTypeContainer string = "Container" +) + // LimitRangeItem defines a min/max usage limit for any resource that matches on kind type LimitRangeItem struct { - // Kind is the resource kind that this limit range is applied (i.e. pods, etc.) - Kind string + // Type of resource that this limit applies to + Type string `json:"type,omitempty"` // Max usage constraints on this kind by resource name Max ResourceList `json:"max,omitempty"` // Min usage constraints on this kind by resource name diff --git a/pkg/api/v1beta1/conversion.go b/pkg/api/v1beta1/conversion.go index 1e539c243e5..309afcdb7ef 100644 --- a/pkg/api/v1beta1/conversion.go +++ b/pkg/api/v1beta1/conversion.go @@ -614,7 +614,7 @@ func init() { }, func(in *newer.LimitRangeItem, out *LimitRangeItem, s conversion.Scope) error { *out = LimitRangeItem{} - out.Kind = in.Kind + out.Type = in.Type if err := s.Convert(&in.Max, &out.Max, 0); err != nil { return err } @@ -625,7 +625,7 @@ func init() { }, func(in *LimitRangeItem, out *newer.LimitRangeItem, s conversion.Scope) error { *out = newer.LimitRangeItem{} - out.Kind = in.Kind + out.Type = in.Type if err := s.Convert(&in.Max, &out.Max, 0); err != nil { return err } diff --git a/pkg/api/v1beta1/types.go b/pkg/api/v1beta1/types.go index 814e3f2bc19..0cb70ddd008 100644 --- a/pkg/api/v1beta1/types.go +++ b/pkg/api/v1beta1/types.go @@ -905,10 +905,17 @@ type List struct { Items []runtime.RawExtension `json:"items" description:"list of objects"` } +const ( + // Limit that applies to all pods in a namespace + LimitTypePod string = "Pod" + // Limit that applies to all containers in a namespace + LimitTypeContainer string = "Container" +) + // LimitRangeItem defines a min/max usage limit for any resource that matches on kind type LimitRangeItem struct { - // Kind is the resource kind that this limit range is applied (i.e. pods, etc.) - Kind string + // Type of resource that this limit applies to + Type string `json:"type,omitempty"` // Max usage constraints on this kind by resource name Max ResourceList `json:"max,omitempty"` // Min usage constraints on this kind by resource name diff --git a/pkg/api/v1beta2/conversion.go b/pkg/api/v1beta2/conversion.go index 2cd037ea03d..6c69682251a 100644 --- a/pkg/api/v1beta2/conversion.go +++ b/pkg/api/v1beta2/conversion.go @@ -531,7 +531,7 @@ func init() { }, func(in *newer.LimitRangeItem, out *LimitRangeItem, s conversion.Scope) error { *out = LimitRangeItem{} - out.Kind = in.Kind + out.Type = in.Type if err := s.Convert(&in.Max, &out.Max, 0); err != nil { return err } @@ -542,7 +542,7 @@ func init() { }, func(in *LimitRangeItem, out *newer.LimitRangeItem, s conversion.Scope) error { *out = newer.LimitRangeItem{} - out.Kind = in.Kind + out.Type = in.Type if err := s.Convert(&in.Max, &out.Max, 0); err != nil { return err } diff --git a/pkg/api/v1beta2/types.go b/pkg/api/v1beta2/types.go index 0e613c89e38..f584bc5b75f 100644 --- a/pkg/api/v1beta2/types.go +++ b/pkg/api/v1beta2/types.go @@ -907,10 +907,17 @@ type List struct { Items []runtime.RawExtension `json:"items" description:"list of objects"` } +const ( + // Limit that applies to all pods in a namespace + LimitTypePod string = "Pod" + // Limit that applies to all containers in a namespace + LimitTypeContainer string = "Container" +) + // LimitRangeItem defines a min/max usage limit for any resource that matches on kind type LimitRangeItem struct { - // Kind is the resource kind that this limit range is applied (i.e. pods, etc.) - Kind string + // Type of resource that this limit applies to + Type string `json:"type,omitempty"` // Max usage constraints on this kind by resource name Max ResourceList `json:"max,omitempty"` // Min usage constraints on this kind by resource name diff --git a/pkg/api/v1beta3/types.go b/pkg/api/v1beta3/types.go index a14b9318b83..1e7b76c8a9d 100644 --- a/pkg/api/v1beta3/types.go +++ b/pkg/api/v1beta3/types.go @@ -1067,10 +1067,17 @@ type List struct { Items []runtime.RawExtension `json:"items" description:"list of objects"` } +const ( + // Limit that applies to all pods in a namespace + LimitTypePod string = "Pod" + // Limit that applies to all containers in a namespace + LimitTypeContainer string = "Container" +) + // LimitRangeItem defines a min/max usage limit for any resource that matches on kind type LimitRangeItem struct { - // Kind is the resource kind that this limit range is applied (i.e. pods, etc.) - Kind string + // Type of resource that this limit applies to + Type string `json:"type,omitempty"` // Max usage constraints on this kind by resource name Max ResourceList `json:"max,omitempty"` // Min usage constraints on this kind by resource name diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index 5dd94873e3d..4555eeab6ca 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -1563,7 +1563,7 @@ func TestValidateLimitRange(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), @@ -1592,7 +1592,7 @@ func TestValidateLimitRange(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), @@ -1613,7 +1613,7 @@ func TestValidateLimitRange(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), diff --git a/pkg/client/limit_ranges_test.go b/pkg/client/limit_ranges_test.go index e8bc09561c7..126baa7ac5f 100644 --- a/pkg/client/limit_ranges_test.go +++ b/pkg/client/limit_ranges_test.go @@ -34,7 +34,7 @@ func TestLimitRangeCreate(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), @@ -70,7 +70,7 @@ func TestLimitRangeGet(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), @@ -130,7 +130,7 @@ func TestLimitRangeUpdate(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), @@ -160,7 +160,7 @@ func TestInvalidLimitRangeUpdate(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), diff --git a/pkg/kubectl/describe.go b/pkg/kubectl/describe.go index 7896e5dc76d..12cad8b6ba1 100644 --- a/pkg/kubectl/describe.go +++ b/pkg/kubectl/describe.go @@ -68,11 +68,10 @@ func (d *LimitRangeDescriber) Describe(namespace, name string) (string, error) { return tabbedString(func(out io.Writer) error { fmt.Fprintf(out, "Name:\t%s\n", limitRange.Name) - fmt.Fprintf(out, "Kind\tResource\tMin\tMax\n") + fmt.Fprintf(out, "Type\tResource\tMin\tMax\n") fmt.Fprintf(out, "----\t--------\t---\t---\n") for i, _ := range limitRange.Spec.Limits { item := limitRange.Spec.Limits[i] - kind := item.Kind maxResources := item.Max minResources := item.Min @@ -100,7 +99,7 @@ func (d *LimitRangeDescriber) Describe(namespace, name string) (string, error) { } msg := "%v\t%v\t%v\t%v\n" - fmt.Fprintf(out, msg, kind, k, minValue, maxValue) + fmt.Fprintf(out, msg, item.Type, k, minValue, maxValue) } } return nil diff --git a/pkg/registry/limitrange/registry_test.go b/pkg/registry/limitrange/registry_test.go index 1eb649b0111..4c081f8d024 100644 --- a/pkg/registry/limitrange/registry_test.go +++ b/pkg/registry/limitrange/registry_test.go @@ -49,7 +49,7 @@ func TestLimitRangeCreate(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100"), api.ResourceMemory: resource.MustParse("10000"), diff --git a/plugin/pkg/admission/limitranger/admission.go b/plugin/pkg/admission/limitranger/admission.go index 41b2e60711d..a404e25abda 100644 --- a/plugin/pkg/admission/limitranger/admission.go +++ b/plugin/pkg/admission/limitranger/admission.go @@ -131,21 +131,21 @@ func PodLimitFunc(limitRange *api.LimitRange, kind string, obj runtime.Object) e switch k { case api.ResourceMemory: enforced = v.Value() - switch limit.Kind { - case "pods": + switch limit.Type { + case api.LimitTypePod: observed = podMem err = fmt.Errorf("Maximum memory usage per pod is %s", v.String()) - case "containers": + case api.LimitTypeContainer: observed = maxContainerMem err = fmt.Errorf("Maximum memory usage per container is %s", v.String()) } case api.ResourceCPU: enforced = v.MilliValue() - switch limit.Kind { - case "pods": + switch limit.Type { + case api.LimitTypePod: observed = podCPU err = fmt.Errorf("Maximum CPU usage per pod is %s, but requested %s", v.String(), resource.NewMilliQuantity(observed, resource.DecimalSI)) - case "containers": + case api.LimitTypeContainer: observed = maxContainerCPU err = fmt.Errorf("Maximum CPU usage per container is %s", v.String()) } @@ -161,21 +161,21 @@ func PodLimitFunc(limitRange *api.LimitRange, kind string, obj runtime.Object) e switch k { case api.ResourceMemory: enforced = v.Value() - switch limit.Kind { - case "pods": + switch limit.Type { + case api.LimitTypePod: observed = podMem err = fmt.Errorf("Minimum memory usage per pod is %s", v.String()) - case "containers": + case api.LimitTypeContainer: observed = maxContainerMem err = fmt.Errorf("Minimum memory usage per container is %s", v.String()) } case api.ResourceCPU: enforced = v.MilliValue() - switch limit.Kind { - case "pods": + switch limit.Type { + case api.LimitTypePod: observed = podCPU err = fmt.Errorf("Minimum CPU usage per pod is %s", v.String()) - case "containers": + case api.LimitTypeContainer: observed = maxContainerCPU err = fmt.Errorf("Minimum CPU usage per container is %s", v.String()) } diff --git a/plugin/pkg/admission/limitranger/admission_test.go b/plugin/pkg/admission/limitranger/admission_test.go index 9aed5a85912..570cdcacc52 100644 --- a/plugin/pkg/admission/limitranger/admission_test.go +++ b/plugin/pkg/admission/limitranger/admission_test.go @@ -31,7 +31,7 @@ func TestPodLimitFunc(t *testing.T) { Spec: api.LimitRangeSpec{ Limits: []api.LimitRangeItem{ { - Kind: "pods", + Type: api.LimitTypePod, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("200m"), api.ResourceMemory: resource.MustParse("4Gi"), @@ -42,7 +42,7 @@ func TestPodLimitFunc(t *testing.T) { }, }, { - Kind: "containers", + Type: api.LimitTypeContainer, Max: api.ResourceList{ api.ResourceCPU: resource.MustParse("100m"), api.ResourceMemory: resource.MustParse("2Gi"),