Skip to content
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

Adding support for tag constraints when adding a package repo with kctrl #1471

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", "", "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)")
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", "", "", "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,
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{
sethiyash marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}
52 changes: 51 additions & 1 deletion cli/test/e2e/package_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -193,3 +193,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"})
})

}
Loading