Skip to content

Commit

Permalink
simplify logging to use slog
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <jason@chainguard.dev>
  • Loading branch information
imjasonh committed Jan 16, 2024
1 parent a01a42a commit 8494232
Show file tree
Hide file tree
Showing 39 changed files with 416 additions and 916 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0
- run: |
make apko
./apko build ./examples/nginx.yaml nginx:build /tmp/nginx-${{ matrix.arch }}.tar --debug --arch ${{ matrix.arch }}
./apko build ./examples/nginx.yaml nginx:build /tmp/nginx-${{ matrix.arch }}.tar --arch ${{ matrix.arch }}
- name: Check SBOM Conformance
run: |
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
go-version-file: 'go.mod'
- run: |
make apko
./apko build ./examples/nginx.yaml nginx:build /tmp/nginx.tar --debug
./apko build ./examples/nginx.yaml nginx:build /tmp/nginx.tar
- name: Check SBOM Conformance
run: |
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
make apko
for cfg in $(find ./examples/ -name '*.yaml'); do
name=$(basename ${cfg} .yaml)
./apko build ${cfg} ${name}:build /tmp/${name}.tar --debug --arch amd64
./apko build ${cfg} ${name}:build /tmp/${name}.tar --arch amd64
done
build-alpine-source-date-epoch:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ bin/
.idea
tmp/
_output

# SBOM outputs from apko
sbom-*
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ apko publish examples/alpine-base.yaml myrepo/alpine-apko:test

See the [docs](./docs/apko_file.md) for details of the file format and the [examples directory](./examples) for more, err, examples!

## Debugging apko Builds

To include debug-level information on apko builds, add `--debug` to your build command:

```shell
docker run --rm -v ${PWD}:/work cgr.dev/chainguard/apko build --debug \
apko.yaml hello-minicli:test hello-minicli.tar \
-k melange.rsa.pub
```

## Why

