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 .apko.lock.json to APK control section #802

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ type Build struct {
FailOnLintWarning bool

EnabledBuildOptions []string

resolvedApkoConfig apko_types.ImageConfiguration
}

var ErrSkipThisArch = errors.New("error: skip this arch")
Expand Down Expand Up @@ -538,6 +540,26 @@ func WithPackageCacheDir(apkCacheDir string) Option {
}
}

func (b *Build) resolveConfig(ctx context.Context, bc *apko_build.Context) error {
ic := bc.ImageConfiguration()
pkgs, _, err := bc.BuildPackageList(ctx)
if err != nil {
return fmt.Errorf("resolving package versions: %w", err)
}
resolved := make([]string, 0, len(pkgs))
for _, pkg := range pkgs {
resolved = append(resolved, fmt.Sprintf("%s=%s", pkg.Name, pkg.Version))
}
ic.Contents.Packages = resolved
ic.Archs = []apko_types.Architecture{b.Arch}
ic.Contents.Repositories = append(ic.Contents.Repositories, b.ExtraRepos...)
ic.Contents.Keyring = append(ic.Contents.Keyring, b.ExtraKeys...)
Comment on lines +544 to +556
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine for now, but can I request that we hoist the resolution / locking logic into a shared place so that this and tf-apko and anywhere else we do this don't diverge?

It'd actually also make a great apko sub-comment to produce the resolved config (I know we have show-packages, but I'm thinking something focused around this logic specifically).


b.resolvedApkoConfig = ic

return nil
}

// BuildGuest invokes apko to build the guest environment.
func (b *Build) BuildGuest(ctx context.Context) error {
ctx, span := otel.Tracer("melange").Start(ctx, "BuildGuest")
Expand Down Expand Up @@ -571,6 +593,10 @@ func (b *Build) BuildGuest(ctx context.Context) error {

bc.Summarize()

if err := b.resolveConfig(ctx, bc); err != nil {
return fmt.Errorf("locking apko config: %w", err)
}

// lay out the contents for the image in a directory.
if err := bc.BuildImage(ctx); err != nil {
return fmt.Errorf("unable to generate image: %w", err)
Expand Down
8 changes: 8 additions & 0 deletions pkg/build/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ func (pc *PackageBuild) generateControlSection(ctx context.Context) ([]byte, err
return nil, fmt.Errorf("unable to build control FS: %w", err)
}

locked, err := json.Marshal(pc.Build.resolvedApkoConfig)
if err != nil {
return nil, fmt.Errorf("unable to marshal apko config: %w", err)
}
if err := fsys.WriteFile(".apko.lock.json", locked, 0644); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i know this is a bike-shed, but...) WDYT about .LOCK.apko.json of .BUILDLOCK.apko.json 🤔

My thinking is that we use .apko.yaml elsewhere to indicate things are the apko dialect of yaml. Here it's json, but similar idea. The "build" qualifier is so it's clearer what it's the locked apko configuration of.

return nil, fmt.Errorf("unable to write .apko.lock.json: %w", err)
}

if pc.Scriptlets.Trigger.Script != "" {
// #nosec G306 -- scriptlets must be executable
if err := fsys.WriteFile(".trigger", []byte(pc.Scriptlets.Trigger.Script), 0755); err != nil {
Expand Down
Loading