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

Skip to content
6 changes: 3 additions & 3 deletions internal/factory/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type Container interface {
SpecAddMount(rspec.Mount)

// SpecAddAnnotations adds annotations to the spec.
SpecAddAnnotations(ctx context.Context, sandbox *sandbox.Sandbox, containerVolume []oci.ContainerVolume, mountPoint, configStopSignal string, imageResult *storage.ImageResult, isSystemd, systemdHasCollectMode bool) error
SpecAddAnnotations(ctx context.Context, sandbox *sandbox.Sandbox, criMounts []*types.Mount, mountPoint, configStopSignal string, imageResult *storage.ImageResult, isSystemd, systemdHasCollectMode bool) error

// SpecAddDevices adds devices from the server config, and container CRI config
SpecAddDevices([]device.Device, []device.Device, bool, bool) error
Expand Down Expand Up @@ -150,7 +150,7 @@ func (c *container) SpecAddMount(r rspec.Mount) {
}

// SpecAddAnnotation adds all annotations to the spec
func (c *container) SpecAddAnnotations(ctx context.Context, sb *sandbox.Sandbox, containerVolumes []oci.ContainerVolume, mountPoint, configStopSignal string, imageResult *storage.ImageResult, isSystemd, systemdHasCollectMode bool) (err error) {
func (c *container) SpecAddAnnotations(ctx context.Context, sb *sandbox.Sandbox, criMounts []*types.Mount, mountPoint, configStopSignal string, imageResult *storage.ImageResult, isSystemd, systemdHasCollectMode bool) (err error) {
// Copied from k8s.io/kubernetes/pkg/kubelet/kuberuntime/labels.go
const podTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod"

Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *container) SpecAddAnnotations(ctx context.Context, sb *sandbox.Sandbox,
}
c.spec.AddAnnotation(annotations.Labels, string(labelsJSON))

volumesJSON, err := json.Marshal(containerVolumes)
volumesJSON, err := json.Marshal(criMounts)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions internal/factory/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/cri-o/cri-o/internal/hostport"
"github.com/cri-o/cri-o/internal/lib"
"github.com/cri-o/cri-o/internal/lib/sandbox"
oci "github.com/cri-o/cri-o/internal/oci"
"github.com/cri-o/cri-o/internal/storage"
crioann "github.com/cri-o/cri-o/pkg/annotations"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -85,7 +84,7 @@ var _ = t.Describe("Container", func() {
err := sut.SetConfig(containerConfig, sandboxConfig)
Expect(err).To(BeNil())
currentTime := time.Now()
volumes := []oci.ContainerVolume{}
mounts := []*types.Mount{}
imageResult := storage.ImageResult{}
mountPoint := "test"
configStopSignal := "test"
Expand All @@ -108,7 +107,7 @@ var _ = t.Describe("Container", func() {
labelsJSON, err := json.Marshal(sut.Config().Labels)
Expect(err).To(BeNil())

volumesJSON, err := json.Marshal(volumes)
mountsJSON, err := json.Marshal(mounts)
Expect(err).To(BeNil())

kubeAnnotationsJSON, err := json.Marshal(sut.Config().Annotations)
Expand All @@ -117,7 +116,7 @@ var _ = t.Describe("Container", func() {
Expect(currentTime).ToNot(BeNil())
Expect(sb).ToNot(BeNil())

err = sut.SpecAddAnnotations(context.Background(), sb, volumes, mountPoint, configStopSignal, &imageResult, false, false)
err = sut.SpecAddAnnotations(context.Background(), sb, mounts, mountPoint, configStopSignal, &imageResult, false, false)
Expect(err).To(BeNil())

Expect(sut.Spec().Config.Annotations[annotations.Image]).To(Equal(image))
Expand All @@ -139,7 +138,7 @@ var _ = t.Describe("Container", func() {
Expect(sut.Spec().Config.Annotations[annotations.Created]).ToNot(BeNil())
Expect(sut.Spec().Config.Annotations[annotations.Metadata]).To(Equal(string(metadataJSON)))
Expect(sut.Spec().Config.Annotations[annotations.Labels]).To(Equal(string(labelsJSON)))
Expect(sut.Spec().Config.Annotations[annotations.Volumes]).To(Equal(string(volumesJSON)))
Expect(sut.Spec().Config.Annotations[annotations.Volumes]).To(Equal(string(mountsJSON)))
Expect(sut.Spec().Config.Annotations[annotations.Annotations]).To(Equal(string(kubeAnnotationsJSON)))
})
})
Expand Down
8 changes: 4 additions & 4 deletions internal/lib/container_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ func (c *ContainerServer) LoadSandbox(ctx context.Context, id string) (sb *sandb
scontainer.SetMountPoint(m.Annotations[annotations.MountPoint])

if m.Annotations[annotations.Volumes] != "" {
containerVolumes := []oci.ContainerVolume{}
if err = json.Unmarshal([]byte(m.Annotations[annotations.Volumes]), &containerVolumes); err != nil {
criMounts := []*types.Mount{}
if err = json.Unmarshal([]byte(m.Annotations[annotations.Volumes]), &criMounts); err != nil {
return sb, fmt.Errorf("failed to unmarshal container volumes: %w", err)
}
for _, cv := range containerVolumes {
scontainer.AddVolume(cv)
for _, cv := range criMounts {
scontainer.AddMount(cv)
}
}

Expand Down
105 changes: 82 additions & 23 deletions internal/oci/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const (
statStartTimeLocation = 22
// The 2nd field is the command, wrapped by ()
statCommField = 2

oomKilledReason = "OOMKilled"
completedReason = "Completed"
errorReason = "Error"
)

var (
Expand All @@ -44,7 +48,8 @@ var (
// Container represents a runtime container.
type Container struct {
criContainer *types.Container
volumes []ContainerVolume
criStatus *types.ContainerStatus
criLock sync.RWMutex
name string
logPath string
runtimeHandler string
Expand Down Expand Up @@ -126,6 +131,19 @@ func NewContainer(id, name, bundlePath, logPath string, labels, crioAnnotations,
},
ImageRef: imageRef,
},
criStatus: &types.ContainerStatus{
Id: id,
CreatedAt: created.UnixNano(),
Metadata: metadata,
Labels: labels,
Annotations: annotations,
ImageRef: imageRef,
Image: &types.ImageSpec{
Image: imageName,
},
Mounts: []*types.Mount{},
LogPath: logPath,
},
name: name,
bundlePath: bundlePath,
logPath: logPath,
Expand Down Expand Up @@ -173,21 +191,21 @@ func (c *Container) CRIContainer() *types.Container {
// If a protobuf message gets mutated mid-request, then the proto library panics.
// We would like to avoid deep copies when possible to avoid excessive garbage
// collection, but need to if the container changes state.
newState := types.ContainerState_CONTAINER_UNKNOWN
switch c.StateNoLock().Status {
case ContainerStateCreated:
newState = types.ContainerState_CONTAINER_CREATED
case ContainerStateRunning, ContainerStatePaused:
newState = types.ContainerState_CONTAINER_RUNNING
case ContainerStateStopped:
newState = types.ContainerState_CONTAINER_EXITED
}
return c.criContainer
}

func (c *Container) CRIStatus() *types.ContainerStatus {
c.criLock.RLock()
defer c.criLock.RUnlock()
return c.criStatus
}

func (c *Container) updateCRIContainerState(newState types.ContainerState) {
if newState != c.criContainer.State {
cpy := *c.criContainer
cpy.State = newState
c.criContainer = &cpy
}
return c.criContainer
}

// SetSpec loads the OCI spec in the container struct
Expand Down Expand Up @@ -281,11 +299,6 @@ func (c *Container) StatePath() string {
return filepath.Join(c.dir, "state.json")
}

// CreatedAt returns the container creation time
func (c *Container) CreatedAt() time.Time {
return c.state.Created
}

// Name returns the name of the container.
func (c *Container) Name() string {
return c.name
Expand Down Expand Up @@ -392,14 +405,18 @@ func (c *Container) StateNoLock() *ContainerState {
return c.state
}

// AddVolume adds a volume to list of container volumes.
func (c *Container) AddVolume(v ContainerVolume) {
c.volumes = append(c.volumes, v)
// AddMount adds a mount to list of container mounts.
func (c *Container) AddMount(m *types.Mount) {
c.criLock.Lock()
c.criStatus.Mounts = append(c.criStatus.Mounts, m)
c.criLock.Unlock()
}

// Volumes returns the list of container volumes.
func (c *Container) Volumes() []ContainerVolume {
return c.volumes
// Mounts returns the list of container mounts.
func (c *Container) Mounts() []*types.Mount {
c.criLock.RLock()
defer c.criLock.RUnlock()
return c.criStatus.Mounts
}

// SetMountPoint sets the container mount point
Expand Down Expand Up @@ -432,14 +449,56 @@ func (c *Container) Created() bool {
return c.created
}

func (c *Container) SetStarted() {
started := time.Now()
c.state.Started = started

c.criLock.Lock()
defer c.criLock.Unlock()
c.updateCRIContainerState(types.ContainerState_CONTAINER_RUNNING)

cpy := *c.criStatus
cpy.State = types.ContainerState_CONTAINER_RUNNING
cpy.StartedAt = started.UnixNano()
c.criStatus = &cpy
}

func (c *Container) SetStopped(finishedTime time.Time, exitCode int32, oomKilled bool) {
c.state.ExitCode = &exitCode

c.criLock.Lock()
defer c.criLock.Unlock()

c.updateCRIContainerState(types.ContainerState_CONTAINER_EXITED)

cpy := *c.criStatus
if cpy.StartedAt == 0 {
cpy.StartedAt = finishedTime.UnixNano()
}

cpy.FinishedAt = finishedTime.UnixNano()
cpy.ExitCode = exitCode
switch {
case oomKilled:
cpy.Reason = oomKilledReason
case cpy.ExitCode == 0:
cpy.Reason = completedReason
default:
cpy.Reason = errorReason
cpy.Message = c.state.Error // TODO FIXME
}
c.criStatus = &cpy
}

// SetStartFailed sets the container state appropriately after a start failure
func (c *Container) SetStartFailed(err error) {
c.opLock.Lock()
defer c.opLock.Unlock()
// adjust finished and started times
c.state.Finished, c.state.Started = c.state.Created, c.state.Created
c.SetStopped(c.state.Created, 0, false)
if err != nil {
c.state.Error = err.Error()
c.state.Error = err.Error() // TODO FIXME
}
}

Expand Down
10 changes: 5 additions & 5 deletions internal/oci/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ var _ = t.Describe("Container", func() {
Expect(sut.IDMappings()).To(Equal(mappings))
})

It("should succeed to add a volume", func() {
It("should succeed to add a mount", func() {
// Given
volume := oci.ContainerVolume{ContainerPath: "/"}
mount := &types.Mount{ContainerPath: "/"}

// When
sut.AddVolume(volume)
sut.AddMount(mount)

// Then
Expect(len(sut.Volumes())).To(BeEquivalentTo(1))
Expect(sut.Volumes()[0]).To(Equal(volume))
Expect(len(sut.Mounts())).To(BeEquivalentTo(1))
Expect(sut.Mounts()[0]).To(Equal(mount))
})

It("should succeed to set the seccomp profile path", func() {
Expand Down
Loading