Skip to content

Commit 2ea4a9f

Browse files
Yash Sethiyasethiyash
authored andcommitted
Adding support for tag constraints when adding a package repo with kctrl
Signed-off-by: Yash Sethiya <[email protected]>
1 parent 855063e commit 2ea4a9f

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

cli/pkg/kctrl/cmd/package/repository/add_or_update.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type AddOrUpdateOptions struct {
3535
SecureNamespaceFlags cmdcore.SecureNamespaceFlags
3636
Name string
3737
URL string
38+
SemverTagConstraints string
3839
CreateNamespace bool
3940

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

7778
// TODO consider how to support other repository types
7879
cmd.Flags().StringVar(&o.URL, "url", "", "OCI registry url for package repository bundle (required)")
80+
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)")
7981
cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print YAML for resources being applied to the cluster without applying them, optional")
8082

8183
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
116118
}
117119

118120
cmd.Flags().StringVarP(&o.URL, "url", "", "", "OCI registry url for package repository bundle (required)")
121+
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)")
119122

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

269272
if tag == "" {
270273
pkgr.Spec.Fetch.ImgpkgBundle.TagSelection = &versions.VersionSelection{
271-
Semver: &versions.VersionSelectionSemver{},
274+
Semver: &versions.VersionSelectionSemver{
275+
Constraints: o.SemverTagConstraints,
276+
},
272277
}
273278
}
274279

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

326331
if tag == "" {
327332
packageRepo.Spec.Fetch.ImgpkgBundle.TagSelection = &versions.VersionSelection{
328-
Semver: &versions.VersionSelectionSemver{},
333+
Semver: &versions.VersionSelectionSemver{
334+
Constraints: o.SemverTagConstraints,
335+
},
329336
}
330337
}
331338

cli/test/e2e/package_repo_dry_run_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestPackageRepoDryRun(t *testing.T) {
1212
kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, logger}
1313

1414
logger.Section("dry-run package repo add", func() {
15-
expectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
15+
tagExpectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
1616
kind: PackageRepository
1717
metadata:
1818
creationTimestamp: null
@@ -26,8 +26,48 @@ status:
2626
conditions: null
2727
friendlyDescription: ""
2828
observedGeneration: 0`
29+
semverExpectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
30+
kind: PackageRepository
31+
metadata:
32+
creationTimestamp: null
33+
name: test-repo
34+
namespace: kctrl-test
35+
spec:
36+
fetch:
37+
imgpkgBundle:
38+
image: registry.carvel.dev/project/repo
39+
tagSelection:
40+
semver:
41+
constraints: 1.0.0
42+
status:
43+
conditions: null
44+
friendlyDescription: ""
45+
observedGeneration: 0
46+
`
47+
tagSemverExpectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
48+
kind: PackageRepository
49+
metadata:
50+
creationTimestamp: null
51+
name: test-repo
52+
namespace: kctrl-test
53+
spec:
54+
fetch:
55+
imgpkgBundle:
56+
image: registry.carvel.dev/project/repo:1.0.0
57+
status:
58+
conditions: null
59+
friendlyDescription: ""
60+
observedGeneration: 0
61+
`
2962

30-
output := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url", "registry.carvel.dev/project/repo:1.0.0", "--dry-run"})
31-
require.Contains(t, output, expectedOutput)
63+
tagOutput := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url",
64+
"registry.carvel.dev/project/repo:1.0.0", "--semver-tag-constraints", "1.0.0", "--dry-run"})
65+
semverOutput := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url",
66+
"registry.carvel.dev/project/repo", "--semver-tag-constraints", "1.0.0", "--dry-run"})
67+
tagSemverOutput := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url",
68+
"registry.carvel.dev/project/repo:1.0.0", "--semver-tag-constraints", "1.0.0", "--dry-run"})
69+
require.Contains(t, tagOutput, tagExpectedOutput)
70+
require.Contains(t, semverOutput, semverExpectedOutput)
71+
require.Contains(t, tagSemverOutput, tagSemverExpectedOutput)
3272
})
3373
}

0 commit comments

Comments
 (0)