mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
Merge pull request #639 from rafael/validate_pods
Validate pod on create and update.
This commit is contained in:
commit
cd0b25f1e5
@ -281,6 +281,16 @@ func ValidateManifest(manifest *ContainerManifest) []error {
|
|||||||
return []error(allErrs)
|
return []error(allErrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pod tests if required fields in the pod are set.
|
||||||
|
func ValidatePod(pod *Pod) []error {
|
||||||
|
allErrs := errorList{}
|
||||||
|
if pod.ID == "" {
|
||||||
|
allErrs.Append(makeInvalidError("Pod.ID", pod.ID))
|
||||||
|
}
|
||||||
|
allErrs.Append(ValidateManifest(&pod.DesiredState.Manifest)...)
|
||||||
|
return []error(allErrs)
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateService tests if required fields in the service are set.
|
// ValidateService tests if required fields in the service are set.
|
||||||
func ValidateService(service *Service) []error {
|
func ValidateService(service *Service) []error {
|
||||||
allErrs := errorList{}
|
allErrs := errorList{}
|
||||||
|
@ -213,6 +213,10 @@ func (storage *PodRegistryStorage) Create(obj interface{}) (<-chan interface{},
|
|||||||
}
|
}
|
||||||
pod.DesiredState.Manifest.ID = pod.ID
|
pod.DesiredState.Manifest.ID = pod.ID
|
||||||
|
|
||||||
|
if errs := api.ValidatePod(&pod); 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.scheduleAndCreatePod(pod)
|
err := storage.scheduleAndCreatePod(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -224,10 +228,9 @@ func (storage *PodRegistryStorage) Create(obj interface{}) (<-chan interface{},
|
|||||||
|
|
||||||
func (storage *PodRegistryStorage) Update(obj interface{}) (<-chan interface{}, error) {
|
func (storage *PodRegistryStorage) Update(obj interface{}) (<-chan interface{}, error) {
|
||||||
pod := obj.(api.Pod)
|
pod := obj.(api.Pod)
|
||||||
if len(pod.ID) == 0 {
|
if errs := api.ValidatePod(&pod); len(errs) > 0 {
|
||||||
return nil, fmt.Errorf("ID should not be empty: %#v", pod)
|
return nil, fmt.Errorf("Validation errors: %v", errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiserver.MakeAsync(func() (interface{}, error) {
|
return apiserver.MakeAsync(func() (interface{}, error) {
|
||||||
err := storage.registry.UpdatePod(pod)
|
err := storage.registry.UpdatePod(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -65,7 +65,12 @@ func TestCreatePodRegistryError(t *testing.T) {
|
|||||||
scheduler: &MockScheduler{},
|
scheduler: &MockScheduler{},
|
||||||
registry: mockRegistry,
|
registry: mockRegistry,
|
||||||
}
|
}
|
||||||
pod := api.Pod{}
|
desiredState := api.PodState{
|
||||||
|
Manifest: api.ContainerManifest{
|
||||||
|
Version: "v1beta1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
pod := api.Pod{DesiredState: desiredState}
|
||||||
ch, err := storage.Create(pod)
|
ch, err := storage.Create(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected %#v, Got %#v", nil, err)
|
t.Errorf("Expected %#v, Got %#v", nil, err)
|
||||||
@ -91,7 +96,12 @@ func TestCreatePodSchedulerError(t *testing.T) {
|
|||||||
storage := PodRegistryStorage{
|
storage := PodRegistryStorage{
|
||||||
scheduler: &mockScheduler,
|
scheduler: &mockScheduler,
|
||||||
}
|
}
|
||||||
pod := api.Pod{}
|
desiredState := api.PodState{
|
||||||
|
Manifest: api.ContainerManifest{
|
||||||
|
Version: "v1beta1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
pod := api.Pod{DesiredState: desiredState}
|
||||||
ch, err := storage.Create(pod)
|
ch, err := storage.Create(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected %#v, Got %#v", nil, err)
|
t.Errorf("Expected %#v, Got %#v", nil, err)
|
||||||
@ -118,7 +128,12 @@ func TestCreatePodSetsIds(t *testing.T) {
|
|||||||
scheduler: &MockScheduler{machine: "test"},
|
scheduler: &MockScheduler{machine: "test"},
|
||||||
registry: mockRegistry,
|
registry: mockRegistry,
|
||||||
}
|
}
|
||||||
pod := api.Pod{}
|
desiredState := api.PodState{
|
||||||
|
Manifest: api.ContainerManifest{
|
||||||
|
Version: "v1beta1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
pod := api.Pod{DesiredState: desiredState}
|
||||||
ch, err := storage.Create(pod)
|
ch, err := storage.Create(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected %#v, Got %#v", nil, err)
|
t.Errorf("Expected %#v, Got %#v", nil, err)
|
||||||
@ -254,6 +269,7 @@ func TestGetPodCloud(t *testing.T) {
|
|||||||
func TestMakePodStatus(t *testing.T) {
|
func TestMakePodStatus(t *testing.T) {
|
||||||
desiredState := api.PodState{
|
desiredState := api.PodState{
|
||||||
Manifest: api.ContainerManifest{
|
Manifest: api.ContainerManifest{
|
||||||
|
Version: "v1beta1",
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{Name: "containerA"},
|
{Name: "containerA"},
|
||||||
{Name: "containerB"},
|
{Name: "containerB"},
|
||||||
@ -337,6 +353,43 @@ func TestMakePodStatus(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func TestPodStorageValidatesCreate(t *testing.T) {
|
||||||
|
mockRegistry := &MockPodStorageRegistry{
|
||||||
|
MockPodRegistry: MockPodRegistry{err: fmt.Errorf("test error")},
|
||||||
|
}
|
||||||
|
storage := PodRegistryStorage{
|
||||||
|
scheduler: &MockScheduler{machine: "test"},
|
||||||
|
registry: mockRegistry,
|
||||||
|
}
|
||||||
|
pod := api.Pod{}
|
||||||
|
c, err := storage.Create(pod)
|
||||||
|
if c != nil {
|
||||||
|
t.Errorf("Expected nil channel")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected to get an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPodStorageValidatesUpdate(t *testing.T) {
|
||||||
|
mockRegistry := &MockPodStorageRegistry{
|
||||||
|
MockPodRegistry: MockPodRegistry{err: fmt.Errorf("test error")},
|
||||||
|
}
|
||||||
|
storage := PodRegistryStorage{
|
||||||
|
scheduler: &MockScheduler{machine: "test"},
|
||||||
|
registry: mockRegistry,
|
||||||
|
}
|
||||||
|
pod := api.Pod{}
|
||||||
|
c, err := storage.Update(pod)
|
||||||
|
if c != nil {
|
||||||
|
t.Errorf("Expected nil channel")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected to get an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreatePod(t *testing.T) {
|
func TestCreatePod(t *testing.T) {
|
||||||
mockRegistry := MockPodRegistry{
|
mockRegistry := MockPodRegistry{
|
||||||
pod: &api.Pod{
|
pod: &api.Pod{
|
||||||
@ -352,8 +405,14 @@ func TestCreatePod(t *testing.T) {
|
|||||||
scheduler: scheduler.MakeRoundRobinScheduler(),
|
scheduler: scheduler.MakeRoundRobinScheduler(),
|
||||||
minionLister: MakeMinionRegistry([]string{"machine"}),
|
minionLister: MakeMinionRegistry([]string{"machine"}),
|
||||||
}
|
}
|
||||||
|
desiredState := api.PodState{
|
||||||
|
Manifest: api.ContainerManifest{
|
||||||
|
Version: "v1beta1",
|
||||||
|
},
|
||||||
|
}
|
||||||
pod := api.Pod{
|
pod := api.Pod{
|
||||||
JSONBase: api.JSONBase{ID: "foo"},
|
JSONBase: api.JSONBase{ID: "foo"},
|
||||||
|
DesiredState: desiredState,
|
||||||
}
|
}
|
||||||
channel, err := storage.Create(pod)
|
channel, err := storage.Create(pod)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user