Skip to content

Commit

Permalink
Modified LimitRangeItem.Kind to LimitRangeItem.Type, added example files
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwaynecarr committed Jan 27, 2015
1 parent 31a1145 commit 74f368f
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 39 deletions.
1 change: 1 addition & 0 deletions cmd/kube-apiserver/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
18 changes: 18 additions & 0 deletions examples/limitrange/invalid-pod.json
Original file line number Diff line number Diff line change
@@ -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",
}]
}
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"spec": {
"limits": [
{
"kind": "pods",
"type": "Pod",
"max": {
"memory": "1073741824",
"cpu": "2",
Expand All @@ -16,7 +16,7 @@
}
},
{
"kind": "containers",
"type": "Container",
"max": {
"memory": "1073741824",
"cpu": "2",
Expand Down
20 changes: 20 additions & 0 deletions examples/limitrange/valid-pod.json
Original file line number Diff line number Diff line change
@@ -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,
}]
}
},
}
11 changes: 9 additions & 2 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1beta2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions pkg/api/v1beta3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/limit_ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
5 changes: 2 additions & 3 deletions pkg/kubectl/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/limitrange/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
24 changes: 12 additions & 12 deletions plugin/pkg/admission/limitranger/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/pkg/admission/limitranger/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down

0 comments on commit 74f368f

Please sign in to comment.