librarymanager

package
v1.2.2-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DatabaseFileName is the name of the database file created by bbolt.
	DatabaseFileName = "datadog-csi-driver.db"
	// LibraryMappingBucket is the name of the bucket to map libraries. Conceptually, the key structure is as follows:
	//     /library-mappings/{{ library_id }}/{{ volume_id }}
	LibraryMappingBucket = "library-mappings"
	// VolumeMappingBucket is the bucket to map volumes. Conceptually, the key structure is as follows:
	//     /volume-mappings/{{ volume_id }}/{{ library_id }}
	VolumeMappingBucket = "volume-mappings"
)
View Source
const (
	// StoreDirectory is the subdirectory where active libraries are stored.
	StoreDirectory = "store"
	// DatabaseDirectory is the subdirectory where the databse file will be stored.
	DatabaseDirectory = "db"
	// ScratchDirectory is the subdirectory used for scratch download space for libraries.
	ScratchDirectory = "scratch"
	// DefaultImageCacheTTL is the max amount of time before we fetch a new image digest.
	DefaultImageCacheTTL = 1 * time.Hour
)

Variables

View Source
var ErrItemNotFound = errors.New("item not found in store")

Functions

This section is empty.

Types

type ArchiveExtractor

type ArchiveExtractor struct {
	// contains filtered or unexported fields
}

ArchiveExtractor extracts directories from a tar archive.

func NewArchiveExtractor

func NewArchiveExtractor(afs afero.Afero, src string, dst string) (*ArchiveExtractor, error)

NewArchiveExtractor initializes a new archive extractor.

func (*ArchiveExtractor) Extract

func (fp *ArchiveExtractor) Extract(ctx context.Context, reader io.Reader) error

Extract will copy files from the configured source directory inside the archive to the desitnation directory outside of the archive through the reader provided.

type CleanupFunc added in v1.2.0

type CleanupFunc func(libraryID string) error

CleanupFunc is a function that performs cleanup for a library. It receives the libraryID and should re-check if cleanup is still needed.

type CleanupStrategy added in v1.2.0

type CleanupStrategy interface {
	// ScheduleCleanup is called when a library has no more volumes using it.
	// The cleanupFunc will be called either immediately or after a delay,
	// depending on the strategy implementation.
	ScheduleCleanup(libraryID string, cleanupFunc CleanupFunc)

	// Stop stops the strategy and executes all pending cleanups.
	Stop()
}

CleanupStrategy defines how libraries are cleaned up when no longer in use.

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database is a wrapper around bbolt with business logic for the library manager.

Transaction Consistency Guarantees

bbolt provides serializable isolation, the highest level of transaction isolation:

  • Write transactions are mutually exclusive (only one can run at a time)
  • Read transactions see a consistent snapshot of the database at the time they started
  • All operations within a single transaction are atomic (all-or-nothing)

The defer tx.Rollback() pattern is safe: if Commit() was already called, Rollback() is a no-op.

External Locking Requirements

While individual database transactions are atomic, operations that span multiple transactions or combine database operations with filesystem operations (e.g., LinkVolume + store.Add) require external synchronization. The LibraryManager uses a Locker to coordinate these compound operations on a per-library basis.

func NewDatabase

func NewDatabase(basePath string) (*Database, error)

NewDatabase initializes a new database. If a database file exists, it will re-use the existing file. Call close when you are done.

func (*Database) Close

func (db *Database) Close() error

Close will clean up the database and should be called before exiting.

func (*Database) GetLibraryForVolume

func (db *Database) GetLibraryForVolume(volumeID string) (string, error)

GetLibraryForVolume returns the library mapped to a volume. A volume should only ever have one library mapped to it.

func (*Database) GetVolumeCount

func (db *Database) GetVolumeCount(libraryID string) (int, error)

GetVolumeCount returns the number of volumes linked to a library.

func (*Database) LinkVolume

func (db *Database) LinkVolume(libraryID string, volumeID string) error

