From d7c5f427cb9bd507ea0bd58ef624e1557982ba8e Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Mon, 30 Mar 2015 13:51:33 -0400 Subject: [PATCH] Pass ctx to Validate, ValidateUpdate Pass ctx to Validate/ValidateUpdate. This is useful so strategies can access data such as the current user. --- pkg/api/rest/create.go | 4 ++-- pkg/api/rest/types.go | 4 ++-- pkg/api/rest/update.go | 4 ++-- pkg/registry/controller/rest.go | 6 +++--- pkg/registry/endpoint/rest.go | 4 ++-- pkg/registry/generic/etcd/etcd_test.go | 4 ++-- pkg/registry/minion/rest.go | 4 ++-- pkg/registry/namespace/rest.go | 8 ++++---- pkg/registry/pod/rest.go | 6 +++--- pkg/registry/resourcequota/rest.go | 6 +++--- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pkg/api/rest/create.go b/pkg/api/rest/create.go index fad73a4958827..d627cc9f2d645 100644 --- a/pkg/api/rest/create.go +++ b/pkg/api/rest/create.go @@ -40,7 +40,7 @@ type RESTCreateStrategy interface { PrepareForCreate(obj runtime.Object) // Validate is invoked after default fields in the object have been filled in before // the object is persisted. - Validate(obj runtime.Object) fielderrors.ValidationErrorList + Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList } // BeforeCreate ensures that common operations for all resources are performed on creation. It only returns @@ -63,7 +63,7 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx api.Context, obj runtime.Obje api.FillObjectMetaSystemFields(ctx, objectMeta) api.GenerateName(strategy, objectMeta) - if errs := strategy.Validate(obj); len(errs) > 0 { + if errs := strategy.Validate(ctx, obj); len(errs) > 0 { return errors.NewInvalid(kind, objectMeta.Name, errs) } return nil diff --git a/pkg/api/rest/types.go b/pkg/api/rest/types.go index 1442efe70a4e4..7354fd99346ac 100644 --- a/pkg/api/rest/types.go +++ b/pkg/api/rest/types.go @@ -75,7 +75,7 @@ func (svcStrategy) PrepareForUpdate(obj, old runtime.Object) { } // Validate validates a new service. -func (svcStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (svcStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { service := obj.(*api.Service) return validation.ValidateService(service) } @@ -84,6 +84,6 @@ func (svcStrategy) AllowCreateOnUpdate() bool { return true } -func (svcStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (svcStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateServiceUpdate(old.(*api.Service), obj.(*api.Service)) } diff --git a/pkg/api/rest/update.go b/pkg/api/rest/update.go index 1eab5fd2b022d..1cafac2f3dfc8 100644 --- a/pkg/api/rest/update.go +++ b/pkg/api/rest/update.go @@ -39,7 +39,7 @@ type RESTUpdateStrategy interface { PrepareForUpdate(obj, old runtime.Object) // ValidateUpdate is invoked after default fields in the object have been filled in before // the object is persisted. - ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList + ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList } // BeforeUpdate ensures that common operations for all resources are performed on update. It only returns @@ -58,7 +58,7 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx api.Context, obj, old runtime objectMeta.Namespace = api.NamespaceNone } strategy.PrepareForUpdate(obj, old) - if errs := strategy.ValidateUpdate(obj, old); len(errs) > 0 { + if errs := strategy.ValidateUpdate(ctx, obj, old); len(errs) > 0 { return errors.NewInvalid(kind, objectMeta.Name, errs) } return nil diff --git a/pkg/registry/controller/rest.go b/pkg/registry/controller/rest.go index ddd196dab03d7..2a333c3b14733 100644 --- a/pkg/registry/controller/rest.go +++ b/pkg/registry/controller/rest.go @@ -18,6 +18,7 @@ package controller import ( "fmt" + "strconv" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation" @@ -26,7 +27,6 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors" - "strconv" ) // rcStrategy implements verification logic for Replication Controllers. @@ -58,7 +58,7 @@ func (rcStrategy) PrepareForUpdate(obj, old runtime.Object) { } // Validate validates a new replication controller. -func (rcStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (rcStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { controller := obj.(*api.ReplicationController) return validation.ValidateReplicationController(controller) } @@ -70,7 +70,7 @@ func (rcStrategy) AllowCreateOnUpdate() bool { } // ValidateUpdate is the default update validation for an end user. -func (rcStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (rcStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateReplicationControllerUpdate(old.(*api.ReplicationController), obj.(*api.ReplicationController)) } diff --git a/pkg/registry/endpoint/rest.go b/pkg/registry/endpoint/rest.go index 019eed97f3a35..08f799cb9c102 100644 --- a/pkg/registry/endpoint/rest.go +++ b/pkg/registry/endpoint/rest.go @@ -58,7 +58,7 @@ func (endpointsStrategy) PrepareForUpdate(obj, old runtime.Object) { } // Validate validates a new endpoints. -func (endpointsStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (endpointsStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateEndpoints(obj.(*api.Endpoints)) } @@ -68,7 +68,7 @@ func (endpointsStrategy) AllowCreateOnUpdate() bool { } // ValidateUpdate is the default update validation for an end user. -func (endpointsStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (endpointsStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateEndpointsUpdate(old.(*api.Endpoints), obj.(*api.Endpoints)) } diff --git a/pkg/registry/generic/etcd/etcd_test.go b/pkg/registry/generic/etcd/etcd_test.go index acb25bb1c7524..bdb49ca08720c 100644 --- a/pkg/registry/generic/etcd/etcd_test.go +++ b/pkg/registry/generic/etcd/etcd_test.go @@ -45,10 +45,10 @@ func (t *testRESTStrategy) AllowCreateOnUpdate() bool { return t.allowCreateOnUp func (t *testRESTStrategy) PrepareForCreate(obj runtime.Object) {} func (t *testRESTStrategy) PrepareForUpdate(obj, old runtime.Object) {} -func (t *testRESTStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (t *testRESTStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { return nil } -func (t *testRESTStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (t *testRESTStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return nil } diff --git a/pkg/registry/minion/rest.go b/pkg/registry/minion/rest.go index 63c200869b9f9..797c5f781b0df 100644 --- a/pkg/registry/minion/rest.go +++ b/pkg/registry/minion/rest.go @@ -67,13 +67,13 @@ func (nodeStrategy) PrepareForUpdate(obj, old runtime.Object) { } // Validate validates a new node. -func (nodeStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (nodeStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { node := obj.(*api.Node) return validation.ValidateMinion(node) } // ValidateUpdate is the default update validation for an end user. -func (nodeStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (nodeStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateMinionUpdate(old.(*api.Node), obj.(*api.Node)) } diff --git a/pkg/registry/namespace/rest.go b/pkg/registry/namespace/rest.go index a9c522e6abe47..75b69625e7b03 100644 --- a/pkg/registry/namespace/rest.go +++ b/pkg/registry/namespace/rest.go @@ -77,7 +77,7 @@ func (namespaceStrategy) PrepareForUpdate(obj, old runtime.Object) { } // Validate validates a new namespace. -func (namespaceStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (namespaceStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { namespace := obj.(*api.Namespace) return validation.ValidateNamespace(namespace) } @@ -88,7 +88,7 @@ func (namespaceStrategy) AllowCreateOnUpdate() bool { } // ValidateUpdate is the default update validation for an end user. -func (namespaceStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (namespaceStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateNamespaceUpdate(obj.(*api.Namespace), old.(*api.Namespace)) } @@ -104,7 +104,7 @@ func (namespaceStatusStrategy) PrepareForUpdate(obj, old runtime.Object) { newNamespace.Spec = oldNamespace.Spec } -func (namespaceStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (namespaceStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateNamespaceStatusUpdate(obj.(*api.Namespace), old.(*api.Namespace)) } @@ -114,7 +114,7 @@ type namespaceFinalizeStrategy struct { var FinalizeStrategy = namespaceFinalizeStrategy{Strategy} -func (namespaceFinalizeStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (namespaceFinalizeStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateNamespaceFinalizeUpdate(obj.(*api.Namespace), old.(*api.Namespace)) } diff --git a/pkg/registry/pod/rest.go b/pkg/registry/pod/rest.go index e301a91963b2f..cbc1b84526842 100644 --- a/pkg/registry/pod/rest.go +++ b/pkg/registry/pod/rest.go @@ -67,7 +67,7 @@ func (podStrategy) PrepareForUpdate(obj, old runtime.Object) { } // Validate validates a new pod. -func (podStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (podStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { pod := obj.(*api.Pod) return validation.ValidatePod(pod) } @@ -78,7 +78,7 @@ func (podStrategy) AllowCreateOnUpdate() bool { } // ValidateUpdate is the default update validation for an end user. -func (podStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (podStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidatePodUpdate(obj.(*api.Pod), old.(*api.Pod)) } @@ -99,7 +99,7 @@ func (podStatusStrategy) PrepareForUpdate(obj, old runtime.Object) { newPod.Spec = oldPod.Spec } -func (podStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (podStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { // TODO: merge valid fields after update return validation.ValidatePodStatusUpdate(obj.(*api.Pod), old.(*api.Pod)) } diff --git a/pkg/registry/resourcequota/rest.go b/pkg/registry/resourcequota/rest.go index ca6125f21beb6..b008adc6dd83c 100644 --- a/pkg/registry/resourcequota/rest.go +++ b/pkg/registry/resourcequota/rest.go @@ -57,7 +57,7 @@ func (resourcequotaStrategy) PrepareForUpdate(obj, old runtime.Object) { } // Validate validates a new resourcequota. -func (resourcequotaStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList { +func (resourcequotaStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { resourcequota := obj.(*api.ResourceQuota) return validation.ValidateResourceQuota(resourcequota) } @@ -68,7 +68,7 @@ func (resourcequotaStrategy) AllowCreateOnUpdate() bool { } // ValidateUpdate is the default update validation for an end user. -func (resourcequotaStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (resourcequotaStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateResourceQuotaUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota)) } @@ -84,7 +84,7 @@ func (resourcequotaStatusStrategy) PrepareForUpdate(obj, old runtime.Object) { newResourcequota.Spec = oldResourcequota.Spec } -func (resourcequotaStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList { +func (resourcequotaStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { return validation.ValidateResourceQuotaStatusUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota)) }