From b232ea3d18be38ebb0296fa43b655cce5bb21fb2 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 25 Feb 2025 11:49:31 +0100 Subject: [PATCH] create artifact store in the libpod runtime Instead of duplicating the NewArtifactStore() call in many places and having to make sure we always pass the same path to it define it as function on the runtime. This allows any caller with access to the libpod runtime to create the store easily. This is suing a sync.OnceValues() function so the store is initialized only once and only when actually needed. Signed-off-by: Paul Holzinger --- libpod/runtime.go | 9 +++++++++ pkg/domain/infra/abi/artifact.go | 20 +++++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/libpod/runtime.go b/libpod/runtime.go index fc358de026..993099cd58 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -34,6 +34,7 @@ import ( "github.com/containers/podman/v5/libpod/shutdown" "github.com/containers/podman/v5/pkg/domain/entities" "github.com/containers/podman/v5/pkg/domain/entities/reports" + artStore "github.com/containers/podman/v5/pkg/libartifact/store" "github.com/containers/podman/v5/pkg/rootless" "github.com/containers/podman/v5/pkg/systemd" "github.com/containers/podman/v5/pkg/util" @@ -83,6 +84,9 @@ type Runtime struct { libimageEventsShutdown chan bool lockManager lock.Manager + // ArtifactStore returns the artifact store created from the runtime. + ArtifactStore func() (*artStore.ArtifactStore, error) + // Worker workerChannel chan func() workerGroup sync.WaitGroup @@ -533,6 +537,11 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) { } runtime.config.Network.NetworkBackend = string(netBackend) runtime.network = netInterface + + // Using sync once value to only init the store exactly once and only when it will be actually be used. + runtime.ArtifactStore = sync.OnceValues(func() (*artStore.ArtifactStore, error) { + return artStore.NewArtifactStore(filepath.Join(runtime.storageConfig.GraphRoot, "artifacts"), runtime.SystemContext()) + }) } // We now need to see if the system has restarted diff --git a/pkg/domain/infra/abi/artifact.go b/pkg/domain/infra/abi/artifact.go index fa70c28aac..0fd3c09b7d 100644 --- a/pkg/domain/infra/abi/artifact.go +++ b/pkg/domain/infra/abi/artifact.go @@ -5,22 +5,16 @@ package abi import ( "context" "os" - "path/filepath" "time" "github.com/containers/common/libimage" "github.com/containers/podman/v5/pkg/domain/entities" - "github.com/containers/podman/v5/pkg/libartifact/store" "github.com/containers/podman/v5/pkg/libartifact/types" "github.com/opencontainers/go-digest" ) -func getDefaultArtifactStore(ir *ImageEngine) string { - return filepath.Join(ir.Libpod.StorageConfig().GraphRoot, "artifacts") -} - func (ir *ImageEngine) ArtifactInspect(ctx context.Context, name string, _ entities.ArtifactInspectOptions) (*entities.ArtifactInspectReport, error) { - artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) + artStore, err := ir.Libpod.ArtifactStore() if err != nil { return nil, err } @@ -41,7 +35,7 @@ func (ir *ImageEngine) ArtifactInspect(ctx context.Context, name string, _ entit func (ir *ImageEngine) ArtifactList(ctx context.Context, _ entities.ArtifactListOptions) ([]*entities.ArtifactListReport, error) { reports := make([]*entities.ArtifactListReport, 0) - artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) + artStore, err := ir.Libpod.ArtifactStore() if err != nil { return nil, err } @@ -80,7 +74,7 @@ func (ir *ImageEngine) ArtifactPull(ctx context.Context, name string, opts entit if !opts.Quiet && pullOptions.Writer == nil { pullOptions.Writer = os.Stderr } - artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) + artStore, err := ir.Libpod.ArtifactStore() if err != nil { return nil, err } @@ -92,7 +86,7 @@ func (ir *ImageEngine) ArtifactRm(ctx context.Context, name string, opts entitie namesOrDigests []string ) artifactDigests := make([]*digest.Digest, 0, len(namesOrDigests)) - artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) + artStore, err := ir.Libpod.ArtifactStore() if err != nil { return nil, err } @@ -133,7 +127,7 @@ func (ir *ImageEngine) ArtifactRm(ctx context.Context, name string, opts entitie func (ir *ImageEngine) ArtifactPush(ctx context.Context, name string, opts entities.ArtifactPushOptions) (*entities.ArtifactPushReport, error) { var retryDelay *time.Duration - artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) + artStore, err := ir.Libpod.ArtifactStore() if err != nil { return nil, err } @@ -189,7 +183,7 @@ func (ir *ImageEngine) ArtifactPush(ctx context.Context, name string, opts entit return &entities.ArtifactPushReport{}, err } func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []string, opts *entities.ArtifactAddOptions) (*entities.ArtifactAddReport, error) { - artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) + artStore, err := ir.Libpod.ArtifactStore() if err != nil { return nil, err } @@ -210,7 +204,7 @@ func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []str } func (ir *ImageEngine) ArtifactExtract(ctx context.Context, name string, target string, opts *entities.ArtifactExtractOptions) error { - artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) + artStore, err := ir.Libpod.ArtifactStore() if err != nil { return err }