github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/build/buildimage/middleware.go (about) 1 // Copyright © 2022 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package buildimage 16 17 import ( 18 "fmt" 19 "path/filepath" 20 21 v1 "github.com/alibaba/sealer/types/api/v1" 22 23 "github.com/alibaba/sealer/common" 24 "github.com/alibaba/sealer/pkg/image/save" 25 "github.com/alibaba/sealer/utils" 26 ) 27 28 var ( 29 imageListWithAuth = "imageListWithAuth.yaml" 30 ) 31 32 type ImageSection struct { 33 Registry string `json:"registry,omitempty"` 34 Username string `json:"username,omitempty"` 35 Password string `json:"password,omitempty"` 36 Images []string `json:"images,omitempty"` 37 } 38 39 type MiddlewarePuller struct { 40 puller save.DefaultImageSaver 41 platform v1.Platform 42 } 43 44 func (m MiddlewarePuller) Process(context, rootfs string) error { 45 //read the filePath named "imageListWithAuth.yaml" if not exists just return; 46 //pares the images and save to rootfs 47 filePath := filepath.Join(context, imageListWithAuth) 48 if !utils.IsExist(filePath) { 49 return nil 50 } 51 52 // pares middleware file: imageListWithAuth.yaml 53 var imageSection []ImageSection 54 err := utils.UnmarshalYamlFile(filePath, &imageSection) 55 if err != nil { 56 return err 57 } 58 59 ia := make(save.ImageListWithAuth, 0) 60 for _, section := range imageSection { 61 if len(section.Images) == 0 { 62 continue 63 } 64 if section.Username == "" || section.Password == "" { 65 return fmt.Errorf("must set username and password at imageListWithAuth.yaml") 66 } 67 68 domainToImages, err := normalizedImageListWithAuth(section) 69 if err != nil { 70 return err 71 } 72 73 ia = append(ia, save.Section{ 74 Registry: section.Registry, 75 Username: section.Username, 76 Password: section.Password, 77 Images: domainToImages, 78 }) 79 } 80 81 if len(ia) == 0 { 82 return nil 83 } 84 85 return m.puller.SaveImagesWithAuth(ia, filepath.Join(rootfs, common.RegistryDirName), m.platform) 86 } 87 88 func normalizedImageListWithAuth(sec ImageSection) (map[string][]save.Named, error) { 89 domainToImages := make(map[string][]save.Named) 90 for _, image := range sec.Images { 91 named, err := save.ParseNormalizedNamed(image, sec.Registry) 92 if err != nil { 93 return nil, fmt.Errorf("parse image name error: %v", err) 94 } 95 domainToImages[named.Domain()+named.Repo()] = append(domainToImages[named.Domain()+named.Repo()], named) 96 } 97 return domainToImages, nil 98 } 99 100 func NewMiddlewarePuller(platform v1.Platform) Differ { 101 return MiddlewarePuller{ 102 platform: platform, 103 puller: save.DefaultImageSaver{}, 104 } 105 }