Skip to content

Commit

Permalink
ensure invariants hold on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Oct 14, 2022
1 parent 51c11c1 commit 1bff276
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion porch/api/porch/types_packagerevisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ type SecretRef struct {
Name string `json:"name"`
}

// OciPackage describes a repository compatible with the Open Coutainer Registry standard.
// OciPackage describes a repository compatible with the Open Container Registry standard.
type OciPackage struct {
// Image is the address of an OCI image.
Image string `json:"image"`
Expand Down
56 changes: 56 additions & 0 deletions porch/pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ func (cad *cadEngine) CreatePackageRevision(ctx context.Context, repositoryObj *
if err != nil {
return nil, err
}

sameOrigin, err := cad.ensureSameOrigin(ctx, obj, repo)
if err != nil {
return nil, fmt.Errorf("error ensuring same origin: %w", err)
}

if !sameOrigin {
return nil, fmt.Errorf("cannot create revision of %s with a different origin than other package revisions in the same object", obj.Spec.PackageName)
}

draft, err := repo.CreatePackageRevision(ctx, obj)
if err != nil {
return nil, err
Expand Down Expand Up @@ -242,6 +252,52 @@ func (cad *cadEngine) CreatePackageRevision(ctx context.Context, repositoryObj *
}, nil
}

func (cad *cadEngine) ensureSameOrigin(ctx context.Context, obj *api.PackageRevision, r repository.Repository) (bool, error) {
revs, err := r.ListPackageRevisions(ctx, repository.ListPackageRevisionFilter{
Package: obj.Spec.PackageName})
if err != nil {
return false, fmt.Errorf("error listing package revisions: %w", err)
}
if len(revs) == 0 {
// no prior package revisions, no need to check anything else
return true, nil
}

tasks := obj.Spec.Tasks
if len(tasks) == 0 || (tasks[0].Type != api.TaskTypeInit && tasks[0].Type != api.TaskTypeClone) {
// If there are no tasks, or the first task is not init or clone, then this revision was not
// created from another package revision. That means we expect it to be the first revision
// for this package.
return false, nil

}

firstObjTask := tasks[0]
// iterate over existing package revisions, and look for one with a matching init or clone task
for _, rev := range revs {
p := rev.GetPackageRevision()
revTasks := p.Spec.Tasks
if len(revTasks) == 0 {
// not a match
continue
}
firstRevTask := revTasks[0]
if firstRevTask.Type != firstObjTask.Type {
// not a match
continue
}
if firstObjTask.Type == api.TaskTypeClone {
// we want to make sure everything is equal except for the git upstream ref,
// so we make the git upstream refs equal before calling reflect.DeepEqual
firstRevTask.Clone.Upstream.Git.Ref = firstObjTask.Clone.Upstream.Git.Ref
if reflect.DeepEqual(firstRevTask, firstObjTask) {
return true, nil
}
}
}
return false, nil
}

func (cad *cadEngine) applyTasks(ctx context.Context, draft repository.PackageDraft, repositoryObj *configapi.Repository, obj *api.PackageRevision) error {
var mutations []mutation

Expand Down
1 change: 1 addition & 0 deletions porch/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,5 +1053,6 @@ func reverseSlice(s []v1alpha1.Task) {
s[first], s[last] = s[last], s[first]
first++
last--

}
}

0 comments on commit 1bff276

Please sign in to comment.