From 19f6cf4981758c5540ba42fb33be1e8ad96d1945 Mon Sep 17 00:00:00 2001 From: Devanshu Date: Thu, 22 Aug 2024 00:18:18 +0530 Subject: [PATCH 1/5] Add secret flag for PackageRepository CRD in cli Signed-off-by: Devanshu --- cli/pkg/kctrl/cmd/package/repository/add_or_update.go | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 8b15e36c9..6ade05100 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -37,6 +37,7 @@ type AddOrUpdateOptions struct { URL string SemverTagConstraints string CreateNamespace bool + Secret string DryRun bool @@ -78,6 +79,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().StringVar(&o.Secret, "secret", "", "SecretRef name for imgpkgbundle, optional") 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)") @@ -119,6 +121,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)") + cmd.Flags().StringVarP(&o.Secret, "secret", "", "", "SecretRef name for imgpkgbundle, optional") o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ AllowDisableWait: true, @@ -257,6 +260,10 @@ func (o *AddOrUpdateOptions) updateExistingPackageRepository(pkgr *kcpkg.Package ImgpkgBundle: &kappctrl.AppFetchImgpkgBundle{Image: o.URL}, } + if strings.TrimSpace(o.Secret) != "" { + pkgr.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.Secret} + } + ref, err := name.ParseReference(o.URL, name.WeakValidation) if err != nil { return pkgr, fmt.Errorf("Parsing OCI registry URL: %s", err) @@ -316,6 +323,10 @@ func (o AddOrUpdateOptions) dryRun() error { }, } + if strings.TrimSpace(o.Secret) != "" { + packageRepo.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.Secret} + } + ref, err := name.ParseReference(o.URL, name.WeakValidation) if err != nil { return fmt.Errorf("Parsing OCI registry URL: %s", err) From c291116f03b432d300de823ac0e58b256072abcd Mon Sep 17 00:00:00 2001 From: Devanshu Date: Fri, 23 Aug 2024 16:24:52 +0530 Subject: [PATCH 2/5] Removed whitespace check Signed-off-by: Devanshu --- cli/pkg/kctrl/cmd/package/repository/add_or_update.go | 4 ++-- 1 file changed, 2 insertions(+), 2 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 6ade05100..e42f9419f 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -260,7 +260,7 @@ func (o *AddOrUpdateOptions) updateExistingPackageRepository(pkgr *kcpkg.Package ImgpkgBundle: &kappctrl.AppFetchImgpkgBundle{Image: o.URL}, } - if strings.TrimSpace(o.Secret) != "" { + if o.Secret != "" { pkgr.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.Secret} } @@ -323,7 +323,7 @@ func (o AddOrUpdateOptions) dryRun() error { }, } - if strings.TrimSpace(o.Secret) != "" { + if o.Secret != "" { packageRepo.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.Secret} } From e60acf87d1bf2a34988a85c2c31e8909487aef34 Mon Sep 17 00:00:00 2001 From: Devanshu Date: Wed, 28 Aug 2024 13:18:14 +0530 Subject: [PATCH 3/5] Changed flagname to secretref also added a shorthand for secretref flag Signed-off-by: Devanshu --- .../kctrl/cmd/package/repository/add_or_update.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 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 e42f9419f..d8725cb3a 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -37,7 +37,7 @@ type AddOrUpdateOptions struct { URL string SemverTagConstraints string CreateNamespace bool - Secret string + SecretRef string DryRun bool @@ -79,7 +79,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().StringVar(&o.Secret, "secret", "", "SecretRef name for imgpkgbundle, optional") + cmd.Flags().StringVarP(&o.SecretRef, "secretref", "s", "", "SecretRef name for imgpkgbundle, optional") 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)") @@ -121,7 +121,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)") - cmd.Flags().StringVarP(&o.Secret, "secret", "", "", "SecretRef name for imgpkgbundle, optional") + cmd.Flags().StringVarP(&o.SecretRef, "secretref", "s", "", "SecretRef name for imgpkgbundle, optional") o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ AllowDisableWait: true, @@ -260,8 +260,8 @@ func (o *AddOrUpdateOptions) updateExistingPackageRepository(pkgr *kcpkg.Package ImgpkgBundle: &kappctrl.AppFetchImgpkgBundle{Image: o.URL}, } - if o.Secret != "" { - pkgr.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.Secret} + if o.SecretRef != "" { + pkgr.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.SecretRef} } ref, err := name.ParseReference(o.URL, name.WeakValidation) @@ -323,8 +323,8 @@ func (o AddOrUpdateOptions) dryRun() error { }, } - if o.Secret != "" { - packageRepo.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.Secret} + if o.SecretRef != "" { + packageRepo.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.SecretRef} } ref, err := name.ParseReference(o.URL, name.WeakValidation) From ae09e99f55e1b1ad03415b0bad3d46bd48209ba4 Mon Sep 17 00:00:00 2001 From: Devanshu Date: Wed, 28 Aug 2024 16:23:23 +0530 Subject: [PATCH 4/5] Add hyphen(-) in flag name, remove shorthand Signed-off-by: Devanshu --- cli/pkg/kctrl/cmd/package/repository/add_or_update.go | 4 ++-- 1 file changed, 2 insertions(+), 2 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 d8725cb3a..03eba165a 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -79,7 +79,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().StringVarP(&o.SecretRef, "secretref", "s", "", "SecretRef name for imgpkgbundle, optional") + cmd.Flags().StringVar(&o.SecretRef, "secret-ref", "", "SecretRef name for imgpkgbundle, optional") 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)") @@ -121,7 +121,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)") - cmd.Flags().StringVarP(&o.SecretRef, "secretref", "s", "", "SecretRef name for imgpkgbundle, optional") + cmd.Flags().StringVarP(&o.SecretRef, "secret-ref", "", "", "SecretRef name for imgpkgbundle, optional") o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ AllowDisableWait: true, From 598f7eab31f476cc02b7235d431424cd0d8d5951 Mon Sep 17 00:00:00 2001 From: Devanshu Date: Wed, 28 Aug 2024 16:45:50 +0530 Subject: [PATCH 5/5] Add dry-run test for secret-ref flag Signed-off-by: Devanshu --- cli/test/e2e/package_repo_dry_run_test.go | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cli/test/e2e/package_repo_dry_run_test.go b/cli/test/e2e/package_repo_dry_run_test.go index e187d6d81..4fceefb8e 100644 --- a/cli/test/e2e/package_repo_dry_run_test.go +++ b/cli/test/e2e/package_repo_dry_run_test.go @@ -71,3 +71,32 @@ status: require.Contains(t, tagSemverOutput, tagSemverExpectedOutput) }) } + +func TestPackageRepoSecretRefDryRun(t *testing.T) { + env := BuildEnv(t) + logger := Logger{} + kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, logger} + + logger.Section("dry-run package repo add", func() { + expectedOutput := `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 + secretRef: + name: regcred +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", "--semver-tag-constraints", "1.0.0", "--secret-ref", "regcred", "--dry-run"}) + require.Contains(t, output, expectedOutput) + }) +}