Skip to content

Commit e08223e

Browse files
authored
feat: add the hooks for pull operation (#169)
Signed-off-by: chlins <[email protected]>
1 parent d88624b commit e08223e

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

pkg/backend/pull.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ func (b *backend) Pull(ctx context.Context, target string, cfg *config.Pull) err
8787
}
8888

8989
for _, layer := range manifest.Layers {
90-
g.Go(func() error { return retry.Do(func() error { return fn(layer) }, retryOpts...) })
90+
g.Go(func() error {
91+
return retry.Do(func() error {
92+
// call the before hook.
93+
cfg.Hooks.BeforePullLayer(layer, manifest)
94+
err := fn(layer)
95+
// call the after hook.
96+
cfg.Hooks.AfterPullLayer(layer, err)
97+
return err
98+
}, retryOpts...)
99+
})
91100
}
92101

93102
if err := g.Wait(); err != nil {

pkg/config/pull.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package config
1818

19-
import "fmt"
19+
import (
20+
"fmt"
21+
22+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
23+
)
2024

2125
const (
2226
// defaultPullConcurrency is the default number of concurrent pull operations.
@@ -30,6 +34,7 @@ type Pull struct {
3034
Insecure bool
3135
ExtractDir string
3236
ExtractFromRemote bool
37+
Hooks PullHooks
3338
}
3439

3540
func NewPull() *Pull {
@@ -40,6 +45,7 @@ func NewPull() *Pull {
4045
Insecure: false,
4146
ExtractDir: "",
4247
ExtractFromRemote: false,
48+
Hooks: &emptyPullHook{},
4349
}
4450
}
4551

@@ -57,3 +63,18 @@ func (p *Pull) Validate() error {
5763

5864
return nil
5965
}
66+
67+
// PullHooks is the hook events during the pull operation.
68+
type PullHooks interface {
69+
// BeforePullLayer will execute before pulling the layer described as desc, will carry the manifest as well.
70+
BeforePullLayer(desc ocispec.Descriptor, manifest ocispec.Manifest)
71+
72+
// AfterPullLayer will execute after pulling the layer described as desc, the error will be nil if pulled successfully.
73+
AfterPullLayer(desc ocispec.Descriptor, err error)
74+
}
75+
76+
// emptyPullHook is the empty pull hook implementation with do nothing.
77+
type emptyPullHook struct{}
78+
79+
func (emptyPullHook) BeforePullLayer(desc ocispec.Descriptor, manifest ocispec.Manifest) {}
80+
func (emptyPullHook) AfterPullLayer(desc ocispec.Descriptor, err error) {}

0 commit comments

Comments
 (0)