mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Updates to review comments
This commit is contained in:
parent
f3859394ab
commit
6625e58a20
@ -41,14 +41,14 @@ func validateObject(obj runtime.Object) (errors []error) {
|
|||||||
errors = append(errors, validateObject(&t.Items[i])...)
|
errors = append(errors, validateObject(&t.Items[i])...)
|
||||||
}
|
}
|
||||||
case *api.Service:
|
case *api.Service:
|
||||||
api.ValidNamespaceOnCreateOrUpdate(ctx, &t.JSONBase)
|
api.ValidNamespace(ctx, &t.JSONBase)
|
||||||
errors = validation.ValidateService(t)
|
errors = validation.ValidateService(t)
|
||||||
case *api.ServiceList:
|
case *api.ServiceList:
|
||||||
for i := range t.Items {
|
for i := range t.Items {
|
||||||
errors = append(errors, validateObject(&t.Items[i])...)
|
errors = append(errors, validateObject(&t.Items[i])...)
|
||||||
}
|
}
|
||||||
case *api.Pod:
|
case *api.Pod:
|
||||||
api.ValidNamespaceOnCreateOrUpdate(ctx, &t.JSONBase)
|
api.ValidNamespace(ctx, &t.JSONBase)
|
||||||
errors = validation.ValidateManifest(&t.DesiredState.Manifest)
|
errors = validation.ValidateManifest(&t.DesiredState.Manifest)
|
||||||
case *api.PodList:
|
case *api.PodList:
|
||||||
for i := range t.Items {
|
for i := range t.Items {
|
||||||
|
@ -63,8 +63,8 @@ func NamespaceFrom(ctx Context) (string, bool) {
|
|||||||
return namespace, ok
|
return namespace, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidNamespaceOnCreateOrUpdate returns false if the namespace on the context differs from the resource. If the resource has no namespace, it is set to the value in the context.
|
// ValidNamespace returns false if the namespace on the context differs from the resource. If the resource has no namespace, it is set to the value in the context.
|
||||||
func ValidNamespaceOnCreateOrUpdate(ctx Context, resource *JSONBase) bool {
|
func ValidNamespace(ctx Context, resource *JSONBase) bool {
|
||||||
ns, ok := NamespaceFrom(ctx)
|
ns, ok := NamespaceFrom(ctx)
|
||||||
if len(resource.Namespace) == 0 {
|
if len(resource.Namespace) == 0 {
|
||||||
resource.Namespace = ns
|
resource.Namespace = ns
|
||||||
|
@ -32,24 +32,31 @@ func TestNamespaceContext(t *testing.T) {
|
|||||||
if api.NamespaceDefault != result {
|
if api.NamespaceDefault != result {
|
||||||
t.Errorf("Expected: %v, Actual: %v", api.NamespaceDefault, result)
|
t.Errorf("Expected: %v, Actual: %v", api.NamespaceDefault, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx = api.NewContext()
|
||||||
|
result, ok = api.NamespaceFrom(ctx)
|
||||||
|
if ok {
|
||||||
|
t.Errorf("Should not be ok because there is no namespace on the context")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidNamespaceOnCreateOrUpdate(t *testing.T) {
|
// TestValidNamespace validates that namespace rules are enforced on a resource prior to create or update
|
||||||
|
func TestValidNamespace(t *testing.T) {
|
||||||
ctx := api.NewDefaultContext()
|
ctx := api.NewDefaultContext()
|
||||||
namespace, _ := api.NamespaceFrom(ctx)
|
namespace, _ := api.NamespaceFrom(ctx)
|
||||||
resource := api.ReplicationController{}
|
resource := api.ReplicationController{}
|
||||||
if !api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) {
|
if !api.ValidNamespace(ctx, &resource.JSONBase) {
|
||||||
t.Errorf("expected success")
|
t.Errorf("expected success")
|
||||||
}
|
}
|
||||||
if namespace != resource.Namespace {
|
if namespace != resource.Namespace {
|
||||||
t.Errorf("expected resource to have the default namespace assigned during validation")
|
t.Errorf("expected resource to have the default namespace assigned during validation")
|
||||||
}
|
}
|
||||||
resource = api.ReplicationController{JSONBase: api.JSONBase{Namespace: "other"}}
|
resource = api.ReplicationController{JSONBase: api.JSONBase{Namespace: "other"}}
|
||||||
if api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) {
|
if api.ValidNamespace(ctx, &resource.JSONBase) {
|
||||||
t.Errorf("Expected error that resource and context errors do not match because resource has different namespace")
|
t.Errorf("Expected error that resource and context errors do not match because resource has different namespace")
|
||||||
}
|
}
|
||||||
ctx = api.NewContext()
|
ctx = api.NewContext()
|
||||||
if api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) {
|
if api.ValidNamespace(ctx, &resource.JSONBase) {
|
||||||
t.Errorf("Expected error that resource and context errors do not match since context has no namespace")
|
t.Errorf("Expected error that resource and context errors do not match since context has no namespace")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
stderrs "errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -60,8 +59,8 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Obje
|
|||||||
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 !api.ValidNamespaceOnCreateOrUpdate(ctx, &controller.JSONBase) {
|
if !api.ValidNamespace(ctx, &controller.JSONBase) {
|
||||||
return nil, errors.NewConflict("controller", controller.Namespace, stderrs.New("Controller.Namespace does not match the provided context"))
|
return nil, errors.NewConflict("controller", controller.Namespace, fmt.Errorf("Controller.Namespace does not match the provided context"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(controller.ID) == 0 {
|
if len(controller.ID) == 0 {
|
||||||
@ -133,8 +132,8 @@ func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Obje
|
|||||||
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 !api.ValidNamespaceOnCreateOrUpdate(ctx, &controller.JSONBase) {
|
if !api.ValidNamespace(ctx, &controller.JSONBase) {
|
||||||
return nil, errors.NewConflict("controller", controller.Namespace, stderrs.New("Controller.Namespace does not match the provided context"))
|
return nil, errors.NewConflict("controller", controller.Namespace, fmt.Errorf("Controller.Namespace does not match the provided context"))
|
||||||
}
|
}
|
||||||
if errs := validation.ValidateReplicationController(controller); len(errs) > 0 {
|
if errs := validation.ValidateReplicationController(controller); len(errs) > 0 {
|
||||||
return nil, errors.NewInvalid("replicationController", controller.ID, errs)
|
return nil, errors.NewInvalid("replicationController", controller.ID, errs)
|
||||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package pod
|
package pod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
stderrs "errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -70,8 +69,8 @@ func NewREST(config *RESTConfig) *REST {
|
|||||||
|
|
||||||
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
||||||
pod := obj.(*api.Pod)
|
pod := obj.(*api.Pod)
|
||||||
if !api.ValidNamespaceOnCreateOrUpdate(ctx, &pod.JSONBase) {
|
if !api.ValidNamespace(ctx, &pod.JSONBase) {
|
||||||
return nil, errors.NewConflict("pod", pod.Namespace, stderrs.New("Pod.Namespace does not match the provided context"))
|
return nil, errors.NewConflict("pod", pod.Namespace, fmt.Errorf("Pod.Namespace does not match the provided context"))
|
||||||
}
|
}
|
||||||
pod.DesiredState.Manifest.UUID = uuid.NewUUID().String()
|
pod.DesiredState.Manifest.UUID = uuid.NewUUID().String()
|
||||||
if len(pod.ID) == 0 {
|
if len(pod.ID) == 0 {
|
||||||
@ -162,8 +161,8 @@ func (*REST) New() runtime.Object {
|
|||||||
|
|
||||||
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
||||||
pod := obj.(*api.Pod)
|
pod := obj.(*api.Pod)
|
||||||
if !api.ValidNamespaceOnCreateOrUpdate(ctx, &pod.JSONBase) {
|
if !api.ValidNamespace(ctx, &pod.JSONBase) {
|
||||||
return nil, errors.NewConflict("pod", pod.Namespace, stderrs.New("Pod.Namespace does not match the provided context"))
|
return nil, errors.NewConflict("pod", pod.Namespace, fmt.Errorf("Pod.Namespace does not match the provided context"))
|
||||||
}
|
}
|
||||||
if errs := validation.ValidatePod(pod); len(errs) > 0 {
|
if errs := validation.ValidatePod(pod); len(errs) > 0 {
|
||||||
return nil, errors.NewInvalid("pod", pod.ID, errs)
|
return nil, errors.NewInvalid("pod", pod.ID, errs)
|
||||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
stderrs "errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -53,8 +52,8 @@ func NewREST(registry Registry, cloud cloudprovider.Interface, machines minion.R
|
|||||||
|
|
||||||
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
||||||
srv := obj.(*api.Service)
|
srv := obj.(*api.Service)
|
||||||
if !api.ValidNamespaceOnCreateOrUpdate(ctx, &srv.JSONBase) {
|
if !api.ValidNamespace(ctx, &srv.JSONBase) {
|
||||||
return nil, errors.NewConflict("service", srv.Namespace, stderrs.New("Service.Namespace does not match the provided context"))
|
return nil, errors.NewConflict("service", srv.Namespace, fmt.Errorf("Service.Namespace does not match the provided context"))
|
||||||
}
|
}
|
||||||
if errs := validation.ValidateService(srv); len(errs) > 0 {
|
if errs := validation.ValidateService(srv); len(errs) > 0 {
|
||||||
return nil, errors.NewInvalid("service", srv.ID, errs)
|
return nil, errors.NewInvalid("service", srv.ID, errs)
|
||||||
@ -169,8 +168,8 @@ func GetServiceEnvironmentVariables(ctx api.Context, registry Registry, machine
|
|||||||
|
|
||||||
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
||||||
srv := obj.(*api.Service)
|
srv := obj.(*api.Service)
|
||||||
if !api.ValidNamespaceOnCreateOrUpdate(ctx, &srv.JSONBase) {
|
if !api.ValidNamespace(ctx, &srv.JSONBase) {
|
||||||
return nil, errors.NewConflict("service", srv.Namespace, stderrs.New("Service.Namespace does not match the provided context"))
|
return nil, errors.NewConflict("service", srv.Namespace, fmt.Errorf("Service.Namespace does not match the provided context"))
|
||||||
}
|
}
|
||||||
if errs := validation.ValidateService(srv); len(errs) > 0 {
|
if errs := validation.ValidateService(srv); len(errs) > 0 {
|
||||||
return nil, errors.NewInvalid("service", srv.ID, errs)
|
return nil, errors.NewInvalid("service", srv.ID, errs)
|
||||||
|
Loading…
Reference in New Issue
Block a user