Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ mock-criostorage: ${MOCKGEN}
${MOCKGEN} \
-package criostoragemock \
-destination ${MOCK_PATH}/criostorage/criostorage.go \
github.com/cri-o/cri-o/internal/storage ImageServer,RuntimeServer
github.com/cri-o/cri-o/internal/storage ImageServer,ImageServerList,RuntimeServer

mock-lib-config: ${MOCKGEN}
${MOCKGEN} \
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/opencontainers/runtime-spec v1.1.0-rc.1
github.com/opencontainers/runtime-tools v0.9.1-0.20230110161035-a6a073817ab0
github.com/opencontainers/selinux v1.11.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.14.0
github.com/psampaz/go-mod-outdated v0.9.0
github.com/seccomp/libseccomp-golang v0.10.0
Expand Down Expand Up @@ -305,7 +306,6 @@ require (
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pjbgf/sha1cd v0.2.3 // indirect
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/sftp v1.13.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
Expand Down
19 changes: 12 additions & 7 deletions internal/factory/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func (s *sandbox) SetConfig(config *types.PodSandboxConfig) error {
return nil
}

// ConstructSandboxNameFromConfig returns the pod sandbox name from the configuration
func ConstructSandboxNameFromConfig(config *types.PodSandboxConfig) string {
return strings.Join([]string{
"k8s",
config.Metadata.Name,
config.Metadata.Namespace,
config.Metadata.Uid,
fmt.Sprintf("%d", config.Metadata.Attempt),
}, "_")
}

// SetNameAndID sets the sandbox name and ID
func (s *sandbox) SetNameAndID() error {
if s.config == nil {
Expand All @@ -109,13 +120,7 @@ func (s *sandbox) SetNameAndID() error {
}

s.id = stringid.GenerateNonCryptoID()
s.name = strings.Join([]string{
"k8s",
s.config.Metadata.Name,
s.config.Metadata.Namespace,
s.config.Metadata.Uid,
fmt.Sprintf("%d", s.config.Metadata.Attempt),
}, "_")
s.name = ConstructSandboxNameFromConfig(s.config)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (c *ContainerServer) exportCheckpoint(ctx context.Context, ctr *oci.Contain
if err != nil {
return fmt.Errorf("error exporting root file-system diff for %q: %w", id, err)
}
mountPoint, err := c.StorageImageServer().GetStore().Mount(id, specgen.Linux.MountLabel)
mountPoint, err := c.StorageImageServer(ctr.ID()).GetStore().Mount(id, specgen.Linux.MountLabel)
if err != nil {
return fmt.Errorf("not able to get mountpoint for container %q: %w", id, err)
}
Expand Down
19 changes: 13 additions & 6 deletions internal/lib/container_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const ContainerManagerCRIO = "cri-o"
type ContainerServer struct {
runtime *oci.Runtime
store cstorage.Store
storageImageServer storage.ImageServer
storageImageServers storage.ImageServerList
storageRuntimeServer storage.RuntimeServer
ctrNameIndex *registrar.Registrar
ctrIDIndex *truncindex.TruncIndex
Expand All @@ -63,8 +63,13 @@ func (c *ContainerServer) Store() cstorage.Store {
}

// StorageImageServer returns the ImageServer for the ContainerServer
func (c *ContainerServer) StorageImageServer() storage.ImageServer {
return c.storageImageServer
func (c *ContainerServer) StorageImageServer(containerID string) storage.ImageServer {
return c.storageImageServers.GetImageServer(containerID)
}

// ImageServerList returns the list of ImageServer instances available
func (c *ContainerServer) ImageServerList() storage.ImageServerList {
return c.storageImageServers
}

// CtrIDIndex returns the TruncIndex for the ContainerServer
Expand Down Expand Up @@ -107,9 +112,11 @@ func New(ctx context.Context, configIface libconfig.Iface) (*ContainerServer, er
return nil, err
}

storageRuntimeService := storage.GetRuntimeService(ctx, imageService)
imageServers := storage.GetImageServiceList(imageService)

storageRuntimeService := storage.GetRuntimeService(ctx, imageServers)

runtime, err := oci.New(config)
runtime, err := oci.New(config, imageServers)
if err != nil {
return nil, err
}
Expand All @@ -122,7 +129,7 @@ func New(ctx context.Context, configIface libconfig.Iface) (*ContainerServer, er
c := &ContainerServer{
runtime: runtime,
store: store,
storageImageServer: imageService,
storageImageServers: imageServers,
storageRuntimeServer: storageRuntimeService,
ctrNameIndex: registrar.NewRegistrar(),
ctrIDIndex: truncindex.NewTruncIndex([]string{}),
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/container_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var _ = t.Describe("ContainerServer", func() {
It("should succeed to get the StorageImageServer", func() {
// Given
// When
res := sut.StorageImageServer()
res := sut.StorageImageServer("")

// Then
Expect(res).NotTo(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/container_server_test_inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func (c *ContainerServer) SetStorageRuntimeServer(server storage.RuntimeServer)

// SetStorageImageServer sets the ImageServer for the ContainerServer
func (c *ContainerServer) SetStorageImageServer(server storage.ImageServer) {
c.storageImageServer = server
c.storageImageServers.SetDefaultImageServer(server)
}
7 changes: 4 additions & 3 deletions internal/lib/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func (c *ContainerServer) ContainerRestore(
return "", err
}
// During checkpointing the container is unmounted. This mounts the container again.
mountPoint, err := c.StorageImageServer().GetStore().Mount(ctr.ID(), ctrSpec.Config.Linux.MountLabel)
store := c.StorageImageServer(ctr.ID()).GetStore()
mountPoint, err := store.Mount(ctr.ID(), ctrSpec.Config.Linux.MountLabel)
if err != nil {
log.Debugf(ctx, "Failed to mount container %q: %v", ctr.ID(), err)
return "", err
Expand All @@ -61,13 +62,13 @@ func (c *ContainerServer) ContainerRestore(
if ctr.RestoreArchive() != "" {
if ctr.RestoreIsOCIImage() {
log.Debugf(ctx, "Restoring from %v", ctr.RestoreArchive())
imageMountPoint, err := c.StorageImageServer().GetStore().MountImage(ctr.RestoreArchive(), nil, "")
imageMountPoint, err := store.MountImage(ctr.RestoreArchive(), nil, "")
if err != nil {
return "", err
}
logrus.Debugf("Checkpoint image mounted at %v", imageMountPoint)
defer func() {
_, err := c.StorageImageServer().GetStore().UnmountImage(ctr.RestoreArchive(), true)
_, err := store.UnmountImage(ctr.RestoreArchive(), true)
if err != nil {
log.Errorf(ctx, "Failed to unmount checkpoint image: %q", err)
}
Expand Down
11 changes: 9 additions & 2 deletions internal/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/storage"
"github.com/cri-o/cri-o/pkg/config"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/net/context"
Expand Down Expand Up @@ -47,6 +48,7 @@ type Runtime struct {
config *config.Config
runtimeImplMap map[string]RuntimeImpl
runtimeImplMapMutex sync.RWMutex
imageServers storage.ImageServerList
}

// RuntimeImpl is an interface used by the caller to interact with the
Expand Down Expand Up @@ -80,7 +82,7 @@ type RuntimeImpl interface {
}

// New creates a new Runtime with options provided
func New(c *config.Config) (*Runtime, error) {
func New(c *config.Config, is storage.ImageServerList) (*Runtime, error) {
execNotifyDir := filepath.Join(c.ContainerAttachSocketDir, "exec-pid-dir")
if err := os.MkdirAll(execNotifyDir, 0o750); err != nil {
return nil, fmt.Errorf("create oci runtime pid dir: %w", err)
Expand All @@ -89,6 +91,7 @@ func New(c *config.Config) (*Runtime, error) {
return &Runtime{
config: c,
runtimeImplMap: make(map[string]RuntimeImpl),
imageServers: is,
}, nil
}

Expand Down Expand Up @@ -176,7 +179,10 @@ func (r *Runtime) newRuntimeImpl(c *Container) (RuntimeImpl, error) {
}

if rh.RuntimeType == config.RuntimeTypeVM {
return newRuntimeVM(rh.RuntimePath, rh.RuntimeRoot, rh.RuntimeConfigPath, r.config.RuntimeConfig.ContainerExitsDir), nil
impl := newRuntimeVM(r.imageServers.GetDefaultImageServer(), rh.RuntimePath, rh.RuntimeRoot, rh.RuntimeConfigPath, r.config.RuntimeConfig.ContainerExitsDir)
// TODO: for peer-pods, register the runtimeVM as the ImageServer for the container
// r.imageServers.SetImageServer(c.ID(), impl)
return impl, nil
}

if rh.RuntimeType == config.RuntimeTypePod {
Expand Down Expand Up @@ -302,6 +308,7 @@ func (r *Runtime) DeleteContainer(ctx context.Context, c *Container) (err error)
r.runtimeImplMapMutex.Lock()
delete(r.runtimeImplMap, c.ID())
r.runtimeImplMapMutex.Unlock()
r.imageServers.DeleteImageServer(c.ID())
}
}()
}
Expand Down
4 changes: 2 additions & 2 deletions internal/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _ = t.Describe("Oci", func() {
c.ContainerAttachSocketDir = t.MustTempDir("crio")

// When
runtime, err := oci.New(c)
runtime, err := oci.New(c, nil)
Expect(err).To(BeNil())

// Then
Expand Down Expand Up @@ -86,7 +86,7 @@ var _ = t.Describe("Oci", func() {
// so we have permission to make a directory within it
config.ContainerAttachSocketDir = t.MustTempDir("crio")

sut, err = oci.New(config)
sut, err = oci.New(config, nil)
Expect(err).To(BeNil())
Expect(sut).NotTo(BeNil())
})
Expand Down
56 changes: 55 additions & 1 deletion internal/oci/runtime_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ import (
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl"
conmonconfig "github.com/containers/conmon/runner/config"
systemCtxTypes "github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/cri-o/cri-o/internal/config/cgmgr"
"github.com/cri-o/cri-o/internal/log"
criostorage "github.com/cri-o/cri-o/internal/storage"
"github.com/cri-o/cri-o/server/metrics"
"github.com/cri-o/cri-o/utils"
"github.com/cri-o/cri-o/utils/errdefs"
Expand All @@ -55,6 +58,8 @@ type runtimeVM struct {
client *ttrpc.Client
task task.TaskService

defaultIs criostorage.ImageServer

sync.Mutex
ctrs map[string]containerInfo
}
Expand All @@ -69,7 +74,7 @@ const (
)

// newRuntimeVM creates a new runtimeVM instance
func newRuntimeVM(path, root, configPath, exitsPath string) RuntimeImpl {
func newRuntimeVM(defaultImageServer criostorage.ImageServer, path, root, configPath, exitsPath string) *runtimeVM {
logrus.Debug("oci.newRuntimeVM() start")
defer logrus.Debug("oci.newRuntimeVM() end")

Expand All @@ -92,6 +97,7 @@ func newRuntimeVM(path, root, configPath, exitsPath string) RuntimeImpl {
fifoDir: filepath.Join(root, "crio", "fifo"),
ctx: context.Background(),
ctrs: make(map[string]containerInfo),
defaultIs: defaultImageServer,
}
}

Expand Down Expand Up @@ -1071,3 +1077,51 @@ func (r *runtimeVM) RestoreContainer(ctx context.Context, c *Container, cgroupPa

return errors.New("restoring not implemented for runtimeVM")
}

func (r *runtimeVM) ListImages(systemContext *systemCtxTypes.SystemContext, filter string) ([]criostorage.ImageResult, error) {
log.Debugf(r.ctx, "RuntimeVM.ListImages() start")
defer log.Debugf(r.ctx, "RuntimeVM.ListImages() end")
// Do not call the default ImageServer here: it is already called for ListImages
// so this would just create duplicates
return []criostorage.ImageResult{}, nil
}

func (r *runtimeVM) ImageStatus(systemContext *systemCtxTypes.SystemContext, nameOrID string) (*criostorage.ImageResult, error) {
log.Debugf(r.ctx, "RuntimeVM.ImageStatus() start")
defer log.Debugf(r.ctx, "RuntimeVM.ImageStatus() end")
// Returning ImageUnknown on purpose, to make the caller call PullImage next
// To be replaced with actual Image Status information.
return nil, storage.ErrImageUnknown
}

func (r *runtimeVM) PrepareImage(inputSystemContext *systemCtxTypes.SystemContext, imageName string) (systemCtxTypes.ImageCloser, error) {
log.Debugf(r.ctx, "RuntimeVM.PrepareImage() start")
defer log.Debugf(r.ctx, "RuntimeVM.PrepareImage() end")
return r.defaultIs.PrepareImage(inputSystemContext, imageName)
}

func (r *runtimeVM) PullImage(systemContext *systemCtxTypes.SystemContext, imageName string, inputOptions *criostorage.ImageCopyOptions) (systemCtxTypes.ImageReference, error) {
log.Debugf(r.ctx, "RuntimeVM.PullImage() start")
defer log.Debugf(r.ctx, "RuntimeVM.PullImage() end")

// if peer_pods, call the shim's PullImage
return r.defaultIs.PullImage(systemContext, imageName, inputOptions)
}

func (r *runtimeVM) UntagImage(systemContext *systemCtxTypes.SystemContext, nameOrID string) error {
log.Debugf(r.ctx, "RuntimeVM.UntagImage() start")
defer log.Debugf(r.ctx, "RuntimeVM.UntagImage() end")
return r.defaultIs.UntagImage(systemContext, nameOrID)
}

func (r *runtimeVM) GetStore() storage.Store {
log.Debugf(r.ctx, "RuntimeVM.GetStore() start")
defer log.Debugf(r.ctx, "RuntimeVM.GetStore() end")
return r.defaultIs.GetStore()
}

func (r *runtimeVM) ResolveNames(systemContext *systemCtxTypes.SystemContext, imageName string) ([]string, error) {
log.Debugf(r.ctx, "RuntimeVM.ResolveNames() start")
defer log.Debugf(r.ctx, "RuntimeVM.ResolveNames() end")
return r.defaultIs.ResolveNames(systemContext, imageName)
}
Loading