Skip to content

Commit 314ad39

Browse files
authored
Revert "NO-JIRA: adm release new: simplify writePayload" (#2000)
1 parent 44b291a commit 314ad39

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

pkg/cli/admin/release/new.go

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,9 +1308,32 @@ 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+
13111328
const imageReferencesImageStreamFilename = "image-references"
13121329

13131330
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+
13141337
// find the newest content date in the input
13151338
var newest time.Time
13161339
if err := iterateExtractedManifests(ordered, metadata, func(_ string, contents []os.FileInfo, _ string) error {
@@ -1332,15 +1355,8 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
13321355
gw := gzip.NewWriter(w)
13331356
tw := tar.NewWriter(gw)
13341357

1335-
manifestDestinationDir := "release-manifests"
13361358
// ensure the directory exists in the tar bundle
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 {
1359+
if err := writeNestedTarHeader(tw, parts, directories, tar.Header{Mode: 0777, ModTime: newest, Typeflag: tar.TypeDir}); err != nil {
13441360
return nil, err
13451361
}
13461362

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

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 {
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 {
13611370
return nil, err
13621371
}
13631372
if _, err := tw.Write(data); err != nil {
@@ -1370,23 +1379,14 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
13701379
if err != nil {
13711380
return nil, err
13721381
}
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 {
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 {
13811383
return nil, err
13821384
}
13831385
if _, err := tw.Write(data); err != nil {
13841386
return nil, err
13851387
}
13861388
}
13871389

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(manifestDestinationDir, dstFilename)
1431+
dst := path.Join(append(append([]string{}, parts...), dstFilename)...)
14321432
klog.V(4).Infof("Copying %s to %s", src, dst)
14331433

14341434
data, err := os.ReadFile(src)
@@ -1446,14 +1446,7 @@ func writePayload(w io.Writer, is *imageapi.ImageStream, cm *CincinnatiMetadata,
14461446
if err != nil {
14471447
return err
14481448
}
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 {
1449+
if err := tw.WriteHeader(&tar.Header{Mode: 0444, ModTime: fi.ModTime(), Typeflag: tar.TypeReg, Name: dst, Size: int64(len(modified))}); err != nil {
14571450
return err
14581451
}
14591452
klog.V(6).Infof("Writing payload to %s\n%s", dst, string(modified))

0 commit comments

Comments
 (0)