Skip to content

Commit

Permalink
clairctl: move to zlog
Browse files Browse the repository at this point in the history
This change uses zlog inside clairctl instead of using a command-only
logging facility. It makes info prints the default output level, with
the existing `D` flag upping the output to "debug" and the new `q` flag
dropping the output to "warn".

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed May 4, 2021
1 parent f3d64ff commit 30f8696
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 50 deletions.
67 changes: 53 additions & 14 deletions cmd/clairctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/quay/claircore"
"github.com/quay/zlog"
"github.com/tomnomnom/linkheader"

"github.com/quay/clair/v4/httptransport"
Expand Down Expand Up @@ -84,7 +85,10 @@ func (c *Client) getValidator(path string) string {
}

func (c *Client) setValidator(path, v string) {
debug.Printf("setting validator %q → %q", path, v)
zlog.Debug(context.Background()).
Str("path", path).
Str("validator", v).
Msg("setting validator")
c.mu.Lock()
defer c.mu.Unlock()
c.validator[path] = v
Expand All @@ -99,7 +103,9 @@ func (c *Client) IndexReport(ctx context.Context, id claircore.Digest, m *clairc
)
fp, err := c.host.Parse(path.Join(c.host.RequestURI(), httptransport.IndexReportAPIPath, id.String()))
if err != nil {
debug.Printf("unable to construct index_report url: %v", err)
zlog.Debug(ctx).
Err(err).
Msg("unable to construct index_report url")
return err
}
req = c.request(ctx, fp, http.MethodGet)
Expand All @@ -109,38 +115,58 @@ func (c *Client) IndexReport(ctx context.Context, id claircore.Digest, m *clairc
res.Body.Close()
}
if err != nil {
debug.Printf("request failed for url %q: %v", req.URL.String(), err)
zlog.Debug(ctx).
Err(err).
Stringer("url", req.URL).
Msg("request failed")
return err
}
debug.Printf("%s %s: %s", res.Request.Method, res.Request.URL.Path, res.Status)
zlog.Debug(ctx).
Str("method", res.Request.Method).
Str("path", res.Request.URL.Path).
Str("status", res.Status).
Send()
switch res.StatusCode {
case http.StatusOK, http.StatusNotFound:
debug.Printf("need to post manifest %v", id)
zlog.Debug(ctx).
Stringer("manifest", id).
Msg("need to post manifest")
case http.StatusNotModified:
return nil
default:
return fmt.Errorf("unexpected return status: %d", res.StatusCode)
}

if m == nil {
debug.Printf("don't have needed manifest %v", id)
zlog.Debug(ctx).
Stringer("manifest", id).
Msg("don't have needed manifest")
return errNeedManifest
}
ru, err := c.host.Parse(path.Join(c.host.RequestURI(), httptransport.IndexAPIPath))
if err != nil {
debug.Printf("unable to construct index_report url: %v", err)
zlog.Debug(ctx).
Err(err).
Msg("unable to construct index_report url")
return err
}

req = c.request(ctx, ru, http.MethodPost)
req.Body = codec.JSONReader(m)
res, err = c.client.Do(req)
if err != nil {
debug.Printf("request failed for url %q: %v", req.URL.String(), err)
zlog.Debug(ctx).
Err(err).
Stringer("url", req.URL).
Msg("request failed")
return err
}
defer res.Body.Close()
debug.Printf("%s %s: %s", res.Request.Method, res.Request.URL.Path, res.Status)
zlog.Debug(ctx).
Str("method", res.Request.Method).
Str("path", res.Request.URL.Path).
Str("status", res.Status).
Send()
switch res.StatusCode {
case http.StatusOK:
case http.StatusCreated:
Expand All @@ -152,7 +178,9 @@ func (c *Client) IndexReport(ctx context.Context, id claircore.Digest, m *clairc
dec := codec.GetDecoder(res.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&report); err != nil {
debug.Printf("unable to decode json payload: %v", err)
zlog.Debug(ctx).
Err(err).
Msg("unable to decode json payload")
return err
}
if !report.Success && report.Err != "" {
Expand All @@ -179,17 +207,26 @@ func (c *Client) VulnerabilityReport(ctx context.Context, id claircore.Digest) (
)
u, err := c.host.Parse(path.Join(c.host.RequestURI(), httptransport.VulnerabilityReportPath, id.String()))
if err != nil {
debug.Printf("unable to construct vulnerability_report url: %v", err)
zlog.Debug(ctx).
Err(err).
Msg("unable to construct vulnerability_report url")
return nil, err
}
req = c.request(ctx, u, http.MethodGet)
res, err = c.client.Do(req)
if err != nil {
debug.Printf("request failed for url %q: %v", req.URL.String(), err)
zlog.Debug(ctx).
Err(err).
Stringer("url", req.URL).
Msg("request failed")
return nil, err
}
defer res.Body.Close()
debug.Printf("%s %s: %s", res.Request.Method, res.Request.URL.Path, res.Status)
zlog.Debug(ctx).
Str("method", res.Request.Method).
Str("path", res.Request.URL.Path).
Str("status", res.Status).
Send()
switch res.StatusCode {
case http.StatusOK:
case http.StatusNotModified:
Expand All @@ -202,7 +239,9 @@ func (c *Client) VulnerabilityReport(ctx context.Context, id claircore.Digest) (
dec := codec.GetDecoder(res.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&report); err != nil {
debug.Printf("unable to decode json payload: %v", err)
zlog.Debug(ctx).
Err(err).
Msg("unable to decode json payload")
return nil, err
}

Expand Down
8 changes: 0 additions & 8 deletions cmd/clairctl/debug.go

This file was deleted.

42 changes: 30 additions & 12 deletions cmd/clairctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ package main

import (
"context"
"log"
"os"
"time"

_ "github.com/quay/claircore/updater/defaults"
"github.com/quay/zlog"
"github.com/rs/zerolog"
"github.com/urfave/cli/v2"
"gopkg.in/square/go-jose.v2/jwt"
)

var (
flagDebug bool
logout = zerolog.New(&zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
}).Level(zerolog.InfoLevel).
With().
Timestamp().
Logger()

commonClaim = jwt.Claims{}
)
Expand All @@ -24,14 +32,18 @@ func main() {

app := &cli.App{
Name: "clairctl",
Version: "0.1.0",
Version: "0.2.0",
Usage: "interact with a clair API",
Description: "A command-line tool for clair v4.",
EnableBashCompletion: true,
Before: func(c *cli.Context) error {
if c.IsSet("q") {
logout = logout.Level(zerolog.WarnLevel)
}
if c.IsSet("D") {
debug.SetOutput(os.Stderr)
logout = logout.Level(zerolog.DebugLevel)
}
zlog.Set(&logout)
commonClaim.Issuer = c.String("issuer")
return nil
},
Expand All @@ -46,6 +58,10 @@ func main() {
Name: "D",
Usage: "print debugging logs",
},
&cli.BoolFlag{
Name: "q",
Usage: "quieter log output",
},
&cli.PathFlag{
Name: "config",
Aliases: []string{"c"},
Expand All @@ -61,14 +77,16 @@ func main() {
Value: "clairctl",
},
},
ExitErrHandler: func(c *cli.Context, err error) {
if err != nil {
exit = 1
if err, ok := err.(cli.ExitCoder); ok {
exit = err.ExitCode()
}
logout.Error().Err(err).Send()
}
},
}
log.SetFlags(log.Flags())

if err := app.RunContext(ctx, os.Args); err != nil {
exit = 1
if err, ok := err.(cli.ExitCoder); ok {
exit = err.ExitCode()
}
log.Println(err)
}
app.RunContext(ctx, os.Args)
}
25 changes: 19 additions & 6 deletions cmd/clairctl/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/quay/claircore"
"github.com/quay/zlog"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"

Expand All @@ -26,7 +27,8 @@ var ManifestCmd = &cli.Command{
}

func manifestAction(c *cli.Context) error {
debug.Println("manifest")
ctx := c.Context
zlog.Debug(ctx).Msg("manifest")
args := c.Args()
if args.Len() == 0 {
return errors.New("missing needed arguments")
Expand All @@ -46,14 +48,19 @@ func manifestAction(c *cli.Context) error {

for i := 0; i < args.Len(); i++ {
name := args.Get(i)
debug.Printf("%s: fetching", name)
zlog.Debug(ctx).Str("name", name).Msg("fetching")
eg.Go(func() error {
m, err := Inspect(ctx, name)
if err != nil {
debug.Printf("%s: err: %v", name, err)
zlog.Debug(ctx).
Str("name", name).
Err(err).
Send()
return err
}
debug.Printf("%s: ok", name)
zlog.Debug(ctx).
Str("name", name).
Msg("ok")
result <- m
return nil
})
Expand Down Expand Up @@ -93,13 +100,19 @@ func Inspect(ctx context.Context, r string) (*claircore.Manifest, error) {
return nil, err
}
out := claircore.Manifest{Hash: ccd}
debug.Printf("%s: found manifest %v", r, ccd)
zlog.Debug(ctx).
Str("ref", r).
Stringer("digest", ccd).
Msg("found manifest")

ls, err := img.Layers()
if err != nil {
return nil, err
}
debug.Printf("%s: found %d layers", r, len(ls))
zlog.Debug(ctx).
Str("ref", r).
Int("count", len(ls)).
Msg("found layers")

repo := ref.Context()
rURL := url.URL{
Expand Down
Loading

0 comments on commit 30f8696

Please sign in to comment.