github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/container/run_runtime.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 container
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/containerd/containerd"
    24  	"github.com/containerd/containerd/containers"
    25  	"github.com/containerd/containerd/oci"
    26  	"github.com/containerd/containerd/plugin"
    27  	runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
    28  	"github.com/containerd/log"
    29  	"github.com/opencontainers/runtime-spec/specs-go"
    30  )
    31  
    32  func generateRuntimeCOpts(cgroupManager, runtimeStr string) ([]containerd.NewContainerOpts, error) {
    33  	runtime := plugin.RuntimeRuncV2
    34  	var (
    35  		runcOpts    runcoptions.Options
    36  		runtimeOpts interface{} = &runcOpts
    37  	)
    38  	if cgroupManager == "systemd" {
    39  		runcOpts.SystemdCgroup = true
    40  	}
    41  	if runtimeStr != "" {
    42  		if strings.HasPrefix(runtimeStr, "io.containerd.") || runtimeStr == "wtf.sbk.runj.v1" {
    43  			runtime = runtimeStr
    44  			if !strings.HasPrefix(runtimeStr, "io.containerd.runc.") {
    45  				if cgroupManager == "systemd" {
    46  					log.L.Warnf("cannot set cgroup manager to %q for runtime %q", cgroupManager, runtimeStr)
    47  				}
    48  				runtimeOpts = nil
    49  			}
    50  		} else {
    51  			// runtimeStr is a runc binary
    52  			runcOpts.BinaryName = runtimeStr
    53  		}
    54  	}
    55  	o := containerd.WithRuntime(runtime, runtimeOpts)
    56  	return []containerd.NewContainerOpts{o}, nil
    57  }
    58  
    59  // WithSysctls sets the provided sysctls onto the spec
    60  func WithSysctls(sysctls map[string]string) oci.SpecOpts {
    61  	return func(ctx context.Context, client oci.Client, c *containers.Container, s *specs.Spec) error {
    62  		if s.Linux == nil {
    63  			s.Linux = &specs.Linux{}
    64  		}
    65  		if s.Linux.Sysctl == nil {
    66  			s.Linux.Sysctl = make(map[string]string)
    67  		}
    68  		for k, v := range sysctls {
    69  			s.Linux.Sysctl[k] = v
    70  		}
    71  		return nil
    72  	}
    73  }