Skip to content

Commit

Permalink
feat: add option to increase logging verbosity
Browse files Browse the repository at this point in the history
Still needs some more work, as the "payload" (ie: audit report) is
delivered through "info" level, which is second lowest. Does not really
use the full level spectrum, but hey, it's better than it was.

Signed-off-by: Jorik Jonker <[email protected]>
  • Loading branch information
jonkerj committed Apr 22, 2022
1 parent a71d1d3 commit 4b8f1c4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
15 changes: 15 additions & 0 deletions cmd/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"github.com/spf13/viper"
"go.uber.org/zap"
)

func getLogger() (*zap.Logger, error) {
config := zap.NewProductionConfig()
if viper.GetBool("debug") {
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
}

return config.Build()
}
9 changes: 7 additions & 2 deletions cmd/once.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ func init() {
}

func once(cmd *cobra.Command, args []string) {
logger, err := getLogger()
if err != nil {
panic(fmt.Errorf("error setting up zap logging: %v", err))
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

group, gctx := errgroup.WithContext(ctx)
rootPrefix := viper.GetString("root-prefix")

for _, address := range viper.GetStringSlice("containerd-address") {
a, err := containerd.NewAuditor(address, rootPrefix, gctx)
a, err := containerd.NewAuditor(address, rootPrefix, gctx, logger)
if err != nil {
panic(err)
}
Expand All @@ -45,7 +50,7 @@ func once(cmd *cobra.Command, args []string) {
})
}

err := group.Wait()
err = group.Wait()
if err != nil {
fmt.Printf("error: %v\n", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {
flags := rootCmd.PersistentFlags()
flags.StringSlice("containerd-address", []string{"/run/containerd/containerd.sock"}, "location of containerd endpoint")
flags.String("root-prefix", "", "root prefix when accessing /proc et al from a hostPath mount")
flags.Bool("debug", false, "increase verbosity")

if err := viper.BindPFlags(flags); err != nil {
panic(err)
Expand Down
9 changes: 7 additions & 2 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ func init() {
}

func watch(cmd *cobra.Command, args []string) {
logger, err := getLogger()
if err != nil {
panic(fmt.Errorf("error setting up zap logging: %v", err))
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

group, gctx := errgroup.WithContext(ctx)
rootPrefix := viper.GetString("root-prefix")

for _, address := range viper.GetStringSlice("containerd-address") {
a, err := containerd.NewAuditor(address, rootPrefix, gctx)
a, err := containerd.NewAuditor(address, rootPrefix, gctx, logger)
if err != nil {
panic(err)
}
Expand All @@ -46,7 +51,7 @@ func watch(cmd *cobra.Command, args []string) {

}

err := group.Wait()
err = group.Wait()
if err != nil {
fmt.Printf("error: %v\n", err)
}
Expand Down
7 changes: 1 addition & 6 deletions internal/runtimes/containerd/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ type Auditor struct {
context context.Context
}

func NewAuditor(address string, prefix string, ctx context.Context) (*Auditor, error) {
func NewAuditor(address string, prefix string, ctx context.Context, logger *zap.Logger) (*Auditor, error) {
client, err := NewClient(address, ctx)
if err != nil {
return nil, fmt.Errorf("error creating client: %v", err)
}

logger, err := zap.NewProduction()
if err != nil {
return nil, fmt.Errorf("error setting up zap logging: %v", err)
}

a := &Auditor{
address: address,
containerdClient: client,
Expand Down

0 comments on commit 4b8f1c4

Please sign in to comment.