Skip to content

Commit

Permalink
kctrl: ensure secretRef is intact when only url is updated for pkgr
Browse files Browse the repository at this point in the history
Signed-off-by: cguruprasad <[email protected]>
  • Loading branch information
cguruprasad committed Oct 18, 2024
1 parent af895c9 commit bb6589c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
8 changes: 6 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 @@ -254,13 +254,17 @@ func (o *AddOrUpdateOptions) newPackageRepository() (*kcpkg.PackageRepository, e
}

func (o *AddOrUpdateOptions) updateExistingPackageRepository(pkgr *kcpkg.PackageRepository) (*kcpkg.PackageRepository, error) {

pkgr = pkgr.DeepCopy()

pkgr.Spec.Fetch = &kcpkg.PackageRepositoryFetch{
fetch := &kcpkg.PackageRepositoryFetch{
ImgpkgBundle: &kappctrl.AppFetchImgpkgBundle{Image: o.URL},
}

if pkgr.Spec.Fetch != nil && pkgr.Spec.Fetch.ImgpkgBundle != nil {
fetch.ImgpkgBundle.SecretRef = pkgr.Spec.Fetch.ImgpkgBundle.SecretRef
}
pkgr.Spec.Fetch = fetch

// the case where a user would want to update the pkgr to remove the secretRef is not supported
if o.SecretRef != "" {
pkgr.Spec.Fetch.ImgpkgBundle.SecretRef = &kappctrl.AppFetchLocalRef{Name: o.SecretRef}
Expand Down
26 changes: 20 additions & 6 deletions cli/test/e2e/package_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ func TestPackageRepository(t *testing.T) {
_, _ = kappCtrl.RunWithOpts([]string{"package", "repository", "add", "-r", pkgrWithSecretName, "--url", pkgrURL, "--secret-ref", pkgrSecretRef}, RunOpts{
AllowError: true})

pkgrYaml := kubectl.Run([]string{"get", kind, pkgrWithSecretName, "-ojson"})
pkgrJson := kubectl.Run([]string{"get", kind, pkgrWithSecretName, "-ojson"})
pkgr := &kcpkg.PackageRepository{}
err := json.Unmarshal([]byte(pkgrYaml), pkgr)
err := json.Unmarshal([]byte(pkgrJson), pkgr)
require.NoError(t, err)
require.Equal(t, pkgrSecretRef, pkgr.Spec.Fetch.ImgpkgBundle.SecretRef.Name)

Expand All @@ -233,22 +233,36 @@ func TestPackageRepository(t *testing.T) {
kappCtrl.RunWithOpts([]string{"package", "repository", "update", "-r", pkgrWithSecretName, "--url", pkgrURL, "--secret-ref", pkgrSecretRef}, RunOpts{
AllowError: true})

pkgrYaml := kubectl.Run([]string{"get", kind, pkgrWithSecretName, "-ojson"})
pkgrJson := kubectl.Run([]string{"get", kind, pkgrWithSecretName, "-ojson"})
pkgr := &kcpkg.PackageRepository{}
err = json.Unmarshal([]byte(pkgrYaml), pkgr)
err = json.Unmarshal([]byte(pkgrJson), pkgr)
require.NoError(t, err)
require.Equal(t, pkgrSecretRef, pkgr.Spec.Fetch.ImgpkgBundle.SecretRef.Name)

// update to a new secret
kappCtrl.RunWithOpts([]string{"package", "repository", "update", "-r", pkgrWithSecretName, "--url", pkgrURL, "--secret-ref", pkgrSecretRef + "-2"}, RunOpts{
AllowError: true})

pkgrYaml = kubectl.Run([]string{"get", kind, pkgrWithSecretName, "-ojson"})
pkgrJson = kubectl.Run([]string{"get", kind, pkgrWithSecretName, "-ojson"})
pkgr = &kcpkg.PackageRepository{}
err = json.Unmarshal([]byte(pkgrYaml), pkgr)
err = json.Unmarshal([]byte(pkgrJson), pkgr)
require.NoError(t, err)
require.Equal(t, pkgrSecretRef+"-2", pkgr.Spec.Fetch.ImgpkgBundle.SecretRef.Name)
})

logger.Section("updating just the url of a repository with secret", func() {
// update just the url, secret should be intact
kappCtrl.RunWithOpts([]string{"package", "repository", "update", "-r", pkgrWithSecretName, "--url", pkgrURL + "-new"}, RunOpts{
AllowError: true})

pkgrJson := kubectl.Run([]string{"get", kind, pkgrWithSecretName, "-ojson"})
pkgr := &kcpkg.PackageRepository{}
err := json.Unmarshal([]byte(pkgrJson), pkgr)
require.NoError(t, err)
require.Equal(t, pkgrURL+"-new", pkgr.Spec.Fetch.ImgpkgBundle.Image)
require.Equal(t, pkgrSecretRef+"-2", pkgr.Spec.Fetch.ImgpkgBundle.SecretRef.Name)
})
}
}

func TestPackageRepositoryTagSemver(t *testing.T) {
Expand Down

0 comments on commit bb6589c

Please sign in to comment.