Skip to content

Commit

Permalink
Adding support for tag constraints when adding a package repo with kctrl
Browse files Browse the repository at this point in the history
Signed-off-by: Yash Sethiya <[email protected]>
  • Loading branch information
Yash Sethiya authored and sethiyash committed Feb 6, 2024
1 parent 855063e commit 3747ae7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
11 changes: 9 additions & 2 deletions cli/pkg/kctrl/cmd/package/repository/add_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type AddOrUpdateOptions struct {
SecureNamespaceFlags cmdcore.SecureNamespaceFlags
Name string
URL string
SemverTagConstraints string
CreateNamespace bool

DryRun bool
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.

// TODO consider how to support other repository types
cmd.Flags().StringVar(&o.URL, "url", "", "OCI registry url for package repository bundle (required)")
cmd.Flags().StringVar(&o.SemverTagConstraints, "semver-tag-constraints", "", "Mention tag/semver constraint when tag is not present in URL separated by ':' (tag always get precedence)")
cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print YAML for resources being applied to the cluster without applying them, optional")

cmd.Flags().BoolVar(&o.CreateNamespace, "create-namespace", false, "Create the package repository namespace if not present (default false)")
Expand Down Expand Up @@ -116,6 +118,7 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob
}

cmd.Flags().StringVarP(&o.URL, "url", "", "", "OCI registry url for package repository bundle (required)")
cmd.Flags().StringVarP(&o.SemverTagConstraints, "semver-tag-constraints", "", "", "Mention tag/semver constraint when tag is not present in URL separated by ':' (tag always get precedence)")

o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{
AllowDisableWait: true,
Expand Down Expand Up @@ -268,7 +271,9 @@ func (o *AddOrUpdateOptions) updateExistingPackageRepository(pkgr *kcpkg.Package

if tag == "" {
pkgr.Spec.Fetch.ImgpkgBundle.TagSelection = &versions.VersionSelection{
Semver: &versions.VersionSelectionSemver{},
Semver: &versions.VersionSelectionSemver{
Constraints: o.SemverTagConstraints,
},
}
}

Expand Down Expand Up @@ -325,7 +330,9 @@ func (o AddOrUpdateOptions) dryRun() error {

if tag == "" {
packageRepo.Spec.Fetch.ImgpkgBundle.TagSelection = &versions.VersionSelection{
Semver: &versions.VersionSelectionSemver{},
Semver: &versions.VersionSelectionSemver{
Constraints: o.SemverTagConstraints,
},
}
}

Expand Down
46 changes: 43 additions & 3 deletions cli/test/e2e/package_repo_dry_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestPackageRepoDryRun(t *testing.T) {
kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, logger}

logger.Section("dry-run package repo add", func() {
expectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
tagExpectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
kind: PackageRepository
metadata:
creationTimestamp: null
Expand All @@ -26,8 +26,48 @@ status:
conditions: null
friendlyDescription: ""
observedGeneration: 0`
semverExpectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
kind: PackageRepository
metadata:
creationTimestamp: null
name: test-repo
namespace: kctrl-test
spec:
fetch:
imgpkgBundle:
image: registry.carvel.dev/project/repo
tagSelection:
semver:
constraints: 1.0.0
status:
conditions: null
friendlyDescription: ""
observedGeneration: 0
`
tagSemverExpectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
kind: PackageRepository
metadata:
creationTimestamp: null
name: test-repo
namespace: kctrl-test
spec:
fetch:
imgpkgBundle:
image: registry.carvel.dev/project/repo:1.0.0
status:
conditions: null
friendlyDescription: ""
observedGeneration: 0
`

output := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url", "registry.carvel.dev/project/repo:1.0.0", "--dry-run"})
require.Contains(t, output, expectedOutput)
tagOutput := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url",
"registry.carvel.dev/project/repo:1.0.0", "--semver-tag-constraints", "1.0.0", "--dry-run"})
semverOutput := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url",
"registry.carvel.dev/project/repo", "--semver-tag-constraints", "1.0.0", "--dry-run"})
tagSemverOutput := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url",
"registry.carvel.dev/project/repo:1.0.0", "--semver-tag-constraints", "1.0.0", "--dry-run"})
require.Contains(t, tagOutput, tagExpectedOutput)
require.Contains(t, semverOutput, semverExpectedOutput)
require.Contains(t, tagSemverOutput, tagSemverExpectedOutput)
})
}

0 comments on commit 3747ae7

Please sign in to comment.