Skip to content

Commit

Permalink
Plumb context to strategy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Aug 9, 2016
1 parent 16621cd commit 4db0049
Show file tree
Hide file tree
Showing 58 changed files with 166 additions and 165 deletions.
8 changes: 4 additions & 4 deletions federation/registry/cluster/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func MatchCluster(label labels.Selector, field fields.Selector) generic.Matcher
}

// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
func (clusterStrategy) PrepareForCreate(obj runtime.Object) {
func (clusterStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
cluster := obj.(*federation.Cluster)
cluster.Status = federation.ClusterStatus{}
}
Expand All @@ -80,7 +80,7 @@ func (clusterStrategy) AllowCreateOnUpdate() bool {
}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (clusterStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (clusterStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
cluster := obj.(*federation.Cluster)
oldCluster := old.(*federation.Cluster)
cluster.Status = oldCluster.Status
Expand All @@ -100,10 +100,10 @@ type clusterStatusStrategy struct {

var StatusStrategy = clusterStatusStrategy{Strategy}

func (clusterStatusStrategy) PrepareForCreate(obj runtime.Object) {
func (clusterStatusStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
_ = obj.(*federation.Cluster)
}
func (clusterStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (clusterStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
cluster := obj.(*federation.Cluster)
oldCluster := old.(*federation.Cluster)
cluster.Spec = oldCluster.Spec
Expand Down
6 changes: 3 additions & 3 deletions federation/registry/cluster/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestClusterStrategy(t *testing.T) {
}

cluster := validNewCluster()
Strategy.PrepareForCreate(cluster)
Strategy.PrepareForCreate(ctx, cluster)
if len(cluster.Status.Conditions) != 0 {
t.Errorf("Cluster should not allow setting conditions on create")
}
Expand All @@ -89,7 +89,7 @@ func TestClusterStrategy(t *testing.T) {
}

invalidCluster := invalidNewCluster()
Strategy.PrepareForUpdate(invalidCluster, cluster)
Strategy.PrepareForUpdate(ctx, invalidCluster, cluster)
if reflect.DeepEqual(invalidCluster.Spec, cluster.Spec) ||
!reflect.DeepEqual(invalidCluster.Status, cluster.Status) {
t.Error("Only spec is expected being changed")
Expand All @@ -114,7 +114,7 @@ func TestClusterStatusStrategy(t *testing.T) {

cluster := validNewCluster()
invalidCluster := invalidNewCluster()
StatusStrategy.PrepareForUpdate(cluster, invalidCluster)
StatusStrategy.PrepareForUpdate(ctx, cluster, invalidCluster)
if !reflect.DeepEqual(invalidCluster.Spec, cluster.Spec) ||
reflect.DeepEqual(invalidCluster.Status, cluster.Status) {
t.Logf("== cluster.Spec: %v\n", cluster.Spec)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/rest/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type RESTCreateStrategy interface {
// the object. For example: remove fields that are not to be persisted,
// sort order-insensitive list fields, etc. This should not remove fields
// whose presence would be considered a validation error.
PrepareForCreate(obj runtime.Object)
PrepareForCreate(ctx api.Context, obj runtime.Object)
// Validate is invoked after default fields in the object have been filled in before
// the object is persisted. This method should not mutate the object.
Validate(ctx api.Context, obj runtime.Object) field.ErrorList
Expand All @@ -67,7 +67,7 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx api.Context, obj runtime.Obje
}
objectMeta.DeletionTimestamp = nil
objectMeta.DeletionGracePeriodSeconds = nil
strategy.PrepareForCreate(obj)
strategy.PrepareForCreate(ctx, obj)
api.FillObjectMetaSystemFields(ctx, objectMeta)
api.GenerateName(strategy, objectMeta)

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/rest/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type RESTDeleteStrategy interface {
type RESTGracefulDeleteStrategy interface {
// CheckGracefulDelete should return true if the object can be gracefully deleted and set
// any default values on the DeleteOptions.
CheckGracefulDelete(obj runtime.Object, options *api.DeleteOptions) bool
CheckGracefulDelete(ctx api.Context, obj runtime.Object, options *api.DeleteOptions) bool
}

// BeforeDelete tests whether the object can be gracefully deleted. If graceful is set the object
Expand Down Expand Up @@ -87,7 +87,7 @@ func BeforeDelete(strategy RESTDeleteStrategy, ctx api.Context, obj runtime.Obje
return false, true, nil
}

if !gracefulStrategy.CheckGracefulDelete(obj, options) {
if !gracefulStrategy.CheckGracefulDelete(ctx, obj, options) {
return false, false, nil
}
now := unversioned.NewTime(unversioned.Now().Add(time.Second * time.Duration(*options.GracePeriodSeconds)))
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/rest/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ limitations under the License.
package rest

import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/runtime"
)

// RESTExportStrategy is the interface that defines how to export a Kubernetes object
type RESTExportStrategy interface {
// Export strips fields that can not be set by the user. If 'exact' is false
// fields specific to the cluster are also stripped
Export(obj runtime.Object, exact bool) error
Export(ctx api.Context, obj runtime.Object, exact bool) error
}
4 changes: 2 additions & 2 deletions pkg/api/rest/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type RESTUpdateStrategy interface {
// the object. For example: remove fields that are not to be persisted,
// sort order-insensitive list fields, etc. This should not remove fields
// whose presence would be considered a validation error.
PrepareForUpdate(obj, old runtime.Object)
PrepareForUpdate(ctx api.Context, obj, old runtime.Object)
// ValidateUpdate is invoked after default fields in the object have been
// filled in before the object is persisted. This method should not mutate
// the object.
Expand Down Expand Up @@ -93,7 +93,7 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx api.Context, obj, old runtime
}
objectMeta.Generation = oldMeta.Generation

strategy.PrepareForUpdate(obj, old)
strategy.PrepareForUpdate(ctx, obj, old)

// Ensure some common fields, like UID, are validated for all resources.
errs, err := validateCommonFields(obj, old)
Expand Down
12 changes: 6 additions & 6 deletions pkg/registry/certificates/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (csrStrategy) AllowCreateOnUpdate() bool {
// information about the requesting user to be injected by the registry
// interface. Clear everything else.
// TODO: check these ordering assumptions. better way to inject user info?
func (csrStrategy) PrepareForCreate(obj runtime.Object) {
func (csrStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
csr := obj.(*certificates.CertificateSigningRequest)

// Be explicit that users cannot create pre-approved certificate requests.
Expand All @@ -64,7 +64,7 @@ func (csrStrategy) PrepareForCreate(obj runtime.Object) {

// PrepareForUpdate clears fields that are not allowed to be set by end users
// on update. Certificate requests are immutable after creation except via subresources.
func (csrStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (csrStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newCSR := obj.(*certificates.CertificateSigningRequest)
oldCSR := old.(*certificates.CertificateSigningRequest)

Expand Down Expand Up @@ -97,13 +97,13 @@ func (csrStrategy) AllowUnconditionalUpdate() bool {
return true
}

func (s csrStrategy) Export(obj runtime.Object, exact bool) error {
func (s csrStrategy) Export(ctx api.Context, obj runtime.Object, exact bool) error {
csr, ok := obj.(*certificates.CertificateSigningRequest)
if !ok {
// unexpected programmer error
return fmt.Errorf("unexpected object: %v", obj)
}
s.PrepareForCreate(obj)
s.PrepareForCreate(ctx, obj)
if exact {
return nil
}
Expand All @@ -119,7 +119,7 @@ type csrStatusStrategy struct {

var StatusStrategy = csrStatusStrategy{Strategy}

func (csrStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (csrStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newCSR := obj.(*certificates.CertificateSigningRequest)
oldCSR := old.(*certificates.CertificateSigningRequest)

Expand All @@ -145,7 +145,7 @@ type csrApprovalStrategy struct {

var ApprovalStrategy = csrApprovalStrategy{Strategy}

func (csrApprovalStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (csrApprovalStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newCSR := obj.(*certificates.CertificateSigningRequest)
oldCSR := old.(*certificates.CertificateSigningRequest)

Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/clusterrole/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func (strategy) AllowCreateOnUpdate() bool {

// PrepareForCreate clears fields that are not allowed to be set by end users
// on creation.
func (strategy) PrepareForCreate(obj runtime.Object) {
func (strategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
_ = obj.(*rbac.ClusterRole)
}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (strategy) PrepareForUpdate(obj, old runtime.Object) {
func (strategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newClusterRole := obj.(*rbac.ClusterRole)
oldClusterRole := old.(*rbac.ClusterRole)

Expand Down Expand Up @@ -97,7 +97,7 @@ func (strategy) AllowUnconditionalUpdate() bool {
return true
}

func (s strategy) Export(obj runtime.Object, exact bool) error {
func (s strategy) Export(ctx api.Context, obj runtime.Object, exact bool) error {
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/clusterrolebinding/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func (strategy) AllowCreateOnUpdate() bool {

// PrepareForCreate clears fields that are not allowed to be set by end users
// on creation.
func (strategy) PrepareForCreate(obj runtime.Object) {
func (strategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
_ = obj.(*rbac.ClusterRoleBinding)
}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (strategy) PrepareForUpdate(obj, old runtime.Object) {
func (strategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newClusterRoleBinding := obj.(*rbac.ClusterRoleBinding)
oldClusterRoleBinding := old.(*rbac.ClusterRoleBinding)

Expand Down Expand Up @@ -97,7 +97,7 @@ func (strategy) AllowUnconditionalUpdate() bool {
return true
}

func (s strategy) Export(obj runtime.Object, exact bool) error {
func (s strategy) Export(ctx api.Context, obj runtime.Object, exact bool) error {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/configmap/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (strategy) NamespaceScoped() bool {
return true
}

func (strategy) PrepareForCreate(obj runtime.Object) {
func (strategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
_ = obj.(*api.ConfigMap)
}

Expand All @@ -67,7 +67,7 @@ func (strategy) AllowCreateOnUpdate() bool {
return false
}

func (strategy) PrepareForUpdate(newObj, oldObj runtime.Object) {
func (strategy) PrepareForUpdate(ctx api.Context, newObj, oldObj runtime.Object) {
_ = oldObj.(*api.ConfigMap)
_ = newObj.(*api.ConfigMap)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/configmap/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestConfigMapStrategy(t *testing.T) {
},
}

Strategy.PrepareForCreate(cfg)
Strategy.PrepareForCreate(ctx, cfg)

errs := Strategy.Validate(ctx, cfg)
if len(errs) != 0 {
Expand All @@ -62,7 +62,7 @@ func TestConfigMapStrategy(t *testing.T) {
},
}

Strategy.PrepareForUpdate(newCfg, cfg)
Strategy.PrepareForUpdate(ctx, newCfg, cfg)

errs = Strategy.ValidateUpdate(ctx, newCfg, cfg)
if len(errs) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/controller/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ func (rcStrategy) NamespaceScoped() bool {
}

// PrepareForCreate clears the status of a replication controller before creation.
func (rcStrategy) PrepareForCreate(obj runtime.Object) {
func (rcStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
controller := obj.(*api.ReplicationController)
controller.Status = api.ReplicationControllerStatus{}

controller.Generation = 1
}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (rcStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (rcStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newController := obj.(*api.ReplicationController)
oldController := old.(*api.ReplicationController)
// update is not allowed to set status
Expand Down Expand Up @@ -133,7 +133,7 @@ type rcStatusStrategy struct {

var StatusStrategy = rcStatusStrategy{Strategy}

func (rcStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (rcStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newRc := obj.(*api.ReplicationController)
oldRc := old.(*api.ReplicationController)
// update is not allowed to set spec
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/controller/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestControllerStrategy(t *testing.T) {
},
}

Strategy.PrepareForCreate(rc)
Strategy.PrepareForCreate(ctx, rc)
if rc.Status.Replicas != 0 {
t.Error("ReplicationController should not allow setting status.replicas on create")
}
Expand All @@ -74,7 +74,7 @@ func TestControllerStrategy(t *testing.T) {
invalidRc := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "4"},
}
Strategy.PrepareForUpdate(invalidRc, rc)
Strategy.PrepareForUpdate(ctx, invalidRc, rc)
errs = Strategy.ValidateUpdate(ctx, invalidRc, rc)
if len(errs) == 0 {
t.Errorf("Expected a validation error")
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestControllerStatusStrategy(t *testing.T) {
ObservedGeneration: int64(11),
},
}
StatusStrategy.PrepareForUpdate(newController, oldController)
StatusStrategy.PrepareForUpdate(ctx, newController, oldController)
if newController.Status.Replicas != 3 {
t.Errorf("Replication controller status updates should allow change of replicas: %v", newController.Status.Replicas)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/daemonset/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func (daemonSetStrategy) NamespaceScoped() bool {
}

// PrepareForCreate clears the status of a daemon set before creation.
func (daemonSetStrategy) PrepareForCreate(obj runtime.Object) {
func (daemonSetStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
daemonSet := obj.(*extensions.DaemonSet)
daemonSet.Status = extensions.DaemonSetStatus{}

daemonSet.Generation = 1
}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (daemonSetStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (daemonSetStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newDaemonSet := obj.(*extensions.DaemonSet)
oldDaemonSet := old.(*extensions.DaemonSet)

Expand Down Expand Up @@ -132,7 +132,7 @@ type daemonSetStatusStrategy struct {

var StatusStrategy = daemonSetStatusStrategy{Strategy}

func (daemonSetStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (daemonSetStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newDaemonSet := obj.(*extensions.DaemonSet)
oldDaemonSet := old.(*extensions.DaemonSet)
newDaemonSet.Spec = oldDaemonSet.Spec
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/deployment/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (deploymentStrategy) NamespaceScoped() bool {
}

// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
func (deploymentStrategy) PrepareForCreate(obj runtime.Object) {
func (deploymentStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
deployment := obj.(*extensions.Deployment)
deployment.Status = extensions.DeploymentStatus{}
deployment.Generation = 1
Expand All @@ -68,7 +68,7 @@ func (deploymentStrategy) AllowCreateOnUpdate() bool {
}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (deploymentStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (deploymentStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newDeployment := obj.(*extensions.Deployment)
oldDeployment := old.(*extensions.Deployment)
newDeployment.Status = oldDeployment.Status
Expand Down Expand Up @@ -98,7 +98,7 @@ type deploymentStatusStrategy struct {
var StatusStrategy = deploymentStatusStrategy{Strategy}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
func (deploymentStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (deploymentStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
newDeployment := obj.(*extensions.Deployment)
oldDeployment := old.(*extensions.Deployment)
newDeployment.Spec = oldDeployment.Spec
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/deployment/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestStatusUpdates(t *testing.T) {
}

for _, test := range tests {
deploymentStatusStrategy{}.PrepareForUpdate(test.obj, test.old)
deploymentStatusStrategy{}.PrepareForUpdate(api.NewContext(), test.obj, test.old)
if !reflect.DeepEqual(test.expected, test.obj) {
t.Errorf("Unexpected object mismatch! Expected:\n%#v\ngot:\n%#v", test.expected, test.obj)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/endpoint/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func (endpointsStrategy) NamespaceScoped() bool {
}

// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
func (endpointsStrategy) PrepareForCreate(obj runtime.Object) {
func (endpointsStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (endpointsStrategy) PrepareForUpdate(obj, old runtime.Object) {
func (endpointsStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
}

// Validate validates a new endpoints.
Expand Down
Loading

0 comments on commit 4db0049

Please sign in to comment.