Skip to content

Commit

Permalink
imgutil(local): Add Logger functionality(buildpacks#282)
Browse files Browse the repository at this point in the history
This commit enhances the Logger functionality by integrating it with the existing
automatic detection of the storage driver type in the NewStore function.
The Logger interface has been added to log warnings related to the containerd storage driver.
Test cases have been created to ensure that warnings are logged correctly when using containerd.

Signed-off-by: Skylar <[email protected]>
  • Loading branch information
wuhenggongzi committed Nov 10, 2024
1 parent 1c31fdb commit 462e720
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
34 changes: 34 additions & 0 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ const someSHA = "sha256:aec070645fe53ee3b3763059376134f058cc337247c978add178b6cc

var localTestRegistry *h.DockerRegistry

type TestLogger struct {
WarnCalled bool
}

func (logger *TestLogger) Warn(msg string) {
logger.WarnCalled = true
}

func TestLocal(t *testing.T) {
localTestRegistry = h.NewDockerRegistry()
localTestRegistry.Start(t)
Expand Down Expand Up @@ -2028,6 +2036,32 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertError(t, err, "daemon response")
})
})
when("logger is set", func() {
when("containerd is used", func() {
testLogger := &TestLogger{}
dockerClient := h.DockerCli(t)
imgName := "test-image"

img, err := local.NewImage(imgName, dockerClient, local.WithLogger(testLogger))
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
err = img.Save()
if err != nil {
t.Fatalf("Failed to save image: %v", err)
}

defer func() {
if err := h.DockerRmi(dockerClient, imgName); err != nil {
t.Errorf("Failed to remove image %s: %v", imgName, err)
}
}()

if !testLogger.WarnCalled {
t.Error("Expected warning to be logged,but it was not.")
}
})
})
})

when("#SaveFile", func() {
Expand Down
6 changes: 5 additions & 1 deletion local/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func NewImage(repoName string, dockerClient DockerClient, ops ...imgutil.ImageOp
baseIdentifier = baseImage.identifier
store = baseImage.layerStore
} else {
store = NewStore(dockerClient)
if options.Logger != nil {
store = NewStore(dockerClient, WithStoreLogger(options.Logger))
} else {
store = NewStore(dockerClient)
}
}

cnbImage, err := imgutil.NewCNBImage(*options)
Expand Down
12 changes: 12 additions & 0 deletions local/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ func WithMediaTypes(m imgutil.MediaTypes) func(*imgutil.ImageOptions) {
func WithPreviousImage(name string) func(*imgutil.ImageOptions) {
return imgutil.WithPreviousImage(name)
}

func WithLogger(logger imgutil.Logger) func(*imgutil.ImageOptions) {
return imgutil.WithLogger(logger)
}

type StoreOption func(*Store)

func WithStoreLogger(logger imgutil.Logger) StoreOption {
return func(s *Store) {
s.Logger = logger
}
}
14 changes: 12 additions & 2 deletions local/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Store struct {
// optional
downloadOnce *sync.Once
onDiskLayersByDiffID map[v1.Hash]annotatedLayer
Logger imgutil.Logger
}

// DockerClient is subset of client.CommonAPIClient required by this package.
Expand All @@ -53,12 +54,18 @@ type annotatedLayer struct {
uncompressedSize int64
}

func NewStore(dockerClient DockerClient) *Store {
return &Store{
func NewStore(dockerClient DockerClient, ops ...StoreOption) *Store {
store := &Store{
dockerClient: dockerClient,
downloadOnce: &sync.Once{},
onDiskLayersByDiffID: make(map[v1.Hash]annotatedLayer),
Logger: nil,
}
for _, op := range ops {
op(store)
}

return store
}

// images
Expand Down Expand Up @@ -95,6 +102,9 @@ func (s *Store) Save(image *Image, withName string, withAdditionalNames ...strin
inspect, err = s.doSave(image, withName)
}
if !canOmitBaseLayers || err != nil {
if s.Logger != nil && !canOmitBaseLayers {
s.Logger.Warn("Containerd is enabled, which will make saving your work more expensive.")
}
if err = image.ensureLayers(); err != nil {
return "", err
}
Expand Down

0 comments on commit 462e720

Please sign in to comment.