apko was created by [Chainguard](https://www.chainguard.dev), who require secure and reproducible
Expand Down Expand Up @@ -143,7 +133,7 @@ Tutorials and guides for apko can be found at the [Chainguard Academy](https://e

For support, please find us on the [Kubernetes Slack](https://kubernetes.slack.com/) in the #apko
channel or [open an issue](https://github.com/chainguard-dev/apko/issue).

## Related Work and Resources

The [melange project](https://github.com/chainguard-dev/melange) is designed to produce apk packages to be used in apko.
Expand Down
17 changes: 10 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module chainguard.dev/apko

go 1.21
go 1.21.2

toolchain go1.21.5

require (
github.com/chainguard-dev/go-apk v0.0.0-20231218235333-2acefacd5846
github.com/chainguard-dev/clog v1.2.3-0.20240116182827-04bee692f7a8
github.com/chainguard-dev/go-apk v0.0.0-20240116193855-4c76fbe27ad7
github.com/dominodatalab/os-release v0.0.0-20190522011736-bcdb4a3e3c2f
github.com/go-git/go-git/v5 v5.11.0
github.com/google/go-cmp v0.6.0
Expand All @@ -14,15 +17,14 @@ require (
github.com/klauspost/pgzip v1.2.6
github.com/package-url/packageurl-go v0.1.2
github.com/sigstore/cosign/v2 v2.2.1
github.com/sirupsen/logrus v1.9.3
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/tmc/dot v0.0.0-20210901225022-f9bc17da75c0
go.opentelemetry.io/otel v1.20.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/sync v0.5.0
golang.org/x/sys v0.15.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/term v0.15.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.28.3
Expand Down Expand Up @@ -67,12 +69,13 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v0.9.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/letsencrypt/boulder v0.0.0-20231026200631-000cd05d5491 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand All @@ -88,6 +91,7 @@ require (
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sigstore/rekor v1.3.3 // indirect
github.com/sigstore/sigstore v1.7.5 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 // indirect
Expand All @@ -98,7 +102,6 @@ require (
go.mongodb.org/mongo-driver v1.12.1 // indirect
go.opentelemetry.io/otel/metric v1.20.0 // indirect
go.opentelemetry.io/otel/trace v1.20.0 // indirect
golang.org/x/build v0.0.0-20220928220451-9294235e16f5 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx2
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chainguard-dev/go-apk v0.0.0-20231218235333-2acefacd5846 h1:5IIBDHIW3qkVVYtwOK6Lsx/loxl+2lHXisPCKGrwBk8=
github.com/chainguard-dev/go-apk v0.0.0-20231218235333-2acefacd5846/go.mod h1:y0BbOQALsoi1T2Lt5KmFNn92G+fRFSUuogQI2171HS8=
github.com/chainguard-dev/clog v1.2.3-0.20240116182827-04bee692f7a8 h1:E2y3L/nM7vRzwyROQgmuB+Egm/d1rHOyip0Bq4AyVow=
github.com/chainguard-dev/clog v1.2.3-0.20240116182827-04bee692f7a8/go.mod h1:cV516KZWqYc/phZsCNwF36u/KMGS+Gj5Uqeb8Hlp95Y=
github.com/chainguard-dev/go-apk v0.0.0-20240116193855-4c76fbe27ad7 h1:lzVhpylUJwoA7eQN6X18ZZXPHovjVDDBXwQkg5qFM9k=
github.com/chainguard-dev/go-apk v0.0.0-20240116193855-4c76fbe27ad7/go.mod h1:OdsmvVJb8RNVcTVQ7x07L319LLeiRaRnnsmj8qBBgb4=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
Expand Down Expand Up @@ -190,8 +192,8 @@ github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -339,8 +341,6 @@ go.opentelemetry.io/otel/metric v1.20.0 h1:ZlrO8Hu9+GAhnepmRGhSU7/VkpjrNowxRN9Gy
go.opentelemetry.io/otel/metric v1.20.0/go.mod h1:90DRw3nfK4D7Sm/75yQ00gTJxtkBxX+wu6YaNymbpVM=
go.opentelemetry.io/otel/trace v1.20.0 h1:+yxVAPZPbQhbC3OfAkeIVTky6iTFpcr4SiY9om7mXSQ=
go.opentelemetry.io/otel/trace v1.20.0/go.mod h1:HJSK7F/hA5RlzpZ0zKDCHCDHm556LCDtKaAo6JmBFUU=
golang.org/x/build v0.0.0-20220928220451-9294235e16f5 h1:g6/rDfRDvg9YmyUh+Gh15jXoboibZhuv+MiKh8uR8dU=
golang.org/x/build v0.0.0-20220928220451-9294235e16f5/go.mod h1:09OhLJI8jZv4jqec7zqh+ZlRZKsSZgDyM5MV3pjurk4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
Expand Down Expand Up @@ -383,8 +383,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -406,8 +406,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand Down
2 changes: 1 addition & 1 deletion hack/ci/00-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ for f in examples/alpine-base-rootless.yaml examples/wolfi-base.yaml; do
echo "=== building $f"

REF="apko.local/ci-testing:$(basename ${f})"
"${APKO}" build --debug "${f}" "${REF}" "${OUTPUT_TAR}"
"${APKO}" build "${f}" "${REF}" "${OUTPUT_TAR}"

# Subtest #1: Does it load?
ARCH_REF="$(docker load < output.tar | grep "Loaded image" | sed 's/^Loaded image: //' | head -1)"
Expand Down
2 changes: 1 addition & 1 deletion hack/ci/01-publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ for f in examples/alpine-base-rootless.yaml examples/wolfi-base.yaml; do
echo "=== building $f"

REF="localhost:${PORT}/ci-testing:$(basename ${f})"
img=$("${APKO}" publish --debug "${f}" "${REF}")
img=$("${APKO}" publish "${f}" "${REF}")

# Run the image.
docker run --rm ${img} echo hello | grep hello
Expand Down
32 changes: 5 additions & 27 deletions internal/cli/build-minirootfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,18 @@ import (
"os"
"runtime"

"github.com/chainguard-dev/clog"
apkfs "github.com/chainguard-dev/go-apk/pkg/fs"
"github.com/spf13/cobra"

"chainguard.dev/apko/pkg/build"
"chainguard.dev/apko/pkg/build/types"
"chainguard.dev/apko/pkg/iocomb"
"chainguard.dev/apko/pkg/log"
)

func buildMinirootFS() *cobra.Command {
var debugEnabled bool
var quietEnabled bool
var buildDate string
var buildArch string
var sbomPath string
var logPolicy []string

cmd := &cobra.Command{
Use: "build-minirootfs",
Expand All @@ -44,43 +40,25 @@ func buildMinirootFS() *cobra.Command {
Example: ` apko build-minirootfs <config.yaml> <output.tar.gz>`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if len(logPolicy) == 0 {
if quietEnabled {
logPolicy = []string{"builtin:discard"}
} else {
logPolicy = []string{"builtin:stderr"}
}
}

logWriter, err := iocomb.Combine(logPolicy)
if err != nil {
return fmt.Errorf("invalid logging policy: %w", err)
}
logger := log.NewLogger(logWriter)

return BuildMinirootFSCmd(cmd.Context(),
build.WithLogger(logger),
build.WithConfig(args[0]),
build.WithTarball(args[1]),
build.WithBuildDate(buildDate),
build.WithSBOM(sbomPath),
build.WithArch(types.ParseArchitecture(buildArch)),
build.WithDebugLogging(debugEnabled),
)
},
}

cmd.Flags().BoolVar(&debugEnabled, "debug", false, "enable debug logging")
cmd.Flags().BoolVar(&quietEnabled, "quiet", false, "disable logging")
cmd.Flags().StringVar(&buildDate, "build-date", "", "date used for the timestamps of the files inside the image")
cmd.Flags().StringVar(&buildArch, "build-arch", runtime.GOARCH, "architecture to build for -- default is Go runtime architecture")
cmd.Flags().StringVar(&sbomPath, "sbom-path", "", "generate an SBOM")
cmd.Flags().StringSliceVar(&logPolicy, "log-policy", []string{}, "logging policy to use")

return cmd
}

func BuildMinirootFSCmd(ctx context.Context, opts ...build.Option) error {
log := clog.FromContext(ctx)
wd, err := os.MkdirTemp("", "apko-*")
if err != nil {
return fmt.Errorf("failed to create working directory: %w", err)
Expand All @@ -96,16 +74,16 @@ func BuildMinirootFSCmd(ctx context.Context, opts ...build.Option) error {
ic := bc.ImageConfiguration()

if len(ic.Archs) != 0 {
bc.Logger().Printf("WARNING: ignoring archs in config, only building for current arch (%s)", bc.Arch())
log.Infof("WARNING: ignoring archs in config, only building for current arch (%s)", bc.Arch())
}

bc.Logger().Printf("building minirootfs '%s'", bc.TarballPath())
log.Infof("building minirootfs '%s'", bc.TarballPath())

layerTarGZ, _, err := bc.BuildLayer(ctx)
if err != nil {
return fmt.Errorf("failed to build layer image: %w", err)
}
bc.Logger().Printf("wrote minirootfs to %s\n", layerTarGZ)
log.Infof("wrote minirootfs to %s\n", layerTarGZ)

return nil
}
Loading

0 comments on commit 8494232

Please sign in to comment.