mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 15:13:08 +00:00
Add a limit range resource
This commit is contained in:
@@ -37,6 +37,7 @@ type Interface interface {
|
||||
VersionInterface
|
||||
NodesInterface
|
||||
EventNamespacer
|
||||
LimitRangesNamespacer
|
||||
}
|
||||
|
||||
func (c *Client) ReplicationControllers(namespace string) ReplicationControllerInterface {
|
||||
@@ -63,6 +64,10 @@ func (c *Client) Services(namespace string) ServiceInterface {
|
||||
return newServices(c, namespace)
|
||||
}
|
||||
|
||||
func (c *Client) LimitRanges(namespace string) LimitRangeInterface {
|
||||
return newLimitRanges(c, namespace)
|
||||
}
|
||||
|
||||
// VersionInterface has a method to retrieve the server version.
|
||||
type VersionInterface interface {
|
||||
ServerVersion() (*version.Info, error)
|
||||
|
@@ -34,15 +34,20 @@ type FakeAction struct {
|
||||
// Fake implements Interface. Meant to be embedded into a struct to get a default
|
||||
// implementation. This makes faking out just the method you want to test easier.
|
||||
type Fake struct {
|
||||
Actions []FakeAction
|
||||
PodsList api.PodList
|
||||
Ctrl api.ReplicationController
|
||||
ServiceList api.ServiceList
|
||||
EndpointsList api.EndpointsList
|
||||
MinionsList api.NodeList
|
||||
EventsList api.EventList
|
||||
Err error
|
||||
Watch watch.Interface
|
||||
Actions []FakeAction
|
||||
PodsList api.PodList
|
||||
Ctrl api.ReplicationController
|
||||
ServiceList api.ServiceList
|
||||
EndpointsList api.EndpointsList
|
||||
MinionsList api.NodeList
|
||||
EventsList api.EventList
|
||||
LimitRangesList api.LimitRangeList
|
||||
Err error
|
||||
Watch watch.Interface
|
||||
}
|
||||
|
||||
func (c *Fake) LimitRanges(namespace string) LimitRangeInterface {
|
||||
return &FakeLimitRanges{Fake: c, Namespace: namespace}
|
||||
}
|
||||
|
||||
func (c *Fake) ReplicationControllers(namespace string) ReplicationControllerInterface {
|
||||
|
54
pkg/client/fake_limit_ranges.go
Normal file
54
pkg/client/fake_limit_ranges.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
)
|
||||
|
||||
// FakeLimitRanges implements PodsInterface. Meant to be embedded into a struct to get a default
|
||||
// implementation. This makes faking out just the methods you want to test easier.
|
||||
type FakeLimitRanges struct {
|
||||
Fake *Fake
|
||||
Namespace string
|
||||
}
|
||||
|
||||
func (c *FakeLimitRanges) List(selector labels.Selector) (*api.LimitRangeList, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-limitRanges"})
|
||||
return api.Scheme.CopyOrDie(&c.Fake.LimitRangesList).(*api.LimitRangeList), nil
|
||||
}
|
||||
|
||||
func (c *FakeLimitRanges) Get(name string) (*api.LimitRange, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-limitRange", Value: name})
|
||||
return &api.LimitRange{ObjectMeta: api.ObjectMeta{Name: name, Namespace: c.Namespace}}, nil
|
||||
}
|
||||
|
||||
func (c *FakeLimitRanges) Delete(name string) error {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-limitRange", Value: name})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *FakeLimitRanges) Create(limitRange *api.LimitRange) (*api.LimitRange, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-limitRange"})
|
||||
return &api.LimitRange{}, nil
|
||||
}
|
||||
|
||||
func (c *FakeLimitRanges) Update(limitRange *api.LimitRange) (*api.LimitRange, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-limitRange", Value: limitRange.Name})
|
||||
return &api.LimitRange{}, nil
|
||||
}
|
94
pkg/client/limit_ranges.go
Normal file
94
pkg/client/limit_ranges.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
)
|
||||
|
||||
// LimitRangesNamespacer has methods to work with LimitRange resources in a namespace
|
||||
type LimitRangesNamespacer interface {
|
||||
LimitRanges(namespace string) LimitRangeInterface
|
||||
}
|
||||
|
||||
// LimitRangeInterface has methods to work with LimitRange resources.
|
||||
type LimitRangeInterface interface {
|
||||
List(selector labels.Selector) (*api.LimitRangeList, error)
|
||||
Get(name string) (*api.LimitRange, error)
|
||||
Delete(name string) error
|
||||
Create(limitRange *api.LimitRange) (*api.LimitRange, error)
|
||||
Update(limitRange *api.LimitRange) (*api.LimitRange, error)
|
||||
}
|
||||
|
||||
// limitRanges implements LimitRangesNamespacer interface
|
||||
type limitRanges struct {
|
||||
r *Client
|
||||
ns string
|
||||
}
|
||||
|
||||
// newLimitRanges returns a limitRanges
|
||||
func newLimitRanges(c *Client, namespace string) *limitRanges {
|
||||
return &limitRanges{
|
||||
r: c,
|
||||
ns: namespace,
|
||||
}
|
||||
}
|
||||
|
||||
// List takes a selector, and returns the list of limitRanges that match that selector.
|
||||
func (c *limitRanges) List(selector labels.Selector) (result *api.LimitRangeList, err error) {
|
||||
result = &api.LimitRangeList{}
|
||||
err = c.r.Get().Namespace(c.ns).Resource("limitRanges").SelectorParam("labels", selector).Do().Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
// Get takes the name of the limitRange, and returns the corresponding Pod object, and an error if it occurs
|
||||
func (c *limitRanges) Get(name string) (result *api.LimitRange, err error) {
|
||||
if len(name) == 0 {
|
||||
return nil, errors.New("name is required parameter to Get")
|
||||
}
|
||||
|
||||
result = &api.LimitRange{}
|
||||
err = c.r.Get().Namespace(c.ns).Resource("limitRanges").Name(name).Do().Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete takes the name of the limitRange, and returns an error if one occurs
|
||||
func (c *limitRanges) Delete(name string) error {
|
||||
return c.r.Delete().Namespace(c.ns).Resource("limitRanges").Name(name).Do().Error()
|
||||
}
|
||||
|
||||
// Create takes the representation of a limitRange. Returns the server's representation of the limitRange, and an error, if it occurs.
|
||||
func (c *limitRanges) Create(limitRange *api.LimitRange) (result *api.LimitRange, err error) {
|
||||
result = &api.LimitRange{}
|
||||
err = c.r.Post().Namespace(c.ns).Resource("limitRanges").Body(limitRange).Do().Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
// Update takes the representation of a limitRange to update. Returns the server's representation of the limitRange, and an error, if it occurs.
|
||||
func (c *limitRanges) Update(limitRange *api.LimitRange) (result *api.LimitRange, err error) {
|
||||
result = &api.LimitRange{}
|
||||
if len(limitRange.ResourceVersion) == 0 {
|
||||
err = fmt.Errorf("invalid update object, missing resource version: %v", limitRange)
|
||||
return
|
||||
}
|
||||
err = c.r.Put().Namespace(c.ns).Resource("limitRanges").Name(limitRange.Name).Body(limitRange).Do().Into(result)
|
||||
return
|
||||
}
|
194
pkg/client/limit_ranges_test.go
Normal file
194
pkg/client/limit_ranges_test.go
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
//"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
func TestLimitRangeCreate(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
limitRange := &api.LimitRange{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc",
|
||||
},
|
||||
Spec: api.LimitRangeSpec{
|
||||
Limits: []api.LimitRangeItem{
|
||||
{
|
||||
Kind: "pods",
|
||||
Max: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100"),
|
||||
api.ResourceMemory: resource.MustParse("10000"),
|
||||
},
|
||||
Min: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("0"),
|
||||
api.ResourceMemory: resource.MustParse("100"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{
|
||||
Method: "POST",
|
||||
Path: buildResourcePath(ns, "/limitRanges"),
|
||||
Query: buildQueryValues(ns, nil),
|
||||
Body: limitRange,
|
||||
},
|
||||
Response: Response{StatusCode: 200, Body: limitRange},
|
||||
}
|
||||
|
||||
response, err := c.Setup().LimitRanges(ns).Create(limitRange)
|
||||
c.Validate(t, response, err)
|
||||
}
|
||||
|
||||
func TestLimitRangeGet(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
limitRange := &api.LimitRange{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc",
|
||||
},
|
||||
Spec: api.LimitRangeSpec{
|
||||
Limits: []api.LimitRangeItem{
|
||||
{
|
||||
Kind: "pods",
|
||||
Max: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100"),
|
||||
api.ResourceMemory: resource.MustParse("10000"),
|
||||
},
|
||||
Min: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("0"),
|
||||
api.ResourceMemory: resource.MustParse("100"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{
|
||||
Method: "GET",
|
||||
Path: buildResourcePath(ns, "/limitRanges/abc"),
|
||||
Query: buildQueryValues(ns, nil),
|
||||
Body: nil,
|
||||
},
|
||||
Response: Response{StatusCode: 200, Body: limitRange},
|
||||
}
|
||||
|
||||
response, err := c.Setup().LimitRanges(ns).Get("abc")
|
||||
c.Validate(t, response, err)
|
||||
}
|
||||
|
||||
func TestLimitRangeList(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
|
||||
limitRangeList := &api.LimitRangeList{
|
||||
Items: []api.LimitRange{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
||||
},
|
||||
},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{
|
||||
Method: "GET",
|
||||
Path: buildResourcePath(ns, "/limitRanges"),
|
||||
Query: buildQueryValues(ns, nil),
|
||||
Body: nil,
|
||||
},
|
||||
Response: Response{StatusCode: 200, Body: limitRangeList},
|
||||
}
|
||||
response, err := c.Setup().LimitRanges(ns).List(labels.Everything())
|
||||
c.Validate(t, response, err)
|
||||
}
|
||||
|
||||
func TestLimitRangeUpdate(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
limitRange := &api.LimitRange{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc",
|
||||
ResourceVersion: "1",
|
||||
},
|
||||
Spec: api.LimitRangeSpec{
|
||||
Limits: []api.LimitRangeItem{
|
||||
{
|
||||
Kind: "pods",
|
||||
Max: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100"),
|
||||
api.ResourceMemory: resource.MustParse("10000"),
|
||||
},
|
||||
Min: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("0"),
|
||||
api.ResourceMemory: resource.MustParse("100"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "PUT", Path: buildResourcePath(ns, "/limitRanges/abc"), Query: buildQueryValues(ns, nil)},
|
||||
Response: Response{StatusCode: 200, Body: limitRange},
|
||||
}
|
||||
response, err := c.Setup().LimitRanges(ns).Update(limitRange)
|
||||
c.Validate(t, response, err)
|
||||
}
|
||||
|
||||
func TestInvalidLimitRangeUpdate(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
limitRange := &api.LimitRange{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc",
|
||||
},
|
||||
Spec: api.LimitRangeSpec{
|
||||
Limits: []api.LimitRangeItem{
|
||||
{
|
||||
Kind: "pods",
|
||||
Max: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100"),
|
||||
api.ResourceMemory: resource.MustParse("10000"),
|
||||
},
|
||||
Min: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("0"),
|
||||
api.ResourceMemory: resource.MustParse("100"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "PUT", Path: buildResourcePath(ns, "/limitRanges/abc"), Query: buildQueryValues(ns, nil)},
|
||||
Response: Response{StatusCode: 200, Body: limitRange},
|
||||
}
|
||||
_, err := c.Setup().LimitRanges(ns).Update(limitRange)
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error due to missing ResourceVersion")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLimitRangeDelete(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "DELETE", Path: buildResourcePath(ns, "/limitRanges/foo"), Query: buildQueryValues(ns, nil)},
|
||||
Response: Response{StatusCode: 200},
|
||||
}
|
||||
err := c.Setup().LimitRanges(ns).Delete("foo")
|
||||
c.Validate(t, nil, err)
|
||||
}
|
Reference in New Issue
Block a user