k3s/pkg/agent/containerd/containerd.go

299 lines
8.8 KiB
Go
Raw Normal View History

2019-01-01 08:23:01 +00:00
package containerd
import (
"bufio"
2019-01-01 08:23:01 +00:00
"context"
"fmt"
"io"
2019-01-01 08:23:01 +00:00
"os"
"os/exec"
2019-03-02 15:56:27 +00:00
"path/filepath"
"strings"
2019-01-01 08:23:01 +00:00
"time"
2019-03-07 00:34:05 +00:00
"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/leases"
2019-03-07 00:34:05 +00:00
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/pkg/cri/constants"
"github.com/containerd/containerd/reference/docker"
"github.com/k3s-io/k3s/pkg/agent/cri"
util2 "github.com/k3s-io/k3s/pkg/agent/util"
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/version"
"github.com/natefinch/lumberjack"
2020-02-24 20:13:59 +00:00
"github.com/pkg/errors"
"github.com/rancher/wharfie/pkg/tarfile"
"github.com/rancher/wrangler/pkg/merr"
2019-01-01 08:23:01 +00:00
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
Update to v1.26.0-k3s1 (#6370) * Update to v1.26.0-alpha.2 Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * go generate Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Default CURRENT_VERSION to VERSION_TAG for alpha versions Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * remove containerd package Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Update k8s to v1.26.0-rc.0-k3s1 cri-tools cri-dockerd and cadvisor Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * replace cri-api reference to the new api Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * go mod tidy Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Fix version script to allow rc and alphas Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Fix version script to allow rc and alphas Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Fix version script to allow rc and alphas Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Update to Kubernetes 1.26.0-rc.1 Signed-off-by: Brad Davidson <brad.davidson@rancher.com> * Undo helm-controller pin Signed-off-by: Brad Davidson <brad.davidson@rancher.com> * Bump containerd to -k3s2 for stargz fix Signed-off-by: Brad Davidson <brad.davidson@rancher.com> * DevicePlugins featuregate is locked to on Signed-off-by: Brad Davidson <brad.davidson@rancher.com> * Bump kine for DeleteRange fix Signed-off-by: Brad Davidson <brad.davidson@rancher.com> * Update to v1.26.0-k3s1 Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * go mod tidy Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Bring back snapshotter checks and update golang to 1.19.4 Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * fix windows containerd snapshotter checks Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> Signed-off-by: Brad Davidson <brad.davidson@rancher.com> Co-authored-by: Brad Davidson <brad.davidson@rancher.com>
2022-12-09 23:42:15 +00:00
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
2019-01-01 08:23:01 +00:00
)
// Run configures and starts containerd as a child process. Once it is up, images are preloaded
// or pulled from files found in the agent images directory.
2019-01-09 16:54:15 +00:00
func Run(ctx context.Context, cfg *config.Node) error {
2019-04-19 21:08:05 +00:00
if err := setupContainerdConfig(ctx, cfg); err != nil {
2019-01-01 08:23:01 +00:00
return err
}
args := getContainerdArgs(cfg)
stdOut := io.Writer(os.Stdout)
stdErr := io.Writer(os.Stderr)
if cfg.Containerd.Log != "" {
logrus.Infof("Logging containerd to %s", cfg.Containerd.Log)
fileOut := &lumberjack.Logger{
Filename: cfg.Containerd.Log,
MaxSize: 50,
MaxBackups: 3,
MaxAge: 28,
Compress: true,
}
// If k3s is started with --debug, write logs to both the log file and stdout/stderr,
// even if a log path is set.
if cfg.Containerd.Debug {
stdOut = io.MultiWriter(stdOut, fileOut)
stdErr = io.MultiWriter(stdErr, fileOut)
} else {
stdOut = fileOut
stdErr = fileOut
}
2019-01-01 08:23:01 +00:00
}
go func() {
env := []string{}
cenv := []string{}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
switch {
case pair[0] == "NOTIFY_SOCKET":
// elide NOTIFY_SOCKET to prevent spurious notifications to systemd
case pair[0] == "CONTAINERD_LOG_LEVEL":
// Turn CONTAINERD_LOG_LEVEL variable into log-level flag
args = append(args, "--log-level", pair[1])
case strings.HasPrefix(pair[0], "CONTAINERD_"):
// Strip variables with CONTAINERD_ prefix before passing through
// This allows doing things like setting a proxy for image pulls by setting
// CONTAINERD_https_proxy=http://proxy.example.com:8080
pair[0] = strings.TrimPrefix(pair[0], "CONTAINERD_")
cenv = append(cenv, strings.Join(pair, "="))
default:
env = append(env, strings.Join(pair, "="))
}
}
2019-01-09 16:54:15 +00:00
logrus.Infof("Running containerd %s", config.ArgString(args[1:]))
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Stdout = stdOut
cmd.Stderr = stdErr
cmd.Env = append(env, cenv...)
addDeathSig(cmd)
2019-01-01 08:23:01 +00:00
if err := cmd.Run(); err != nil {
logrus.Errorf("containerd exited: %s", err)
2019-01-01 08:23:01 +00:00
}
os.Exit(1)
}()
if err := cri.WaitForService(ctx, cfg.Containerd.Address, "containerd"); err != nil {
return err
}
return preloadImages(ctx, cfg)
}
// preloadImages reads the contents of the agent images directory, and attempts to
// import into containerd any files found there. Supported compressed types are decompressed, and
// any .txt files are processed as a list of images that should be pre-pulled from remote registries.
// If configured, imported images are retagged as being pulled from additional registries.
func preloadImages(ctx context.Context, cfg *config.Node) error {
2019-03-04 20:45:30 +00:00
fileInfo, err := os.Stat(cfg.Images)
2019-03-07 16:47:37 +00:00
if os.IsNotExist(err) {
return nil
} else if err != nil {
2019-03-07 00:34:05 +00:00
logrus.Errorf("Unable to find images in %s: %v", cfg.Images, err)
return nil
}
if !fileInfo.IsDir() {
return nil
}
fileInfos, err := os.ReadDir(cfg.Images)
2019-03-07 00:34:05 +00:00
if err != nil {
logrus.Errorf("Unable to read images in %s: %v", cfg.Images, err)
return nil
}
client, err := Client(cfg.Containerd.Address)
2019-03-07 00:34:05 +00:00
if err != nil {
return err
}
defer client.Close()
criConn, err := cri.Connection(ctx, cfg.Containerd.Address)
if err != nil {
return err
}
defer criConn.Close()
// Ensure that our images are imported into the correct namespace
ctx = namespaces.WithNamespace(ctx, constants.K8sContainerdNamespace)
// At startup all leases from k3s are cleared
ls := client.LeasesService()
existingLeases, err := ls.List(ctx)
if err != nil {
return err
}
for _, lease := range existingLeases {
if lease.ID == version.Program {
logrus.Debugf("Deleting existing lease: %v", lease)
ls.Delete(ctx, lease)
}
}
// Any images found on import are given a lease that never expires
lease, err := ls.Create(ctx, leases.WithID(version.Program))
if err != nil {
return err
}
2019-03-07 00:34:05 +00:00
// Ensure that our images are locked by the lease
ctx = leases.WithLease(ctx, lease.ID)
2019-03-07 00:34:05 +00:00
for _, fileInfo := range fileInfos {
2019-03-02 15:56:27 +00:00
if fileInfo.IsDir() {
2019-03-07 00:34:05 +00:00
continue
2019-03-02 15:56:27 +00:00
}
start := time.Now()
2019-03-07 00:34:05 +00:00
filePath := filepath.Join(cfg.Images, fileInfo.Name())
if err := preloadFile(ctx, cfg, client, criConn, filePath); err != nil {
logrus.Errorf("Error encountered while importing %s: %v", filePath, err)
2019-03-07 00:34:05 +00:00
continue
}
logrus.Infof("Imported images from %s in %s", filePath, time.Since(start))
}
return nil
}
2019-03-07 00:34:05 +00:00
// preloadFile handles loading images from a single tarball or pre-pull image list.
// This is in its own function so that we can ensure that the various readers are properly closed, as some
// decompressing readers need to be explicitly closed and others do not.
func preloadFile(ctx context.Context, cfg *config.Node, client *containerd.Client, criConn *grpc.ClientConn, filePath string) error {
if util2.HasSuffixI(filePath, ".txt") {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
logrus.Infof("Pulling images from %s", filePath)
return prePullImages(ctx, criConn, file)
}
opener, err := tarfile.GetOpener(filePath)
if err != nil {
return err
}
imageReader, err := opener()
if err != nil {
return err
}
defer imageReader.Close()
logrus.Infof("Importing images from %s", filePath)
images, err := client.Import(ctx, imageReader, containerd.WithAllPlatforms(true))
if err != nil {
return err
}
return retagImages(ctx, client, images, cfg.AgentConfig.AirgapExtraRegistry)
}
// retagImages retags all listed images as having been pulled from the given remote registries.
// If duplicate images exist, they are overwritten. This is most useful when using a private registry
// for all images, as can be configured by the RKE2/Rancher system-default-registry setting.
func retagImages(ctx context.Context, client *containerd.Client, images []images.Image, registries []string) error {
var errs []error
imageService := client.ImageService()
for _, image := range images {
name, err := parseNamedTagged(image.Name)
2019-03-07 00:34:05 +00:00
if err != nil {
errs = append(errs, errors.Wrap(err, "failed to parse image name"))
continue
}
logrus.Infof("Imported %s", image.Name)
for _, registry := range registries {
image.Name = fmt.Sprintf("%s/%s:%s", registry, docker.Path(name), name.Tag())
if _, err = imageService.Create(ctx, image); err != nil {
if errdefs.IsAlreadyExists(err) {
if err = imageService.Delete(ctx, image.Name); err != nil {
errs = append(errs, errors.Wrap(err, "failed to delete existing image"))
continue
}
if _, err = imageService.Create(ctx, image); err != nil {
errs = append(errs, errors.Wrap(err, "failed to tag after deleting existing image"))
continue
}
} else {
errs = append(errs, errors.Wrap(err, "failed to tag image"))
continue
}
}
logrus.Infof("Tagged %s", image.Name)
2019-03-07 00:34:05 +00:00
}
}
return merr.NewErrors(errs...)
2019-01-01 08:23:01 +00:00
}
2019-04-19 21:08:05 +00:00
// parseNamedTagged parses and normalizes an image name, and converts the resulting reference
// to a type that exposes the tag.
func parseNamedTagged(name string) (docker.NamedTagged, error) {
ref, err := docker.ParseNormalizedNamed(name)
if err != nil {
return nil, err
}
tagged, ok := ref.(docker.NamedTagged)
if !ok {
return nil, fmt.Errorf("can't cast %T to NamedTagged", ref)
}
return tagged, nil
}
// prePullImages asks containerd to pull images in a given list, so that they
// are ready when the containers attempt to start later.
func prePullImages(ctx context.Context, conn *grpc.ClientConn, images io.Reader) error {
imageClient := runtimeapi.NewImageServiceClient(conn)
scanner := bufio.NewScanner(images)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
resp, err := imageClient.ImageStatus(ctx, &runtimeapi.ImageStatusRequest{
Image: &runtimeapi.ImageSpec{
Image: line,
},
})
if err == nil && resp.Image != nil {
continue
}
logrus.Infof("Pulling image %s...", line)
_, err = imageClient.PullImage(ctx, &runtimeapi.PullImageRequest{
Image: &runtimeapi.ImageSpec{
Image: line,
},
})
if err != nil {
logrus.Errorf("Failed to pull %s: %v", line, err)
}
}
return nil
}