From aadba405193d488d64efea1cb6fd30c48b343252 Mon Sep 17 00:00:00 2001 From: Yash Sethiya Date: Thu, 1 Feb 2024 14:03:47 +0530 Subject: [PATCH] Adding support for tag constraints when adding a package repo with kctrl Signed-off-by: Yash Sethiya --- .../cmd/package/repository/add_or_update.go | 11 +++++++-- cli/test/e2e/package_repo_dry_run_test.go | 24 ++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index 1c778d4b3a..a7c92166a2 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -35,6 +35,7 @@ type AddOrUpdateOptions struct { SecureNamespaceFlags cmdcore.SecureNamespaceFlags Name string URL string + SemverTagConstraints string CreateNamespace bool DryRun bool @@ -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 ':'") 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)") @@ -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 ':'") o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ AllowDisableWait: true, @@ -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, + }, } } @@ -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, + }, } } diff --git a/cli/test/e2e/package_repo_dry_run_test.go b/cli/test/e2e/package_repo_dry_run_test.go index e8f53777b1..757b28ce98 100644 --- a/cli/test/e2e/package_repo_dry_run_test.go +++ b/cli/test/e2e/package_repo_dry_run_test.go @@ -26,8 +26,30 @@ 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 +` - output := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url", "registry.carvel.dev/project/repo:1.0.0", "--dry-run"}) + output := 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"}) require.Contains(t, output, expectedOutput) + require.Contains(t, semverOutput, semverExpectedOutput) }) }