github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/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  	"github.com/sealerio/sealer/common"
    22  	"github.com/sealerio/sealer/pkg/image/save"
    23  	v1 "github.com/sealerio/sealer/types/api/v1"
    24  )
    25  
    26  type ImageSection struct {
    27  	Registry string   `json:"registry,omitempty"`
    28  	Username string   `json:"username,omitempty"`
    29  	Password string   `json:"password,omitempty"`
    30  	Images   []string `json:"images,omitempty"`
    31  }
    32  
    33  type MiddlewarePuller struct {
    34  	puller   save.DefaultImageSaver
    35  	platform v1.Platform
    36  }
    37  
    38  func NewMiddlewarePuller(platform v1.Platform) MiddlewarePuller {
    39  	return MiddlewarePuller{
    40  		platform: platform,
    41  		puller:   save.DefaultImageSaver{},
    42  	}
    43  }
    44  
    45  func (m MiddlewarePuller) PullWithImageSection(rootfs string, imageSectionList []ImageSection) error {
    46  	ia := make(save.ImageListWithAuth, 0)
    47  	for _, section := range imageSectionList {
    48  		if len(section.Images) == 0 {
    49  			continue
    50  		}
    51  		if section.Username == "" || section.Password == "" {
    52  			return fmt.Errorf("must set username and password at imageListWithAuth.yaml")
    53  		}
    54  
    55  		domainToImages, err := normalizedImageListWithAuth(section)
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		ia = append(ia, save.Section{
    61  			Registry: section.Registry,
    62  			Username: section.Username,
    63  			Password: section.Password,
    64  			Images:   domainToImages,
    65  		})
    66  	}
    67  
    68  	if len(ia) == 0 {
    69  		return nil
    70  	}
    71  
    72  	return m.puller.SaveImagesWithAuth(ia, filepath.Join(rootfs, common.RegistryDirName), m.platform)
    73  }
    74  
    75  func normalizedImageListWithAuth(sec ImageSection) (map[string][]save.Named, error) {
    76  	domainToImages := make(map[string][]save.Named)
    77  	for _, image := range sec.Images {
    78  		named, err := save.ParseNormalizedNamed(image, sec.Registry)
    79  		if err != nil {
    80  			return nil, fmt.Errorf("parse image name error: %v", err)
    81  		}
    82  		domainToImages[named.Domain()+named.Repo()] = append(domainToImages[named.Domain()+named.Repo()], named)
    83  	}
    84  	return domainToImages, nil
    85  }