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 1c778d4b3..25b3ec4d4 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", "", "tag/semver constraint when tag is not present in URL (If both tags and semver are present, then tag gets 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)") @@ -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", "", "", "tag/semver constraint when tag is not present in URL (If both tags and semver are present, then tag gets precedence)") 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 e8f53777b..e187d6d81 100644 --- a/cli/test/e2e/package_repo_dry_run_test.go +++ b/cli/test/e2e/package_repo_dry_run_test.go @@ -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 @@ -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) }) } diff --git a/cli/test/e2e/package_repository_test.go b/cli/test/e2e/package_repository_test.go index 40acce43f..b4b459a5b 100644 --- a/cli/test/e2e/package_repository_test.go +++ b/cli/test/e2e/package_repository_test.go @@ -19,7 +19,7 @@ func TestPackageRepository(t *testing.T) { kubectl := Kubectl{t, env.Namespace, logger} pkgrName := "test-package-repository" - pkgrURL := `index.docker.io/k8slt/kc-e2e-test-repo:latest` + pkgrURL := `ghcr.io/carvel-dev/kc-e2e-test-repo:latest` newRepoNamespace := "carvel-test-repo-a" @@ -206,3 +206,53 @@ func TestPackageRepository(t *testing.T) { }) } + +func TestPackageRepositoryTagSemver(t *testing.T) { + env := BuildEnv(t) + logger := Logger{} + kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, logger} + kubectl := Kubectl{t, env.Namespace, logger} + + pkgrName := "test-package-repository" + pkgrURL := `ghcr.io/carvel-dev/kc-e2e-test-repo` + + kind := "PackageRepository" + + cleanUp := func() { + RemoveClusterResource(t, kind, pkgrName, env.Namespace, kubectl) + } + + cleanUp() + defer cleanUp() + + logger.Section("adding a repository", func() { + kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL + ":v1.0.0"}) + + out := kubectl.Run([]string{"get", kind, pkgrName, "-oyaml"}) + require.Contains(t, out, "tag: v1.0.0") + kubectl.Run([]string{"get", "pkgm/pkg.test.carvel.dev"}) + kubectl.Run([]string{"get", "pkg/pkg.test.carvel.dev.1.0.0"}) + kubectl.Run([]string{"get", "pkg/pkg.test.carvel.dev.2.0.0"}) + }) + + logger.Section("deleting a repository", func() { + kappCtrl.Run([]string{"package", "repository", "delete", "-r", pkgrName}) + }) + + logger.Section("adding a repository", func() { + kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL + ":v2.0.0", "--semver-tag-constraints", "1.0.0"}) + out := kubectl.Run([]string{"get", kind, pkgrName, "-oyaml"}) + require.Contains(t, out, "tag: v2.0.0") + }) + + logger.Section("updating a repository", func() { + kappCtrl.Run([]string{"package", "repository", "update", "-r", pkgrName, "--url", pkgrURL, "--semver-tag-constraints", ">1.0.0"}) + + out := kubectl.Run([]string{"get", kind, pkgrName, "-oyaml"}) + require.Contains(t, out, "tag: v3.0.0") + kubectl.Run([]string{"get", "pkgm/pkg.test.carvel.dev"}) + kubectl.Run([]string{"get", "pkg/pkg.test.carvel.dev.1.0.0"}) + kubectl.Run([]string{"get", "pkg/pkg.test.carvel.dev.2.0.0"}) + }) + +}