Skip to content

Commit

Permalink
kctrl: Add secret flag for package repo add/update (#1612)
Browse files Browse the repository at this point in the history
* Add secret flag for PackageRepository CRD in cli

Signed-off-by: Devanshu <[email protected]>

* Removed whitespace check

Signed-off-by: Devanshu <[email protected]>

* Changed flagname to secretref

also added a shorthand  for secretref flag

Signed-off-by: Devanshu <[email protected]>

* Add hyphen(-) in flag name, remove shorthand

Signed-off-by: Devanshu <[email protected]>

* Add dry-run test for secret-ref flag

Signed-off-by: Devanshu <[email protected]>

---------

Signed-off-by: Devanshu <[email protected]>
  • Loading branch information
devanshuVmware authored Aug 28, 2024
1 parent 2a68e24 commit 8a9b3f7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cli/pkg/kctrl/cmd/package/repository/add_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type AddOrUpdateOptions struct {
URL string
SemverTagConstraints string
CreateNamespace bool
SecretRef string

DryRun bool

Expand Down Expand Up @@ -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.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)")
Expand Down Expand Up @@ -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.SecretRef, "secret-ref", "", "", "SecretRef name for imgpkgbundle, optional")

o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{
AllowDisableWait: true,
Expand Down Expand Up @@ -257,6 +260,10 @@ func (o *AddOrUpdateOptions) updateExistingPackageRepository(pkgr *kcpkg.Package
ImgpkgBundle: &kappctrl.AppFetchImgpkgBundle{Image: o.URL},
}

if o.SecretRef != "" {
pkgr.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.SecretRef}
}

ref, err := name.ParseReference(o.URL, name.WeakValidation)
if err != nil {
return pkgr, fmt.Errorf("Parsing OCI registry URL: %s", err)
Expand Down Expand Up @@ -316,6 +323,10 @@ func (o AddOrUpdateOptions) dryRun() error {
},
}

if o.SecretRef != "" {
packageRepo.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.SecretRef}
}

ref, err := name.ParseReference(o.URL, name.WeakValidation)
if err != nil {
return fmt.Errorf("Parsing OCI registry URL: %s", err)
Expand Down
29 changes: 29 additions & 0 deletions cli/test/e2e/package_repo_dry_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 8a9b3f7

Please sign in to comment.