github.com/containerd/nerdctl@v1.7.7/pkg/composer/pull.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package composer
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  
    24  	"github.com/compose-spec/compose-go/types"
    25  	"github.com/containerd/log"
    26  	"github.com/containerd/nerdctl/pkg/composer/serviceparser"
    27  )
    28  
    29  type PullOptions struct {
    30  	Quiet bool
    31  }
    32  
    33  func (c *Composer) Pull(ctx context.Context, po PullOptions, services []string) error {
    34  	return c.project.WithServices(services, func(svc types.ServiceConfig) error {
    35  		ps, err := serviceparser.Parse(c.project, svc)
    36  		if err != nil {
    37  			return err
    38  		}
    39  		return c.pullServiceImage(ctx, ps.Image, ps.Unparsed.Platform, ps, po)
    40  	})
    41  }
    42  
    43  func (c *Composer) pullServiceImage(ctx context.Context, image string, platform string, ps *serviceparser.Service, po PullOptions) error {
    44  	log.G(ctx).Infof("Pulling image %s", image)
    45  
    46  	var args []string // nolint: prealloc
    47  	if platform != "" {
    48  		args = append(args, "--platform="+platform)
    49  	}
    50  	if po.Quiet {
    51  		args = append(args, "--quiet")
    52  	}
    53  	if verifier, ok := ps.Unparsed.Extensions[serviceparser.ComposeVerify]; ok {
    54  		args = append(args, "--verify="+verifier.(string))
    55  	}
    56  	if publicKey, ok := ps.Unparsed.Extensions[serviceparser.ComposeCosignPublicKey]; ok {
    57  		args = append(args, "--cosign-key="+publicKey.(string))
    58  	}
    59  	if certificateIdentity, ok := ps.Unparsed.Extensions[serviceparser.ComposeCosignCertificateIdentity]; ok {
    60  		args = append(args, "--cosign-certificate-identity="+certificateIdentity.(string))
    61  	}
    62  	if certificateIdentityRegexp, ok := ps.Unparsed.Extensions[serviceparser.ComposeCosignCertificateIdentityRegexp]; ok {
    63  		args = append(args, "--cosign-certificate-identity-regexp="+certificateIdentityRegexp.(string))
    64  	}
    65  	if certificateOidcIssuer, ok := ps.Unparsed.Extensions[serviceparser.ComposeCosignCertificateOidcIssuer]; ok {
    66  		args = append(args, "--cosign-certificate-oidc-issuer="+certificateOidcIssuer.(string))
    67  	}
    68  	if certificateOidcIssuerRegexp, ok := ps.Unparsed.Extensions[serviceparser.ComposeCosignCertificateOidcIssuerRegexp]; ok {
    69  		args = append(args, "--cosign-certificate-oidc-issuer-regexp="+certificateOidcIssuerRegexp.(string))
    70  	}
    71  
    72  	if c.Options.Experimental {
    73  		args = append(args, "--experimental")
    74  	}
    75  
    76  	args = append(args, image)
    77  
    78  	cmd := c.createNerdctlCmd(ctx, append([]string{"pull"}, args...)...)
    79  	if c.DebugPrintFull {
    80  		log.G(ctx).Debugf("Running %v", cmd.Args)
    81  	}
    82  	cmd.Stdin = os.Stdin
    83  	cmd.Stdout = os.Stdout
    84  	cmd.Stderr = os.Stderr
    85  	if err := cmd.Run(); err != nil {
    86  		return fmt.Errorf("error while pulling image %s: %w", image, err)
    87  	}
    88  	return nil
    89  }