Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug statements to the extender #1298

Merged
merged 3 commits into from
Feb 16, 2024
Merged
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
56 changes: 47 additions & 9 deletions phase/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,36 +140,46 @@
return fmt.Errorf("getting run image to extend: %w", err)
}

origTopLayer, err := topLayer(origBaseImage)
origTopLayer, err := topLayerDigest(origBaseImage, logger)
if err != nil {
return fmt.Errorf("getting original run image top layer: %w", err)
}
logger.Debugf("Original image top layer digest: %s", origTopLayer)

extendedImage, err := e.extend(buildpack.DockerfileKindRun, origBaseImage, logger)
if err != nil {
return fmt.Errorf("extending run image: %w", err)
}

if err = e.saveSparse(extendedImage, origTopLayer); err != nil {
return fmt.Errorf("copying extended image to output directory: %w", err)
if err = e.saveSparse(extendedImage, origTopLayer, logger); err != nil {
return fmt.Errorf("failed to copy extended image to output directory: %w", err)

Check warning on line 155 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L155

Added line #L155 was not covered by tests
}
return e.DockerfileApplier.Cleanup()
}

func topLayer(image v1.Image) (string, error) {
func topLayerDigest(image v1.Image, logger log.Logger) (string, error) {
imageHash, err := image.Digest()
if err != nil {
return "", err
}

Check warning on line 164 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L163-L164

Added lines #L163 - L164 were not covered by tests

manifest, err := image.Manifest()
if err != nil {
return "", fmt.Errorf("getting image manifest: %w", err)
}
layers := manifest.Layers
if len(layers) == 0 {

allLayers := manifest.Layers
logger.Debugf("Found %d layers in original image with digest: %s", len(allLayers), imageHash)

if len(allLayers) == 0 {
return "", nil
}
layer := layers[len(layers)-1]

layer := allLayers[len(allLayers)-1]

Check warning on line 178 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L178

Added line #L178 was not covered by tests
return layer.Digest.String(), nil
}

func (e *Extender) saveSparse(image v1.Image, origTopLayerHash string) error {
func (e *Extender) saveSparse(image v1.Image, origTopLayerHash string, logger log.Logger) error {
// save sparse image (manifest and config)
imageHash, err := image.Digest()
if err != nil {
Expand All @@ -189,6 +199,8 @@
if err != nil {
return fmt.Errorf("getting image layers: %w", err)
}
logger.Debugf("Found %d layers in extended image with digest: %s", len(allLayers), imageHash)

var (
currentHash v1.Hash
needsCopying bool
Expand All @@ -205,13 +217,16 @@
switch {
case needsCopying:
currentLayer := currentLayer // allow use in closure
logger.Debugf("Copying top layer with digest: %s", currentHash)

Check warning on line 220 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L220

Added line #L220 was not covered by tests
group.Go(func() error {
return copyLayer(currentLayer, toPath)
})
case currentHash.String() == origTopLayerHash:
logger.Debugf("Found original top layer with digest: %s", currentHash)

Check warning on line 225 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L225

Added line #L225 was not covered by tests
needsCopying = true
continue
default:
logger.Debugf("Skipping base layer with digest: %s", currentHash)

Check warning on line 229 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L229

Added line #L229 was not covered by tests
continue
}
}
Expand Down Expand Up @@ -256,6 +271,12 @@
rebasable = true // we don't require the initial base image to have io.buildpacks.rebasable=true
workingHistory []v1.History
)
digest, err := baseImage.Digest()
if err != nil {
return nil, err
}

Check warning on line 277 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L276-L277

Added lines #L276 - L277 were not covered by tests
logger.Debugf("Original image has digest: %s", digest)

// get config
baseImage, err = imgutil.OverrideHistoryIfNeeded(baseImage)
if err != nil {
Expand Down Expand Up @@ -284,6 +305,12 @@
); err != nil {
return nil, fmt.Errorf("applying Dockerfile to image: %w", err)
}
digest, err = baseImage.Digest()
if err != nil {
return nil, err
}

Check warning on line 311 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L310-L311

Added lines #L310 - L311 were not covered by tests
logger.Debugf("Intermediate image has digest: %s", digest)

// update rebasable, history in config, and user/group IDs
configFile, err = baseImage.ConfigFile()
if err != nil || configFile == nil {
Expand Down Expand Up @@ -320,10 +347,21 @@
if userID != origUserID {
logger.Warnf("The original user ID was %s but the final extension left the user ID set to %s.", origUserID, userID)
}
// build images don't mutate the config
if kind == buildpack.DockerfileKindBuild {
return baseImage, nil
}
return mutate.ConfigFile(baseImage, configFile)
// run images mutate the config
baseImage, err = mutate.ConfigFile(baseImage, configFile)
if err != nil {
return nil, err
}

Check warning on line 358 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L357-L358

Added lines #L357 - L358 were not covered by tests
digest, err = baseImage.Digest()
if err != nil {
return nil, err
}

Check warning on line 362 in phase/extender.go

View check run for this annotation

Codecov / codecov/patch

phase/extender.go#L361-L362

Added lines #L361 - L362 were not covered by tests
logger.Debugf("Final extended image has digest: %s", digest)
return baseImage, nil
}

func userFrom(config v1.ConfigFile) (string, string) {
Expand Down
Loading