Skip to content
Merged
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
3 changes: 3 additions & 0 deletions internal/oci/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func (c *Container) State() *ContainerState {
}

// StateNoLock returns the state of a container without using a lock.
// It has been known to cause segfaults in the past so it really should be used sparingly.
func (c *Container) StateNoLock() *ContainerState {
return c.state
}
Expand Down Expand Up @@ -504,6 +505,8 @@ func (c *Container) exitFilePath() string {

// Living checks if a container's init PID exists and it's running, without calling
// a given runtime directly to check the state, which is expensive.
// This can't be used for runtimeVM because it uses the VM's pid as a container pid,
// and the VM doesn't necessarily stop when the container stops.
func (c *Container) Living() error {
_, _, err := c.pid()
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
ReopenContainerLog(context.Context, *Container) error
CheckpointContainer(context.Context, *Container, *rspec.Spec, bool) error
RestoreContainer(context.Context, *Container, string, string) error
IsContainerAlive(*Container) bool
}

// New creates a new Runtime with options provided.
Expand Down Expand Up @@ -504,3 +505,12 @@

return impl.RestoreContainer(ctx, c, cgroupParent, mountLabel)
}

func (r *Runtime) IsContainerAlive(c *Container) (bool, error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return false, err
}

Check warning on line 513 in internal/oci/oci.go

View check run for this annotation

Codecov / codecov/patch

internal/oci/oci.go#L509-L513

Added lines #L509 - L513 were not covered by tests

return impl.IsContainerAlive(c), nil

Check warning on line 515 in internal/oci/oci.go

View check run for this annotation

Codecov / codecov/patch

internal/oci/oci.go#L515

Added line #L515 was not covered by tests
}
4 changes: 4 additions & 0 deletions internal/oci/runtime_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -1576,3 +1576,7 @@
}
return nil
}

func (r *runtimeOCI) IsContainerAlive(c *Container) bool {
return c.Living() == nil

Check warning on line 1581 in internal/oci/runtime_oci.go

View check run for this annotation

Codecov / codecov/patch

internal/oci/runtime_oci.go#L1580-L1581

Added lines #L1580 - L1581 were not covered by tests
}
4 changes: 4 additions & 0 deletions internal/oci/runtime_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,7 @@
ID: c.ID(),
})
}

func (r *runtimePod) IsContainerAlive(c *Container) bool {
return c.Living() == nil

Check warning on line 299 in internal/oci/runtime_pod.go

View check run for this annotation

Codecov / codecov/patch

internal/oci/runtime_pod.go#L298-L299

Added lines #L298 - L299 were not covered by tests
}
4 changes: 4 additions & 0 deletions internal/oci/runtime_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,3 +1275,7 @@
option := base64.StdEncoding.EncodeToString(validKataVirtualVolumeJSON)
return option, nil
}

func (r *runtimeVM) IsContainerAlive(c *Container) bool {
return r.kill(c.ID(), "", 0, false) == nil

Check warning on line 1280 in internal/oci/runtime_vm.go

View check run for this annotation

Codecov / codecov/patch

internal/oci/runtime_vm.go#L1279-L1280

Added lines #L1279 - L1280 were not covered by tests
}
10 changes: 4 additions & 6 deletions server/container_reopen_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
types "k8s.io/cri-api/pkg/apis/runtime/v1"

"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/oci"
)

// ReopenContainerLog reopens the containers log file.
Expand All @@ -20,13 +19,12 @@
return nil, fmt.Errorf("could not find container %s: %w", req.ContainerId, err)
}

if err := s.ContainerServer.Runtime().UpdateContainerStatus(ctx, c); err != nil {
isRunning, err := s.ContainerServer.Runtime().IsContainerAlive(c)
if err != nil {

Check warning on line 23 in server/container_reopen_log.go

View check run for this annotation

Codecov / codecov/patch

server/container_reopen_log.go#L22-L23

Added lines #L22 - L23 were not covered by tests
return nil, err
}

cState := c.State()
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
return nil, errors.New("container is not created or running")
if !isRunning {
return nil, errors.New("container is not running")

Check warning on line 27 in server/container_reopen_log.go

View check run for this annotation

Codecov / codecov/patch

server/container_reopen_log.go#L26-L27

Added lines #L26 - L27 were not covered by tests
}

if err := s.ContainerServer.Runtime().ReopenContainerLog(ctx, c); err != nil {
Expand Down
53 changes: 53 additions & 0 deletions test/logs.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bats
# vim:set ft=bash :

load helpers

function setup() {
setup_test
}

function teardown() {
cleanup_test
}

@test "ReopenContainerLog should succeed when the container is running" {
start_crio

jq '.metadata.name = "sleep"
| .command = ["/bin/sh", "-c", "sleep 600"]' \
"$TESTDATA"/container_config.json > "$TESTDIR"/trap.json

ctr_id=$(crictl run "$TESTDIR"/trap.json "$TESTDATA"/sandbox_config.json)
crictl logs -r "$ctr_id"
}

@test "ReopenContainerLog should fail when the container is stopped" {
start_crio

jq '.metadata.name = "sleep"
| .command = ["/bin/sh", "-c", "sleep 600"]' \
"$TESTDATA"/container_config.json > "$TESTDIR"/trap.json

ctr_id=$(crictl run "$TESTDIR"/trap.json "$TESTDATA"/sandbox_config.json)
crictl stop "$ctr_id"
run ! crictl logs -r "$ctr_id"
}

@test "ReopenContainerLog should not be blocked during deletion" {
start_crio

jq '.metadata.name = "trap"
| .command = ["/bin/sh", "-c", "trap \"sleep 600\" TERM && sleep 600"]' \
"$TESTDATA"/container_config.json > "$TESTDIR"/trap.json

ctr_id=$(crictl run "$TESTDIR"/trap.json "$TESTDATA"/sandbox_config.json)
# Especially when using kata, it sometimes takes a few seconds to actually run container
sleep 5

crictl stop -t 10 "$ctr_id" &
wait_for_log "Request: &StopContainerRequest"
crictl logs -r "$ctr_id"
output=$(crictl inspect "$ctr_id" | jq -r ".status.state")
[[ "$output" == "CONTAINER_RUNNING" ]]
}
14 changes: 14 additions & 0 deletions test/mocks/oci/oci.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.