Skip to content

Commit

Permalink
Merge pull request #1119 from sfc-gh-mhazy/overlay
Browse files Browse the repository at this point in the history
Fix duplicates when overlaying the config with config with no contents
  • Loading branch information
jonjohnsonjr authored May 14, 2024
2 parents 20850c6 + db9fc07 commit eacdde3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pkg/build/types/image_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func (ic *ImageConfiguration) parse(ctx context.Context, configData []byte, conf

mergedIc := ImageConfiguration{}

// Merge packages, repositories and keyrings from base and overlay configurations
keyring := append([]string{}, baseIc.Contents.Keyring...)
keyring = append(keyring, ic.Contents.Keyring...)

repos := append([]string{}, baseIc.Contents.Repositories...)
repos = append(repos, ic.Contents.Repositories...)

pkgs := append([]string{}, baseIc.Contents.Packages...)
pkgs = append(pkgs, ic.Contents.Packages...)

// Copy the base configuration...
if err := copier.Copy(&mergedIc, &baseIc); err != nil {
return fmt.Errorf("failed to copy base configuration: %w", err)
Expand All @@ -80,17 +90,9 @@ func (ic *ImageConfiguration) parse(ctx context.Context, configData []byte, conf
return fmt.Errorf("failed to copy merged configuration: %w", err)
}

// Merge packages, repositories and keyrings.
keyring := append([]string{}, baseIc.Contents.Keyring...)
keyring = append(keyring, mergedIc.Contents.Keyring...)
// Finally, update the repeated fields to the merged ones.
ic.Contents.Keyring = keyring

repos := append([]string{}, baseIc.Contents.Repositories...)
repos = append(repos, mergedIc.Contents.Repositories...)
ic.Contents.Repositories = repos

pkgs := append([]string{}, baseIc.Contents.Packages...)
pkgs = append(pkgs, mergedIc.Contents.Packages...)
ic.Contents.Packages = pkgs
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/build/types/image_configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package types_test

import (
"context"
"crypto/sha256"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"chainguard.dev/apko/pkg/build/types"
)

func TestOverlayWithEmptyContents(t *testing.T) {
ctx := context.Background()

configPath := filepath.Join("testdata", "overlay", "overlay.apko.yaml")
hasher := sha256.New()
ic := types.ImageConfiguration{}

require.NoError(t, ic.Load(ctx, configPath, hasher))
require.ElementsMatch(t, ic.Contents.Repositories, []string{"repository"})
require.ElementsMatch(t, ic.Contents.Keyring, []string{"key"})
require.ElementsMatch(t, ic.Contents.Packages, []string{"package"})
}

func TestOverlayWithAdditionalPackages(t *testing.T) {
ctx := context.Background()

configPath := filepath.Join("testdata", "overlay", "overlay_with_package.apko.yaml")
hasher := sha256.New()
ic := types.ImageConfiguration{}

require.NoError(t, ic.Load(ctx, configPath, hasher))
require.ElementsMatch(t, ic.Contents.Repositories, []string{"repository"})
require.ElementsMatch(t, ic.Contents.Keyring, []string{"key"})
require.ElementsMatch(t, ic.Contents.Packages, []string{"package", "other_package"})
}
7 changes: 7 additions & 0 deletions pkg/build/types/testdata/overlay/base.apko.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contents:
repositories:
- "repository"
keyring:
- "key"
packages:
- "package"
1 change: 1 addition & 0 deletions pkg/build/types/testdata/overlay/overlay.apko.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: testdata/overlay/base.apko.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include: testdata/overlay/overlay.apko.yaml

contents:
packages:
- "other_package"

0 comments on commit eacdde3

Please sign in to comment.