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

build: add support for configurable logging policies #420

Merged
merged 2 commits into from
May 2, 2023
Merged
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
1 change: 1 addition & 0 deletions docs/md/melange_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ melange build [flags]
--guest-dir string directory used for the build environment guest
-h, --help help for build
-k, --keyring-append strings path to extra keys to include in the build environment keyring
--log-policy strings logging policy to use (default [builtin:stderr])
--namespace string namespace to use in package URLs in SBOM (eg wolfi, alpine) (default "unknown")
--out-dir string directory where packages will be output (default "./packages/")
--overlay-binsh string use specified file as /bin/sh overlay in build environment
Expand Down
26 changes: 21 additions & 5 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
apko_build "chainguard.dev/apko/pkg/build"
apko_oci "chainguard.dev/apko/pkg/build/oci"
apko_types "chainguard.dev/apko/pkg/build/types"
apko_iocomb "chainguard.dev/apko/pkg/iocomb"
apko_log "chainguard.dev/apko/pkg/log"
"k8s.io/kube-openapi/pkg/util/sets"

Expand Down Expand Up @@ -327,6 +328,7 @@ type Context struct {
imgDigest name.Digest
containerConfig *container.Config
Debug bool
LogPolicy []string

EnabledBuildOptions []string
}
Expand All @@ -342,17 +344,13 @@ type Dependencies struct {
var ErrSkipThisArch = errors.New("error: skip this arch")

func New(opts ...Option) (*Context, error) {
logger := &apko_log.Adapter{
Out: os.Stderr,
Level: apko_log.InfoLevel,
}

ctx := Context{
WorkspaceIgnore: ".melangeignore",
SourceDir: ".",
OutDir: ".",
CacheDir: "./melange-cache/",
Arch: apko_types.ParseArchitecture(runtime.GOARCH),
LogPolicy: []string{"builtin:stderr"},
}

for _, opt := range opts {
Expand All @@ -361,6 +359,16 @@ func New(opts ...Option) (*Context, error) {
}
}

writer, err := apko_iocomb.Combine(ctx.LogPolicy)
if err != nil {
return nil, err
}

logger := &apko_log.Adapter{
Out: writer,
Level: apko_log.InfoLevel,
}

fields := apko_log.Fields{
"arch": ctx.Arch.ToAPK(),
}
Expand Down Expand Up @@ -725,6 +733,14 @@ func WithDebug(debug bool) Option {
}
}

// WithLogPolicy sets the logging policy to use during builds.
func WithLogPolicy(policy []string) Option {
return func(ctx *Context) error {
ctx.LogPolicy = policy
return nil
}
}

type ConfigurationParsingOption func(*configOptions)

type configOptions struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Build() *cobra.Command {
var varsFile string
var purlNamespace string
var buildOption []string
var logPolicy []string
var createBuildLog bool
var debug bool

Expand Down Expand Up @@ -89,6 +90,7 @@ func Build() *cobra.Command {
build.WithEnabledBuildOptions(buildOption),
build.WithCreateBuildLog(createBuildLog),
build.WithDebug(debug),
build.WithLogPolicy(logPolicy),
}

if len(args) > 0 {
Expand Down Expand Up @@ -128,6 +130,7 @@ func Build() *cobra.Command {
cmd.Flags().StringVar(&purlNamespace, "namespace", "unknown", "namespace to use in package URLs in SBOM (eg wolfi, alpine)")
cmd.Flags().StringSliceVar(&archstrs, "arch", nil, "architectures to build for (e.g., x86_64,ppc64le,arm64) -- default is all, unless specified in config")
cmd.Flags().StringSliceVar(&buildOption, "build-option", []string{}, "build options to enable")
cmd.Flags().StringSliceVar(&logPolicy, "log-policy", []string{"builtin:stderr"}, "logging policy to use")
cmd.Flags().StringSliceVarP(&extraKeys, "keyring-append", "k", []string{}, "path to extra keys to include in the build environment keyring")
cmd.Flags().StringSliceVarP(&extraRepos, "repository-append", "r", []string{}, "path to extra repositories to include in the build environment")
cmd.Flags().BoolVar(&createBuildLog, "create-build-log", false, "creates a package.log file containing a list of packages that were built by the command")
Expand Down