Skip to content

Commit

Permalink
Add CreateStrategy to validate object changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed May 6, 2022
1 parent e0be85f commit 2ee5ca0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
10 changes: 5 additions & 5 deletions porch/pkg/registry/porch/packagecommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type packageCommon struct {
coreClient client.Client
gr schema.GroupResource
updateStrategy SimpleRESTUpdateStrategy
createStrategy SimpleRESTCreateStrategy
}

func (r *packageCommon) listPackages(ctx context.Context, filter packageFilter, callback func(p repository.PackageRevision) error) error {
Expand Down Expand Up @@ -204,11 +205,10 @@ func (r *packageCommon) updatePackageRevision(ctx context.Context, name string,
}
}

// TODO: ValidateCreate function ?
// fieldErrors := r.updateStrategy.ValidateCreate(ctx, newRuntimeObj, oldRuntimeObj)
// if len(fieldErrors) > 0 {
// return nil, false, apierrors.NewInvalid(api.SchemeGroupVersion.WithKind("PackageRevision").GroupKind(), name, fieldErrors)
// }
fieldErrors := r.createStrategy.Validate(ctx, newRuntimeObj)
if len(fieldErrors) > 0 {
return nil, false, apierrors.NewInvalid(api.SchemeGroupVersion.WithKind("PackageRevision").GroupKind(), name, fieldErrors)
}
}

r.updateStrategy.Canonicalize(newRuntimeObj)
Expand Down
31 changes: 31 additions & 0 deletions porch/pkg/registry/porch/packagerevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func (r *packageRevisions) Create(ctx context.Context, runtimeObject runtime.Obj
return nil, apierrors.NewInternalError(fmt.Errorf("error getting repository %v: %w", repositoryID, err))
}

fieldErrors := r.createStrategy.Validate(ctx, runtimeObject)
if len(fieldErrors) > 0 {
return nil, apierrors.NewInvalid(api.SchemeGroupVersion.WithKind("PackageRevision").GroupKind(), obj.Name, fieldErrors)
}

rev, err := r.cad.CreatePackageRevision(ctx, &repositoryObj, obj)
if err != nil {
return nil, apierrors.NewInternalError(err)
Expand Down Expand Up @@ -265,3 +270,29 @@ func (s packageRevisionStrategy) Canonicalize(obj runtime.Object) {
pr.Spec.Lifecycle = api.PackageRevisionLifecycleDraft
}
}

var _ SimpleRESTCreateStrategy = packageRevisionStrategy{}

// Validate returns an ErrorList with validation errors or nil. 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.
func (s packageRevisionStrategy) Validate(ctx context.Context, runtimeObj runtime.Object) field.ErrorList {
allErrs := field.ErrorList{}

obj := runtimeObj.(*api.PackageRevision)

switch lifecycle := obj.Spec.Lifecycle; lifecycle {
case "", api.PackageRevisionLifecycleDraft:
// valid

default:
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "lifecycle"), lifecycle, fmt.Sprintf("value can be only created as %s",
strings.Join([]string{
string(api.PackageRevisionLifecycleDraft),
}, ",")),
))
}

return allErrs
}
14 changes: 14 additions & 0 deletions porch/pkg/registry/porch/packagerevisions_approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,17 @@ func (s packageRevisionApprovalStrategy) ValidateUpdate(ctx context.Context, obj
}

func (s packageRevisionApprovalStrategy) Canonicalize(obj runtime.Object) {}

var _ SimpleRESTCreateStrategy = packageRevisionApprovalStrategy{}

// Validate returns an ErrorList with validation errors or nil. 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.
func (s packageRevisionApprovalStrategy) Validate(ctx context.Context, runtimeObj runtime.Object) field.ErrorList {
allErrs := field.ErrorList{}

// obj := runtimeObj.(*api.PackageRevision)

return allErrs
}
10 changes: 10 additions & 0 deletions porch/pkg/registry/porch/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ type SimpleRESTUpdateStrategy interface {
Canonicalize(obj runtime.Object)
}

// SimpleRESTCreateStrategy is similar to rest.RESTCreateStrategy, though only contains
// methods currently required.
type SimpleRESTCreateStrategy interface {
// Validate returns an ErrorList with validation errors or nil. 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 context.Context, obj runtime.Object) field.ErrorList
}

type NoopUpdateStrategy struct{}

func (s NoopUpdateStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {}
Expand Down
2 changes: 2 additions & 0 deletions porch/pkg/registry/porch/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewRESTStorage(scheme *runtime.Scheme, codecs serializer.CodecFactory, cad
gr: porch.Resource("packagerevisions"),
coreClient: coreClient,
updateStrategy: packageRevisionStrategy{},
createStrategy: packageRevisionStrategy{},
},
}

Expand All @@ -46,6 +47,7 @@ func NewRESTStorage(scheme *runtime.Scheme, codecs serializer.CodecFactory, cad
coreClient: coreClient,
gr: porch.Resource("packagerevisions"),
updateStrategy: packageRevisionApprovalStrategy{},
createStrategy: packageRevisionApprovalStrategy{},
},
}

Expand Down

0 comments on commit 2ee5ca0

Please sign in to comment.