Skip to content

Commit 44b291a

Browse files
Merge pull request #1997 from petr-muller/ocpbugs-30080-cvo-placeholder-replacement
NO-JIRA: `adm release new`: simplify `writePayload`
2 parents b105cb3 + 38f6e6a commit 44b291a

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

pkg/cli/admin/release/new.go

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,32 +1308,9 @@ type nopCloser struct {
13081308

13091309
func (_ nopCloser) Close() error { return nil }
13101310

1311-
// writeNestedTarHeader writes a series of nested tar headers, starting with parts[0] and joining each
1312-
// successive part, but only if the path does not exist already.
1313-
func writeNestedTarHeader(tw *tar.Writer, parts []string, existing map[string]struct{}, hdr tar.Header) error {
1314-
for i := range parts {
1315-
componentDir := path.Join(parts[:i+1]...)
1316-
if _, ok := existing[componentDir]; ok {
1317-
continue
1318-
}
1319-
existing[componentDir] = struct{}{}
1320-
hdr.Name = componentDir
1321-
if err := tw.WriteHeader(&hdr); err != nil {
1322-
return err
1323-
}
1324-
}
1325-
return nil
1326-
}
1327-
13281311
const imageReferencesImageStreamFilename = "image-references"
13291312

13301313
func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata, ordered []string, metadata map[string]imageData, allowMissingImages bool, verifiers []PayloadVerifier, errOut io.Writer) ([]string, error) {
1331-
var operators []string
1332-
directories := make(map[string]struct{})
1333-
files := make(map[string]int)
1334-
1335-
parts := []string{"release-manifests"}
1336-
13371314
// find the newest content date in the input
13381315
var newest time.Time
13391316
if err := iterateExtractedManifests(ordered, metadata, func(_ string, contents []os.FileInfo, _ string) error {
@@ -1355,8 +1332,15 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
13551332
gw := gzip.NewWriter(w)
13561333
tw := tar.NewWriter(gw)
13571334

1335+
manifestDestinationDir := "release-manifests"
13581336
// ensure the directory exists in the tar bundle
1359-
if err := writeNestedTarHeader(tw, parts, directories, tar.Header{Mode: 0777, ModTime: newest, Typeflag: tar.TypeDir}); err != nil {
1337+
releaseManifestsDirHdr := tar.Header{
1338+
Name: manifestDestinationDir,
1339+
Mode: 0777,
1340+
ModTime: newest,
1341+
Typeflag: tar.TypeDir,
1342+
}
1343+
if err := tw.WriteHeader(&releaseManifestsDirHdr); err != nil {
13601344
return nil, err
13611345
}
13621346

@@ -1366,7 +1350,14 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
13661350
return nil, err
13671351
}
13681352

1369-
if err := tw.WriteHeader(&tar.Header{Mode: 0444, ModTime: newest, Typeflag: tar.TypeReg, Name: path.Join(append(append([]string{}, parts...), imageReferencesImageStreamFilename)...), Size: int64(len(data))}); err != nil {
1353+
imageReferencesHdr := tar.Header{
1354+
Name: path.Join(manifestDestinationDir, imageReferencesImageStreamFilename),
1355+
Mode: 0444,
1356+
ModTime: newest,
1357+
Typeflag: tar.TypeReg,
1358+
Size: int64(len(data)),
1359+
}
1360+
if err := tw.WriteHeader(&imageReferencesHdr); err != nil {
13701361
return nil, err
13711362
}
13721363
if _, err := tw.Write(data); err != nil {
@@ -1379,14 +1370,23 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
13791370
if err != nil {
13801371
return nil, err
13811372
}
1382-
if err := tw.WriteHeader(&tar.Header{Mode: 0444, ModTime: newest, Typeflag: tar.TypeReg, Name: path.Join(append(append([]string{}, parts...), "release-metadata")...), Size: int64(len(data))}); err != nil {
1373+
releaseMetadataHdr := tar.Header{
1374+
Name: path.Join(manifestDestinationDir, "release-metadata"),
1375+
Mode: 0444,
1376+
ModTime: newest,
1377+
Typeflag: tar.TypeReg,
1378+
Size: int64(len(data)),
1379+
}
1380+
if err := tw.WriteHeader(&releaseMetadataHdr); err != nil {
13831381
return nil, err
13841382
}
13851383
if _, err := tw.Write(data); err != nil {
13861384
return nil, err
13871385
}
13881386
}
13891387

1388+
var operators []string
1389+
files := make(map[string]int)
13901390
// read each directory, processing the manifests in order and updating the contents into the tar output
13911391
if err := iterateExtractedManifests(ordered, metadata, func(directory string, contents []os.FileInfo, operator string) error {
13921392
transform := NewSimpleVersionsMapper(is.Name)
@@ -1428,7 +1428,7 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
14281428
files[dstFilename] = 1
14291429

14301430
src := filepath.Join(directory, srcFilename)
1431-
dst := path.Join(append(append([]string{}, parts...), dstFilename)...)
1431+
dst := path.Join(manifestDestinationDir, dstFilename)
14321432
klog.V(4).Infof("Copying %s to %s", src, dst)
14331433

14341434
data, err := os.ReadFile(src)
@@ -1446,7 +1446,14 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
14461446
if err != nil {
14471447
return err
14481448
}
1449-
if err := tw.WriteHeader(&tar.Header{Mode: 0444, ModTime: fi.ModTime(), Typeflag: tar.TypeReg, Name: dst, Size: int64(len(modified))}); err != nil {
1449+
dstHdr := tar.Header{
1450+
Name: dst,
1451+
Mode: 0444,
1452+
ModTime: fi.ModTime(),
1453+
Typeflag: tar.TypeReg,
1454+
Size: int64(len(modified)),
1455+
}
1456+
if err := tw.WriteHeader(&dstHdr); err != nil {
14501457
return err
14511458
}
14521459
klog.V(6).Infof("Writing payload to %s\n%s", dst, string(modified))

0 commit comments

Comments
 (0)