github.com/containerd/nerdctl@v1.7.7/pkg/composer/push.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 PushOptions struct {
    30  }
    31  
    32  func (c *Composer) Push(ctx context.Context, po PushOptions, services []string) error {
    33  	return c.project.WithServices(services, func(svc types.ServiceConfig) error {
    34  		ps, err := serviceparser.Parse(c.project, svc)
    35  		if err != nil {
    36  			return err
    37  		}
    38  		return c.pushServiceImage(ctx, ps.Image, ps.Unparsed.Platform, ps, po)
    39  	})
    40  }
    41  
    42  func (c *Composer) pushServiceImage(ctx context.Context, image string, platform string, ps *serviceparser.Service, po PushOptions) error {
    43  	log.G(ctx).Infof("Pushing image %s", image)
    44  
    45  	var args []string // nolint: prealloc
    46  	if platform != "" {
    47  		args = append(args, "--platform="+platform)
    48  	}
    49  	if signer, ok := ps.Unparsed.Extensions[serviceparser.ComposeSign]; ok {
    50  		args = append(args, "--sign="+signer.(string))
    51  	}
    52  	if privateKey, ok := ps.Unparsed.Extensions[serviceparser.ComposeCosignPrivateKey]; ok {
    53  		args = append(args, "--cosign-key="+privateKey.(string))
    54  	}
    55  	if c.Options.Experimental {
    56  		args = append(args, "--experimental")
    57  	}
    58  
    59  	args = append(args, image)
    60  
    61  	cmd := c.createNerdctlCmd(ctx, append([]string{"push"}, args...)...)
    62  	if c.DebugPrintFull {
    63  		log.G(ctx).Debugf("Running %v", cmd.Args)
    64  	}
    65  	cmd.Stdin = os.Stdin
    66  	cmd.Stdout = os.Stdout
    67  	cmd.Stderr = os.Stderr
    68  	if err := cmd.Run(); err != nil {
    69  		return fmt.Errorf("error while pushing image %s: %w", image, err)
    70  	}
    71  	return nil
    72  }