forked from cnabio/cnab-to-oci
/
fixupoptions.go
160 lines (145 loc) · 5.06 KB
/
fixupoptions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package remotes
import (
"fmt"
"io"
"io/ioutil"
"github.com/cnabio/cnab-go/bundle"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes"
"github.com/docker/cnab-to-oci/internal"
"github.com/docker/cnab-to-oci/relocation"
"github.com/docker/distribution/reference"
ocischemav1 "github.com/opencontainers/image-spec/specs-go/v1"
)
const (
defaultMaxConcurrentJobs = 4
defaultJobsBufferLength = 50
)
func noopEventCallback(FixupEvent) {}
// fixupConfig defines the input required for a Fixup operation
type fixupConfig struct {
bundle *bundle.Bundle
relocationMap relocation.ImageRelocationMap
targetRef reference.Named
eventCallback func(FixupEvent)
maxConcurrentJobs int
jobsBufferLength int
resolver remotes.Resolver
invocationImagePlatformFilter platforms.Matcher
componentImagePlatformFilter platforms.Matcher
autoBundleUpdate bool
pushImages bool
imageClient internal.ImageClient
pushOut io.Writer
}
// FixupOption is a helper for configuring a FixupBundle
type FixupOption func(*fixupConfig) error
func newFixupConfig(b *bundle.Bundle, ref reference.Named, resolver remotes.Resolver, options ...FixupOption) (fixupConfig, error) {
cfg := fixupConfig{
bundle: b,
relocationMap: relocation.ImageRelocationMap{},
targetRef: ref,
resolver: resolver,
eventCallback: noopEventCallback,
jobsBufferLength: defaultJobsBufferLength,
maxConcurrentJobs: defaultMaxConcurrentJobs,
}
for _, opt := range options {
if err := opt(&cfg); err != nil {
return fixupConfig{}, err
}
}
return cfg, nil
}
// WithInvocationImagePlatforms use filters platforms for an invocation image
func WithInvocationImagePlatforms(supportedPlatforms []string) FixupOption {
return func(cfg *fixupConfig) error {
if len(supportedPlatforms) == 0 {
return nil
}
plats, err := toPlatforms(supportedPlatforms)
if err != nil {
return err
}
cfg.invocationImagePlatformFilter = platforms.Any(plats...)
return nil
}
}
// WithComponentImagePlatforms use filters platforms for an invocation image
func WithComponentImagePlatforms(supportedPlatforms []string) FixupOption {
return func(cfg *fixupConfig) error {
if len(supportedPlatforms) == 0 {
return nil
}
plats, err := toPlatforms(supportedPlatforms)
if err != nil {
return err
}
cfg.componentImagePlatformFilter = platforms.Any(plats...)
return nil
}
}
func toPlatforms(supportedPlatforms []string) ([]ocischemav1.Platform, error) {
result := make([]ocischemav1.Platform, len(supportedPlatforms))
for ix, p := range supportedPlatforms {
plat, err := platforms.Parse(p)
if err != nil {
return nil, err
}
result[ix] = plat
}
return result, nil
}
// WithEventCallback specifies a callback to execute for each Fixup event
func WithEventCallback(callback func(FixupEvent)) FixupOption {
return func(cfg *fixupConfig) error {
cfg.eventCallback = callback
return nil
}
}
// WithParallelism provides a way to change the max concurrent jobs and the max number of jobs queued up
func WithParallelism(maxConcurrentJobs int, jobsBufferLength int) FixupOption {
return func(cfg *fixupConfig) error {
cfg.maxConcurrentJobs = maxConcurrentJobs
cfg.jobsBufferLength = jobsBufferLength
return nil
}
}
// WithAutoBundleUpdate updates the bundle with content digests and size provided by the registry
func WithAutoBundleUpdate() FixupOption {
return func(cfg *fixupConfig) error {
cfg.autoBundleUpdate = true
return nil
}
}
// WithPushImages authorizes the fixup command to push missing images.
// By default the fixup will look at images defined in the bundle.
// Existing images in the target registry or accessible from an other registry will be copied or mounted under the
// target tag.
// But local only images (for example after a local build of components of the bundle) must be pushed.
// This option will allow to push images that are only available in the docker daemon image store to the defined target.
func WithPushImages(imageClient internal.ImageClient, out io.Writer) FixupOption {
return func(cfg *fixupConfig) error {
cfg.pushImages = true
if imageClient == nil {
return fmt.Errorf("could not configure fixup, 'imageClient' cannot be nil to push images")
}
cfg.imageClient = imageClient
if out == nil {
cfg.pushOut = ioutil.Discard
} else {
cfg.pushOut = out
}
return nil
}
}
// WithRelocationMap stores a previously generated relocation map. This map will be used to copy or mount images
// based on local images but already pushed on a registry.
// This way if a bundle is pulled on a machine that doesn't contain the images, when the bundle is pushed and images
// are not found the fixup will try to resolve the corresponding images from the relocation map.
func WithRelocationMap(relocationMap relocation.ImageRelocationMap) FixupOption {
return func(cfg *fixupConfig) error {
cfg.relocationMap = relocationMap
return nil
}
}