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

fix: make sure fill defualts and expand args for all types #481

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
246 changes: 246 additions & 0 deletions deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package dalec

import (
goerrors "errors"
"slices"

"github.com/pkg/errors"
)

// PackageConstraints is used to specify complex constraints for a package dependency.
type PackageConstraints struct {
// Version is a list of version constraints for the package.
// The format of these strings is dependent on the package manager of the target system.
// Examples:
// [">=1.0.0", "<2.0.0"]
Version []string `yaml:"version,omitempty" json:"version,omitempty"`
// Arch is a list of architecture constraints for the package.
// Use this to specify that a package constraint only applies to certain architectures.
Arch []string `yaml:"arch,omitempty" json:"arch,omitempty"`
}

// PackageDependencies is a list of dependencies for a package.
// This will be included in the package metadata so that the package manager can install the dependencies.
// It also includes build-time dedendencies, which we'll install before running any build steps.
type PackageDependencies struct {
// Build is the list of packagese required to build the package.
Build map[string]PackageConstraints `yaml:"build,omitempty" json:"build,omitempty"`
// Runtime is the list of packages required to install/run the package.
Runtime map[string]PackageConstraints `yaml:"runtime,omitempty" json:"runtime,omitempty"`
// Recommends is the list of packages recommended to install with the generated package.
// Note: Not all package managers support this (e.g. rpm)
Recommends map[string]PackageConstraints `yaml:"recommends,omitempty" json:"recommends,omitempty"`

// Test lists any extra packages required for running tests
// These packages are only installed for tests which have steps that require
// running a command in the built container.
// See [TestSpec] for more information.
Test []string `yaml:"test,omitempty" json:"test,omitempty"`

// ExtraRepos is used to inject extra package repositories that may be used to
// satisfy package dependencies in various stages.
ExtraRepos []PackageRepositoryConfig `yaml:"extra_repos,omitempty" json:"extra_repos,omitempty"`
}

// PackageRepositoryConfig
type PackageRepositoryConfig struct {
// Keys are the list of keys that need to be imported to use the configured
// repositories
Keys map[string]Source `yaml:"keys,omitempty" json:"keys,omitempty"`

// Config list of repo configs to to add to the environment. The format of
// these configs are distro specific (e.g. apt/yum configs).
Config map[string]Source `yaml:"config" json:"config"`

// Data lists all the extra data that needs to be made available for the
// provided repository config to work.
// As an example, if the provided config is referencing a file backed repository
// then data would include the file data, assuming its not already available
// in the environment.
Data []SourceMount `yaml:"data,omitempty" json:"data,omitempty"`
// Envs specifies the list of environments to make the repositories available
// during.
// Acceptable values are:
// - "build" - Repositories are added prior to installing build dependencies
// - "test" - Repositories are added prior to installing test dependencies
// - "install" - Repositories are added prior to installing the output
// package in a container build target.
Envs []string `yaml:"envs" json:"envs" jsonschema:"enum=build,enum=test,enum=install"`
}

func (d *PackageDependencies) processBuildArgs(args map[string]string, allowArg func(string) bool) error {
if d == nil {
return nil
}

var errs []error
for i, repo := range d.ExtraRepos {
if err := repo.processBuildArgs(args, allowArg); err != nil {
errs = append(errs, errors.Wrapf(err, "extra repos index %d", i))
}
d.ExtraRepos[i] = repo
}
return goerrors.Join(errs...)
}

func (r *PackageRepositoryConfig) processBuildArgs(args map[string]string, allowArg func(string) bool) error {
if r == nil {
return nil
}

var errs []error

for k := range r.Config {
src := r.Config[k]
if err := src.processBuildArgs(args, allowArg); err != nil {
errs = append(errs, errors.Wrapf(err, "config %s", k))
continue
}
r.Config[k] = src
}

for k := range r.Keys {
src := r.Keys[k]
if err := src.processBuildArgs(args, allowArg); err != nil {
errs = append(errs, errors.Wrapf(err, "key %s", k))
continue
}
r.Keys[k] = src
}

for i := range r.Data {
d := r.Data[i]
if err := d.processBuildArgs(args, allowArg); err != nil {
errs = append(errs, errors.Wrapf(err, "data index %d", i))
continue
}
r.Data[i] = d
}

return goerrors.Join(errs...)
}

func (d *PackageDependencies) fillDefaults() {
if d == nil {
return
}

for i, r := range d.ExtraRepos {
r.fillDefaults()
d.ExtraRepos[i] = r
}
}

func (r *PackageRepositoryConfig) fillDefaults() {
if len(r.Envs) == 0 {
// default to all stages for the extra repo if unspecified
r.Envs = []string{"build", "install", "test"}
}

for i, src := range r.Config {
fillDefaults(&src)
r.Config[i] = src
}

for i, src := range r.Keys {
fillDefaults(&src)

// Default to 0644 permissions for gpg keys. This is because apt will will only import
// keys with a particular permission set.
if src.HTTP != nil {
src.HTTP.Permissions = 0644
}
r.Keys[i] = src
}

for i, mount := range r.Data {
mount.fillDefaults()
r.Data[i] = mount
}
}

func (d *PackageDependencies) validate() error {
if d == nil {
return nil
}

var errs []error
for i, r := range d.ExtraRepos {
if err := r.validate(); err != nil {
errs = append(errs, errors.Wrapf(err, "extra repo %d", i))
}
}

return goerrors.Join(errs...)
}

