From bf691128d2506b9f3e729f456d9c52780d24cb05 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Tue, 7 May 2024 16:59:16 +0200 Subject: [PATCH 1/2] refactor: remove copies from components to a filter (#2474) ## Description This change refactors remove copies from components to a filter. ## Related Issue Depends on: #2473 Fixes #2337 ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: razzle --- src/pkg/packager/creator/differential.go | 48 -------------- src/pkg/packager/creator/normal.go | 4 +- src/pkg/packager/filters/diff.go | 63 +++++++++++++++++++ .../diff_test.go} | 46 +++++++------- 4 files changed, 91 insertions(+), 70 deletions(-) create mode 100644 src/pkg/packager/filters/diff.go rename src/pkg/packager/{creator/differential_test.go => filters/diff_test.go} (59%) diff --git a/src/pkg/packager/creator/differential.go b/src/pkg/packager/creator/differential.go index 59915eead1..ee373836d0 100644 --- a/src/pkg/packager/creator/differential.go +++ b/src/pkg/packager/creator/differential.go @@ -5,17 +5,13 @@ package creator import ( - "fmt" "os" "github.com/defenseunicorns/zarf/src/config" - "github.com/defenseunicorns/zarf/src/internal/packager/git" "github.com/defenseunicorns/zarf/src/pkg/layout" "github.com/defenseunicorns/zarf/src/pkg/packager/sources" - "github.com/defenseunicorns/zarf/src/pkg/transform" "github.com/defenseunicorns/zarf/src/pkg/utils" "github.com/defenseunicorns/zarf/src/types" - "github.com/go-git/go-git/v5/plumbing" ) // loadDifferentialData sets any images and repos from the existing reference package in the DifferentialData and returns it. @@ -58,47 +54,3 @@ func loadDifferentialData(diffPkgPath string) (diffData *types.DifferentialData, DifferentialPackageVersion: diffPkg.Metadata.Version, }, nil } - -// removeCopiesFromComponents removes any images and repos already present in the reference package components. -func removeCopiesFromComponents(components []types.ZarfComponent, loadedDiffData *types.DifferentialData) (diffComponents []types.ZarfComponent, err error) { - for _, component := range components { - newImageList := []string{} - newRepoList := []string{} - - for _, img := range component.Images { - imgRef, err := transform.ParseImageRef(img) - if err != nil { - return nil, fmt.Errorf("unable to parse image ref %s: %s", img, err.Error()) - } - - imgTag := imgRef.TagOrDigest - includeImage := imgTag == ":latest" || imgTag == ":stable" || imgTag == ":nightly" - if includeImage || !loadedDiffData.DifferentialImages[img] { - newImageList = append(newImageList, img) - } - } - - 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 || !loadedDiffData.DifferentialRepos[repoURL] { - newRepoList = append(newRepoList, repoURL) - } - } - - component.Images = newImageList - component.Repos = newRepoList - diffComponents = append(diffComponents, component) - } - - return diffComponents, nil -} diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index fed5003380..81478c730e 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -27,6 +27,7 @@ import ( "github.com/defenseunicorns/zarf/src/pkg/layout" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/packager/actions" + "github.com/defenseunicorns/zarf/src/pkg/packager/filters" "github.com/defenseunicorns/zarf/src/pkg/packager/sources" "github.com/defenseunicorns/zarf/src/pkg/transform" "github.com/defenseunicorns/zarf/src/pkg/utils" @@ -110,7 +111,8 @@ func (pc *PackageCreator) LoadPackageDefinition(dst *layout.PackagePaths) (pkg t return types.ZarfPackage{}, nil, errors.New(lang.PkgCreateErrDifferentialNoVersion) } - pkg.Components, err = removeCopiesFromComponents(pkg.Components, diffData) + filter := filters.ByDifferentialData(diffData) + pkg.Components, err = filter.Apply(pkg) if err != nil { return types.ZarfPackage{}, nil, err } diff --git a/src/pkg/packager/filters/diff.go b/src/pkg/packager/filters/diff.go new file mode 100644 index 0000000000..fe722e9741 --- /dev/null +++ b/src/pkg/packager/filters/diff.go @@ -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 +} diff --git a/src/pkg/packager/creator/differential_test.go b/src/pkg/packager/filters/diff_test.go similarity index 59% rename from src/pkg/packager/creator/differential_test.go rename to src/pkg/packager/filters/diff_test.go index cc616f92ed..8ee64fab84 100644 --- a/src/pkg/packager/creator/differential_test.go +++ b/src/pkg/packager/filters/diff_test.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2021-Present The Zarf Authors -package creator +package filters import ( "testing" @@ -10,25 +10,27 @@ import ( "github.com/stretchr/testify/require" ) -func TestRemoveCopiesFromComponents(t *testing.T) { - 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", +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", + }, }, }, } @@ -46,7 +48,9 @@ func TestRemoveCopiesFromComponents(t *testing.T) { "https://example.com/diff-commit.git@524980951ff16e19dc25232e9aea8fd693989ba6": true, }, } - diffComponents, err := removeCopiesFromComponents(components, &loadedDiffData) + + filter := ByDifferentialData(&loadedDiffData) + diffComponents, err := filter.Apply(pkg) require.NoError(t, err) expectedImages := []string{ From 15a73e09485347a28d0620c3aad563194e21ef27 Mon Sep 17 00:00:00 2001 From: schristoff <28318173+schristoff@users.noreply.github.com> Date: Tue, 7 May 2024 09:16:18 -0600 Subject: [PATCH 2/2] chore: add support.md (#2480) Signed-off-by: schristoff <28318173+schristoff@users.noreply.github.com> Co-authored-by: Lucas Rodriguez --- SUPPORT.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 SUPPORT.md diff --git a/SUPPORT.md b/SUPPORT.md new file mode 100644 index 0000000000..690b7d8402 --- /dev/null +++ b/SUPPORT.md @@ -0,0 +1,47 @@ +# Support Guidelines + +We strive to create clear guidelines on communication to the Zarf team to provide a good community experience. + +## Questions +For guidance on using Zarf, [the documentation](https://docs.zarf.dev/) should cover most use cases. +For all questions documentation may not cover, we suggest utilizing [Github Discussions](https://github.com/defenseunicorns/zarf/discussions). + +## Standard Process +All code issues should be a [Github Issue](https://github.com/defenseunicorns/zarf/issues/new/choose) that follows the issue template. + +Following the templates provides the Zarf community a foundation of understanding to be able to assist quickly. +After an issue is made, this issue can be brought into other chanels such as the [Kubernetes Slack #Zarf](https://zarf.dev/slack) channel or the [bi-weekly Zarf Community Meeting](https://docs.zarf.dev/contribute/contributor-guide/). + + Github Issue + / \ + Zarf Slack Channel Zarf Community Call + + + +## Sensitive Information Process +For issues from those who are unable to post on Github, you may send an email using the following issue template filled out to [zarf-dev-private@googlegroups.com](zarf-dev-private@googlegroups.com) + +The response time to emails may be delayed as they are not able to receive community help, so we encourage participation into Github Issues as much as possible. + +``` +### Environment +Device and OS: +App version: +Kubernetes distro being used: +Other: + +### Steps to reproduce +1. + +### Expected result + +### Actual Result + +### Visual Proof (screenshots, videos, text, etc) + +### Severity/Priority + +### Additional Context +Add any other context or screenshots about the technical debt here. + +``` \ No newline at end of file