88 "net/http"
99 "os"
1010 "path/filepath"
11+ "strings"
1112 "time"
1213
1314 "github.com/flashbots/gh-artifacts-sync/job"
@@ -30,9 +31,9 @@ func (s *Server) downloadGithubContainer(
3031
3132 var downloadsDir string
3233 { // create container downloads dir
33- downloadsDir = filepath .Join (
34- s .cfg .Dir .Downloads , j .GetRepoOwner (), j .GetRepo (), "containers" , j .GetPackageName (), j .GetTag (),
35- )
34+ downloadsDir = strings . ReplaceAll ( filepath .Join (
35+ s .cfg .Dir .Downloads , j .GetRepoOwner (), j .GetRepo (), "containers" , j .GetPackageName (), j .GetDigest (),
36+ ), ":" , "-" )
3637 if err := os .MkdirAll (downloadsDir , 0750 ); err != nil {
3738 return "" , fmt .Errorf ("failed to create container download directory: %s: %w" ,
3839 downloadsDir , err ,
@@ -89,7 +90,7 @@ func (s *Server) downloadGithubContainer(
8990 desc = _desc
9091 }
9192
92- var images = make (map [string ]cr.Image )
93+ var images = make (map [string ][] cr.Image )
9394 { // get images
9495 switch {
9596 case desc .MediaType .IsImage ():
@@ -98,16 +99,21 @@ func (s *Server) downloadGithubContainer(
9899 return "" , fmt .Errorf ("failed to retrieve container image: %w" , err )
99100 }
100101
101- platform := ""
102+ platform := "unknown/unknown "
102103 if desc .Platform != nil {
103104 platform = desc .Platform .String ()
104105 }
105-
106- if _ , exists := images [platform ]; exists {
107- return "" , fmt .Errorf ("invalid container image: duplicate platform: %s" , platform )
106+ if _ , exists := images [platform ]; ! exists {
107+ images [platform ] = make ([]cr.Image , 0 )
108108 }
109109
110- images [platform ] = image
110+ images [platform ] = append (images [platform ], image )
111+
112+ l .Debug ("Downloaded a manifest" ,
113+ zap .String ("digest" , desc .Digest .String ()),
114+ zap .String ("platform" , platform ),
115+ zap .Any ("annotations" , desc .Annotations ),
116+ )
111117
112118 case desc .MediaType .IsIndex ():
113119 index , err := crremote .Index (ref , crremote .WithAuth (auth ))
@@ -122,24 +128,39 @@ func (s *Server) downloadGithubContainer(
122128 )
123129 }
124130
131+ l .Debug ("Downloaded an index" ,
132+ zap .String ("digest" , desc .Digest .String ()),
133+ zap .String ("reference" , ref .Name ()),
134+ zap .Any ("annotations" , indexManifest .Annotations ),
135+ )
136+
125137 for _ , desc := range indexManifest .Manifests {
126- if ! desc .MediaType .IsImage () || desc . Platform == nil {
138+ if ! desc .MediaType .IsImage () {
127139 continue
128140 }
129141
130- platform := desc .Platform .String ()
131- if _ , exists := images [platform ]; exists {
132- return "" , fmt .Errorf ("invalid container image: duplicate platform: %s" , platform )
133- }
134-
135142 image , err := index .Image (desc .Digest )
136143 if err != nil {
137144 return "" , fmt .Errorf ("failed to get image from an index: %s: %s: %w" ,
138145 j .GetPackageUrl (), desc .Digest , err ,
139146 )
140147 }
141148
142- images [platform ] = image
149+ platform := "unknown/unknown"
150+ if desc .Platform != nil {
151+ platform = desc .Platform .String ()
152+ }
153+ if _ , exists := images [platform ]; ! exists {
154+ images [platform ] = make ([]cr.Image , 0 )
155+ }
156+
157+ images [platform ] = append (images [platform ], image )
158+
159+ l .Debug ("Downloaded a manifest" ,
160+ zap .String ("digest" , desc .Digest .String ()),
161+ zap .String ("platform" , platform ),
162+ zap .Any ("annotations" , desc .Annotations ),
163+ )
143164 }
144165 }
145166 }
@@ -167,35 +188,35 @@ func (s *Server) downloadGithubContainer(
167188 zipper := zip .NewWriter (file )
168189 defer zipper .Close ()
169190
170- for platform , image := range images {
171- digest , err := image .Digest ()
172- if err != nil {
173- return "" , fmt .Errorf ("failed to get image digest: %s: %w" ,
174- j .GetPackageUrl (), err ,
175- )
176- }
191+ for platform , _images := range images {
192+ for _ , image := range _images {
193+ digest , err := image .Digest ()
194+ if err != nil {
195+ return "" , fmt .Errorf ("failed to get image digest: %s: %w" ,
196+ j .GetPackageUrl (), err ,
197+ )
198+ }
177199
178- stream , err := zipper .Create (filepath .Join (platform , digest .Hex + ".tar" ))
179- if err != nil {
180- return "" , fmt .Errorf ("failed to create add container tarball to file: %w" , err )
181- }
200+ stream , err := zipper .Create (filepath .Join (platform , digest .Hex + ".tar" ))
201+ if err != nil {
202+ return "" , fmt .Errorf ("failed to create add container tarball to file: %w" , err )
203+ }
182204
183- l .Debug ("Downloading container image..." ,
184- zap .String ("digest" , digest .String ()),
185- zap .String ("platform" , platform ),
186- )
205+ l .Debug ("Archiving container manifest..." ,
206+ zap .String ("digest" , digest .String ()),
207+ )
187208
188- start := time .Now ()
209+ start := time .Now ()
189210
190- if err := crtarball .Write (ref , image , stream ); err != nil {
191- return "" , fmt .Errorf ("failed to write container tarball: %w" , err )
192- }
211+ if err := crtarball .Write (ref , image , stream ); err != nil {
212+ return "" , fmt .Errorf ("failed to write container tarball: %w" , err )
213+ }
193214
194- l .Info ("Downloaded a container image " ,
195- zap .String ("digest" , digest .String ()),
196- zap .String ( "platform " , platform ),
197- zap . Duration ( "duration" , time . Since ( start )),
198- )
215+ l .Info ("Archived a container manifest " ,
216+ zap .String ("digest" , digest .String ()),
217+ zap .Duration ( "duration " , time . Since ( start ) ),
218+ )
219+ }
199220 }
200221 }
201222
0 commit comments