func (r *PackageRepositoryConfig) validate() error {
var errs []error
for name, src := range r.Keys {
if err := src.validate(); err != nil {
errs = append(errs, errors.Wrapf(err, "key %s", name))
}
}
for name, src := range r.Config {
if err := src.validate(); err != nil {
errs = append(errs, errors.Wrapf(err, "config %s", name))
}
}
for _, mnt := range r.Data {
if err := mnt.validate("/"); err != nil {
errs = append(errs, errors.Wrapf(err, "data mount path %s", mnt.Dest))
}
}

return goerrors.Join(errs...)
}

func (p *PackageDependencies) GetExtraRepos(env string) []PackageRepositoryConfig {
return GetExtraRepos(p.ExtraRepos, env)
}

func GetExtraRepos(repos []PackageRepositoryConfig, env string) []PackageRepositoryConfig {
var out []PackageRepositoryConfig
for _, repo := range repos {
if slices.Contains(repo.Envs, env) {
out = append(repos, repo)
}
}
return out
}

func (s *Spec) GetBuildRepos(targetKey string) []PackageRepositoryConfig {
deps := s.GetPackageDeps(targetKey)
if deps == nil {
deps = s.Dependencies
if deps == nil {
return nil
}
}

return deps.GetExtraRepos("build")
}

func (s *Spec) GetInstallRepos(targetKey string) []PackageRepositoryConfig {
deps := s.GetPackageDeps(targetKey)
if deps == nil {
deps = s.Dependencies
if deps == nil {
return nil
}
}

return deps.GetExtraRepos("install")
}

func (s *Spec) GetTestRepos(targetKey string) []PackageRepositoryConfig {
deps := s.GetPackageDeps(targetKey)
if deps == nil {
deps = s.Dependencies
if deps == nil {
return nil
}
}

return deps.GetExtraRepos("test")
}
36 changes: 34 additions & 2 deletions frontend/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,39 @@ import (
"github.com/pkg/errors"
)

func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform) (*dalec.Spec, error) {
type LoadConfig struct {
SubstituteOpts []dalec.SubstituteOpt
}

type LoadOpt func(*LoadConfig)

func WithAllowArgs(args ...string) LoadOpt {
return func(cfg *LoadConfig) {
set := make(map[string]struct{}, len(args))
for _, arg := range args {
set[arg] = struct{}{}
}
cfg.SubstituteOpts = append(cfg.SubstituteOpts, func(cfg *dalec.SubstituteConfig) {
orig := cfg.AllowArg

cfg.AllowArg = func(key string) bool {
if orig != nil && orig(key) {
return true
}
_, ok := set[key]
return ok
}
})
}
}

func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform, opts ...LoadOpt) (*dalec.Spec, error) {
cfg := LoadConfig{}

for _, o := range opts {
o(&cfg)
}

src, err := client.ReadEntrypoint(ctx, "Dockerfile")
if err != nil {
return nil, fmt.Errorf("could not read spec file: %w", err)
Expand All @@ -35,7 +67,7 @@ func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.P
fillPlatformArgs("TARGET", args, *platform)
fillPlatformArgs("BUILD", args, client.BuildPlatforms[0])

if err := spec.SubstituteArgs(args); err != nil {
if err := spec.SubstituteArgs(args, cfg.SubstituteOpts...); err != nil {
return nil, errors.Wrap(err, "error resolving build args")
}
return spec, nil
Expand Down
6 changes: 5 additions & 1 deletion frontend/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ func (m *BuildMux) loadSpec(ctx context.Context, client gwclient.Client) (*dalec
}

// Note: this is not suitable for passing to builds since it does not have platform information
spec, err := LoadSpec(ctx, dc, nil)
spec, err := LoadSpec(ctx, dc, nil, func(cfg *LoadConfig) {
// We want to allow any arg to be passed to the spec since we don't know what
// args are valid at this point, nor do we care here.
cfg.SubstituteOpts = append(cfg.SubstituteOpts, dalec.WithAllowAnyArg)
})
if err != nil {
return nil, err
}
Expand Down
50 changes: 0 additions & 50 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,42 +324,6 @@ func (s *Spec) GetBuildDeps(targetKey string) map[string]PackageConstraints {
return deps.Build
}

func (s *Spec) GetBuildRepos(targetKey string) []PackageRepositoryConfig {
deps := s.GetPackageDeps(targetKey)
if deps == nil {
deps = s.Dependencies
if deps == nil {
return nil
}
}

return deps.GetExtraRepos("build")
}

func (s *Spec) GetInstallRepos(targetKey string) []PackageRepositoryConfig {
deps := s.GetPackageDeps(targetKey)
if deps == nil {
deps = s.Dependencies
if deps == nil {
return nil
}
}

return deps.GetExtraRepos("install")
}

func (s *Spec) GetTestRepos(targetKey string) []PackageRepositoryConfig {
deps := s.GetPackageDeps(targetKey)
if deps == nil {
deps = s.Dependencies
if deps == nil {
return nil
}
}

return deps.GetExtraRepos("test")
}

func (s *Spec) GetTestDeps(targetKey string) []string {
var deps *PackageDependencies
if t, ok := s.Targets[targetKey]; ok {
Expand Down Expand Up @@ -623,17 +587,3 @@ func BaseImageConfig(platform *ocispecs.Platform) *DockerImageSpec {

return img
}

func (p *PackageDependencies) GetExtraRepos(env string) []PackageRepositoryConfig {
return GetExtraRepos(p.ExtraRepos, env)
}

func GetExtraRepos(repos []PackageRepositoryConfig, env string) []PackageRepositoryConfig {
var out []PackageRepositoryConfig
for _, repo := range repos {
if slices.Contains(repo.Envs, env) {
out = append(repos, repo)
}
}
return out
}
Loading
Loading