-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor remove copies from components to a filter
- Loading branch information
1 parent
08c49dd
commit 5685987
Showing
4 changed files
with
136 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package filters | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/defenseunicorns/zarf/src/internal/packager/git" | ||
"github.com/defenseunicorns/zarf/src/pkg/transform" | ||
"github.com/defenseunicorns/zarf/src/types" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
// ByDifferentialData filters any images and repos already present in the reference package components. | ||
func ByDifferentialData(diffData *types.DifferentialData) ComponentFilterStrategy { | ||
return &differentialDataFilter{ | ||
diffData: diffData, | ||
} | ||
} | ||
|
||
type differentialDataFilter struct { | ||
diffData *types.DifferentialData | ||
} | ||
|
||
func (f *differentialDataFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, error) { | ||
diffComponents := []types.ZarfComponent{} | ||
for _, component := range pkg.Components { | ||
filteredImages := []string{} | ||
for _, img := range component.Images { | ||
imgRef, err := transform.ParseImageRef(img) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse image ref %s: %w", img, err) | ||
} | ||
imgTag := imgRef.TagOrDigest | ||
includeImage := imgTag == ":latest" || imgTag == ":stable" || imgTag == ":nightly" | ||
if includeImage || !f.diffData.DifferentialImages[img] { | ||
filteredImages = append(filteredImages, img) | ||
} | ||
} | ||
component.Images = filteredImages | ||
|
||
filteredRepos := []string{} | ||
for _, repoURL := range component.Repos { | ||
_, refPlain, err := transform.GitURLSplitRef(repoURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var ref plumbing.ReferenceName | ||
if refPlain != "" { | ||
ref = git.ParseRef(refPlain) | ||
} | ||
includeRepo := ref == "" || (!ref.IsTag() && !plumbing.IsHash(refPlain)) | ||
if includeRepo || !f.diffData.DifferentialRepos[repoURL] { | ||
filteredRepos = append(filteredRepos, repoURL) | ||
} | ||
} | ||
component.Repos = filteredRepos | ||
|
||
diffComponents = append(diffComponents, component) | ||
} | ||
return diffComponents, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package filters | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/defenseunicorns/zarf/src/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCopyFilter(t *testing.T) { | ||
pkg := types.ZarfPackage{ | ||
Components: []types.ZarfComponent{ | ||
{ | ||
Images: []string{ | ||
"example.com/include-image-tag:latest", | ||
"example.com/image-with-tag:v1", | ||
"example.com/diff-image-with-tag:v1", | ||
"example.com/image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
"example.com/diff-image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
"example.com/image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
"example.com/diff-image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
}, | ||
Repos: []string{ | ||
"https://example.com/no-ref.git", | ||
"https://example.com/branch.git@refs/heads/main", | ||
"https://example.com/tag.git@v1", | ||
"https://example.com/diff-tag.git@v1", | ||
"https://example.com/commit.git@524980951ff16e19dc25232e9aea8fd693989ba6", | ||
"https://example.com/diff-commit.git@524980951ff16e19dc25232e9aea8fd693989ba6", | ||
}, | ||
}, | ||
}, | ||
} | ||
loadedDiffData := types.DifferentialData{ | ||
DifferentialImages: map[string]bool{ | ||
"example.com/include-image-tag:latest": true, | ||
"example.com/diff-image-with-tag:v1": true, | ||
"example.com/diff-image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855": true, | ||
"example.com/diff-image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855": true, | ||
}, | ||
DifferentialRepos: map[string]bool{ | ||
"https://example.com/no-ref.git": true, | ||
"https://example.com/branch.git@refs/heads/main": true, | ||
"https://example.com/diff-tag.git@v1": true, | ||
"https://example.com/diff-commit.git@524980951ff16e19dc25232e9aea8fd693989ba6": true, | ||
}, | ||
} | ||
|
||
filter := ByDifferentialData(&loadedDiffData) | ||
diffComponents, err := filter.Apply(pkg) | ||
require.NoError(t, err) | ||
|
||
expectedImages := []string{ | ||
"example.com/include-image-tag:latest", | ||
"example.com/image-with-tag:v1", | ||
"example.com/image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
"example.com/image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
} | ||
require.ElementsMatch(t, expectedImages, diffComponents[0].Images) | ||
expectedRepos := []string{ | ||
"https://example.com/no-ref.git", | ||
"https://example.com/branch.git@refs/heads/main", | ||
"https://example.com/tag.git@v1", | ||
"https://example.com/commit.git@524980951ff16e19dc25232e9aea8fd693989ba6", | ||
} | ||
require.ElementsMatch(t, expectedRepos, diffComponents[0].Repos) | ||
} |