mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Modified LimitRangeItem.Kind to LimitRangeItem.Type, added example files
This commit is contained in:
parent
31a1145abd
commit
74f368f50e
@ -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
examples/limitrange/invalid-pod.json
Normal file
18
examples/limitrange/invalid-pod.json
Normal file
@ -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",
|
||||
}]
|
||||
}
|
||||
},
|
||||
}
|
@ -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",
|
20
examples/limitrange/valid-pod.json
Normal file
20
examples/limitrange/valid-pod.json
Normal file
@ -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,
|
||||
}]
|
||||
}
|
||||
},
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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"),
|
||||
|
@ -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"),
|
||||
|
@ -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
|
||||
|
@ -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"),
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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"),
|
||||
|
Loading…
Reference in New Issue
Block a user