LinkVolume creates a bidrectional mapping between the library and volume.

func (*Database) UnlinkVolume

func (db *Database) UnlinkVolume(libraryID string, volumeID string) error

UnlinkVolume removes the link for a given volume.

type DelayedCleanupStrategy added in v1.2.0

type DelayedCleanupStrategy struct {
	// contains filtered or unexported fields
}

DelayedCleanupStrategy waits for a configurable delay before executing cleanup. This allows rolling updates to reuse libraries without re-downloading them.

func NewDelayedCleanupStrategy added in v1.2.0

func NewDelayedCleanupStrategy(delay time.Duration) *DelayedCleanupStrategy

NewDelayedCleanupStrategy creates a new delayed cleanup strategy. The delay parameter specifies how long to wait before cleaning up unused libraries.

func (*DelayedCleanupStrategy) ScheduleCleanup added in v1.2.0

func (s *DelayedCleanupStrategy) ScheduleCleanup(libraryID string, cleanupFunc CleanupFunc)

func (*DelayedCleanupStrategy) Stop added in v1.2.0

func (s *DelayedCleanupStrategy) Stop()

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

Downloader enables downloading and extracting directories from container images.

func NewDownloader

func NewDownloader() *Downloader

NewDownloader creates a new downloader with the default settings.

func NewDownloaderWithRoundTripper

func NewDownloaderWithRoundTripper(roundTripper http.RoundTripper) *Downloader

NewDownloaderWithRoundTripper creates a new downloader with the provided round tripper.

func (*Downloader) Download

func (d *Downloader) Download(ctx context.Context, afs afero.Afero, image string, dst string) error

Download will stream a container image and extract the source directory from inside of the image to the destination directory on disk.

func (*Downloader) FetchDigest

func (d *Downloader) FetchDigest(ctx context.Context, image string) (string, error)

FetchDigest will fetch a sha256 sum of the image and return it.

type ImageCache

type ImageCache struct {
	// contains filtered or unexported fields
}

ImageCache provides an in memory cache of container image digests so we don't have to resolve a container tag to sha256sum each time.

func NewImageCache

func NewImageCache(d *Downloader, ttl time.Duration) *ImageCache

NewImageChace initializes a new, empty image cache.

func (*ImageCache) FetchDigest

func (ic *ImageCache) FetchDigest(ctx context.Context, image string, pull bool) (string, error)

FetchDigest returns the sha256 digest for a container image, using the cache when possible.

