mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-13 13:14:05 +00:00
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:
@@ -48,6 +48,14 @@ func (rcStrategy) PrepareForCreate(obj runtime.Object) {
|
||||
controller.Status = api.ReplicationControllerStatus{}
|
||||
}
|
||||
|
||||
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
||||
func (rcStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
// TODO: once RC has a status sub-resource we can enable this.
|
||||
//newController := obj.(*api.ReplicationController)
|
||||
//oldController := old.(*api.ReplicationController)
|
||||
//newController.Status = oldController.Status
|
||||
}
|
||||
|
||||
// Validate validates a new replication controller.
|
||||
func (rcStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
|
||||
controller := obj.(*api.ReplicationController)
|
||||
|
@@ -48,6 +48,12 @@ func (endpointsStrategy) PrepareForCreate(obj runtime.Object) {
|
||||
_ = obj.(*api.Endpoints)
|
||||
}
|
||||
|
||||
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
||||
func (endpointsStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
_ = obj.(*api.Endpoints)
|
||||
_ = old.(*api.Endpoints)
|
||||
}
|
||||
|
||||
// Validate validates a new endpoints.
|
||||
func (endpointsStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
|
||||
return validation.ValidateEndpoints(obj.(*api.Endpoints))
|
||||
|
@@ -43,7 +43,8 @@ type testRESTStrategy struct {
|
||||
func (t *testRESTStrategy) NamespaceScoped() bool { return t.namespaceScoped }
|
||||
func (t *testRESTStrategy) AllowCreateOnUpdate() bool { return t.allowCreateOnUpdate }
|
||||
|
||||
func (t *testRESTStrategy) PrepareForCreate(obj runtime.Object) {}
|
||||
func (t *testRESTStrategy) PrepareForCreate(obj runtime.Object) {}
|
||||
func (t *testRESTStrategy) PrepareForUpdate(obj, old runtime.Object) {}
|
||||
func (t *testRESTStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
|
||||
return nil
|
||||
}
|
||||
|
@@ -59,6 +59,13 @@ func (nodeStrategy) PrepareForCreate(obj runtime.Object) {
|
||||
// Nodes allow *all* fields, including status, to be set.
|
||||
}
|
||||
|
||||
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
||||
func (nodeStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
_ = obj.(*api.Node)
|
||||
_ = old.(*api.Node)
|
||||
// Nodes allow *all* fields, including status, to be set.
|
||||
}
|
||||
|
||||
// Validate validates a new node.
|
||||
func (nodeStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
|
||||
node := obj.(*api.Node)
|
||||
|
@@ -68,6 +68,14 @@ func (namespaceStrategy) PrepareForCreate(obj runtime.Object) {
|
||||
}
|
||||
}
|
||||
|
||||
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
||||
func (namespaceStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
newNamespace := obj.(*api.Namespace)
|
||||
oldNamespace := old.(*api.Namespace)
|
||||
newNamespace.Spec.Finalizers = oldNamespace.Spec.Finalizers
|
||||
newNamespace.Status = oldNamespace.Status
|
||||
}
|
||||
|
||||
// Validate validates a new namespace.
|
||||
func (namespaceStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
|
||||
namespace := obj.(*api.Namespace)
|
||||
@@ -90,6 +98,12 @@ type namespaceStatusStrategy struct {
|
||||
|
||||
var StatusStrategy = namespaceStatusStrategy{Strategy}
|
||||
|
||||
func (namespaceStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
newNamespace := obj.(*api.Namespace)
|
||||
oldNamespace := old.(*api.Namespace)
|
||||
newNamespace.Spec = oldNamespace.Spec
|
||||
}
|
||||
|
||||
func (namespaceStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList {
|
||||
return validation.ValidateNamespaceStatusUpdate(obj.(*api.Namespace), old.(*api.Namespace))
|
||||
}
|
||||
|
@@ -59,6 +59,13 @@ func (podStrategy) PrepareForCreate(obj runtime.Object) {
|
||||
}
|
||||
}
|
||||
|
||||
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
||||
func (podStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
newPod := obj.(*api.Pod)
|
||||
oldPod := old.(*api.Pod)
|
||||
newPod.Status = oldPod.Status
|
||||
}
|
||||
|
||||
// Validate validates a new pod.
|
||||
func (podStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
|
||||
pod := obj.(*api.Pod)
|
||||
@@ -86,6 +93,12 @@ type podStatusStrategy struct {
|
||||
|
||||
var StatusStrategy = podStatusStrategy{Strategy}
|
||||
|
||||
func (podStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
newPod := obj.(*api.Pod)
|
||||
oldPod := old.(*api.Pod)
|
||||
newPod.Spec = oldPod.Spec
|
||||
}
|
||||
|
||||
func (podStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList {
|
||||
// TODO: merge valid fields after update
|
||||
return validation.ValidatePodStatusUpdate(obj.(*api.Pod), old.(*api.Pod))
|
||||
|
@@ -49,6 +49,13 @@ func (resourcequotaStrategy) PrepareForCreate(obj runtime.Object) {
|
||||
resourcequota.Status = api.ResourceQuotaStatus{}
|
||||
}
|
||||
|
||||
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
||||
func (resourcequotaStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
newResourcequota := obj.(*api.ResourceQuota)
|
||||
oldResourcequota := old.(*api.ResourceQuota)
|
||||
newResourcequota.Status = oldResourcequota.Status
|
||||
}
|
||||
|
||||
// Validate validates a new resourcequota.
|
||||
func (resourcequotaStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
|
||||
resourcequota := obj.(*api.ResourceQuota)
|
||||
@@ -71,6 +78,12 @@ type resourcequotaStatusStrategy struct {
|
||||
|
||||
var StatusStrategy = resourcequotaStatusStrategy{Strategy}
|
||||
|
||||
func (resourcequotaStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
newResourcequota := obj.(*api.ResourceQuota)
|
||||
oldResourcequota := old.(*api.ResourceQuota)
|
||||
newResourcequota.Spec = oldResourcequota.Spec
|
||||
}
|
||||
|
||||
func (resourcequotaStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList {
|
||||
return validation.ValidateResourceQuotaStatusUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota))
|
||||
}
|
||||
|
Reference in New Issue
Block a user