Skip to content

Commit

Permalink
Merge pull request #10219 from vrothberg/image-events
Browse files Browse the repository at this point in the history
add libimage events
  • Loading branch information
openshift-merge-robot authored May 20, 2021
2 parents 62c14dc + 8352e5b commit 7bcec86
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/containernetworking/cni v0.8.1
github.com/containernetworking/plugins v0.9.1
github.com/containers/buildah v1.20.2-0.20210519094241-c3a3fe847ee1
github.com/containers/common v0.38.4-0.20210519112800-40a1e9ee42fb
github.com/containers/common v0.38.4
github.com/containers/conmon v2.0.20+incompatible
github.com/containers/image/v5 v5.12.0
github.com/containers/ocicrypt v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ github.com/containernetworking/plugins v0.9.1/go.mod h1:xP/idU2ldlzN6m4p5LmGiwRD
github.com/containers/buildah v1.20.2-0.20210519094241-c3a3fe847ee1 h1:XrHrbHwkTMmZCNhRglW4H3k8ApOWyupsSp5snJmsb24=
github.com/containers/buildah v1.20.2-0.20210519094241-c3a3fe847ee1/go.mod h1:7qXQ9hVQxgkE3XCoCjtzrV0VKh5GzgugkQDfvFxcVus=
github.com/containers/common v0.38.3/go.mod h1:egfpX/Y3+19Dz4Wa1eRZDdgzoEOeneieF9CQppKzLBg=
github.com/containers/common v0.38.4-0.20210519112800-40a1e9ee42fb h1:KEwgtZFf3o/Dk8oLkYrh+CrKENV2fRbZHfcJwgpA9i4=
github.com/containers/common v0.38.4-0.20210519112800-40a1e9ee42fb/go.mod h1:egfpX/Y3+19Dz4Wa1eRZDdgzoEOeneieF9CQppKzLBg=
github.com/containers/common v0.38.4 h1:WYv4R6Sw1qiOPZtBNbKglrmisXdPcq3fZ3bGy4prrjo=
github.com/containers/common v0.38.4/go.mod h1:egfpX/Y3+19Dz4Wa1eRZDdgzoEOeneieF9CQppKzLBg=
github.com/containers/conmon v2.0.20+incompatible h1:YbCVSFSCqFjjVwHTPINGdMX1F6JXHGTUje2ZYobNrkg=
github.com/containers/conmon v2.0.20+incompatible/go.mod h1:hgwZ2mtuDrppv78a/cOBNiCm6O0UMWGx1mu7P00nu5I=
github.com/containers/image/v5 v5.12.0 h1:1hNS2QkzFQ4lH3GYQLyAXB0acRMhS1Ubm6oV++8vw4w=
Expand Down
90 changes: 78 additions & 12 deletions libpod/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"sync"
"syscall"
"time"

"github.com/containers/common/libimage"
"github.com/containers/common/pkg/config"
Expand Down Expand Up @@ -68,17 +69,18 @@ type Runtime struct {
storageConfig storage.StoreOptions
storageSet storageSet

state State
store storage.Store
storageService *storageService
imageContext *types.SystemContext
defaultOCIRuntime OCIRuntime
ociRuntimes map[string]OCIRuntime
runtimeFlags []string
netPlugin ocicni.CNIPlugin
conmonPath string
libimageRuntime *libimage.Runtime
lockManager lock.Manager
state State
store storage.Store
storageService *storageService
imageContext *types.SystemContext
defaultOCIRuntime OCIRuntime
ociRuntimes map[string]OCIRuntime
runtimeFlags []string
netPlugin ocicni.CNIPlugin
conmonPath string
libimageRuntime *libimage.Runtime
libimageEventsShutdown chan bool
lockManager lock.Manager

// doRenumber indicates that the runtime should perform a lock renumber
// during initialization.
Expand Down Expand Up @@ -215,6 +217,8 @@ func newRuntimeFromConfig(ctx context.Context, conf *config.Config, options ...R
return nil, err
}

runtime.libimageEventsShutdown = make(chan bool)

return runtime, nil
}

Expand Down Expand Up @@ -680,6 +684,62 @@ func (r *Runtime) GetConfig() (*config.Config, error) {
return config, nil
}

