github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/push.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 buildah
    16  
    17  import (
    18  	"os"
    19  	"strings"
    20  
    21  	"github.com/containers/buildah"
    22  	"github.com/containers/buildah/define"
    23  	"github.com/containers/buildah/util"
    24  	"github.com/containers/common/libimage"
    25  	"github.com/containers/common/pkg/auth"
    26  	"github.com/containers/image/v5/manifest"
    27  	"github.com/containers/image/v5/transports"
    28  	"github.com/containers/image/v5/transports/alltransports"
    29  	"github.com/containers/image/v5/types"
    30  	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
    31  	"github.com/pkg/errors"
    32  	"github.com/sealerio/sealer/pkg/define/options"
    33  	"github.com/sirupsen/logrus"
    34  )
    35  
    36  func (engine *Engine) Push(opts *options.PushOptions) error {
    37  	if len(opts.Image) == 0 {
    38  		return errors.New("At least a source image ID must be specified")
    39  	}
    40  	systemCxt := engine.SystemContext()
    41  	if err := auth.CheckAuthFile(systemCxt.AuthFilePath); err != nil {
    42  		return err
    43  	}
    44  	systemCxt.OCIInsecureSkipTLSVerify = opts.SkipTLSVerify
    45  	systemCxt.DockerInsecureSkipTLSVerify = types.NewOptionalBool(opts.SkipTLSVerify)
    46  
    47  	src, destSpec := opts.Image, opts.Image
    48  	if len(opts.Destination) != 0 {
    49  		destSpec = opts.Destination
    50  	}
    51  	compress := define.Gzip
    52  	store := engine.ImageStore()
    53  	dest, err := alltransports.ParseImageName(destSpec)
    54  	// add the docker:// transport to see if they neglected it.
    55  	if err != nil {
    56  		destTransport := strings.Split(destSpec, ":")[0]
    57  		if t := transports.Get(destTransport); t != nil {
    58  			return err
    59  		}
    60  
    61  		if strings.Contains(destSpec, "://") {
    62  			return err
    63  		}
    64  
    65  		destSpec = "docker://" + destSpec
    66  		dest2, err2 := alltransports.ParseImageName(destSpec)
    67  		if err2 != nil {
    68  			return err
    69  		}
    70  		dest = dest2
    71  		logrus.Debugf("Assuming docker:// as the transport method for DESTINATION: %s", destSpec)
    72  	}
    73  
    74  	img, _, err := engine.ImageRuntime().LookupImage(src, &libimage.LookupImageOptions{
    75  		ManifestList: true,
    76  	})
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	isManifest, err := img.IsManifestList(getContext())
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	if isManifest {
    87  		if manifestsErr := engine.PushManifest(src, destSpec, opts); manifestsErr == nil {
    88  			return nil
    89  		}
    90  		return util.GetFailureCause(err, errors.Wrapf(err, "error pushing image %q to %q", src, destSpec))
    91  	}
    92  
    93  	var manifestType string
    94  	if opts.Format != "" {
    95  		switch opts.Format {
    96  		case "oci":
    97  			manifestType = imgspecv1.MediaTypeImageManifest
    98  		case "v2s1":
    99  			manifestType = manifest.DockerV2Schema1SignedMediaType
   100  		case "v2s2", "docker":
   101  			manifestType = manifest.DockerV2Schema2MediaType
   102  		default:
   103  			return errors.Errorf("unknown format %q. Choose one of the supported formats: 'oci', 'v2s1', or 'v2s2'", opts.Format)
   104  		}
   105  	}
   106  
   107  	options := buildah.PushOptions{
   108  		Compression:   compress,
   109  		ManifestType:  manifestType,
   110  		Store:         store,
   111  		SystemContext: systemCxt,
   112  		MaxRetries:    maxPullPushRetries,
   113  		RetryDelay:    pullPushRetryDelay,
   114  	}
   115  	if !opts.Quiet {
   116  		options.ReportWriter = os.Stderr
   117  	}
   118  	ref, digest, err := buildah.Push(getContext(), src, dest, options)
   119  	if err != nil {
   120  		return util.GetFailureCause(err, errors.Wrapf(err, "error pushing image %q to %q", src, destSpec))
   121  	}
   122  	if ref != nil {
   123  		logrus.Debugf("pushed image %q with digest %s", ref, digest.String())
   124  	} else {
   125  		logrus.Debugf("pushed image with digest %s", digest.String())
   126  	}
   127  
   128  	logrus.Infof("successfully pushed %s with digest %s", transports.ImageName(dest), digest.String())
   129  
   130  	return nil
   131  }