Skip to content

Commit

Permalink
client: update for httputil changes
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jan 10, 2023
1 parent e08f397 commit d8ad1ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
28 changes: 19 additions & 9 deletions httptransport/client/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import (
"github.com/quay/claircore/libvuln/driver"
)

// uoCache caches an UpdateOperation
// map when the server provides a conditional
// response
// UoCache caches an UpdateOperation map when the server provides a conditional
// response.
type uoCache struct {
sync.RWMutex
validator string
uo map[string][]driver.UpdateOperation
validator string
}

// Set persists the update operations map and it's associated
// validator string used in conditional requests.
// Set persists the update operations map and its associated validator string
// used in conditional requests.
//
// It is safe for concurrent use.
func (c *uoCache) Set(m map[string][]driver.UpdateOperation, v string) {
Expand All @@ -38,7 +37,7 @@ func (c *uoCache) Copy() map[string][]driver.UpdateOperation {
c.RLock()
defer c.RUnlock()
for u, ops := range c.uo {
o := make([]driver.UpdateOperation, len(ops), len(ops))
o := make([]driver.UpdateOperation, len(ops))
copy(o, ops)
m[u] = o
}
Expand All @@ -53,12 +52,12 @@ func newOUCache() *uoCache {

// HTTP implements access to clair interfaces over HTTP
type HTTP struct {
diffValidator atomic.Value
addr *url.URL
c *http.Client
uoCache *uoCache
uoLatestCache *uoCache

diffValidator atomic.Value
signer Signer
}

// DefaultAddr is used if the WithAddr Option isn't provided to New.
Expand Down Expand Up @@ -119,3 +118,14 @@ func WithClient(c *http.Client) Option {
return nil
}
}

func WithSigner(v Signer) Option {
return func(s *HTTP) error {
s.signer = v
return nil
}
}

type Signer interface {
Sign(context.Context, *http.Request) error
}
11 changes: 6 additions & 5 deletions httptransport/client/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/quay/clair/v4/httptransport"
"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/internal/codec"
"github.com/quay/clair/v4/internal/httputil"
)

var _ indexer.Service = (*HTTP)(nil)
Expand All @@ -27,7 +28,7 @@ func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerabilit
}{
v,
})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), rd)
req, err := httputil.NewRequestWithContext(ctx, http.MethodPost, u.String(), rd)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func (s *HTTP) Index(ctx context.Context, manifest *claircore.Manifest) (*clairc
return nil, fmt.Errorf("failed to create request: %v", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), codec.JSONReader(manifest))
req, err := httputil.NewRequestWithContext(ctx, http.MethodPost, u.String(), codec.JSONReader(manifest))
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
Expand Down Expand Up @@ -108,7 +109,7 @@ func (s *HTTP) IndexReport(ctx context.Context, manifest claircore.Digest) (*cla
return nil, false, fmt.Errorf("failed to create request: %v", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, false, fmt.Errorf("failed to create request: %v", err)
}
Expand Down Expand Up @@ -144,7 +145,7 @@ func (s *HTTP) State(ctx context.Context) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to create request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return "", fmt.Errorf("failed to create request: %v", err)
}
Expand All @@ -169,7 +170,7 @@ func (s *HTTP) DeleteManifests(ctx context.Context, d ...claircore.Digest) ([]cl
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), codec.JSONReader(d))
req, err := httputil.NewRequestWithContext(ctx, http.MethodDelete, u.String(), codec.JSONReader(d))
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
Expand Down
13 changes: 7 additions & 6 deletions httptransport/client/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
clairerror "github.com/quay/clair/v4/clair-error"
"github.com/quay/clair/v4/httptransport"
"github.com/quay/clair/v4/internal/codec"
"github.com/quay/clair/v4/internal/httputil"
"github.com/quay/clair/v4/matcher"
)

Expand All @@ -26,7 +27,7 @@ func (c *HTTP) Scan(ctx context.Context, ir *claircore.IndexReport) (*claircore.
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), codec.JSONReader(ir))
req, err := httputil.NewRequestWithContext(ctx, http.MethodPost, u.String(), codec.JSONReader(ir))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -84,7 +85,7 @@ func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) (in
errs[i] = err
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodDelete, u.String(), nil)
if err != nil {
errs[i] = err
return
Expand All @@ -109,7 +110,7 @@ func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) (in

var b strings.Builder
var errd bool
var deleted = int64(len(ref))
deleted := int64(len(ref))
for _, err := range errs {
if err != nil {
deleted--
Expand Down Expand Up @@ -142,7 +143,7 @@ func (c *HTTP) UpdateOperations(ctx context.Context, k driver.UpdateKind, update
v := url.Values{}
v.Add("kind", string(k))
u.RawQuery = v.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
Expand All @@ -160,7 +161,7 @@ func (c *HTTP) LatestUpdateOperations(ctx context.Context, k driver.UpdateKind)
v.Add("kind", string(k))
u.RawQuery = v.Encode()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -216,7 +217,7 @@ func (c *HTTP) UpdateDiff(ctx context.Context, prev, cur uuid.UUID) (*driver.Upd
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d8ad1ba

Please sign in to comment.