// libimageEventsMap translates a libimage event type to a libpod event status.
var libimageEventsMap = map[libimage.EventType]events.Status{
libimage.EventTypeImagePull: events.Pull,
libimage.EventTypeImagePush: events.Push,
libimage.EventTypeImageRemove: events.Remove,
libimage.EventTypeImageLoad: events.LoadFromArchive,
libimage.EventTypeImageSave: events.Save,
libimage.EventTypeImageTag: events.Tag,
libimage.EventTypeImageUntag: events.Untag,
libimage.EventTypeImageMount: events.Mount,
libimage.EventTypeImageUnmount: events.Unmount,
}

// libimageEvents spawns a goroutine in the background which is listenting for
// events on the libimage.Runtime. The gourtine will be cleaned up implicitly
// when the main() exists.
func (r *Runtime) libimageEvents() {
toLibpodEventStatus := func(e *libimage.Event) events.Status {
status, found := libimageEventsMap[e.Type]
if !found {
return "Unknown"
}
return status
}

go func() {
eventChannel := r.libimageRuntime.EventChannel()

for {
// Make sure to read and write all events before
// checking if we're about to shutdown.
for len(eventChannel) > 0 {
libimageEvent := <-eventChannel
e := events.Event{
ID: libimageEvent.ID,
Name: libimageEvent.Name,
Status: toLibpodEventStatus(libimageEvent),
Time: libimageEvent.Time,
Type: events.Image,
}
if err := r.eventer.Write(e); err != nil {
logrus.Errorf("unable to write image event: %q", err)
}
}

select {
case <-r.libimageEventsShutdown:
return

default:
time.Sleep(100 * time.Millisecond)
}
}
}()
}

// DeferredShutdown shuts down the runtime without exposing any
// errors. This is only meant to be used when the runtime is being
// shutdown within a defer statement; else use Shutdown
Expand Down Expand Up @@ -719,7 +779,11 @@ func (r *Runtime) Shutdown(force bool) error {
// If no store was requested, it can be nil and there is no need to
// attempt to shut it down
if r.store != nil {
if _, err := r.store.Shutdown(force); err != nil {
// Wait for the events to be written.
r.libimageEventsShutdown <- true

// Note that the libimage runtime shuts down the store.
if err := r.libimageRuntime.Shutdown(force); err != nil {
lastError = errors.Wrapf(err, "error shutting down container storage")
}
}
Expand Down Expand Up @@ -845,6 +909,8 @@ func (r *Runtime) configureStore() error {
return err
}
r.libimageRuntime = libimageRuntime
// Run the libimage events routine.
r.libimageEvents()

return nil
}
Expand Down
36 changes: 36 additions & 0 deletions test/system/090-events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,39 @@ load helpers
run_podman events --filter type=container --filter container=$cname --filter event=start --stream=false
is "$output" "$expect" "filtering just by label"
}

@test "image events" {
skip_if_remote "FIXME: remove events on podman-remote seem to be broken"
pushedDir=$PODMAN_TMPDIR/dir
mkdir -p $pushedDir

tarball=$PODMAN_TMPDIR/ball.tar

run_podman image inspect --format "{{.ID}}" $IMAGE
imageID="$output"

t0=$(date --iso-8601=seconds)
tag=registry.com/$(random_string 10 | tr A-Z a-z)

# Force using the file backend since the journal backend is eating events
# (see containers/podman/pull/10219#issuecomment-842325032).
run_podman --events-backend=file push $IMAGE dir:$pushedDir
run_podman --events-backend=file save $IMAGE -o $tarball
run_podman --events-backend=file load -i $tarball
run_podman --events-backend=file pull docker-archive:$tarball
run_podman --events-backend=file tag $IMAGE $tag
run_podman --events-backend=file untag $IMAGE $tag
run_podman --events-backend=file tag $IMAGE $tag
run_podman --events-backend=file rmi $tag

run_podman --events-backend=file events --stream=false --filter type=image --since $t0
is "$output" ".*image push $imageID dir:$pushedDir
.*image save $imageID $tarball
.*image loadfromarchive *$tarball
.*image pull *docker-archive:$tarball
.*image tag $imageID $tag
.*image untag $imageID $tag:latest
.*image tag $imageID $tag
.*image remove $imageID $tag.*" \
"podman events"
}

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ github.com/containers/buildah/pkg/overlay
github.com/containers/buildah/pkg/parse
github.com/containers/buildah/pkg/rusage
github.com/containers/buildah/util
# github.com/containers/common v0.38.4-0.20210519112800-40a1e9ee42fb
# github.com/containers/common v0.38.4
github.com/containers/common/libimage
github.com/containers/common/libimage/manifests
github.com/containers/common/pkg/apparmor
Expand Down

0 comments on commit 7bcec86

Please sign in to comment.