-
Notifications
You must be signed in to change notification settings - Fork 798
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
buildah build
: use the same overlay for the context directory for the whole build
#5975
Open
nalind
wants to merge
5
commits into
containers:main
Choose a base branch
from
nalind:overlay-build-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
430783a
overlay: sync the upper directory's timestamp to the lower's, too
nalind 18108e7
overlay: chown()ing the upper dir: ignore EINVAL on overflow IDs
nalind da0e8a8
imagebuildah: use a longer-lived overlay over the build context
nalind 0cf64ac
imagebuildah: try to rein in use of transport names in image specs
nalind c7c4667
iamgebuildah.StageExecutor.runStageMountPoints(): correct an error
nalind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package imagebuildah | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
|
||
"github.com/containers/buildah/define" | ||
"github.com/containers/buildah/internal/tmpdir" | ||
"github.com/containers/buildah/pkg/overlay" | ||
"github.com/containers/storage" | ||
"github.com/opencontainers/selinux/go-selinux/label" | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// platformSetupContextDirectoryOverlay() may set up an overlay _over_ the | ||
// build context directory, and sorts out labeling. Returns either the new | ||
// location which should be used as the base build context or the old location; | ||
// the process label and mount label for the build; a boolean value that | ||
// indicates whether we did, in fact, mount an overlay; a cleanup function | ||
// which should be called when the location is no longer needed (on success); | ||
// and a non-nil fatal error if any of that failed. | ||
func platformSetupContextDirectoryOverlay(store storage.Store, options *define.BuildOptions) (string, string, string, bool, func(), error) { | ||
var succeeded bool | ||
var tmpDir, contentDir string | ||
cleanup := func() { | ||
if contentDir != "" { | ||
if err := overlay.CleanupContent(tmpDir); err != nil { | ||
logrus.Debugf("cleaning up overlay scaffolding for build context directory: %v", err) | ||
} | ||
} | ||
if tmpDir != "" { | ||
if err := os.Remove(tmpDir); err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
logrus.Debugf("removing should-be-empty temporary directory %q: %v", tmpDir, err) | ||
} | ||
} | ||
} | ||
defer func() { | ||
if !succeeded { | ||
cleanup() | ||
} | ||
}() | ||
// double-check that the context directory location is an absolute path | ||
contextDirectoryAbsolute, err := filepath.Abs(options.ContextDirectory) | ||
if err != nil { | ||
return "", "", "", false, nil, fmt.Errorf("determining absolute path of %q: %w", options.ContextDirectory, err) | ||
} | ||
var st unix.Stat_t | ||
if err := unix.Stat(contextDirectoryAbsolute, &st); err != nil { | ||
return "", "", "", false, nil, fmt.Errorf("checking ownership of %q: %w", options.ContextDirectory, err) | ||
} | ||
// figure out the labeling situation | ||
processLabel, mountLabel, err := label.InitLabels(options.CommonBuildOpts.LabelOpts) | ||
if err != nil { | ||
return "", "", "", false, nil, err | ||
} | ||
// create a temporary directory | ||
tmpDir, err = os.MkdirTemp(tmpdir.GetTempDir(), "buildah-context-") | ||
if err != nil { | ||
return "", "", "", false, nil, fmt.Errorf("creating temporary directory: %w", err) | ||
} | ||
// create the scaffolding for an overlay mount under it | ||
contentDir, err = overlay.TempDir(tmpDir, 0, 0) | ||
if err != nil { | ||
return "", "", "", false, nil, fmt.Errorf("creating overlay scaffolding for build context directory: %w", err) | ||
} | ||
// mount an overlay that uses it as a lower | ||
overlayOptions := overlay.Options{ | ||
GraphOpts: slices.Clone(store.GraphOptions()), | ||
ForceMount: true, | ||
MountLabel: mountLabel, | ||
} | ||
targetDir := filepath.Join(contentDir, "target") | ||
contextDirMountSpec, err := overlay.MountWithOptions(contentDir, contextDirectoryAbsolute, targetDir, &overlayOptions) | ||
if err != nil { | ||
return "", "", "", false, nil, fmt.Errorf("creating overlay scaffolding for build context directory: %w", err) | ||
} | ||
// going forward, pretend that the merged directory is the actual context directory | ||
logrus.Debugf("mounted an overlay at %q over %q", contextDirMountSpec.Source, contextDirectoryAbsolute) | ||
succeeded = true | ||
return contextDirMountSpec.Source, processLabel, mountLabel, true, cleanup, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build !linux | ||
|
||
package imagebuildah | ||
|
||
import ( | ||
"github.com/containers/buildah/define" | ||
"github.com/containers/storage" | ||
) | ||
|
||
// platformSetupContextDirectoryOverlay() may set up an overlay _over_ the | ||
// build context directory, and sorts out labeling. Returns either the new | ||
// location which should be used as the base build context or the old location; | ||
// the process label and mount label for the build; a boolean value that | ||
// indicates whether we did, in fact, mount an overlay; a cleanup function | ||
// which should be called when the location is no longer needed (on success); | ||
// and a non-nil fatal error if any of that failed. | ||
func platformSetupContextDirectoryOverlay(store storage.Store, options *define.BuildOptions) (string, string, string, bool, func(), error) { | ||
return options.ContextDirectory, "", "", false, func() {}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package imagebuildah | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
var logLevel string | ||
debug := false | ||
if InitReexec() { | ||
return | ||
} | ||
flag.BoolVar(&debug, "debug", false, "turn on debug logging") | ||
flag.StringVar(&logLevel, "log-level", "error", "log level") | ||
flag.Parse() | ||
level, err := logrus.ParseLevel(logLevel) | ||
if err != nil { | ||
logrus.Fatalf("error parsing log level %q: %v", logLevel, err) | ||
} | ||
if debug && level < logrus.DebugLevel { | ||
level = logrus.DebugLevel | ||
} | ||
logrus.SetLevel(level) | ||
os.Exit(m.Run()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return values are a bit unwieldy, maybe a
struct
instead? Although I guess the single caller unpacks them anyways, so doesn't matter. Yeah, one caller, so makes sense as is.