From 2ea4a9f1f50c50ed9f8d1bbb2c4f52c63343a60a Mon Sep 17 00:00:00 2001 From: Yash Sethiya Date: Thu, 1 Feb 2024 14:03:47 +0530 Subject: [PATCH 1/4] 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 | 46 +++++++++++++++++-- 2 files changed, 52 insertions(+), 5 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 1c778d4b3..11934a13b 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 (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", "", "", "Mention 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) }) } From c91aaa1e23b1560a0c9d7c50e4948959e9180087 Mon Sep 17 00:00:00 2001 From: Yash Sethiya Date: Fri, 9 Feb 2024 16:49:29 +0530 Subject: [PATCH 2/4] Added additional e2e testcases Signed-off-by: Yash Sethiya --- cli/test/e2e/package_repository_test.go | 49 ++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/cli/test/e2e/package_repository_test.go b/cli/test/e2e/package_repository_test.go index 0bc735692..3cd5dc377 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" @@ -193,3 +193,50 @@ 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"}) + + kubectl.Run([]string{"get", kind, pkgrName}) + 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("adding a repository", func() { + kappCtrl.Run([]string{"package", "repository", "delete", "-r", pkgrName}) + }) + + logger.Section("adding a repository", func() { + out := kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL + ":v2.0.0", "--semver-tag-constraints", "1.0.0"}) + t.Logf(out) + }) + + logger.Section("adding a repository", func() { + kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL, "--semver-tag-constraints", ">1.0.0"}) + + kubectl.Run([]string{"get", kind, pkgrName}) + 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"}) + }) + +} From c5beb489b8b238fb564ef46cfa5f6c79e2244455 Mon Sep 17 00:00:00 2001 From: Yash Sethiya Date: Mon, 12 Feb 2024 13:49:18 +0530 Subject: [PATCH 3/4] Added ghcr registry auth in kctrl test gh action Signed-off-by: Yash Sethiya --- .github/workflows/test-kctrl-gh.yml | 6 ++++++ cli/test/e2e/package_repository_test.go | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test-kctrl-gh.yml b/.github/workflows/test-kctrl-gh.yml index e2be03c79..a22734e49 100644 --- a/.github/workflows/test-kctrl-gh.yml +++ b/.github/workflows/test-kctrl-gh.yml @@ -34,6 +34,12 @@ jobs: with: only: ytt, kbld, imgpkg, vendir, kapp token: ${{ secrets.GITHUB_TOKEN }} + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - name: Run Tests run: | set -e -x diff --git a/cli/test/e2e/package_repository_test.go b/cli/test/e2e/package_repository_test.go index 3cd5dc377..504513502 100644 --- a/cli/test/e2e/package_repository_test.go +++ b/cli/test/e2e/package_repository_test.go @@ -4,11 +4,8 @@ package e2e import ( - "fmt" "testing" - "time" - uitest "github.com/cppforlife/go-cli-ui/ui/test" "github.com/stretchr/testify/require" ) @@ -210,30 +207,33 @@ func TestPackageRepositoryTagSemver(t *testing.T) { } cleanUp() - defer cleanUp() + //defer cleanUp() logger.Section("adding a repository", func() { kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL + ":v1.0.0"}) - kubectl.Run([]string{"get", kind, pkgrName}) + 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("adding a repository", func() { + logger.Section("deleting a repository", func() { kappCtrl.Run([]string{"package", "repository", "delete", "-r", pkgrName}) }) logger.Section("adding a repository", func() { - out := kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL + ":v2.0.0", "--semver-tag-constraints", "1.0.0"}) - t.Logf(out) + 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("adding a repository", func() { - kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL, "--semver-tag-constraints", ">1.0.0"}) + logger.Section("updating a repository", func() { + kappCtrl.Run([]string{"package", "repository", "update", "-r", pkgrName, "--url", pkgrURL, "--semver-tag-constraints", ">1.0.0"}) - kubectl.Run([]string{"get", kind, pkgrName}) + 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"}) From b6d8c0a288f05e9b9c6830f26e2be193bc268fc8 Mon Sep 17 00:00:00 2001 From: Yash Sethiya Date: Thu, 15 Feb 2024 15:59:00 +0530 Subject: [PATCH 4/4] updated command description Signed-off-by: Yash Sethiya --- .github/workflows/test-kctrl-gh.yml | 6 ------ cli/pkg/kctrl/cmd/package/repository/add_or_update.go | 4 ++-- cli/test/e2e/package_repository_test.go | 5 ++++- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test-kctrl-gh.yml b/.github/workflows/test-kctrl-gh.yml index a22734e49..e2be03c79 100644 --- a/.github/workflows/test-kctrl-gh.yml +++ b/.github/workflows/test-kctrl-gh.yml @@ -34,12 +34,6 @@ jobs: with: only: ytt, kbld, imgpkg, vendir, kapp token: ${{ secrets.GITHUB_TOKEN }} - - name: Login to GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - name: Run Tests run: | set -e -x 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 11934a13b..25b3ec4d4 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -77,7 +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 (If both tags and semver are present, then tag gets precedence)") + 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)") @@ -118,7 +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 (If both tags and semver are present, then tag gets precedence)") + 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, diff --git a/cli/test/e2e/package_repository_test.go b/cli/test/e2e/package_repository_test.go index 504513502..8b7316fd7 100644 --- a/cli/test/e2e/package_repository_test.go +++ b/cli/test/e2e/package_repository_test.go @@ -4,8 +4,11 @@ package e2e import ( + "fmt" "testing" + "time" + uitest "github.com/cppforlife/go-cli-ui/ui/test" "github.com/stretchr/testify/require" ) @@ -207,7 +210,7 @@ func TestPackageRepositoryTagSemver(t *testing.T) { } cleanUp() - //defer cleanUp() + defer cleanUp() logger.Section("adding a repository", func() { kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL + ":v1.0.0"})