Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

drop logger, use slog #185

Merged
merged 3 commits into from
Jan 16, 2024
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
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module github.com/chainguard-dev/go-apk

go 1.21
go 1.21.2

toolchain go1.21.5

require (
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/chainguard-dev/clog v1.2.3-0.20240116182827-04bee692f7a8
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-hclog v0.9.2
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/klauspost/compress v1.17.4
github.com/psanford/memfs v0.0.0-20230130182539-4dbf7e3e865e
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
go.lsp.dev/uri v0.3.0
go.opentelemetry.io/otel v1.19.0
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand All @@ -24,11 +25,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/psanford/memfs v0.0.0-20230130182539-4dbf7e3e865e h1:51xcRlSMBU5rhM9KahnJGfEsBPVPz3182TgFRowA8yY=
github.com/psanford/memfs v0.0.0-20230130182539-4dbf7e3e865e/go.mod h1:tcaRap0jS3eifrEEllL6ZMd9dg8IlDpi2S1oARrQ+NI=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.lsp.dev/uri v0.3.0 h1:KcZJmh6nFIBeJzTugn5JTU6OOyG0lDOo3R9KwTxTYbo=
Expand All @@ -43,14 +40,12 @@ golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
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-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54 changes: 33 additions & 21 deletions pkg/apk/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"sync"
"time"

"github.com/chainguard-dev/clog"
"github.com/chainguard-dev/go-apk/pkg/expandapk"
"gopkg.in/ini.v1"

Expand All @@ -46,7 +47,7 @@ import (

"github.com/chainguard-dev/go-apk/internal/tarfs"
apkfs "github.com/chainguard-dev/go-apk/pkg/fs"
"github.com/chainguard-dev/go-apk/pkg/logger"
"github.com/hashicorp/go-hclog"
retryablehttp "github.com/hashicorp/go-retryablehttp"
)

Expand All @@ -58,7 +59,6 @@ var globalApkCache = &apkCache{}
type APK struct {
arch string
version string
logger logger.Logger
fs apkfs.FullFS
executor Executor
ignoreMknodErrors bool
Expand All @@ -77,9 +77,12 @@ func New(options ...Option) (*APK, error) {
return nil, err
}
}
rhttp := retryablehttp.NewClient()
rhttp.Logger = hclog.Default()

return &APK{
client: rhttp.StandardClient(),
fs: opt.fs,
logger: opt.logger,
arch: opt.arch,
executor: opt.executor,
ignoreMknodErrors: opt.ignoreMknodErrors,
Expand Down Expand Up @@ -217,10 +220,11 @@ func (a *APK) ListInitFiles() []tar.Header {
// unless those files will be included in the installed database, in which case they can
// be retrieved via GetInstalled().
func (a *APK) InitDB(ctx context.Context, alpineVersions ...string) error {
log := clog.FromContext(ctx)
/*
equivalent of: "apk add --initdb --arch arch --root root"
*/
a.logger.Infof("initializing apk database")
log.Info("initializing apk database")

// additionalFiles are files we need but can only be resolved in the context of
// this func, e.g. we need the architecture
Expand Down Expand Up @@ -291,18 +295,19 @@ func (a *APK) InitDB(ctx context.Context, alpineVersions ...string) error {
if !errors.As(err, &nokeysErr) {
return fmt.Errorf("failed to fetch alpine-keys: %w", err)
}
a.logger.Infof("ignoring missing keys: %s", err.Error())
log.Infof("ignoring missing keys: %s", err.Error())
}
}

a.logger.Infof("finished initializing apk database")
log.Info("finished initializing apk database")
return nil
}

// loadSystemKeyring returns the keys found in the system keyring
// directory by trying some common locations. These can be overridden
// by passing one or more directories as arguments.
func (a *APK) loadSystemKeyring(locations ...string) ([]string, error) {
func (a *APK) loadSystemKeyring(ctx context.Context, locations ...string) ([]string, error) {
log := clog.FromContext(ctx)
var ring []string
if len(locations) == 0 {
locations = []string{
Expand All @@ -313,7 +318,7 @@ func (a *APK) loadSystemKeyring(locations ...string) ([]string, error) {
keyFiles, err := fs.ReadDir(a.fs, d)

if errors.Is(err, os.ErrNotExist) {
a.logger.Warnf("%s doesn't exist, skipping...", d)
log.Warnf("%s doesn't exist, skipping...", d)
continue
}

Expand All @@ -328,7 +333,7 @@ func (a *APK) loadSystemKeyring(locations ...string) ([]string, error) {
if ext == ".pub" {
ring = append(ring, p)
} else {
a.logger.Infof("%s has invalid extension (%s), skipping...", p, ext)
log.Warnf("%s has invalid extension (%s), skipping...", p, ext)
}
}
}
Expand All @@ -341,7 +346,8 @@ func (a *APK) loadSystemKeyring(locations ...string) ([]string, error) {

// Installs the specified keys into the APK keyring inside the build context.
func (a *APK) InitKeyring(ctx context.Context, keyFiles, extraKeyFiles []string) error {
a.logger.Infof("initializing apk keyring")
log := clog.FromContext(ctx)
log.Info("initializing apk keyring")

ctx, span := otel.Tracer("go-apk").Start(ctx, "InitKeyring")
defer span.End()
Expand All @@ -351,7 +357,7 @@ func (a *APK) InitKeyring(ctx context.Context, keyFiles, extraKeyFiles []string)
}

if len(extraKeyFiles) > 0 {
a.logger.Debugf("appending %d extra keys to keyring", len(extraKeyFiles))
log.Debugf("appending %d extra keys to keyring", len(extraKeyFiles))
keyFiles = append(keyFiles, extraKeyFiles...)
}

Expand All @@ -360,7 +366,7 @@ func (a *APK) InitKeyring(ctx context.Context, keyFiles, extraKeyFiles []string)
for _, element := range keyFiles {
element := element
eg.Go(func() error {
a.logger.Debugf("installing key %v", element)
log.Debugf("installing key %v", element)

var asURL *url.URL
var err error
Expand Down Expand Up @@ -434,7 +440,8 @@ func (a *APK) InitKeyring(ctx context.Context, keyFiles, extraKeyFiles []string)

// ResolveWorld determine the target state for the requested dependencies in /etc/apk/world. Does not install anything.
func (a *APK) ResolveWorld(ctx context.Context) (toInstall []*RepositoryPackage, conflicts []string, err error) {
a.logger.Infof("determining desired apk world")
log := clog.FromContext(ctx)
log.Info("determining desired apk world")

ctx, span := otel.Tracer("go-apk").Start(ctx, "ResolveWorld")
defer span.End()
Expand All @@ -446,7 +453,7 @@ func (a *APK) ResolveWorld(ctx context.Context) (toInstall []*RepositoryPackage,
return toInstall, conflicts, fmt.Errorf("error getting repository indexes: %w", err)
}
// debugging info, if requested
a.logger.Debugf("got %d indexes:\n%s", len(indexes), strings.Join(indexNames(indexes), "\n"))
log.Debugf("got %d indexes:\n%s", len(indexes), strings.Join(indexNames(indexes), "\n"))

// 2. Get the dependency tree for each package from the world file
directPkgs, err := a.GetWorld()
Expand All @@ -458,12 +465,13 @@ func (a *APK) ResolveWorld(ctx context.Context) (toInstall []*RepositoryPackage,
if err != nil {
return
}
a.logger.Debugf("got %d packages to install:\n%s", len(toInstall), strings.Join(packageRefs(toInstall), "\n"))
log.Debugf("got %d packages to install:\n%s", len(toInstall), strings.Join(packageRefs(toInstall), "\n"))
return
}

func (a *APK) ResolveAndCalculateWorld(ctx context.Context) ([]*APKResolved, error) {
a.logger.Infof("resolving and calculating 'world' (packages to install) ")
log := clog.FromContext(ctx)
log.Info("resolving and calculating 'world' (packages to install) ")

ctx, span := otel.Tracer("go-apk").Start(ctx, "CalculateWorld")
defer span.End()
Expand Down Expand Up @@ -520,13 +528,14 @@ func (a *APK) ResolveAndCalculateWorld(ctx context.Context) ([]*APKResolved, err

// FixateWorld force apk's resolver to re-resolve the requested dependencies in /etc/apk/world.
func (a *APK) FixateWorld(ctx context.Context, sourceDateEpoch *time.Time) error {
log := clog.FromContext(ctx)
/*
equivalent of: "apk fix --arch arch --root root"
with possible options for --no-scripts, --no-cache, --update-cache

current default is: cache=false, updateCache=true, executeScripts=false
*/
a.logger.Infof("synchronizing with desired apk world")
log.Info("synchronizing with desired apk world")

ctx, span := otel.Tracer("go-apk").Start(ctx, "FixateWorld")
defer span.End()
Expand Down Expand Up @@ -921,6 +930,7 @@ func (a *APK) expandPackage(ctx context.Context, pkg InstallablePackage) (*expan
}

func expandPackage(ctx context.Context, a *APK, pkg InstallablePackage) (*expandapk.APKExpanded, error) {
log := clog.FromContext(ctx)
ctx, span := otel.Tracer("go-apk").Start(ctx, "expandPackage", trace.WithAttributes(attribute.String("package", pkg.PackageName())))
defer span.End()

Expand All @@ -934,11 +944,11 @@ func expandPackage(ctx context.Context, a *APK, pkg InstallablePackage) (*expand

exp, err := a.cachedPackage(ctx, pkg, cacheDir)
if err == nil {
a.logger.Debugf("cache hit (%s)", pkg.PackageName())
log.Debugf("cache hit (%s)", pkg.PackageName())
return exp, nil
}

a.logger.Debugf("cache miss (%s): %v", pkg.PackageName(), err)
log.Debugf("cache miss (%s): %v", pkg.PackageName(), err)

if err := os.MkdirAll(cacheDir, 0o755); err != nil {
return nil, fmt.Errorf("unable to create cache directory %q: %w", cacheDir, err)
Expand Down Expand Up @@ -984,7 +994,8 @@ func packageAsURL(pkg InstallablePackage) (*url.URL, error) {
}

func (a *APK) FetchPackage(ctx context.Context, pkg InstallablePackage) (io.ReadCloser, error) {
a.logger.Debugf("fetching %s", pkg)
log := clog.FromContext(ctx)
log.Debugf("fetching %s", pkg)

ctx, span := otel.Tracer("go-apk").Start(ctx, "fetchPackage", trace.WithAttributes(attribute.String("package", pkg.PackageName())))
defer span.End()
Expand Down Expand Up @@ -1065,7 +1076,8 @@ func packageInfo(exp *expandapk.APKExpanded) (*Package, error) {

// installPackage installs a single package and updates installed db.
func (a *APK) installPackage(ctx context.Context, pkg *Package, expanded *expandapk.APKExpanded, sourceDateEpoch *time.Time) ([]tar.Header, error) {
a.logger.Debugf("installing %s (%s)", pkg.Name, pkg.Version)
log := clog.FromContext(ctx)
log.Debugf("installing %s (%s)", pkg.Name, pkg.Version)

ctx, span := otel.Tracer("go-apk").Start(ctx, "installPackage", trace.WithAttributes(attribute.String("package", pkg.Name)))
defer span.End()
Expand Down
18 changes: 12 additions & 6 deletions pkg/apk/implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestInitDB(t *testing.T) {
}

func TestSetWorld(t *testing.T) {
ctx := context.Background()
src := apkfs.NewMemFS()
apk, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors))
require.NoError(t, err)
Expand All @@ -102,7 +103,7 @@ func TestSetWorld(t *testing.T) {

// set these packages in a random order; it should write them to world in the correct order
packages := []string{"foo", "bar", "abc", "zulu"}
err = apk.SetWorld(packages)
err = apk.SetWorld(ctx, packages)
require.NoError(t, err)

// check all of the contents
Expand All @@ -115,6 +116,7 @@ func TestSetWorld(t *testing.T) {
}

func TestSetRepositories(t *testing.T) {
ctx := context.Background()
src := apkfs.NewMemFS()
apk, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors))
require.NoError(t, err)
Expand All @@ -124,7 +126,7 @@ func TestSetRepositories(t *testing.T) {
require.NoError(t, err)

repos := []string{"https://dl-cdn.alpinelinux.org/alpine/v3.16/main", "https://dl-cdn.alpinelinux.org/alpine/v3.16/community"}
err = apk.SetRepositories(repos)
err = apk.SetRepositories(ctx, repos)
require.NoError(t, err)

// check all of the contents
Expand All @@ -136,6 +138,7 @@ func TestSetRepositories(t *testing.T) {
}

func TestSetRepositories_Empty(t *testing.T) {
ctx := context.Background()
src := apkfs.NewMemFS()
apk, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors))
require.NoError(t, err)
Expand All @@ -145,7 +148,7 @@ func TestSetRepositories_Empty(t *testing.T) {
require.NoError(t, err)

repos := []string{}
err = apk.SetRepositories(repos)
err = apk.SetRepositories(ctx, repos)
require.Error(t, err)
}

Expand Down Expand Up @@ -202,15 +205,17 @@ func TestInitKeyring(t *testing.T) {

func TestLoadSystemKeyring(t *testing.T) {
t.Run("non-existent dir", func(t *testing.T) {
ctx := context.Background()
src := apkfs.NewMemFS()
a, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors))
require.NoError(t, err)

// Read the empty dir, passing a non-existent location should err
_, err = a.loadSystemKeyring("/non/existent/dir")
_, err = a.loadSystemKeyring(ctx, "/non/existent/dir")
require.Error(t, err)
})
t.Run("empty dir", func(t *testing.T) {
ctx := context.Background()
src := apkfs.NewMemFS()
a, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors))
require.NoError(t, err)
Expand All @@ -219,7 +224,7 @@ func TestLoadSystemKeyring(t *testing.T) {
emptyDir := "/var/test/keyring"
err = src.MkdirAll(emptyDir, 0o755)
require.NoError(t, err)
_, err = a.loadSystemKeyring(emptyDir)
_, err = a.loadSystemKeyring(ctx, emptyDir)
require.Error(t, err)
})
tests := []struct {
Expand All @@ -231,6 +236,7 @@ func TestLoadSystemKeyring(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
arch := ArchToAPK(runtime.GOARCH)
src := apkfs.NewMemFS()
a, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors))
Expand Down Expand Up @@ -267,7 +273,7 @@ func TestLoadSystemKeyring(t *testing.T) {
))

// Successful read
keyFiles, err := a.loadSystemKeyring(tt.paths...)
keyFiles, err := a.loadSystemKeyring(ctx, tt.paths...)
require.NoError(t, err)
require.Len(t, keyFiles, 5)
// should not take into account extraneous files
Expand Down
Loading