Skip to content

Commit

Permalink
correct automated usage of kbld in PackageRepository (when imgpkgBund…
Browse files Browse the repository at this point in the history
…le is specified) (#737)

Co-authored-by: Dmitriy Kalinin <[email protected]>
  • Loading branch information
cppforlife and Dmitriy Kalinin committed Jun 10, 2022
1 parent d49800d commit e0d5ff2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
14 changes: 14 additions & 0 deletions pkg/pkgrepository/app_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
)

func (a *App) template(dirPath string) exec.CmdRunResult {
if len(a.app.Spec.Template) != 0 {
panic("Internal inconsistency: Package repository templates are not configurable")
}

genericOpts := ctltpl.GenericOpts{Name: a.app.Name, Namespace: a.app.Namespace}

// We have multiple ytt sections because we want to squash all the user yamls together
Expand Down Expand Up @@ -58,6 +62,16 @@ func (a *App) template(dirPath string) exec.CmdRunResult {
stream = strings.NewReader(resources)
result = a.templateFactory.NewYtt(
a.yttTemplateAddIdenticalRsRebase(), genericOpts).TemplateStream(stream, dirPath)
if result.Error != nil {
return result
}

// Optionally use kbld to apply .imgpkg/images.yml if content came from imgpkgBundle
if a.app.Spec.Fetch[0].ImgpkgBundle != nil {
stream = strings.NewReader(result.Stdout)
kbldOpts := kcv1alpha1.AppTemplateKbld{Paths: []string{"-", ".imgpkg/images.yml"}}
result = a.templateFactory.NewKbld(kbldOpts, genericOpts).TemplateStream(stream, dirPath)
}

return result
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/pkgrepository/package_repo_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ func NewPackageRepoApp(pkgRepository *pkgingv1alpha1.PackageRepository) (*kcv1al
desiredApp.Spec.SyncPeriod = pkgRepository.Spec.SyncPeriod
}

if desiredApp.Spec.Fetch[0].ImgpkgBundle != nil {
desiredApp.Spec.Template = append(desiredApp.Spec.Template,
kcv1alpha1.AppTemplate{Kbld: &kcv1alpha1.AppTemplateKbld{Paths: []string{"-", ".imgpkg/images.yml"}}})
}

desiredApp.Status = kcv1alpha1.AppStatus{
Fetch: pkgRepository.Status.Fetch,
Template: pkgRepository.Status.Template,
Expand Down
46 changes: 36 additions & 10 deletions test/e2e/kappcontroller/package_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func Test_PackageRepoBundle_PackagesAvailable(t *testing.T) {
logger := e2e.Logger{}
kubectl := e2e.Kubectl{t, env.Namespace, logger}
kapp := e2e.Kapp{t, env.Namespace, logger}

// contents of this bundle (k8slt/k8slt/kappctrl-e2e-repo-bundle)
// under examples/packaging-demo/repo-bundle
yamlRepo := `---
Expand All @@ -174,19 +175,44 @@ spec:
}
defer cleanUp()

logger.Section("deploy PackageRepository", func() {
verifyPkg := func(resourceName, imgRef string) {
out := kubectl.Run([]string{"get", resourceName, "-o", "yaml"})
assert.Contains(t, out, "packaging.carvel.dev/package-repository-ref: kappctrl-test/basic.test.carvel.dev")
assert.Contains(t, out, "image: "+imgRef+"\n")
}

logger.Section("deploy pkg repository", func() {
kapp.RunWithOpts([]string{"deploy", "-a", name, "-f", "-"}, e2e.RunOpts{StdinReader: strings.NewReader(yamlRepo)})

out := kubectl.Run([]string{"get", "pkgm/pkg.test.carvel.dev", "-o", "yaml"})
assert.Contains(t, out, "packaging.carvel.dev/package-repository-ref: kappctrl-test/basic.test.carvel.dev")

verifyPkg("pkg/pkg.test.carvel.dev.1.0.0", "index.docker.io/k8slt/kctrl-example-pkg@sha256:8ffa7f9352149dba1d539d0006b38eda357917edcdd39b82497a61dab2c27b75")
verifyPkg("pkg/pkg.test.carvel.dev.2.0.0", "index.docker.io/k8slt/kctrl-example-pkg@sha256:73713d922b5f561c0db2a7ea5f4f6384f7d2d6289886f8400a8aaf5e8fdf134a")
})

logger.Section("check PackageMetadata/Packages created", func() {
verify := func(resourceName string) {
kctlOutput := kubectl.Run([]string{"get", resourceName, "-o", "yaml"})
assert.Contains(t, kctlOutput, "kapp.k14s.io/identity:")
assert.Contains(t, kctlOutput, "packaging.carvel.dev/package-repository-ref: kappctrl-test/basic.test.carvel.dev")
}
verify("pkgm/pkg.test.carvel.dev")
verify("pkg/pkg.test.carvel.dev.1.0.0")
verify("pkg/pkg.test.carvel.dev.2.0.0")
logger.Section("deploy pkg repository with same content from a different location (where bundle was copied)", func() {
// Change location of the bundle to be different
// (imgpkg copy was used to copy original repo to a new location)
updatedRepo := `---
apiVersion: packaging.carvel.dev/v1alpha1
kind: PackageRepository
metadata:
name: basic.test.carvel.dev
spec:
fetch:
imgpkgBundle:
image: index.docker.io/k8slt/kc-e2e-test-repo-copied@sha256:ddd93b67b97c1460580ca1afd04326d16900dc716c4357cade85b83deab76f1c`

kapp.RunWithOpts([]string{"deploy", "-a", name, "-f", "-"}, e2e.RunOpts{StdinReader: strings.NewReader(updatedRepo)})

out := kubectl.Run([]string{"get", "pkgm/pkg.test.carvel.dev", "-o", "yaml"})
assert.Contains(t, out, "packaging.carvel.dev/package-repository-ref: kappctrl-test/basic.test.carvel.dev")

// Note that location of Packages has also changed to k8slt/kc-e2e-test-repo-copied
// since kbld is being applied with imgpkg's images.yml relocation data
verifyPkg("pkg/pkg.test.carvel.dev.1.0.0", "index.docker.io/k8slt/kc-e2e-test-repo-copied@sha256:8ffa7f9352149dba1d539d0006b38eda357917edcdd39b82497a61dab2c27b75")
verifyPkg("pkg/pkg.test.carvel.dev.2.0.0", "index.docker.io/k8slt/kc-e2e-test-repo-copied@sha256:73713d922b5f561c0db2a7ea5f4f6384f7d2d6289886f8400a8aaf5e8fdf134a")
})
}

Expand Down

0 comments on commit e0d5ff2

Please sign in to comment.