Commit 74f368f5 authored by derekwaynecarr's avatar derekwaynecarr

Modified LimitRangeItem.Kind to LimitRangeItem.Type, added example files

parent 31a1145a
...@@ -28,5 +28,6 @@ import ( ...@@ -28,5 +28,6 @@ import (
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/admit" _ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/admit"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/deny" _ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/deny"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/limitranger"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/resourcedefaults" _ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/resourcedefaults"
) )
{
"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
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"spec": { "spec": {
"limits": [ "limits": [
{ {
"kind": "pods", "type": "Pod",
"max": { "max": {
"memory": "1073741824", "memory": "1073741824",
"cpu": "2", "cpu": "2",
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
} }
}, },
{ {
"kind": "containers", "type": "Container",
"max": { "max": {
"memory": "1073741824", "memory": "1073741824",
"cpu": "2", "cpu": "2",
......
{
"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
...@@ -1139,10 +1139,17 @@ type List struct { ...@@ -1139,10 +1139,17 @@ type List struct {
Items []runtime.Object `json:"items"` 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 // LimitRangeItem defines a min/max usage limit for any resource that matches on kind
type LimitRangeItem struct { type LimitRangeItem struct {
// Kind is the resource kind that this limit range is applied (i.e. pods, etc.) // Type of resource that this limit applies to
Kind string Type string `json:"type,omitempty"`
// Max usage constraints on this kind by resource name // Max usage constraints on this kind by resource name
Max ResourceList `json:"max,omitempty"` Max ResourceList `json:"max,omitempty"`
// Min usage constraints on this kind by resource name // Min usage constraints on this kind by resource name
......
...@@ -614,7 +614,7 @@ func init() { ...@@ -614,7 +614,7 @@ func init() {
}, },
func(in *newer.LimitRangeItem, out *LimitRangeItem, s conversion.Scope) error { func(in *newer.LimitRangeItem, out *LimitRangeItem, s conversion.Scope) error {
*out = LimitRangeItem{} *out = LimitRangeItem{}
out.Kind = in.Kind out.Type = in.Type
if err := s.Convert(&in.Max, &out.Max, 0); err != nil { if err := s.Convert(&in.Max, &out.Max, 0); err != nil {
return err return err
} }
...@@ -625,7 +625,7 @@ func init() { ...@@ -625,7 +625,7 @@ func init() {
}, },
func(in *LimitRangeItem, out *newer.LimitRangeItem, s conversion.Scope) error { func(in *LimitRangeItem, out *newer.LimitRangeItem, s conversion.Scope) error {
*out = newer.LimitRangeItem{} *out = newer.LimitRangeItem{}
out.Kind = in.Kind out.Type = in.Type
if err := s.Convert(&in.Max, &out.Max, 0); err != nil { if err := s.Convert(&in.Max, &out.Max, 0); err != nil {
return err return err
} }
......
...@@ -905,10 +905,17 @@ type List struct { ...@@ -905,10 +905,17 @@ type List struct {
Items []runtime.RawExtension `json:"items" description:"list of objects"` 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 // LimitRangeItem defines a min/max usage limit for any resource that matches on kind
type LimitRangeItem struct { type LimitRangeItem struct {
// Kind is the resource kind that this limit range is applied (i.e. pods, etc.) // Type of resource that this limit applies to
Kind string Type string `json:"type,omitempty"`
// Max usage constraints on this kind by resource name // Max usage constraints on this kind by resource name
Max ResourceList `json:"max,omitempty"` Max ResourceList `json:"max,omitempty"`
// Min usage constraints on this kind by resource name // Min usage constraints on this kind by resource name
......
...@@ -531,7 +531,7 @@ func init() { ...@@ -531,7 +531,7 @@ func init() {
}, },
func(in *newer.LimitRangeItem, out *LimitRangeItem, s conversion.Scope) error { func(in *newer.LimitRangeItem, out *LimitRangeItem, s conversion.Scope) error {
*out = LimitRangeItem{} *out = LimitRangeItem{}
out.Kind = in.Kind out.Type = in.Type
if err := s.Convert(&in.Max, &out.Max, 0); err != nil { if err := s.Convert(&in.Max, &out.Max, 0); err != nil {
return err return err
} }
...@@ -542,7 +542,7 @@ func init() { ...@@ -542,7 +542,7 @@ func init() {
}, },
func(in *LimitRangeItem, out *newer.LimitRangeItem, s conversion.Scope) error { func(in *LimitRangeItem, out *newer.LimitRangeItem, s conversion.Scope) error {
*out = newer.LimitRangeItem{} *out = newer.LimitRangeItem{}
out.Kind = in.Kind out.Type = in.Type
if err := s.Convert(&in.Max, &out.Max, 0); err != nil { if err := s.Convert(&in.Max, &out.Max, 0); err != nil {
return err return err
} }
......
...@@ -907,10 +907,17 @@ type List struct { ...@@ -907,10 +907,17 @@ type List struct {
Items []runtime.RawExtension `json:"items" description:"list of objects"` 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 // LimitRangeItem defines a min/max usage limit for any resource that matches on kind
type LimitRangeItem struct { type LimitRangeItem struct {
// Kind is the resource kind that this limit range is applied (i.e. pods, etc.) // Type of resource that this limit applies to
Kind string Type string `json:"type,omitempty"`
// Max usage constraints on this kind by resource name // Max usage constraints on this kind by resource name
Max ResourceList `json:"max,omitempty"` Max ResourceList `json:"max,omitempty"`
// Min usage constraints on this kind by resource name // Min usage constraints on this kind by resource name
......
...@@ -1067,10 +1067,17 @@ type List struct { ...@@ -1067,10 +1067,17 @@ type List struct {
Items []runtime.RawExtension `json:"items" description:"list of objects"` 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 // LimitRangeItem defines a min/max usage limit for any resource that matches on kind
type LimitRangeItem struct { type LimitRangeItem struct {
// Kind is the resource kind that this limit range is applied (i.e. pods, etc.) // Type of resource that this limit applies to
Kind string Type string `json:"type,omitempty"`
// Max usage constraints on this kind by resource name // Max usage constraints on this kind by resource name
Max ResourceList `json:"max,omitempty"` Max ResourceList `json:"max,omitempty"`
// Min usage constraints on this kind by resource name // Min usage constraints on this kind by resource name
......
...@@ -1563,7 +1563,7 @@ func TestValidateLimitRange(t *testing.T) { ...@@ -1563,7 +1563,7 @@ func TestValidateLimitRange(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
...@@ -1592,7 +1592,7 @@ func TestValidateLimitRange(t *testing.T) { ...@@ -1592,7 +1592,7 @@ func TestValidateLimitRange(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
...@@ -1613,7 +1613,7 @@ func TestValidateLimitRange(t *testing.T) { ...@@ -1613,7 +1613,7 @@ func TestValidateLimitRange(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
......
...@@ -34,7 +34,7 @@ func TestLimitRangeCreate(t *testing.T) { ...@@ -34,7 +34,7 @@ func TestLimitRangeCreate(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
...@@ -70,7 +70,7 @@ func TestLimitRangeGet(t *testing.T) { ...@@ -70,7 +70,7 @@ func TestLimitRangeGet(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
...@@ -130,7 +130,7 @@ func TestLimitRangeUpdate(t *testing.T) { ...@@ -130,7 +130,7 @@ func TestLimitRangeUpdate(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
...@@ -160,7 +160,7 @@ func TestInvalidLimitRangeUpdate(t *testing.T) { ...@@ -160,7 +160,7 @@ func TestInvalidLimitRangeUpdate(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
......
...@@ -68,11 +68,10 @@ func (d *LimitRangeDescriber) Describe(namespace, name string) (string, error) { ...@@ -68,11 +68,10 @@ func (d *LimitRangeDescriber) Describe(namespace, name string) (string, error) {
return tabbedString(func(out io.Writer) error { return tabbedString(func(out io.Writer) error {
fmt.Fprintf(out, "Name:\t%s\n", limitRange.Name) 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") fmt.Fprintf(out, "----\t--------\t---\t---\n")
for i, _ := range limitRange.Spec.Limits { for i, _ := range limitRange.Spec.Limits {
item := limitRange.Spec.Limits[i] item := limitRange.Spec.Limits[i]
kind := item.Kind
maxResources := item.Max maxResources := item.Max
minResources := item.Min minResources := item.Min
...@@ -100,7 +99,7 @@ func (d *LimitRangeDescriber) Describe(namespace, name string) (string, error) { ...@@ -100,7 +99,7 @@ func (d *LimitRangeDescriber) Describe(namespace, name string) (string, error) {
} }
msg := "%v\t%v\t%v\t%v\n" 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 return nil
......
...@@ -49,7 +49,7 @@ func TestLimitRangeCreate(t *testing.T) { ...@@ -49,7 +49,7 @@ func TestLimitRangeCreate(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100"), api.ResourceCPU: resource.MustParse("100"),
api.ResourceMemory: resource.MustParse("10000"), api.ResourceMemory: resource.MustParse("10000"),
......
...@@ -131,21 +131,21 @@ func PodLimitFunc(limitRange *api.LimitRange, kind string, obj runtime.Object) e ...@@ -131,21 +131,21 @@ func PodLimitFunc(limitRange *api.LimitRange, kind string, obj runtime.Object) e
switch k { switch k {
case api.ResourceMemory: case api.ResourceMemory:
enforced = v.Value() enforced = v.Value()
switch limit.Kind { switch limit.Type {
case "pods": case api.LimitTypePod:
observed = podMem observed = podMem
err = fmt.Errorf("Maximum memory usage per pod is %s", v.String()) err = fmt.Errorf("Maximum memory usage per pod is %s", v.String())
case "containers": case api.LimitTypeContainer:
observed = maxContainerMem observed = maxContainerMem
err = fmt.Errorf("Maximum memory usage per container is %s", v.String()) err = fmt.Errorf("Maximum memory usage per container is %s", v.String())
} }
case api.ResourceCPU: case api.ResourceCPU:
enforced = v.MilliValue() enforced = v.MilliValue()
switch limit.Kind { switch limit.Type {
case "pods": case api.LimitTypePod:
observed = podCPU observed = podCPU
err = fmt.Errorf("Maximum CPU usage per pod is %s, but requested %s", v.String(), resource.NewMilliQuantity(observed, resource.DecimalSI)) 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 observed = maxContainerCPU
err = fmt.Errorf("Maximum CPU usage per container is %s", v.String()) 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 ...@@ -161,21 +161,21 @@ func PodLimitFunc(limitRange *api.LimitRange, kind string, obj runtime.Object) e
switch k { switch k {
case api.ResourceMemory: case api.ResourceMemory:
enforced = v.Value() enforced = v.Value()
switch limit.Kind { switch limit.Type {
case "pods": case api.LimitTypePod:
observed = podMem observed = podMem
err = fmt.Errorf("Minimum memory usage per pod is %s", v.String()) err = fmt.Errorf("Minimum memory usage per pod is %s", v.String())
case "containers": case api.LimitTypeContainer:
observed = maxContainerMem observed = maxContainerMem
err = fmt.Errorf("Minimum memory usage per container is %s", v.String()) err = fmt.Errorf("Minimum memory usage per container is %s", v.String())
} }
case api.ResourceCPU: case api.ResourceCPU:
enforced = v.MilliValue() enforced = v.MilliValue()
switch limit.Kind { switch limit.Type {
case "pods": case api.LimitTypePod:
observed = podCPU observed = podCPU
err = fmt.Errorf("Minimum CPU usage per pod is %s", v.String()) err = fmt.Errorf("Minimum CPU usage per pod is %s", v.String())
case "containers": case api.LimitTypeContainer:
observed = maxContainerCPU observed = maxContainerCPU
err = fmt.Errorf("Minimum CPU usage per container is %s", v.String()) err = fmt.Errorf("Minimum CPU usage per container is %s", v.String())
} }
......
...@@ -31,7 +31,7 @@ func TestPodLimitFunc(t *testing.T) { ...@@ -31,7 +31,7 @@ func TestPodLimitFunc(t *testing.T) {
Spec: api.LimitRangeSpec{ Spec: api.LimitRangeSpec{
Limits: []api.LimitRangeItem{ Limits: []api.LimitRangeItem{
{ {
Kind: "pods", Type: api.LimitTypePod,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("200m"), api.ResourceCPU: resource.MustParse("200m"),
api.ResourceMemory: resource.MustParse("4Gi"), api.ResourceMemory: resource.MustParse("4Gi"),
...@@ -42,7 +42,7 @@ func TestPodLimitFunc(t *testing.T) { ...@@ -42,7 +42,7 @@ func TestPodLimitFunc(t *testing.T) {
}, },
}, },
{ {
Kind: "containers", Type: api.LimitTypeContainer,
Max: api.ResourceList{ Max: api.ResourceList{
api.ResourceCPU: resource.MustParse("100m"), api.ResourceCPU: resource.MustParse("100m"),
api.ResourceMemory: resource.MustParse("2Gi"), api.ResourceMemory: resource.MustParse("2Gi"),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment