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 <[email protected]>
  • Loading branch information
phillebaba committed Jan 7, 2025
1 parent d6e00af commit 0964849
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/internal/packager2/layout/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ func CreateSkeleton(ctx context.Context, packagePath string, opt CreateOptions)
return buildPath, nil
}

<<<<<<< Updated upstream

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / codeql-scan (go)

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / validate-docs-and-schema

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / test-unit

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / test-e2e-without-cluster

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / test-checksums

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / build-upgrade

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / validate-unit

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / build-e2e

syntax error: non-declaration statement outside function body

Check failure on line 114 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / validate-external

syntax error: non-declaration statement outside function body
=======
func loadPackage(ctx context.Context, packagePath, flavor string, setVariables map[string]string) (v1alpha1.ZarfPackage, error) {
b, err := os.ReadFile(filepath.Join(packagePath, ZarfYAML))
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
pkg, err := ParseZarfPackage(b)
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
pkg.Metadata.Architecture = config.GetArch(pkg.Metadata.Architecture)
pkg, err = resolveImports(ctx, pkg, packagePath, pkg.Metadata.Architecture, flavor, map[string]interface{}{})
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
if setVariables != nil {
pkg, _, err = fillActiveTemplate(ctx, pkg, setVariables)
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
}
err = validate(pkg, packagePath, setVariables)
if err != nil {
return v1alpha1.ZarfPackage{}, err
}
return pkg, nil
}

>>>>>>> Stashed changes

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / codeql-scan (go)

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / validate-docs-and-schema

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / test-unit

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / test-e2e-without-cluster

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / test-checksums

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / build-upgrade

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / validate-unit

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / build-e2e

syntax error: non-declaration statement outside function body

Check failure on line 143 in src/internal/packager2/layout/create.go

View workflow job for this annotation

GitHub Actions / validate-external

syntax error: non-declaration statement outside function body
func validate(pkg v1alpha1.ZarfPackage, packagePath string, setVariables map[string]string) error {
err := lint.ValidatePackage(pkg)
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions src/internal/packager2/layout/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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", importPath, 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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions src/internal/packager2/layout/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@
package layout

import (
"fmt"
"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")

pkg, err := loadPackage(ctx, "./testdata/import/first", "", nil)
require.EqualError(t, err, "package testdata/import/second imported in cycle by testdata/import/third")
fmt.Println(pkg)
}

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

Expand Down
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 0964849

Please sign in to comment.