Skip to content

Commit 1355dab

Browse files
authored
Add CreateStrategy to validate object changes. (#3090)
1 parent 39bbc89 commit 1355dab

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

porch/pkg/registry/porch/packagecommon.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type packageCommon struct {
4343
coreClient client.Client
4444
gr schema.GroupResource
4545
updateStrategy SimpleRESTUpdateStrategy
46+
createStrategy SimpleRESTCreateStrategy
4647
}
4748

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

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

214214
r.updateStrategy.Canonicalize(newRuntimeObj)

porch/pkg/registry/porch/packagerevision.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ func (r *packageRevisions) Create(ctx context.Context, runtimeObject runtime.Obj
134134
return nil, apierrors.NewInternalError(fmt.Errorf("error getting repository %v: %w", repositoryID, err))
135135
}
136136

137+
fieldErrors := r.createStrategy.Validate(ctx, runtimeObject)
138+
if len(fieldErrors) > 0 {
139+
return nil, apierrors.NewInvalid(api.SchemeGroupVersion.WithKind("PackageRevision").GroupKind(), obj.Name, fieldErrors)
140+
}
141+
137142
rev, err := r.cad.CreatePackageRevision(ctx, &repositoryObj, obj)
138143
if err != nil {
139144
return nil, apierrors.NewInternalError(err)
@@ -265,3 +270,29 @@ func (s packageRevisionStrategy) Canonicalize(obj runtime.Object) {
265270
pr.Spec.Lifecycle = api.PackageRevisionLifecycleDraft
266271
}
267272
}
273+
274+
var _ SimpleRESTCreateStrategy = packageRevisionStrategy{}
275+
276+
// Validate returns an ErrorList with validation errors or nil. Validate
277+
// is invoked after default fields in the object have been filled in
278+
// before the object is persisted. This method should not mutate the
279+
// object.
280+
func (s packageRevisionStrategy) Validate(ctx context.Context, runtimeObj runtime.Object) field.ErrorList {
281+
allErrs := field.ErrorList{}
282+
283+
obj := runtimeObj.(*api.PackageRevision)
284+
285+
switch lifecycle := obj.Spec.Lifecycle; lifecycle {
286+
case "", api.PackageRevisionLifecycleDraft:
287+
// valid
288+
289+
default:
290+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "lifecycle"), lifecycle, fmt.Sprintf("value can be only created as %s",
291+
strings.Join([]string{
292+
string(api.PackageRevisionLifecycleDraft),
293+
}, ",")),
294+
))
295+
}
296+
297+
return allErrs
298+
}

porch/pkg/registry/porch/packagerevisions_approval.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,17 @@ func (s packageRevisionApprovalStrategy) ValidateUpdate(ctx context.Context, obj
9191
}
9292

9393
func (s packageRevisionApprovalStrategy) Canonicalize(obj runtime.Object) {}
94+
95+
var _ SimpleRESTCreateStrategy = packageRevisionApprovalStrategy{}
96+
97+
// Validate returns an ErrorList with validation errors or nil. Validate
98+
// is invoked after default fields in the object have been filled in
99+
// before the object is persisted. This method should not mutate the
100+
// object.
101+
func (s packageRevisionApprovalStrategy) Validate(ctx context.Context, runtimeObj runtime.Object) field.ErrorList {
102+
allErrs := field.ErrorList{}
103+
104+
// obj := runtimeObj.(*api.PackageRevision)
105+
106+
return allErrs
107+
}

porch/pkg/registry/porch/rest.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ type SimpleRESTUpdateStrategy interface {
2929
Canonicalize(obj runtime.Object)
3030
}
3131

32+
// SimpleRESTCreateStrategy is similar to rest.RESTCreateStrategy, though only contains
33+
// methods currently required.
34+
type SimpleRESTCreateStrategy interface {
35+
// Validate returns an ErrorList with validation errors or nil. Validate
36+
// is invoked after default fields in the object have been filled in
37+
// before the object is persisted. This method should not mutate the
38+
// object.
39+
Validate(ctx context.Context, obj runtime.Object) field.ErrorList
40+
}
41+
3242
type NoopUpdateStrategy struct{}
3343

3444
func (s NoopUpdateStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {}

porch/pkg/registry/porch/storage.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func NewRESTStorage(scheme *runtime.Scheme, codecs serializer.CodecFactory, cad
3636
gr: porch.Resource("packagerevisions"),
3737
coreClient: coreClient,
3838
updateStrategy: packageRevisionStrategy{},
39+
createStrategy: packageRevisionStrategy{},
3940
},
4041
}
4142

@@ -46,6 +47,7 @@ func NewRESTStorage(scheme *runtime.Scheme, codecs serializer.CodecFactory, cad
4647
coreClient: coreClient,
4748
gr: porch.Resource("packagerevisions"),
4849
updateStrategy: packageRevisionApprovalStrategy{},
50+
createStrategy: packageRevisionApprovalStrategy{},
4951
},
5052
}
5153

0 commit comments

Comments
 (0)