Skip to content

Commit

Permalink
fix: compose dropping only.localOS in an only filter (#2173)
Browse files Browse the repository at this point in the history
## Description

This fixes an issue where compose could drop the `only.localOS` filter
through compose.

## Related Issue

Fixes #2158

## Type of change

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [X] Test, docs, adr added or updated as needed
- [X] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow)
followed

---------

Co-authored-by: Lucas Rodriguez <lucas.rodriguez9616@gmail.com>
  • Loading branch information
Racer159 and lucasrod16 committed Dec 6, 2023
1 parent 66b6006 commit 82faa37
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/pkg/packager/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (p *Packager) composeComponents() error {
if err != nil {
return err
}
components = append(components, composed)
components = append(components, *composed)

// merge variables and constants
pkgVars = chain.MergeVariables(pkgVars)
Expand Down
22 changes: 13 additions & 9 deletions src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,33 +216,37 @@ func (ic *ImportChain) Migrate(build types.ZarfBuildData) (warnings []string) {

// Compose merges the import chain into a single component
// fixing paths, overriding metadata, etc
func (ic *ImportChain) Compose() (composed types.ZarfComponent, err error) {
composed = ic.tail.ZarfComponent
func (ic *ImportChain) Compose() (composed *types.ZarfComponent, err error) {
composed = &ic.tail.ZarfComponent

if ic.tail.prev == nil {
// only had one component in the import chain
return composed, nil
}

if err := ic.fetchOCISkeleton(); err != nil {
return composed, err
return nil, err
}

// start with an empty component to compose into
composed = types.ZarfComponent{}
composed = &types.ZarfComponent{}

// start overriding with the tail node
node := ic.tail
for node != nil {
fixPaths(&node.ZarfComponent, node.relativeToHead)

// perform overrides here
overrideMetadata(&composed, node.ZarfComponent)
overrideDeprecated(&composed, node.ZarfComponent)
overrideResources(&composed, node.ZarfComponent)
overrideActions(&composed, node.ZarfComponent)
err := overrideMetadata(composed, node.ZarfComponent)
if err != nil {
return nil, err
}

overrideDeprecated(composed, node.ZarfComponent)
overrideResources(composed, node.ZarfComponent)
overrideActions(composed, node.ZarfComponent)

composeExtensions(&composed, node.ZarfComponent, node.relativeToHead)
composeExtensions(composed, node.ZarfComponent, node.relativeToHead)

node = node.prev
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/composer/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func TestCompose(t *testing.T) {
if testCase.returnError {
require.Contains(t, err.Error(), testCase.expectedErrorMessage)
} else {
require.EqualValues(t, testCase.expectedComposed, composed)
require.EqualValues(t, &testCase.expectedComposed, composed)
}
})
}
Expand Down
14 changes: 13 additions & 1 deletion src/pkg/packager/composer/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package composer

import (
"fmt"

"github.com/defenseunicorns/zarf/src/types"
)

func overrideMetadata(c *types.ZarfComponent, override types.ZarfComponent) {
func overrideMetadata(c *types.ZarfComponent, override types.ZarfComponent) error {
c.Name = override.Name
c.Default = override.Default
c.Required = override.Required
Expand All @@ -17,6 +19,16 @@ func overrideMetadata(c *types.ZarfComponent, override types.ZarfComponent) {
if override.Description != "" {
c.Description = override.Description
}

if override.Only.LocalOS != "" {
if c.Only.LocalOS != "" {
return fmt.Errorf("component %q \"only.localOS\" %q cannot be redefined as %q during compose", c.Name, c.Only.LocalOS, override.Only.LocalOS)
}

c.Only.LocalOS = override.Only.LocalOS
}

return nil
}

func overrideDeprecated(c *types.ZarfComponent, override types.ZarfComponent) {
Expand Down
21 changes: 16 additions & 5 deletions src/test/e2e/09_component_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ type CompositionSuite struct {
}

var (
composeExample = filepath.Join("examples", "composable-packages")
composeExamplePath string
composeTest = filepath.Join("src", "test", "packages", "09-composable-packages")
composeTestPath string
relCacheDir string
composeExample = filepath.Join("examples", "composable-packages")
composeExamplePath string
composeTest = filepath.Join("src", "test", "packages", "09-composable-packages")
composeTestPath string
composeTestBadLocalOS = filepath.Join("src", "test", "packages", "09-composable-packages", "bad-local-os")
relCacheDir string
)

func (suite *CompositionSuite) SetupSuite() {
Expand Down Expand Up @@ -87,6 +88,8 @@ func (suite *CompositionSuite) Test_1_FullComposability() {
- name: test-compose-package
description: A contrived example for podinfo using many Zarf primitives for compose testing
required: true
only:
localOS: linux
`)

// Check files
Expand Down Expand Up @@ -184,6 +187,14 @@ func (suite *CompositionSuite) Test_1_FullComposability() {
condition: available`)
}

func (suite *CompositionSuite) Test_2_ComposabilityBadLocalOS() {
suite.T().Log("E2E: Package Compose Example")

_, stdErr, err := e2e.Zarf("package", "create", composeTestBadLocalOS, "-o", "build", "--zarf-cache", relCacheDir, "--no-color", "--confirm")
suite.Error(err)
suite.Contains(stdErr, "\"only.localOS\" \"linux\" cannot be\n redefined as \"windows\" during compose")
}

func TestCompositionSuite(t *testing.T) {
suite.Run(t, new(CompositionSuite))
}
14 changes: 14 additions & 0 deletions src/test/packages/09-composable-packages/bad-local-os/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
kind: ZarfPackageConfig
metadata:
name: test-compose-package
description: A contrived example for localOS compose testing

components:
- name: test-compose-package
description: A contrived example for localOS compose testing
only:
localOS: windows
required: true
import:
path: ../sub-package
name: test-compose-sub-package
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ metadata:

components:
- name: test-compose-sub-package
only:
localOS: linux
charts:
- name: podinfo-compose
releaseName: podinfo-compose
Expand Down

0 comments on commit 82faa37

Please sign in to comment.