-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor remove copies from components to a filter
- Loading branch information
1 parent
ad7bf08
commit d178222
Showing
4 changed files
with
94 additions
and
70 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: 2024-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