Skip to content

Commit

Permalink
chore: unexport build pkg methods
Browse files Browse the repository at this point in the history
These methods are used only in the build package.

Also delete unused functions.

Signed-off-by: Dan Luhring <[email protected]>
  • Loading branch information
luhring committed Oct 24, 2024
1 parent 6e4cf63 commit 0c22153
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 49 deletions.
78 changes: 33 additions & 45 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func New(ctx context.Context, opts ...Option) (*Build, error) {
log.Infof("applying configuration patches for build option %s", optName)

if opt, ok := b.Configuration.Options[optName]; ok {
if err := b.ApplyBuildOption(opt); err != nil {
if err := b.applyBuildOption(opt); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -270,11 +270,11 @@ func (b *Build) Close(ctx context.Context) error {
return errors.Join(errs...)
}

// BuildGuest invokes apko to build the guest environment, returning a reference to the image
// buildGuest invokes apko to build the guest environment, returning a reference to the image
// loaded by the OCI Image loader.
func (b *Build) BuildGuest(ctx context.Context, imgConfig apko_types.ImageConfiguration, guestFS apkofs.FullFS) (string, error) {
func (b *Build) buildGuest(ctx context.Context, imgConfig apko_types.ImageConfiguration, guestFS apkofs.FullFS) (string, error) {
log := clog.FromContext(ctx)
ctx, span := otel.Tracer("melange").Start(ctx, "BuildGuest")
ctx, span := otel.Tracer("melange").Start(ctx, "buildGuest")
defer span.End()

tmp, err := os.MkdirTemp(os.TempDir(), "apko-temp-*")
Expand Down Expand Up @@ -303,7 +303,7 @@ func (b *Build) BuildGuest(ctx context.Context, imgConfig apko_types.ImageConfig
}

bc.Summarize(ctx)
log.Infof("auth configured for: %s", maps.Keys(b.Auth)) // TODO: add this to Summarize
log.Infof("auth configured for: %s", maps.Keys(b.Auth)) // TODO: add this to summarize

// lay out the contents for the image in a directory.
if err := bc.BuildImage(ctx); err != nil {
Expand Down Expand Up @@ -364,8 +364,8 @@ func copyFile(base, src, dest string, perm fs.FileMode) error {
return nil
}

// ApplyBuildOption applies a patch described by a BuildOption to a package build.
func (b *Build) ApplyBuildOption(bo config.BuildOption) error {
// applyBuildOption applies a patch described by a BuildOption to a package build.
func (b *Build) applyBuildOption(bo config.BuildOption) error {
// Patch the variables block.
if b.Configuration.Vars == nil {
b.Configuration.Vars = make(map[string]string)
Expand Down Expand Up @@ -435,7 +435,7 @@ func (b *Build) loadIgnoreRules(ctx context.Context) ([]*xignore.Pattern, error)
return ignorePatterns, nil
}

func (b *Build) OverlayBinSh() error {
func (b *Build) overlayBinSh() error {
if b.BinShOverlay == "" {
return nil
}
Expand Down Expand Up @@ -521,10 +521,10 @@ func fetchBucket(ctx context.Context, cacheSource string, cmm CacheMembershipMap
return tmp, nil
}

// IsBuildLess returns true if the build context does not actually do any building.
// isBuildLess returns true if the build context does not actually do any building.
// TODO(kaniini): Improve the heuristic for this by checking for uses/runs statements
// in the pipeline.
func (b *Build) IsBuildLess() bool {
func (b *Build) isBuildLess() bool {
return len(b.Configuration.Pipeline) == 0
}

Expand All @@ -549,9 +549,9 @@ func (b Build) getBuildConfigPURL() (*purl.PackageURL, error) {
return u, nil
}

func (b *Build) PopulateCache(ctx context.Context) error {
func (b *Build) populateCache(ctx context.Context) error {
log := clog.FromContext(ctx)
ctx, span := otel.Tracer("melange").Start(ctx, "PopulateCache")
ctx, span := otel.Tracer("melange").Start(ctx, "populateCache")
defer span.End()

if b.CacheDir == "" {
Expand Down Expand Up @@ -625,9 +625,9 @@ func (b *Build) PopulateCache(ctx context.Context) error {
return nil
}

func (b *Build) PopulateWorkspace(ctx context.Context, src fs.FS) error {
func (b *Build) populateWorkspace(ctx context.Context, src fs.FS) error {
log := clog.FromContext(ctx)
_, span := otel.Tracer("melange").Start(ctx, "PopulateWorkspace")
_, span := otel.Tracer("melange").Start(ctx, "populateWorkspace")
defer span.End()

ignorePatterns, err := b.loadIgnoreRules(ctx)
Expand Down Expand Up @@ -676,7 +676,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
ctx, span := otel.Tracer("melange").Start(ctx, "BuildPackage")
defer span.End()

b.Summarize(ctx)
b.summarize(ctx)

namespace := b.Namespace
if namespace == "" {
Expand Down Expand Up @@ -762,7 +762,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
pr := &pipelineRunner{
interactive: b.Interactive,
debug: b.Debug,
config: b.WorkspaceConfig(ctx),
config: b.workspaceConfig(ctx),
runner: b.Runner,
}

Expand All @@ -775,7 +775,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
}

log.Infof("populating workspace %s from %s", b.WorkspaceDir, b.SourceDir)
if err := b.PopulateWorkspace(ctx, os.DirFS(b.SourceDir)); err != nil {
if err := b.populateWorkspace(ctx, os.DirFS(b.SourceDir)); err != nil {
return fmt.Errorf("unable to populate workspace: %w", err)
}
}
Expand All @@ -785,9 +785,9 @@ func (b *Build) BuildPackage(ctx context.Context) error {
}

linterQueue := []linterTarget{}
cfg := b.WorkspaceConfig(ctx)
cfg := b.workspaceConfig(ctx)

if !b.IsBuildLess() {
if !b.isBuildLess() {
// Prepare guest directory
if err := os.MkdirAll(b.GuestDir, 0755); err != nil {
return fmt.Errorf("mkdir -p %s: %w", b.GuestDir, err)
Expand All @@ -796,7 +796,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
log.Infof("building workspace in '%s' with apko", b.GuestDir)

guestFS := apkofs.DirFS(b.GuestDir, apkofs.WithCreateDir())
imgRef, err := b.BuildGuest(ctx, b.Configuration.Environment, guestFS)
imgRef, err := b.buildGuest(ctx, b.Configuration.Environment, guestFS)
if err != nil {
return fmt.Errorf("unable to build guest: %w", err)
}
Expand All @@ -806,11 +806,11 @@ func (b *Build) BuildPackage(ctx context.Context) error {

// TODO(kaniini): Make overlay-binsh work with Docker and Kubernetes.
// Probably needs help from apko.
if err := b.OverlayBinSh(); err != nil {
if err := b.overlayBinSh(); err != nil {
return fmt.Errorf("unable to install overlay /bin/sh: %w", err)
}

if err := b.PopulateCache(ctx); err != nil {
if err := b.populateCache(ctx); err != nil {
return fmt.Errorf("unable to populate cache: %w", err)
}

Expand Down Expand Up @@ -858,7 +858,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
// run any pipelines for subpackages
for _, sp := range b.Configuration.Subpackages {
sp := sp
if !b.IsBuildLess() {
if !b.isBuildLess() {
log.Infof("running pipeline for subpackage %s", sp.Name)

ctx := clog.WithLogger(ctx, log.With("subpackage", sp.Name))
Expand All @@ -883,7 +883,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
// Retrieve the post build workspace from the runner
log.Infof("retrieving workspace from builder: %s", cfg.PodID)
fsys := apkofs.DirFS(b.WorkspaceDir)
if err := b.RetrieveWorkspace(ctx, fsys); err != nil {
if err := b.retrieveWorkspace(ctx, fsys); err != nil {
return fmt.Errorf("retrieving workspace: %w", err)
}
log.Infof("retrieved and wrote post-build workspace to: %s", b.WorkspaceDir)
Expand Down Expand Up @@ -945,7 +945,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
}
}

if !b.IsBuildLess() {
if !b.isBuildLess() {
// clean build guest container
if err := os.RemoveAll(b.GuestDir); err != nil {
log.Warnf("unable to clean guest container: %s", err)
Expand Down Expand Up @@ -1058,37 +1058,25 @@ func (b *Build) SummarizePaths(ctx context.Context) {
}
}

func (b *Build) Summarize(ctx context.Context) {
func (b *Build) summarize(ctx context.Context) {
log := clog.FromContext(ctx)
log.Infof("melange is building:")
log.Infof(" configuration file: %s", b.ConfigFile)
b.SummarizePaths(ctx)
}

// BuildFlavor determines if a build context uses glibc or musl, it returns
// buildFlavor determines if a build context uses glibc or musl, it returns
// "gnu" for GNU systems, and "musl" for musl systems.
func (b *Build) BuildFlavor() string {
func (b *Build) buildFlavor() string {
if b.Libc == "" {
return "gnu"
}
return b.Libc
}

// BuildTripletGnu returns the GNU autoconf build triplet, for example
// `x86_64-pc-linux-gnu`.
func (b *Build) BuildTripletGnu() string {
return b.Arch.ToTriplet(b.BuildFlavor())
}

// BuildTripletRust returns the Rust/Cargo build triplet, for example
// `x86_64-unknown-linux-gnu`.
func (b *Build) BuildTripletRust() string {
return b.Arch.ToRustTriplet(b.BuildFlavor())
}

func (b *Build) buildWorkspaceConfig(ctx context.Context) *container.Config {
log := clog.FromContext(ctx)
if b.IsBuildLess() {
if b.isBuildLess() {
return &container.Config{
Arch: b.Arch,
WorkspaceDir: b.WorkspaceDir,
Expand Down Expand Up @@ -1144,19 +1132,19 @@ func (b *Build) buildWorkspaceConfig(ctx context.Context) *container.Config {
return &cfg
}

func (b *Build) WorkspaceConfig(ctx context.Context) *container.Config {
func (b *Build) workspaceConfig(ctx context.Context) *container.Config {
if b.containerConfig == nil {
b.containerConfig = b.buildWorkspaceConfig(ctx)
}

return b.containerConfig
}

// RetrieveWorkspace retrieves the workspace from the container and unpacks it
// retrieveWorkspace retrieves the workspace from the container and unpacks it
// to the workspace directory. The workspace retrieved from the runner is in a
// tar stream containing the workspace contents rooted at ./melange-out
func (b *Build) RetrieveWorkspace(ctx context.Context, fs apkofs.FullFS) error {
ctx, span := otel.Tracer("melange").Start(ctx, "RetrieveWorkspace")
func (b *Build) retrieveWorkspace(ctx context.Context, fs apkofs.FullFS) error {
ctx, span := otel.Tracer("melange").Start(ctx, "retrieveWorkspace")
defer span.End()

r, err := b.Runner.WorkspaceTar(ctx, b.containerConfig)
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (t *Test) Compile(ctx context.Context) error {
// Compile compiles all configuration, including tests, by loading any pipelines and substituting all variables.
func (b *Build) Compile(ctx context.Context) error {
cfg := b.Configuration
sm, err := NewSubstitutionMap(&cfg, b.Arch, b.BuildFlavor(), b.EnabledBuildOptions)
sm, err := NewSubstitutionMap(&cfg, b.Arch, b.buildFlavor(), b.EnabledBuildOptions)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/build/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (t *Test) Close() error {
// Returns the imgRef for the created image, or error.
func (t *Test) BuildGuest(ctx context.Context, imgConfig apko_types.ImageConfiguration, guestFS apkofs.FullFS) (string, error) {
log := clog.FromContext(ctx)
ctx, span := otel.Tracer("melange").Start(ctx, "BuildGuest")
ctx, span := otel.Tracer("melange").Start(ctx, "buildGuest")
defer span.End()

tmp, err := os.MkdirTemp(os.TempDir(), "apko-temp-*")
Expand Down Expand Up @@ -229,7 +229,7 @@ func (t *Test) IsTestless() bool {

func (t *Test) PopulateCache(ctx context.Context) error {
log := clog.FromContext(ctx)
ctx, span := otel.Tracer("melange").Start(ctx, "PopulateCache")
ctx, span := otel.Tracer("melange").Start(ctx, "populateCache")
defer span.End()

if t.CacheDir == "" {
Expand Down Expand Up @@ -300,7 +300,7 @@ func (t *Test) PopulateCache(ctx context.Context) error {

func (t *Test) PopulateWorkspace(ctx context.Context, src fs.FS) error {
log := clog.FromContext(ctx)
_, span := otel.Tracer("melange").Start(ctx, "PopulateWorkspace")
_, span := otel.Tracer("melange").Start(ctx, "populateWorkspace")
defer span.End()

if t.SourceDir == "" {
Expand Down

0 comments on commit 0c22153

Please sign in to comment.