Skip to content

Commit

Permalink
Merge pull request #420 from chainguard-dev/feature/log-policy
Browse files Browse the repository at this point in the history
build: add support for configurable logging policies
  • Loading branch information
kaniini authored May 2, 2023
2 parents 40098bf + a4091a4 commit b996a3e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
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

0 comments on commit b996a3e

Please sign in to comment.