mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-20 09:05:26 +00:00
Update Service and ReplControllers to validate
Validations are performed on update and on create.
This commit is contained in:
parent
d32024870a
commit
5bc19584a5
@ -154,12 +154,3 @@ func ParseSelector(selector string) (Selector, error) {
|
|||||||
}
|
}
|
||||||
return andTerm(items), nil
|
return andTerm(items), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustParseSelector parses the selection or panics.
|
|
||||||
func MustParseSelector(selector string) Selector {
|
|
||||||
s, err := ParseSelector(selector)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
@ -91,6 +91,10 @@ func (storage *ControllerRegistryStorage) Create(obj interface{}) (<-chan interf
|
|||||||
// Pod Manifest ID should be assigned by the pod API
|
// Pod Manifest ID should be assigned by the pod API
|
||||||
controller.DesiredState.PodTemplate.DesiredState.Manifest.ID = ""
|
controller.DesiredState.PodTemplate.DesiredState.Manifest.ID = ""
|
||||||
|
|
||||||
|
if errs := api.ValidateReplicationController(&controller); len(errs) > 0 {
|
||||||
|
return nil, fmt.Errorf("Validation errors: %v", errs)
|
||||||
|
}
|
||||||
|
|
||||||
return apiserver.MakeAsync(func() (interface{}, error) {
|
return apiserver.MakeAsync(func() (interface{}, error) {
|
||||||
err := storage.registry.CreateController(controller)
|
err := storage.registry.CreateController(controller)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -106,8 +110,8 @@ func (storage *ControllerRegistryStorage) Update(obj interface{}) (<-chan interf
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("not a replication controller: %#v", obj)
|
return nil, fmt.Errorf("not a replication controller: %#v", obj)
|
||||||
}
|
}
|
||||||
if len(controller.ID) == 0 {
|
if errs := api.ValidateReplicationController(&controller); len(errs) > 0 {
|
||||||
return nil, fmt.Errorf("ID should not be empty: %#v", controller)
|
return nil, fmt.Errorf("Validation errors: %v", errs)
|
||||||
}
|
}
|
||||||
return apiserver.MakeAsync(func() (interface{}, error) {
|
return apiserver.MakeAsync(func() (interface{}, error) {
|
||||||
err := storage.registry.UpdateController(controller)
|
err := storage.registry.UpdateController(controller)
|
||||||
|
@ -187,12 +187,27 @@ func TestControllerParsing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var validPodTemplate = api.PodTemplate{
|
||||||
|
DesiredState: api.PodState{
|
||||||
|
Manifest: api.ContainerManifest{
|
||||||
|
Version: "v1beta1",
|
||||||
|
Containers: []api.Container{
|
||||||
|
{
|
||||||
|
Name: "test",
|
||||||
|
Image: "test_image",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateController(t *testing.T) {
|
func TestCreateController(t *testing.T) {
|
||||||
mockRegistry := MockControllerRegistry{}
|
mockRegistry := MockControllerRegistry{}
|
||||||
mockPodRegistry := MockPodRegistry{
|
mockPodRegistry := MockPodRegistry{
|
||||||
pods: []api.Pod{
|
pods: []api.Pod{
|
||||||
{
|
{
|
||||||
JSONBase: api.JSONBase{ID: "foo"},
|
JSONBase: api.JSONBase{ID: "foo"},
|
||||||
|
Labels: map[string]string{"a": "b"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -204,10 +219,15 @@ func TestCreateController(t *testing.T) {
|
|||||||
controller := api.ReplicationController{
|
controller := api.ReplicationController{
|
||||||
JSONBase: api.JSONBase{ID: "test"},
|
JSONBase: api.JSONBase{ID: "test"},
|
||||||
DesiredState: api.ReplicationControllerState{
|
DesiredState: api.ReplicationControllerState{
|
||||||
Replicas: 2,
|
Replicas: 2,
|
||||||
|
ReplicaSelector: map[string]string{"a": "b"},
|
||||||
|
PodTemplate: validPodTemplate,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
channel, err := storage.Create(controller)
|
channel, err := storage.Create(controller)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -221,9 +241,11 @@ func TestCreateController(t *testing.T) {
|
|||||||
mockPodRegistry.pods = []api.Pod{
|
mockPodRegistry.pods = []api.Pod{
|
||||||
{
|
{
|
||||||
JSONBase: api.JSONBase{ID: "foo"},
|
JSONBase: api.JSONBase{ID: "foo"},
|
||||||
|
Labels: map[string]string{"a": "b"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
JSONBase: api.JSONBase{ID: "bar"},
|
JSONBase: api.JSONBase{ID: "bar"},
|
||||||
|
Labels: map[string]string{"a": "b"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
mockPodRegistry.Unlock()
|
mockPodRegistry.Unlock()
|
||||||
@ -235,3 +257,65 @@ func TestCreateController(t *testing.T) {
|
|||||||
// Do nothing, this is expected
|
// Do nothing, this is expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestControllerStorageValidatesCreate(t *testing.T) {
|
||||||
|
mockRegistry := MockControllerRegistry{}
|
||||||
|
storage := ControllerRegistryStorage{
|
||||||
|
registry: &mockRegistry,
|
||||||
|
podRegistry: nil,
|
||||||
|
pollPeriod: time.Millisecond * 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
failureCases := map[string]api.ReplicationController{
|
||||||
|
"empty ID": api.ReplicationController{
|
||||||
|
JSONBase: api.JSONBase{ID: ""},
|
||||||
|
DesiredState: api.ReplicationControllerState{
|
||||||
|
ReplicaSelector: map[string]string{"bar": "baz"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"empty selector": api.ReplicationController{
|
||||||
|
JSONBase: api.JSONBase{ID: "abc"},
|
||||||
|
DesiredState: api.ReplicationControllerState{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, failureCase := range failureCases {
|
||||||
|
c, err := storage.Create(failureCase)
|
||||||
|
if c != nil {
|
||||||
|
t.Errorf("Expected nil channel")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected to get an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestControllerStorageValidatesUpdate(t *testing.T) {
|
||||||
|
mockRegistry := MockControllerRegistry{}
|
||||||
|
storage := ControllerRegistryStorage{
|
||||||
|
registry: &mockRegistry,
|
||||||
|
podRegistry: nil,
|
||||||
|
pollPeriod: time.Millisecond * 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
failureCases := map[string]api.ReplicationController{
|
||||||
|
"empty ID": api.ReplicationController{
|
||||||
|
JSONBase: api.JSONBase{ID: ""},
|
||||||
|
DesiredState: api.ReplicationControllerState{
|
||||||
|
ReplicaSelector: map[string]string{"bar": "baz"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"empty selector": api.ReplicationController{
|
||||||
|
JSONBase: api.JSONBase{ID: "abc"},
|
||||||
|
DesiredState: api.ReplicationControllerState{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, failureCase := range failureCases {
|
||||||
|
c, err := storage.Update(failureCase)
|
||||||
|
if c != nil {
|
||||||
|
t.Errorf("Expected nil channel")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected to get an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -147,8 +147,7 @@ func (sr *ServiceRegistryStorage) Extract(body []byte) (interface{}, error) {
|
|||||||
|
|
||||||
func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, error) {
|
func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, error) {
|
||||||
srv := obj.(api.Service)
|
srv := obj.(api.Service)
|
||||||
errs := api.ValidateService(&srv)
|
if errs := api.ValidateService(&srv); len(errs) > 0 {
|
||||||
if len(errs) > 0 {
|
|
||||||
return nil, fmt.Errorf("Validation errors: %v", errs)
|
return nil, fmt.Errorf("Validation errors: %v", errs)
|
||||||
}
|
}
|
||||||
return apiserver.MakeAsync(func() (interface{}, error) {
|
return apiserver.MakeAsync(func() (interface{}, error) {
|
||||||
@ -187,6 +186,9 @@ func (sr *ServiceRegistryStorage) Update(obj interface{}) (<-chan interface{}, e
|
|||||||
if srv.ID == "" {
|
if srv.ID == "" {
|
||||||
return nil, fmt.Errorf("ID should not be empty: %#v", srv)
|
return nil, fmt.Errorf("ID should not be empty: %#v", srv)
|
||||||
}
|
}
|
||||||
|
if errs := api.ValidateService(&srv); len(errs) > 0 {
|
||||||
|
return nil, fmt.Errorf("Validation errors: %v", errs)
|
||||||
|
}
|
||||||
return apiserver.MakeAsync(func() (interface{}, error) {
|
return apiserver.MakeAsync(func() (interface{}, error) {
|
||||||
// TODO: check to see if external load balancer status changed
|
// TODO: check to see if external load balancer status changed
|
||||||
err := sr.registry.UpdateService(srv)
|
err := sr.registry.UpdateService(srv)
|
||||||
|
@ -49,6 +49,60 @@ func TestServiceRegistry(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServiceStorageValidatesCreate(t *testing.T) {
|
||||||
|
memory := MakeMemoryRegistry()
|
||||||
|
storage := MakeServiceRegistryStorage(memory, nil, nil)
|
||||||
|
|
||||||
|
failureCases := map[string]api.Service{
|
||||||
|
"empty ID": api.Service{
|
||||||
|
JSONBase: api.JSONBase{ID: ""},
|
||||||
|
Selector: map[string]string{"bar": "baz"},
|
||||||
|
},
|
||||||
|
"empty selector": api.Service{
|
||||||
|
JSONBase: api.JSONBase{ID: "foo"},
|
||||||
|
Selector: map[string]string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, failureCase := range failureCases {
|
||||||
|
c, err := storage.Create(failureCase)
|
||||||
|
if c != nil {
|
||||||
|
t.Errorf("Expected nil channel")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected to get an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceStorageValidatesUpdate(t *testing.T) {
|
||||||
|
memory := MakeMemoryRegistry()
|
||||||
|
memory.CreateService(api.Service{
|
||||||
|
JSONBase: api.JSONBase{ID: "foo"},
|
||||||
|
Selector: map[string]string{"bar": "baz"},
|
||||||
|
})
|
||||||
|
storage := MakeServiceRegistryStorage(memory, nil, nil)
|
||||||
|
|
||||||
|
failureCases := map[string]api.Service{
|
||||||
|
"empty ID": api.Service{
|
||||||
|
JSONBase: api.JSONBase{ID: ""},
|
||||||
|
Selector: map[string]string{"bar": "baz"},
|
||||||
|
},
|
||||||
|
"empty selector": api.Service{
|
||||||
|
JSONBase: api.JSONBase{ID: "foo"},
|
||||||
|
Selector: map[string]string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, failureCase := range failureCases {
|
||||||
|
c, err := storage.Update(failureCase)
|
||||||
|
if c != nil {
|
||||||
|
t.Errorf("Expected nil channel")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected to get an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestServiceRegistryExternalService(t *testing.T) {
|
func TestServiceRegistryExternalService(t *testing.T) {
|
||||||
memory := MakeMemoryRegistry()
|
memory := MakeMemoryRegistry()
|
||||||
fakeCloud := &cloudprovider.FakeCloud{}
|
fakeCloud := &cloudprovider.FakeCloud{}
|
||||||
|
Loading…
Reference in New Issue
Block a user