11package main
22
33import (
4+ "archive/tar"
5+ "bytes"
46 "flag"
57 "fmt"
8+ "github.com/google/go-containerregistry/pkg/v1/empty"
9+ "github.com/google/go-containerregistry/pkg/v1/tarball"
10+ "io"
611 "io/fs"
712 "log"
813 "os"
14+ "path/filepath"
15+ "sort"
916 "strings"
1017
1118 "github.com/google/go-containerregistry/pkg/crane"
12- v1 "github.com/google/go-containerregistry/pkg/v1"
19+ "github.com/google/go-containerregistry/pkg/v1"
1320 "github.com/google/go-containerregistry/pkg/v1/mutate"
1421 "github.com/spf13/pflag"
1522 "gopkg.in/yaml.v2"
1623)
1724
1825const (
19- bundlesSubPath string = "bundles"
20- catalogsSubPath string = "catalogs"
26+ controllersSubPath string = "controllers"
27+ bundlesSubPath string = "bundles"
28+ catalogsSubPath string = "catalogs"
2129)
2230
2331func main () {
@@ -34,6 +42,7 @@ func main() {
3442
3543 bundlesFullPath := fmt .Sprintf ("%s/%s" , imagesPath , bundlesSubPath )
3644 catalogsFullPath := fmt .Sprintf ("%s/%s" , imagesPath , catalogsSubPath )
45+ controllersFullPath := fmt .Sprintf ("%s/%s" , imagesPath , controllersSubPath )
3746
3847 bundles , err := buildBundles (bundlesFullPath )
3948 if err != nil {
@@ -43,6 +52,10 @@ func main() {
4352 if err != nil {
4453 log .Fatalf ("failed to build catalogs: %s" , err .Error ())
4554 }
55+ controllers , err := buildControllers (controllersFullPath )
56+ if err != nil {
57+ log .Fatalf ("failed to build controllers: %s" , err .Error ())
58+ }
4659 // Push the images
4760 for name , image := range bundles {
4861 dest := fmt .Sprintf ("%s/%s" , registryAddr , name )
@@ -58,6 +71,13 @@ func main() {
5871 log .Fatalf ("failed to push catalog images: %s" , err .Error ())
5972 }
6073 }
74+ for name , image := range controllers {
75+ dest := fmt .Sprintf ("%s/%s" , registryAddr , name )
76+ log .Printf ("pushing controller %s to %s" , name , dest )
77+ if err := crane .Push (image , dest ); err != nil {
78+ log .Fatalf ("failed to push controller images: %s" , err .Error ())
79+ }
80+ }
6181 log .Printf ("finished" )
6282 os .Exit (0 )
6383}
@@ -122,6 +142,27 @@ func buildCatalogs(path string) (map[string]v1.Image, error) {
122142 return mutatedMap , nil
123143}
124144
145+ func buildControllers (path string ) (map [string ]v1.Image , error ) {
146+ controllers , err := processImageDirTree (path )
147+ if err != nil {
148+ return nil , err
149+ }
150+ mutatedMap := make (map [string ]v1.Image , 0 )
151+ // Apply required catalog label
152+ for key , img := range controllers {
153+ cfg := v1.Config {
154+ WorkingDir : "/" ,
155+ Entrypoint : []string {"/manager" },
156+ User : "65532:65532" ,
157+ }
158+ mutatedMap [fmt .Sprintf ("controllers/%s" , key )], err = mutate .Config (img , cfg )
159+ if err != nil {
160+ return nil , fmt .Errorf ("failed to apply image labels: %w" , err )
161+ }
162+ }
163+ return mutatedMap , nil
164+ }
165+
125166func processImageDirTree (path string ) (map [string ]v1.Image , error ) {
126167 imageMap := make (map [string ]v1.Image , 0 )
127168 images , err := os .ReadDir (path )
@@ -145,38 +186,66 @@ func processImageDirTree(path string) (map[string]v1.Image, error) {
145186 continue
146187 }
147188 tagFullPath := fmt .Sprintf ("%s/%s" , entryFullPath , tag .Name ())
189+ b := & bytes.Buffer {}
190+ w := tar .NewWriter (b )
148191
149- var fileMap map [string ][]byte
150- fileMap , err = createFileMap (tagFullPath )
192+ files , err := collectFiles (tagFullPath )
151193 if err != nil {
152194 return nil , fmt .Errorf ("failed to read files for image: %w" , err )
153195 }
196+ sort .Strings (files )
197+
198+ for _ , f := range files {
199+ filePath := filepath .Join (tagFullPath , f )
200+ fileBytes , err := os .ReadFile (filePath )
201+ if err != nil {
202+ return nil , fmt .Errorf ("failed to read file %q for image: %w" , filePath , err )
203+ }
204+ if err := w .WriteHeader (& tar.Header {
205+ Name : f ,
206+ Mode : 0755 ,
207+ Size : int64 (len (fileBytes )),
208+ }); err != nil {
209+ return nil , err
210+ }
211+ if _ , err := w .Write (fileBytes ); err != nil {
212+ return nil , err
213+ }
214+ }
215+ if err := w .Close (); err != nil {
216+ return nil , err
217+ }
218+
219+ // Return a new copy of the buffer each time it's opened.
220+ layer , err := tarball .LayerFromOpener (func () (io.ReadCloser , error ) {
221+ return io .NopCloser (bytes .NewBuffer (b .Bytes ())), nil
222+ })
223+ if err != nil {
224+ return nil , fmt .Errorf ("failed to create image layer: %w" , err )
225+ }
154226
155- image , err := crane . Image ( fileMap )
227+ image , err := mutate . AppendLayers ( empty . Image , layer )
156228 if err != nil {
157- return nil , fmt .Errorf ("failed to generate image: %w" , err )
229+ return nil , fmt .Errorf ("failed to append layer to image: %w" , err )
158230 }
159231 imageMap [fmt .Sprintf ("%s:%s" , entry .Name (), tag .Name ())] = image
160232 }
161233 }
162234 return imageMap , nil
163235}
164236
165- func createFileMap (originPath string ) (map [ string ][] byte , error ) {
166- fileMap := make ( map [ string ][] byte )
237+ func collectFiles (originPath string ) ([] string , error ) {
238+ var files [] string
167239 if err := fs .WalkDir (os .DirFS (originPath ), "." , func (path string , d fs.DirEntry , err error ) error {
168240 if err != nil {
169241 return err
170242 }
171243 if d != nil && ! d .IsDir () {
172- fileMap [path ], err = os .ReadFile (fmt .Sprintf ("%s/%s" , originPath , path ))
173- if err != nil {
174- return err
175- }
244+ files = append (files , path )
176245 }
177246 return nil
178247 }); err != nil {
179248 return nil , err
180249 }
181- return fileMap , nil
250+ return files , nil
182251}
0 commit comments