Add REST PrepareForUpdate() hook

As per discussion with @snmarterclayton.  I implemented this for most
types in the "obvious" way.  I am not sure how to implement
this for a couple types, though.
This commit is contained in:
Tim Hockin
2015-03-25 17:03:30 -07:00
parent b2687c1a84
commit 0f36c68244
9 changed files with 76 additions and 1 deletions

View File

@@ -66,6 +66,14 @@ func (svcStrategy) PrepareForCreate(obj runtime.Object) {
service.Status = api.ServiceStatus{}
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (svcStrategy) PrepareForUpdate(obj, old runtime.Object) {
// TODO: once service has a status sub-resource we can enable this.
//newService := obj.(*api.Service)
//oldService := old.(*api.Service)
//newService.Status = oldService.Status
}
// Validate validates a new service.
func (svcStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
service := obj.(*api.Service)

View File

@@ -33,6 +33,10 @@ type RESTUpdateStrategy interface {
NamespaceScoped() bool
// AllowCreateOnUpdate returns true if the object can be created by a PUT.
AllowCreateOnUpdate() bool
// PrepareForUpdate is invoked on update before validation to normalize
// the object. For example: remove fields that are not to be persisted,
// sort order-insensitive list fields, etc.
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
@@ -53,6 +57,7 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx api.Context, obj, old runtime
} else {
objectMeta.Namespace = api.NamespaceNone
}
strategy.PrepareForUpdate(obj, old)
if errs := strategy.ValidateUpdate(obj, old); len(errs) > 0 {
return errors.NewInvalid(kind, objectMeta.Name, errs)
}