mirror of
https://github.com/crazy-max/diun.git
synced 2025-04-11 06:01:21 +00:00
Fix registry client context
This commit is contained in:
parent
747a41fad2
commit
1611629141
5 changed files with 26 additions and 20 deletions
|
@ -1,5 +1,9 @@
|
|||
# Changelog
|
||||
|
||||
## 0.3.2 (2019/06/08)
|
||||
|
||||
* Fix registry client context
|
||||
|
||||
## 0.3.1 (2019/06/08)
|
||||
|
||||
* Fix email template
|
||||
|
|
|
@ -9,8 +9,7 @@ import (
|
|||
|
||||
// RegistryClient represents an active docker registry object
|
||||
type RegistryClient struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
opts RegistryOptions
|
||||
sysCtx *types.SystemContext
|
||||
}
|
||||
|
||||
|
@ -26,13 +25,6 @@ type RegistryOptions struct {
|
|||
|
||||
// NewRegistryClient creates new docker registry client instance
|
||||
func NewRegistryClient(opts RegistryOptions) (*RegistryClient, error) {
|
||||
// Context
|
||||
ctx := context.Background()
|
||||
var cancel context.CancelFunc = func() {}
|
||||
if opts.Timeout > 0 {
|
||||
ctx, cancel = context.WithTimeout(ctx, opts.Timeout)
|
||||
}
|
||||
|
||||
// Auth
|
||||
auth := &types.DockerAuthConfig{}
|
||||
if opts.Username != "" {
|
||||
|
@ -52,8 +44,15 @@ func NewRegistryClient(opts RegistryOptions) (*RegistryClient, error) {
|
|||
}
|
||||
|
||||
return &RegistryClient{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
sysCtx: sysCtx,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *RegistryClient) timeoutContext() (context.Context, context.CancelFunc) {
|
||||
ctx := context.Background()
|
||||
var cancel context.CancelFunc = func() {}
|
||||
if c.opts.Timeout > 0 {
|
||||
ctx, cancel = context.WithTimeout(ctx, c.opts.Timeout)
|
||||
}
|
||||
return ctx, cancel
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
|
@ -8,7 +9,7 @@ import (
|
|||
"github.com/containers/image/types"
|
||||
)
|
||||
|
||||
func (c *RegistryClient) newImage(imageStr string) (types.ImageCloser, error) {
|
||||
func (c *RegistryClient) newImage(ctx context.Context, imageStr string) (types.ImageCloser, error) {
|
||||
if !strings.HasPrefix(imageStr, "//") {
|
||||
imageStr = fmt.Sprintf("//%s", imageStr)
|
||||
}
|
||||
|
@ -18,7 +19,7 @@ func (c *RegistryClient) newImage(imageStr string) (types.ImageCloser, error) {
|
|||
return nil, fmt.Errorf("invalid image name %s: %v", imageStr, err)
|
||||
}
|
||||
|
||||
img, err := ref.NewImage(c.ctx, c.sysCtx)
|
||||
img, err := ref.NewImage(ctx, c.sysCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -23,20 +23,21 @@ type Manifest struct {
|
|||
|
||||
// Manifest returns the manifest for a specific image
|
||||
func (c *RegistryClient) Manifest(image registry.Image) (Manifest, error) {
|
||||
defer c.cancel()
|
||||
ctx, cancel := c.timeoutContext()
|
||||
defer cancel()
|
||||
|
||||
imgCls, err := c.newImage(image.String())
|
||||
imgCls, err := c.newImage(ctx, image.String())
|
||||
if err != nil {
|
||||
return Manifest{}, err
|
||||
}
|
||||
defer imgCls.Close()
|
||||
|
||||
rawManifest, _, err := imgCls.Manifest(c.ctx)
|
||||
rawManifest, _, err := imgCls.Manifest(ctx)
|
||||
if err != nil {
|
||||
return Manifest{}, err
|
||||
}
|
||||
|
||||
imgInspect, err := imgCls.Inspect(c.ctx)
|
||||
imgInspect, err := imgCls.Inspect(ctx)
|
||||
if err != nil {
|
||||
return Manifest{}, err
|
||||
}
|
||||
|
|
|
@ -9,15 +9,16 @@ type Tags []string
|
|||
|
||||
// Tags returns tags of a Docker repository
|
||||
func (c *RegistryClient) Tags(image registry.Image) (Tags, error) {
|
||||
defer c.cancel()
|
||||
ctx, cancel := c.timeoutContext()
|
||||
defer cancel()
|
||||
|
||||
imgCls, err := c.newImage(image.String())
|
||||
imgCls, err := c.newImage(ctx, image.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer imgCls.Close()
|
||||
|
||||
tags, err := docker.GetRepositoryTags(c.ctx, c.sysCtx, imgCls.Reference())
|
||||
tags, err := docker.GetRepositoryTags(ctx, c.sysCtx, imgCls.Reference())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue