Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cmd/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package app

import (
"context"
metrics "github.com/labring/cri-shim/pkg/metric"
"go.opentelemetry.io/otel"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/spf13/cobra"
"go.opentelemetry.io/otel"

imageutil "github.com/labring/cri-shim/pkg/image"
metrics "github.com/labring/cri-shim/pkg/metric"
"github.com/labring/cri-shim/pkg/server"
"github.com/labring/cri-shim/pkg/types"
"github.com/spf13/cobra"
)

var cfg *types.Config
Expand All @@ -39,6 +40,7 @@ func run(cfg *types.Config) {
CRISocket: cfg.RuntimeSocket,
ContainerdNamespace: cfg.ContainerdNamespace,
PoolSize: cfg.PoolSize,
Estargz: cfg.Estargz,
MetricFlag: cfg.MetricsConfig.Metric,
},
imageutil.RegistryOptions{
Expand Down
4 changes: 3 additions & 1 deletion pkg/image/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ImageInterface interface {
Pull(context.Context, string, string, string) error
Commit(ctx context.Context, imageName, containerID string, pause bool) error
Login(ctx context.Context, serverAddress, username, password string) error
ConvertToEstargz(ctx context.Context, srcRawRef, destRawRef string) error
Squash(ctx context.Context, SourceImageRef, TargetImageName string) error
Remove(ctx context.Context, args string, force, async bool) error
Tag(ctx context.Context, src, dest string) error
Expand Down Expand Up @@ -94,11 +95,12 @@ func (impl *imageInterfaceImpl) Commit(ctx context.Context, imageName, container
// convert converts an image to the specified format
// srcRawRef: the source image reference
// destRawRef: the destination image reference
func (impl *imageInterfaceImpl) convert(ctx context.Context, srcRawRef, destRawRef string) error {
func (impl *imageInterfaceImpl) ConvertToEstargz(ctx context.Context, srcRawRef, destRawRef string) error {
slog.Info("Converting image", "Source", srcRawRef, "Destination", destRawRef)
opt := types.ImageConvertOptions{
GOptions: impl.GlobalOptions,
Oci: true,
Estargz: true,
Stdout: impl.Stdout,
}
return image.Convert(ctx, impl.Client, srcRawRef, destRawRef, opt)
Expand Down
26 changes: 19 additions & 7 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package server

import (
"context"
"go.opentelemetry.io/otel/metric"
"log/slog"
"net"
"os"
"runtime"
"strings"
"time"

"go.opentelemetry.io/otel/metric"

"github.com/labring/cri-shim/pkg/container"
imageutil "github.com/labring/cri-shim/pkg/image"
netutil "github.com/labring/cri-shim/pkg/net"
Expand Down Expand Up @@ -38,6 +39,7 @@ type Options struct {
// PoolSize is the size of the pool of goroutines.
PoolSize int

Estargz bool
MetricFlag bool
}

Expand Down Expand Up @@ -416,8 +418,8 @@ func (s *Server) CommitContainer(task types.Task) error {
if err = s.imageClient.Pull(ctx, info.ImageRef, registry.UserName, registry.Password); err != nil {
slog.Error("failed to pull image", "image name", imageName, "error", err)
}

if err := s.imageClient.Commit(ctx, initialImageName, statusResp.Status.Id, false); err != nil {
currentImageName := initialImageName
if err := s.imageClient.Commit(ctx, currentImageName, statusResp.Status.Id, false); err != nil {
slog.Error("failed to commit container after retries", "containerId", statusResp.Status.Id, "image name", initialImageName, "error", err)
s.pool.SetCommitStatus(task.ContainerID, types.ErrorCommit)
if s.options.MetricFlag {
Expand All @@ -433,17 +435,27 @@ func (s *Server) CommitContainer(task types.Task) error {
defer s.imageClient.Remove(ctx, initialImageName, false, false)

if info.SquashEnabled {
if err = s.imageClient.Squash(ctx, initialImageName, imageName); err != nil {
slog.Info("squash enabled, start squash image")
if err = s.imageClient.Squash(ctx, currentImageName, currentImageName+"-squashed"); err != nil {
slog.Error("failed to squash image", "image name", imageName, "error", err)
s.pool.SetCommitStatus(task.ContainerID, types.ErrorCommit)
return err
}
} else {
if err = s.imageClient.Tag(ctx, initialImageName, imageName); err != nil {
slog.Error("failed to tag image", "image name", imageName, "error", err)
currentImageName = currentImageName + "-squashed"
}
if s.options.Estargz {
slog.Info("estargz enabled, start convert to estargz image")
if err = s.imageClient.ConvertToEstargz(ctx, currentImageName, currentImageName+"-converted"); err != nil {
slog.Error("failed to convert image to estargz", "image name", imageName, "error", err)
s.pool.SetCommitStatus(task.ContainerID, types.ErrorCommit)
return err
}
currentImageName = currentImageName + "-converted"
}
if err = s.imageClient.Tag(ctx, currentImageName, imageName); err != nil {
slog.Error("failed to tag image", "image name", imageName, "error", err)
s.pool.SetCommitStatus(task.ContainerID, types.ErrorCommit)
return err
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
GlobalRegistryRepo string
ContainerdNamespace string
PoolSize int
Estargz bool
Debug bool
Trace bool
MetricsConfig MetricsConfig
Expand Down Expand Up @@ -48,6 +49,7 @@ func BindOptions(cmd *cobra.Command) *Config {
cmd.Flags().IntVar(&cfg.PoolSize, "pool-size", 10, "Pool size")
cmd.Flags().BoolVar(&cfg.Debug, "debug", false, "enable debug logging")
cmd.Flags().BoolVar(&cfg.Trace, "trace", false, "enable pprof to trace")
cmd.Flags().BoolVar(&cfg.Estargz, "estargz", false, "enable estargz image")
cmd.Flags().BoolVar(&cfg.MetricsConfig.Metric, "metric", false, "enable otel to metric")
cmd.Flags().StringVar(&cfg.MetricsConfig.Endpoint, "metric-endpoint", "localhost:8428", "VictoriaMetrics endpoint - host:port")
cmd.Flags().StringVar(&cfg.MetricsConfig.IngestPath, "metric-ingestPath", "/opentelemetry/api/v1/push", "url path for ingestion path")
Expand Down