The image parameter must be a valid container image reference as accepted by crane (https://pkg.go.dev/github.com/google/go-containerregistry/pkg/crane). Examples:

  • "gcr.io/datadoghq/dd-lib-java-init:v1.2.3"
  • "gcr.io/datadoghq/dd-lib-java-init@sha256:abc123..."
  • "nginx:latest" (defaults to docker.io registry)

If the image already contains a digest (@sha256:...), this function will still resolve the full digest from the registry to ensure it exists and is valid.

If pull is true, the cache is bypassed and a fresh digest is always fetched from the registry. If pull is false, the cache is checked first and a remote call is only made on cache miss.

type ImmediateCleanupStrategy added in v1.2.0

type ImmediateCleanupStrategy struct{}

ImmediateCleanupStrategy executes cleanup immediately when a library is no longer used. This is the default behavior.

func NewImmediateCleanupStrategy added in v1.2.0

func NewImmediateCleanupStrategy() *ImmediateCleanupStrategy

NewImmediateCleanupStrategy creates a new immediate cleanup strategy.

func (*ImmediateCleanupStrategy) ScheduleCleanup added in v1.2.0

func (s *ImmediateCleanupStrategy) ScheduleCleanup(libraryID string, cleanupFunc CleanupFunc)

func (*ImmediateCleanupStrategy) Stop added in v1.2.0

func (s *ImmediateCleanupStrategy) Stop()

type Library

type Library struct {
	// contains filtered or unexported fields
}

Library represents a Datadog package to download and mount as part of a DatadogLibrary volume request.

func NewLibrary

func NewLibrary(name string, registry string, version string, pull bool) (*Library, error)

NewLibrary instatiates a new library from the provided fields and ensures they are valid.

func (*Library) Image

func (l *Library) Image() string

Image provides a container image path pullable by crane. Handles tag, digest, and tag@digest versions:

  • Tags: registry/name:v1.0.0
  • Digests: registry/name@sha256:abc123...
  • Tag+Digest: registry/name:v1.0.0@sha256:abc123...

func (*Library) Pull

func (l *Library) Pull() bool

Pull returns if this library should be pulled or not based on the pull policy.

type LibraryManager

type LibraryManager struct {
	// contains filtered or unexported fields
}

LibraryManager is a high level object to manage fetching libraries for volumes. It will download, extract, store, and track libraries and how they map to a volume.

func NewLibraryManager

func NewLibraryManager(basePath string, opts ...LibraryManagerOption) (*LibraryManager, error)

NewLibraryManager creates a new library manager with all of the required dependencies. The basePath is required as an absolute path (rather than using afero.NewBasePathFs) because bind mounts need absolute paths.

func (*LibraryManager) GetLibraryForVolume

func (lm *LibraryManager) GetLibraryForVolume(ctx context.Context, volumeID string, lib *Library) (string, error)

GetLibraryForVolume fetches the remote library if it doesn't exist, records its usage, and returns the path on disk that can be mounted for the volume.

func (*LibraryManager) HasVolume added in v1.2.0

func (lm *LibraryManager) HasVolume(volumeID string) (bool, error)

HasVolume returns true if the volume is managed by the library manager.

func (*LibraryManager) RemoveVolume

func (lm *LibraryManager) RemoveVolume(ctx context.Context, volumeID string) error

RemoveVolume removes the link between the LibraryID and the VolumeID in the database. If there are no more uses of the library, it is also removed from disk.

func (*LibraryManager) Stop

func (lm *LibraryManager) Stop() error

Stop ensures all dependencies are stopped correctly.

type LibraryManagerOption

type LibraryManagerOption func(*LibraryManager)

LibraryManagerOption is a functional option for configuring a LibraryManager.

func WithCleanupStrategy added in v1.2.0

func WithCleanupStrategy(s CleanupStrategy) LibraryManagerOption

WithCleanupStrategy sets the cleanup strategy to use. If not set, ImmediateCleanupStrategy is used by default.

func WithDownloader

func WithDownloader(d *Downloader) LibraryManagerOption

WithDownloader sets the downloader to use. Useful for testing.

func WithFilesystem

func WithFilesystem(fs afero.Afero) LibraryManagerOption

WithFilesystem sets the filesystem to use. Useful for testing.

type Locker

type Locker struct {
	// contains filtered or unexported fields
}

Locker is a sharded mutex to be able to perform concurrent operations on unrelated keys.

func NewLocker

func NewLocker() *Locker

NewLocker initializes a new locker.

func (*Locker) Lock

func (l *Locker) Lock(id string)

Lock will acquire a lock for the given ID. The caller MUST call unlock.

func (*Locker) Unlock

func (l *Locker) Unlock(id string)

Unlock will release a lock for the given ID.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store provides a file based storage solution for packages. It is not thread safe and it is up to the caller to manage concurrency.

func NewStore

func NewStore(afs afero.Afero, basePath string) (*Store, error)

NewStore creates a new store and ensures the base path exists.

func (*Store) Add

func (s *Store) Add(id string, src string) (string, error)

Add will move a source directory into the store. This is intended to be used with a downloader and scratch space. If a package already exists at the provided ID, it will not be re-added.

func (*Store) Exists

func (s *Store) Exists(id string) (bool, error)

Exists determines if an item exists in the store.

func (*Store) Get

func (s *Store) Get(id string) (string, error)

Get returns an item in the store if it exists.

func (*Store) Remove

func (s *Store) Remove(id string) error

Remove deletes an item from the store.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL