Skip to content

Commit a4f9e7a

Browse files
committed
change mirror to use new logic
Signed-off-by: Austin Abro <[email protected]>
1 parent 7ccb603 commit a4f9e7a

File tree

2 files changed

+34
-103
lines changed

2 files changed

+34
-103
lines changed

src/internal/packager2/layout/package.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,37 @@ func (p *PackageLayout) GetComponentDir(destPath, componentName string, ct Compo
183183
// GetImage returns the image with the given reference in the package layout.
184184
func (p *PackageLayout) GetImage(ref transform.Image) (registryv1.Image, error) {
185185
// Use the manifest within the index.json to load the specific image we want
186-
layoutPath := layout.Path(filepath.Join(p.dirPath, ImagesDir))
186+
imgPath := filepath.Join(p.dirPath, ImagesDir)
187+
layoutPath := layout.Path(imgPath)
187188
imgIdx, err := layoutPath.ImageIndex()
188189
if err != nil {
189-
return nil, err
190+
return nil, fmt.Errorf("failed to get image index: %w", err)
190191
}
191192
idxManifest, err := imgIdx.IndexManifest()
192193
if err != nil {
193-
return nil, err
194+
return nil, fmt.Errorf("failed to get image manifest: %w", err)
194195
}
196+
195197
// Search through all the manifests within this package until we find the annotation that matches our ref
196198
for _, manifest := range idxManifest.Manifests {
197-
if manifest.Annotations[ocispec.AnnotationBaseImageName] == ref.Reference ||
199+
if manifest.Annotations[ocispec.AnnotationRefName] == ref.Reference ||
198200
// A backwards compatibility shim for older Zarf versions that would leave docker.io off of image annotations
199-
(manifest.Annotations[ocispec.AnnotationBaseImageName] == ref.Path+ref.TagOrDigest && ref.Host == "docker.io") {
201+
(manifest.Annotations[ocispec.AnnotationRefName] == ref.Path+ref.TagOrDigest && ref.Host == "docker.io") {
200202
// This is the image we are looking for, load it and then return
201-
return layoutPath.Image(manifest.Digest)
203+
img, err := layoutPath.Image(manifest.Digest)
204+
if err != nil {
205+
return nil, fmt.Errorf("failed to lookup image %s: %w", ref.Reference, err)
206+
}
207+
return img, nil
202208
}
203209
}
204-
return nil, fmt.Errorf("unable to find the image %s", ref.Reference)
210+
211+
return nil, fmt.Errorf("unable to find image (%s) at the path (%s)", ref.Reference, imgPath)
212+
}
213+
214+
func (p *PackageLayout) GetImageDir() string {
215+
// Use the manifest within the index.json to load the specific image we want
216+
return filepath.Join(p.dirPath, ImagesDir)
205217
}
206218

207219
func (p *PackageLayout) Archive(ctx context.Context, dirPath string, maxPackageSize int) error {

src/internal/packager2/mirror.go

+15-96
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"net/http"
1110
"os"
1211
"time"
1312

1413
"github.com/avast/retry-go/v4"
15-
"github.com/defenseunicorns/pkg/helpers/v2"
16-
"github.com/google/go-containerregistry/pkg/authn"
17-
"github.com/google/go-containerregistry/pkg/crane"
18-
v1 "github.com/google/go-containerregistry/pkg/v1"
1914

2015
"github.com/zarf-dev/zarf/src/config"
2116
"github.com/zarf-dev/zarf/src/internal/dns"
2217
"github.com/zarf-dev/zarf/src/internal/git"
2318
"github.com/zarf-dev/zarf/src/internal/gitea"
19+
"github.com/zarf-dev/zarf/src/internal/packager/images"
2420
"github.com/zarf-dev/zarf/src/internal/packager2/layout"
2521
"github.com/zarf-dev/zarf/src/pkg/cluster"
2622
"github.com/zarf-dev/zarf/src/pkg/logger"
@@ -56,113 +52,36 @@ func Mirror(ctx context.Context, opt MirrorOptions) error {
5652
}
5753

5854
func pushImagesToRegistry(ctx context.Context, c *cluster.Cluster, pkgLayout *layout.PackageLayout, filter filters.ComponentFilterStrategy, regInfo types.RegistryInfo, noImgChecksum bool, retries int) error {
59-
l := logger.From(ctx)
60-
6155
components, err := filter.Apply(pkgLayout.Pkg)
6256
if err != nil {
6357
return err
6458
}
6559

66-
images := map[transform.Image]v1.Image{}
60+
refs := []transform.Image{}
6761
for _, component := range components {
6862
for _, img := range component.Images {
6963
ref, err := transform.ParseImageRef(img)
7064
if err != nil {
7165
return fmt.Errorf("failed to create ref for image %s: %w", img, err)
7266
}
73-
if _, ok := images[ref]; ok {
74-
continue
75-
}
76-
img, err := pkgLayout.GetImage(ref)
77-
if err != nil {
78-
return err
79-
}
80-
images[ref] = img
67+
refs = append(refs, ref)
8168
}
8269
}
83-
if len(images) == 0 {
70+
if len(refs) == 0 {
8471
return nil
8572
}
86-
87-
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
88-
defaultTransport.TLSClientConfig.InsecureSkipVerify = config.CommonOptions.InsecureSkipTLSVerify
89-
// TODO (@WSTARR) This is set to match the TLSHandshakeTimeout to potentially mitigate effects of https://github.com/zarf-dev/zarf/issues/1444
90-
defaultTransport.ResponseHeaderTimeout = 10 * time.Second
91-
transport := helpers.NewTransport(defaultTransport, nil)
92-
93-
pushOptions := []crane.Option{
94-
crane.WithPlatform(&v1.Platform{OS: "linux", Architecture: pkgLayout.Pkg.Build.Architecture}),
95-
crane.WithTransport(transport),
96-
crane.WithAuth(authn.FromConfig(authn.AuthConfig{
97-
Username: regInfo.PushUsername,
98-
Password: regInfo.PushPassword,
99-
})),
100-
crane.WithUserAgent("zarf"),
101-
crane.WithNoClobber(true),
102-
crane.WithJobs(1),
73+
pushCfg := images.PushConfig{
74+
// TODO set as an option
75+
PlainHTTP: config.CommonOptions.PlainHTTP,
76+
SourceDirectory: pkgLayout.GetImageDir(),
77+
ImageList: refs,
78+
NoChecksum: noImgChecksum,
79+
Arch: pkgLayout.Pkg.Build.Architecture,
80+
RegInfo: regInfo,
10381
}
104-
if config.CommonOptions.InsecureSkipTLSVerify {
105-
pushOptions = append(pushOptions, crane.Insecure)
106-
}
107-
108-
for refInfo, img := range images {
109-
err = retry.Do(func() error {
110-
pushImage := func(registryUrl string) error {
111-
names := []string{}
112-
if !noImgChecksum {
113-
offlineNameCRC, err := transform.ImageTransformHost(registryUrl, refInfo.Reference)
114-
if err != nil {
115-
return retry.Unrecoverable(err)
116-
}
117-
names = append(names, offlineNameCRC)
118-
}
119-
offlineName, err := transform.ImageTransformHostWithoutChecksum(registryUrl, refInfo.Reference)
120-
if err != nil {
121-
return retry.Unrecoverable(err)
122-
}
123-
names = append(names, offlineName)
124-
for _, name := range names {
125-
message.Infof("Pushing image %s", name)
126-
l.Info("pushing image", "name", name)
127-
err = crane.Push(img, name, pushOptions...)
128-
if err != nil {
129-
return err
130-
}
131-
}
132-
return nil
133-
}
134-
135-
if !dns.IsServiceURL(regInfo.Address) {
136-
return pushImage(regInfo.Address)
137-
}
138-
139-
if c == nil {
140-
return retry.Unrecoverable(errors.New("cannot push to internal OCI registry when cluster is nil"))
141-
}
142-
namespace, name, port, err := dns.ParseServiceURL(regInfo.Address)
143-
if err != nil {
144-
return err
145-
}
146-
tunnel, err := c.NewTunnel(namespace, cluster.SvcResource, name, "", 0, port)
147-
if err != nil {
148-
return err
149-
}
150-
_, err = tunnel.Connect(ctx)
151-
if err != nil {
152-
return err
153-
}
154-
defer tunnel.Close()
155-
err = tunnel.Wrap(func() error {
156-
return pushImage(tunnel.Endpoint())
157-
})
158-
if err != nil {
159-
return err
160-
}
161-
return nil
162-
}, retry.Context(ctx), retry.Attempts(uint(retries)), retry.Delay(500*time.Millisecond))
163-
if err != nil {
164-
return err
165-
}
82+
err = images.Push(ctx, pushCfg)
83+
if err != nil {
84+
return fmt.Errorf("failed to mirror images: %w", err)
16685
}
16786
return nil
16887
}

0 commit comments

Comments
 (0)