@@ -23,6 +23,9 @@ import (
23
23
24
24
apimanifests "github.com/operator-framework/api/pkg/manifests"
25
25
apierrors "github.com/operator-framework/api/pkg/validation/errors"
26
+ "github.com/operator-framework/operator-registry/pkg/containertools"
27
+ registryimage "github.com/operator-framework/operator-registry/pkg/image"
28
+ "github.com/operator-framework/operator-registry/pkg/image/execregistry"
26
29
registrybundle "github.com/operator-framework/operator-registry/pkg/lib/bundle"
27
30
log "github.com/sirupsen/logrus"
28
31
"github.com/spf13/cobra"
@@ -96,30 +99,10 @@ To build and validate an image:
96
99
return fmt .Errorf ("invalid command args: %v" , err )
97
100
}
98
101
99
- // If the argument isn't a directory, assume it's an image.
100
- if isExist (args [0 ]) {
101
- if c .directory , err = relWd (args [0 ]); err != nil {
102
- logger .Fatal (err )
103
- }
104
- } else {
105
- c .directory , err = ioutil .TempDir ("" , "bundle-" )
106
- if err != nil {
107
- return err
108
- }
109
- defer func () {
110
- if err = os .RemoveAll (c .directory ); err != nil {
111
- logger .Errorf ("Error removing temp bundle dir: %v" , err )
112
- }
113
- }()
114
-
115
- logger .Info ("Unpacking image layers" )
116
-
117
- if err := c .unpackImageIntoDir (args [0 ], c .directory ); err != nil {
118
- logger .Fatalf ("Error unpacking image %s: %v" , args [0 ], err )
119
- }
102
+ result , err := c .run (logger , args [0 ])
103
+ if err != nil {
104
+ logger .Fatal (err )
120
105
}
121
-
122
- result := c .run ()
123
106
if err := result .PrintWithFormat (c .outputFormat ); err != nil {
124
107
logger .Fatal (err )
125
108
}
@@ -152,25 +135,59 @@ func (c *bundleValidateCmd) addToFlagSet(fs *pflag.FlagSet) {
152
135
fs .StringVarP (& c .imageBuilder , "image-builder" , "b" , "docker" ,
153
136
"Tool to extract bundle image data. Only used when validating a bundle image. " +
154
137
"One of: [docker, podman]" )
138
+
155
139
fs .StringVarP (& c .outputFormat , "output" , "o" , internal .Text ,
156
140
"Result format for results. One of: [text, json-alpha1]" )
157
-
158
141
// It is hidden because it is an alpha option
159
142
// The idea is the next versions of Operator Registry will return a List of errors
160
143
if err := fs .MarkHidden ("output" ); err != nil {
161
144
panic (err )
162
145
}
163
146
}
164
147
165
- func (c bundleValidateCmd ) run () (res internal.Result ) {
148
+ func (c bundleValidateCmd ) run (logger * log.Entry , bundle string ) (res internal.Result , err error ) {
149
+ // Create a registry to validate bundle files and optionally unpack the image with.
150
+ reg , err := newImageRegistryForTool (logger , c .imageBuilder )
151
+ if err != nil {
152
+ return res , fmt .Errorf ("error creating image registry: %v" , err )
153
+ }
154
+ defer func () {
155
+ if err := reg .Destroy (); err != nil {
156
+ logger .Errorf ("Error destroying image registry: %v" , err )
157
+ }
158
+ }()
159
+
160
+ // If bundle isn't a directory, assume it's an image.
161
+ if isExist (bundle ) {
162
+ if c .directory , err = relWd (bundle ); err != nil {
163
+ return res , err
164
+ }
165
+ } else {
166
+ c .directory , err = ioutil .TempDir ("" , "bundle-" )
167
+ if err != nil {
168
+ return res , err
169
+ }
170
+ defer func () {
171
+ if err = os .RemoveAll (c .directory ); err != nil {
172
+ logger .Errorf ("Error removing temp bundle dir: %v" , err )
173
+ }
174
+ }()
175
+
176
+ logger .Info ("Unpacking image layers" )
177
+
178
+ if err := c .unpackImageIntoDir (reg , bundle , c .directory ); err != nil {
179
+ return res , fmt .Errorf ("error unpacking image %s: %v" , bundle , err )
180
+ }
181
+ }
182
+
166
183
// Create Result to be outputted
167
184
res = internal .NewResult ()
168
185
169
- logger := log .WithFields (log.Fields {
186
+ logger = logger .WithFields (log.Fields {
170
187
"bundle-dir" : c .directory ,
171
188
"container-tool" : c .imageBuilder ,
172
189
})
173
- val := registrybundle .NewImageValidator (c . imageBuilder , logger )
190
+ val := registrybundle .NewImageValidator (reg , logger )
174
191
175
192
// Validate bundle format.
176
193
if err := val .ValidateBundleFormat (c .directory ); err != nil {
@@ -190,16 +207,30 @@ func (c bundleValidateCmd) run() (res internal.Result) {
190
207
// from the ValidateBundleContent to add the output(s) into the result
191
208
checkResults (results , & res )
192
209
193
- return res
210
+ return res , nil
211
+ }
212
+
213
+ // newImageRegistryForTool returns an image registry based on what type of image tool is passed.
214
+ // If toolStr is empty, a containerd registry is returned.
215
+ func newImageRegistryForTool (logger * log.Entry , toolStr string ) (reg registryimage.Registry , err error ) {
216
+ switch toolStr {
217
+ case containertools .DockerTool .String ():
218
+ reg , err = execregistry .NewRegistry (containertools .DockerTool , logger )
219
+ case containertools .PodmanTool .String ():
220
+ reg , err = execregistry .NewRegistry (containertools .PodmanTool , logger )
221
+ default :
222
+ err = fmt .Errorf ("unrecognized image-builder option: %s" , toolStr )
223
+ }
224
+ return reg , err
194
225
}
195
226
196
227
// unpackImageIntoDir writes files in image layers found in image imageTag to dir.
197
- func (c bundleValidateCmd ) unpackImageIntoDir (imageTag , dir string ) error {
228
+ func (c bundleValidateCmd ) unpackImageIntoDir (reg registryimage. Registry , imageTag , dir string ) error {
198
229
logger := log .WithFields (log.Fields {
199
230
"bundle-dir" : dir ,
200
231
"container-tool" : c .imageBuilder ,
201
232
})
202
- val := registrybundle .NewImageValidator (c . imageBuilder , logger )
233
+ val := registrybundle .NewImageValidator (reg , logger )
203
234
204
235
return val .PullBundleImage (imageTag , dir )
205
236
}
0 commit comments