Skip to content

Commit

Permalink
Add support for import chains in packager2
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
  • Loading branch information
phillebaba committed Jan 7, 2025
1 parent d6e00af commit 0ffc3aa
Showing 6 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/internal/packager2/layout/create.go
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ func CreateSkeleton(ctx context.Context, packagePath string, opt CreateOptions)

pkg.Metadata.Architecture = config.GetArch()

pkg, err = resolveImports(ctx, pkg, packagePath, pkg.Metadata.Architecture, opt.Flavor)
pkg, err = resolveImports(ctx, pkg, packagePath, pkg.Metadata.Architecture, opt.Flavor, map[string]interface{}{})
if err != nil {
return "", err
}
16 changes: 11 additions & 5 deletions src/internal/packager2/layout/import.go
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ import (
"github.com/zarf-dev/zarf/src/pkg/zoci"
)

func resolveImports(ctx context.Context, pkg v1alpha1.ZarfPackage, packagePath, arch, flavor string) (v1alpha1.ZarfPackage, error) {
func resolveImports(ctx context.Context, pkg v1alpha1.ZarfPackage, packagePath, arch, flavor string, seenImports map[string]interface{}) (v1alpha1.ZarfPackage, error) {
variables := pkg.Variables
constants := pkg.Constants
components := []v1alpha1.ZarfComponent{}
@@ -45,14 +45,23 @@ func resolveImports(ctx context.Context, pkg v1alpha1.ZarfPackage, packagePath,

var importedPkg v1alpha1.ZarfPackage
if component.Import.Path != "" {
b, err := os.ReadFile(filepath.Join(packagePath, component.Import.Path, layout.ZarfYAML))
importPath := filepath.Join(packagePath, component.Import.Path)
if _, ok := seenImports[importPath]; ok {
return v1alpha1.ZarfPackage{}, fmt.Errorf("package %s imported in cycle by %s", filepath.ToSlash(importPath), filepath.ToSlash(packagePath))
}
seenImports[importPath] = nil
b, err := os.ReadFile(filepath.Join(importPath, layout.ZarfYAML))
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
importedPkg, err = ParseZarfPackage(b)
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
importedPkg, err = resolveImports(ctx, importedPkg, importPath, arch, flavor, seenImports)
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
} else if component.Import.URL != "" {
remote, err := zoci.NewRemote(ctx, component.Import.URL, zoci.PlatformForSkeleton())
if err != nil {
@@ -84,9 +93,6 @@ func resolveImports(ctx context.Context, pkg v1alpha1.ZarfPackage, packagePath,
return v1alpha1.ZarfPackage{}, fmt.Errorf("multiple components named %s found", name)
}
importedComponent := found[0]
if importedComponent.Import.Path != "" || importedComponent.Import.URL != "" {
return v1alpha1.ZarfPackage{}, fmt.Errorf("imported component %s has imports which is not supported", importedComponent.Name)
}

importPath, err := fetchOCISkeleton(ctx, component, packagePath)
if err != nil {
20 changes: 20 additions & 0 deletions src/internal/packager2/layout/import_test.go
Original file line number Diff line number Diff line change
@@ -4,14 +4,34 @@
package layout

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/test/testutil"
)

func TestResolveImportsCircular(t *testing.T) {
t.Parallel()

ctx := testutil.TestContext(t)

lint.ZarfSchema = testutil.LoadSchema(t, "../../../../zarf.schema.json")

b, err := os.ReadFile(filepath.Join("./testdata/import/first", ZarfYAML))
require.NoError(t, err)
pkg, err := ParseZarfPackage(b)
require.NoError(t, err)

_, err = resolveImports(ctx, pkg, "./testdata/import/first", "", "", map[string]interface{}{})
require.EqualError(t, err, "package testdata/import/second imported in cycle by testdata/import/third")
}

func TestValidateComponentCompose(t *testing.T) {
t.Parallel()

8 changes: 8 additions & 0 deletions src/internal/packager2/layout/testdata/import/first/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: ZarfPackageConfig
metadata:
name: first
components:
- name: component
required: true
import:
path: ../second
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: ZarfPackageConfig
metadata:
name: second
components:
- name: component
required: true
import:
path: ../third
8 changes: 8 additions & 0 deletions src/internal/packager2/layout/testdata/import/third/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: ZarfPackageConfig
metadata:
name: third
components:
- name: component
required: true
import:
path: ../second

0 comments on commit 0ffc3aa

Please sign in to comment.