-
Notifications
You must be signed in to change notification settings - Fork 36
Refactor PackageRevision controller code for introduction of subpackage clone and upgrade #1139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liamfallon
wants to merge
4
commits into
kptdev:main
Choose a base branch
from
Nordix:refactor-crd-for-subpkg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
controllers/packagerevisions/pkg/controllers/packagerevision/clone.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright 2026 The kpt Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package packagerevision | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "path" | ||
|
|
||
| kptfilev1 "github.com/kptdev/kpt/api/kptfile/v1" | ||
| "github.com/kptdev/kpt/pkg/lib/kptops" | ||
| porchv1alpha2 "github.com/kptdev/porch/api/porch/v1alpha2" | ||
| "github.com/kptdev/porch/pkg/repository" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| // clonePackage reads the source package referenced by CloneFrom and returns its resources | ||
| // with Kptfile upstream/upstreamLock updated. | ||
| // Currently only supports upstreamRef (registered repo). Raw git URL is not yet implemented. | ||
| func (r *PackageRevisionReconciler) clonePackage(ctx context.Context, pr *porchv1alpha2.PackageRevision) (map[string]string, error) { | ||
| cloneFrom := r.getCloneFrom(pr) | ||
|
|
||
| if cloneFrom.UpstreamRef != nil { | ||
| return r.cloneFromUpstreamRef(ctx, pr, cloneFrom.UpstreamRef) | ||
| } | ||
| if cloneFrom.Git != nil { | ||
| return r.cloneFromGit(ctx, pr, cloneFrom.Git) | ||
| } | ||
| return nil, fmt.Errorf("clone source must specify either upstreamRef or git") | ||
| } | ||
|
|
||
| func (r *PackageRevisionReconciler) cloneFromUpstreamRef(ctx context.Context, pr *porchv1alpha2.PackageRevision, ref *porchv1alpha2.PackageRevisionRef) (map[string]string, error) { | ||
| log := log.FromContext(ctx) | ||
| var sourcePR porchv1alpha2.PackageRevision | ||
| if err := r.Get(ctx, client.ObjectKey{Namespace: pr.Namespace, Name: ref.Name}, &sourcePR); err != nil { | ||
| return nil, fmt.Errorf("failed to get upstream package %q: %w", ref.Name, err) | ||
| } | ||
|
|
||
| if !porchv1alpha2.LifecycleIsPublished(sourcePR.Spec.Lifecycle) { | ||
| return nil, fmt.Errorf("upstream package %q must be published", ref.Name) | ||
| } | ||
|
|
||
| log.V(1).Info("cloning from upstream ref", "upstream", ref.Name) | ||
|
|
||
| repoKey := repository.RepositoryKey{Namespace: pr.Namespace, Name: sourcePR.Spec.RepositoryName} | ||
| content, err := r.ContentCache.GetPackageContent(ctx, repoKey, sourcePR.Spec.PackageName, sourcePR.Spec.WorkspaceName) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get upstream package content: %w", err) | ||
| } | ||
|
|
||
| resources, err := content.GetResourceContents(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read upstream resources: %w", err) | ||
| } | ||
|
|
||
| upstream, lock, err := content.GetLock(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get upstream lock for %q: %w", ref.Name, err) | ||
| } | ||
|
|
||
| if err := kptops.UpdateKptfileUpstream(r.getClonePackagename(pr), resources, upstream, lock); err != nil { | ||
| return nil, fmt.Errorf("failed to update Kptfile upstream: %w", err) | ||
| } | ||
|
|
||
| return resources, nil | ||
| } | ||
|
|
||
| func (r *PackageRevisionReconciler) cloneFromGit(ctx context.Context, pr *porchv1alpha2.PackageRevision, gitSpec *porchv1alpha2.GitPackage) (map[string]string, error) { | ||
| log.FromContext(ctx).V(1).Info("cloning from git", "repo", gitSpec.Repo, "ref", gitSpec.Ref, "directory", gitSpec.Directory) | ||
| resources, lock, err := r.ExternalPackageFetcher.FetchExternalGitPackage(ctx, gitSpec, pr.Namespace) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to fetch from git: %w", err) | ||
| } | ||
|
|
||
| if err := kptops.UpdateKptfileUpstream(r.getClonePackagename(pr), resources, kptfilev1.Upstream{ | ||
| Type: kptfilev1.GitOrigin, | ||
| Git: &kptfilev1.Git{ | ||
| Repo: lock.Repo, | ||
| Directory: lock.Directory, | ||
| Ref: lock.Ref, | ||
| }, | ||
| }, kptfilev1.Locator{ | ||
| Type: kptfilev1.GitOrigin, | ||
| Git: &lock, | ||
| }); err != nil { | ||
| return nil, fmt.Errorf("failed to update Kptfile upstream: %w", err) | ||
| } | ||
|
|
||
| return resources, nil | ||
| } | ||
|
|
||
| // getCloneFrom returns the upstream package for a clone in the case of a source clone or a subpackage | ||
| // operation clone | ||
| func (r *PackageRevisionReconciler) getCloneFrom(pr *porchv1alpha2.PackageRevision) *porchv1alpha2.UpstreamPackage { | ||
| if pr.Status.CreationSource != "" && pr.Spec.SubpackageOperation != nil && pr.Spec.SubpackageOperation.CloneFrom != nil { | ||
| return pr.Spec.SubpackageOperation.CloneFrom | ||
| } | ||
| return pr.Spec.Source.CloneFrom | ||
| } | ||
|
|
||
| // getClonePackagename returns the package name of a clone in the case of a source clone or a subpackage | ||
| // operation clone | ||
| func (r *PackageRevisionReconciler) getClonePackagename(pr *porchv1alpha2.PackageRevision) string { | ||
| if pr.Status.CreationSource != "" && pr.Spec.SubpackageOperation != nil && pr.Spec.SubpackageOperation.CloneFrom != nil { | ||
| return path.Base(pr.Spec.SubpackageOperation.SubpackageDir) | ||
| } | ||
| return pr.Spec.PackageName | ||
| } | ||
120 changes: 120 additions & 0 deletions
120
controllers/packagerevisions/pkg/controllers/packagerevision/clone_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package packagerevision | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| porchv1alpha2 "github.com/kptdev/porch/api/porch/v1alpha2" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetCloneFromSourceClone(t *testing.T) { | ||
| r := &PackageRevisionReconciler{} | ||
| pr := &porchv1alpha2.PackageRevision{ | ||
| Spec: porchv1alpha2.PackageRevisionSpec{ | ||
| Source: &porchv1alpha2.PackageSource{ | ||
| CloneFrom: &porchv1alpha2.UpstreamPackage{ | ||
| UpstreamRef: &porchv1alpha2.PackageRevisionRef{Name: "source.pkg.v1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| result := r.getCloneFrom(pr) | ||
| assert.Equal(t, "source.pkg.v1", result.UpstreamRef.Name) | ||
| } | ||
|
|
||
| func TestGetCloneFromSubpackageClone(t *testing.T) { | ||
| r := &PackageRevisionReconciler{} | ||
| pr := &porchv1alpha2.PackageRevision{ | ||
| Spec: porchv1alpha2.PackageRevisionSpec{ | ||
| Source: &porchv1alpha2.PackageSource{ | ||
| CloneFrom: &porchv1alpha2.UpstreamPackage{ | ||
| UpstreamRef: &porchv1alpha2.PackageRevisionRef{Name: "source.pkg.v1"}, | ||
| }, | ||
| }, | ||
| SubpackageOperation: &porchv1alpha2.SubpackageOperation{ | ||
| SubpackageDir: "my-subpkg", | ||
| CloneFrom: &porchv1alpha2.UpstreamPackage{ | ||
| UpstreamRef: &porchv1alpha2.PackageRevisionRef{Name: "subpkg-upstream.pkg.v1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| Status: porchv1alpha2.PackageRevisionStatus{ | ||
| CreationSource: "init", | ||
| }, | ||
| } | ||
|
|
||
| result := r.getCloneFrom(pr) | ||
| assert.Equal(t, "subpkg-upstream.pkg.v1", result.UpstreamRef.Name) | ||
| } | ||
|
|
||
| func TestGetCloneFromSubpackageCloneWithoutCreationSource(t *testing.T) { | ||
| // Without CreationSource set, getCloneFrom should fall back to Source.CloneFrom | ||
| r := &PackageRevisionReconciler{} | ||
| pr := &porchv1alpha2.PackageRevision{ | ||
| Spec: porchv1alpha2.PackageRevisionSpec{ | ||
| Source: &porchv1alpha2.PackageSource{ | ||
| CloneFrom: &porchv1alpha2.UpstreamPackage{ | ||
| UpstreamRef: &porchv1alpha2.PackageRevisionRef{Name: "source.pkg.v1"}, | ||
| }, | ||
| }, | ||
| SubpackageOperation: &porchv1alpha2.SubpackageOperation{ | ||
| SubpackageDir: "my-subpkg", | ||
| CloneFrom: &porchv1alpha2.UpstreamPackage{ | ||
| UpstreamRef: &porchv1alpha2.PackageRevisionRef{Name: "subpkg-upstream.pkg.v1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| result := r.getCloneFrom(pr) | ||
| assert.Equal(t, "source.pkg.v1", result.UpstreamRef.Name) | ||
| } | ||
|
liamfallon marked this conversation as resolved.
|
||
|
|
||
| func TestGetClonePackagenameSource(t *testing.T) { | ||
| r := &PackageRevisionReconciler{} | ||
| pr := &porchv1alpha2.PackageRevision{ | ||
| Spec: porchv1alpha2.PackageRevisionSpec{ | ||
| PackageName: "my-pkg", | ||
| }, | ||
| } | ||
|
|
||
| assert.Equal(t, "my-pkg", r.getClonePackagename(pr)) | ||
| } | ||
|
|
||
| func TestGetClonePackagenameSubpackage(t *testing.T) { | ||
| r := &PackageRevisionReconciler{} | ||
| pr := &porchv1alpha2.PackageRevision{ | ||
| Spec: porchv1alpha2.PackageRevisionSpec{ | ||
| PackageName: "my-pkg", | ||
| SubpackageOperation: &porchv1alpha2.SubpackageOperation{ | ||
| SubpackageDir: "level1/level2/my-subpkg", | ||
| CloneFrom: &porchv1alpha2.UpstreamPackage{ | ||
| UpstreamRef: &porchv1alpha2.PackageRevisionRef{Name: "upstream.pkg.v1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| Status: porchv1alpha2.PackageRevisionStatus{ | ||
| CreationSource: "init", | ||
| }, | ||
| } | ||
|
|
||
| assert.Equal(t, "my-subpkg", r.getClonePackagename(pr)) | ||
| } | ||
|
|
||
| func TestGetClonePackagenameSubpackageWithoutCreationSource(t *testing.T) { | ||
| r := &PackageRevisionReconciler{} | ||
| pr := &porchv1alpha2.PackageRevision{ | ||
| Spec: porchv1alpha2.PackageRevisionSpec{ | ||
| PackageName: "my-pkg", | ||
| SubpackageOperation: &porchv1alpha2.SubpackageOperation{ | ||
| SubpackageDir: "level1/my-subpkg", | ||
| CloneFrom: &porchv1alpha2.UpstreamPackage{ | ||
| UpstreamRef: &porchv1alpha2.PackageRevisionRef{Name: "upstream.pkg.v1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| assert.Equal(t, "my-pkg", r.getClonePackagename(pr)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.