Skip to content

Commit 489d932

Browse files
committed
pkg/payload/image: avoid rendering manifests
The `image` command currently loads the image-references file and then renders all manifests. Clearly the manifest rendering can be avoided when we just want to look up an image reference. This command is currently run 17 times by bootkube, and my quick test shows this refactor halving the roughly 10 seconds this takes. The main refactoring here is to split the loadUpdatePayloadMetadata() function into getPayloadTasks() and loadImageReferences() so that the latter can be used standalone in this particular case.
1 parent d4e138f commit 489d932

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

pkg/payload/image.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@ package payload
22

33
import (
44
"fmt"
5+
"path/filepath"
56

67
"github.com/pkg/errors"
78
)
89

910
// ImageForShortName returns the image using the updatepayload embedded in
1011
// the Operator.
1112
func ImageForShortName(name string) (string, error) {
12-
up, err := LoadUpdate(DefaultPayloadDir, "")
13+
if err := ValidateDirectory(DefaultPayloadDir); err != nil {
14+
return "", err
15+
}
16+
17+
releaseDir := filepath.Join(DefaultPayloadDir, ReleaseManifestDir)
18+
19+
imageRef, err := loadImageReferences(releaseDir)
1320
if err != nil {
14-
return "", errors.Wrapf(err, "error loading release manifests from %q", DefaultPayloadDir)
21+
return "", errors.Wrapf(err, "error loading image references from %q", releaseDir)
1522
}
1623

17-
for _, tag := range up.ImageRef.Spec.Tags {
24+
for _, tag := range imageRef.Spec.Tags {
1825
if tag.Name == name {
1926
// we found the short name in ImageStream
2027
if tag.From != nil && tag.From.Kind == "DockerImage" {

pkg/payload/payload.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,23 @@ func loadUpdatePayloadMetadata(dir, releaseImage string) (*Update, []payloadTask
216216
releaseDir = filepath.Join(dir, ReleaseManifestDir)
217217
)
218218

219-
// XXX: load cincinnatiJSONFile
220-
cjf := filepath.Join(releaseDir, cincinnatiJSONFile)
221-
// XXX: load imageReferencesFile
222-
irf := filepath.Join(releaseDir, imageReferencesFile)
223-
imageRefData, err := ioutil.ReadFile(irf)
219+
imageRef, err := loadImageReferences(releaseDir)
224220
if err != nil {
225221
return nil, nil, err
226222
}
227223

228-
imageRef, err := resourceread.ReadImageStreamV1(imageRefData)
229-
if err != nil {
230-
return nil, nil, errors.Wrapf(err, "invalid image-references data %s", irf)
231-
}
224+
tasks := getPayloadTasks(releaseDir, cvoDir, releaseImage)
225+
226+
return &Update{ImageRef: imageRef, ReleaseImage: releaseImage, ReleaseVersion: imageRef.Name}, tasks, nil
227+
}
228+
229+
func getPayloadTasks(releaseDir, cvoDir, releaseImage string) []payloadTasks {
230+
cjf := filepath.Join(releaseDir, cincinnatiJSONFile)
231+
irf := filepath.Join(releaseDir, imageReferencesFile)
232232

233233
mrc := manifestRenderConfig{ReleaseImage: releaseImage}
234-
tasks := []payloadTasks{{
234+
235+
return []payloadTasks{{
235236
idir: cvoDir,
236237
preprocess: func(ib []byte) ([]byte, error) { return renderManifest(mrc, ib) },
237238
skipFiles: sets.NewString(),
@@ -240,5 +241,19 @@ func loadUpdatePayloadMetadata(dir, releaseImage string) (*Update, []payloadTask
240241
preprocess: nil,
241242
skipFiles: sets.NewString(cjf, irf),
242243
}}
243-
return &Update{ImageRef: imageRef, ReleaseImage: releaseImage, ReleaseVersion: imageRef.Name}, tasks, nil
244+
}
245+
246+
func loadImageReferences(releaseDir string) (*imagev1.ImageStream, error) {
247+
irf := filepath.Join(releaseDir, imageReferencesFile)
248+
imageRefData, err := ioutil.ReadFile(irf)
249+
if err != nil {
250+
return nil, err
251+
}
252+
253+
imageRef, err := resourceread.ReadImageStreamV1(imageRefData)
254+
if err != nil {
255+
return nil, errors.Wrapf(err, "invalid image-references data %s", irf)
256+
}
257+
258+
return imageRef, nil
244259
}

0 commit comments

Comments
 (0)