-
Notifications
You must be signed in to change notification settings - Fork 157
/
differential.go
113 lines (94 loc) · 3.51 KB
/
differential.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package creator contains functions for creating Zarf packages.
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/message"
"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.
func (pc *PackageCreator) loadDifferentialData() (diffData *types.DifferentialData, err error) {
tmpdir, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return nil, err
}
diffLayout := layout.New(tmpdir)
defer os.RemoveAll(diffLayout.Base)
src, err := sources.New(&types.ZarfPackageOptions{
PackageSource: pc.createOpts.DifferentialPackagePath,
})
if err != nil {
return nil, err
}
if err := src.LoadPackageMetadata(diffLayout, false, false); err != nil {
return nil, err
}
var diffPkg types.ZarfPackage
if err := utils.ReadYaml(diffLayout.ZarfYAML, &diffPkg); err != nil {
return nil, fmt.Errorf("error reading the differential Zarf package: %w", err)
}
allIncludedImagesMap := map[string]bool{}
allIncludedReposMap := map[string]bool{}
for _, component := range diffPkg.Components {
for _, image := range component.Images {
allIncludedImagesMap[image] = true
}
for _, repo := range component.Repos {
allIncludedReposMap[repo] = true
}
}
return &types.DifferentialData{
DifferentialImages: allIncludedImagesMap,
DifferentialRepos: allIncludedReposMap,
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)
} else {
message.Debugf("Image %s is already included in the differential package", 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)
} else {
message.Debugf("Repo %s is already included in the differential package", repoURL)
}
}
component.Images = newImageList
component.Repos = newRepoList
diffComponents = append(diffComponents, component)
}
return diffComponents, nil
}