88 "fmt"
99 "io"
1010 "os"
11- "path"
1211 "path/filepath"
1312 "sync"
1413 "time"
@@ -20,6 +19,7 @@ import (
2019
2120 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2221 "k8s.io/apimachinery/pkg/runtime/schema"
22+ "k8s.io/apimachinery/pkg/util/sets"
2323 "k8s.io/cli-runtime/pkg/genericiooptions"
2424 "k8s.io/client-go/rest"
2525 kcmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -349,7 +349,7 @@ func (o *ExtractOptions) Run(ctx context.Context) error {
349349 }
350350 }
351351
352- var manifestErrs [] error
352+ manifestReceiver := ManifestReceiver { skipNames : sets . New [ string ]( "image-references" , "release-metadata" )}
353353 // o.ExtractManifests implies o.File == ""
354354 if o .ExtractManifests {
355355 expectedProviderSpecKind := credRequestCloudProviderSpecKindMapping [o .Cloud ]
@@ -379,8 +379,8 @@ func (o *ExtractOptions) Run(ctx context.Context) error {
379379 include = newIncluder (inclusionConfig )
380380 }
381381
382- opts . TarEntryCallback = func (hdr * tar. Header , _ extract. LayerInfo , r io.Reader ) (bool , error ) {
383- if hdr . Name == "image-references" && ! o .CredentialsRequests {
382+ manifestReceiver . manifestsCallback = func (filename string , ms []manifest. Manifest , r io.Reader ) (bool , error ) {
383+ if filename == "image-references" && ! o .CredentialsRequests {
384384 buf := & bytes.Buffer {}
385385 if _ , err := io .Copy (buf , r ); err != nil {
386386 return false , fmt .Errorf ("unable to load image-references from release payload: %w" , err )
@@ -398,7 +398,7 @@ func (o *ExtractOptions) Run(ctx context.Context) error {
398398
399399 out := o .Out
400400 if o .Directory != "" {
401- out , err = os .Create (filepath .Join (o .Directory , hdr . Name ))
401+ out , err = os .Create (filepath .Join (o .Directory , filename ))
402402 if err != nil {
403403 return false , err
404404 }
@@ -408,10 +408,10 @@ func (o *ExtractOptions) Run(ctx context.Context) error {
408408 return true , err
409409 }
410410 return true , nil
411- } else if hdr . Name == "release-metadata" && ! o .CredentialsRequests {
411+ } else if filename == "release-metadata" && ! o .CredentialsRequests {
412412 out := o .Out
413413 if o .Directory != "" {
414- out , err = os .Create (filepath .Join (o .Directory , hdr . Name ))
414+ out , err = os .Create (filepath .Join (o .Directory , filename ))
415415 if err != nil {
416416 return false , err
417417 }
@@ -423,16 +423,6 @@ func (o *ExtractOptions) Run(ctx context.Context) error {
423423 return true , nil
424424 }
425425
426- if ext := path .Ext (hdr .Name ); len (ext ) == 0 || ! (ext == ".yaml" || ext == ".yml" || ext == ".json" ) {
427- return true , nil
428- }
429- klog .V (4 ).Infof ("Found manifest %s" , hdr .Name )
430- ms , err := manifest .ParseManifests (r )
431- if err != nil {
432- manifestErrs = append (manifestErrs , errors .Wrapf (err , "error parsing %s" , hdr .Name ))
433- return true , nil
434- }
435-
436426 for i := len (ms ) - 1 ; i >= 0 ; i -- {
437427 if o .Included && o .CredentialsRequests && ms [i ].GVK == credentialsRequestGVK && len (ms [i ].Obj .GetAnnotations ()) == 0 {
438428 klog .V (4 ).Infof ("Including %s for manual CredentialsRequests, despite lack of annotations" , ms [i ].String ())
@@ -469,25 +459,26 @@ func (o *ExtractOptions) Run(ctx context.Context) error {
469459
470460 out := o .Out
471461 if o .Directory != "" {
472- out , err = os .Create (filepath .Join (o .Directory , hdr . Name ))
462+ out , err = os .Create (filepath .Join (o .Directory , filename ))
473463 if err != nil {
474- return false , errors .Wrapf (err , "error creating manifest in %s" , hdr . Name )
464+ return false , errors .Wrapf (err , "error creating manifest in %s" , filename )
475465 }
476466 }
477467 if out != nil {
478468 for _ , m := range manifestsToWrite {
479469 yamlBytes , err := yaml .JSONToYAML (m .Raw )
480470 if err != nil {
481- return false , errors .Wrapf (err , "error serializing manifest in %s" , hdr . Name )
471+ return false , errors .Wrapf (err , "error serializing manifest in %s" , filename )
482472 }
483473 fmt .Fprintf (out , "---\n " )
484474 if _ , err := out .Write (yamlBytes ); err != nil {
485- return false , errors .Wrapf (err , "error writing manifest in %s" , hdr . Name )
475+ return false , errors .Wrapf (err , "error writing manifest in %s" , filename )
486476 }
487477 }
488478 }
489479 return true , nil
490480 }
481+ opts .TarEntryCallback = manifestReceiver .TarEntryCallback
491482 }
492483
493484 fileFound := false
@@ -506,6 +497,7 @@ func (o *ExtractOptions) Run(ctx context.Context) error {
506497 return err
507498 }
508499
500+ manifestErrs := manifestReceiver .ManifestErrs
509501 if metadataVerifyMsg != "" {
510502 if o .File == "" && o .Out != nil {
511503 fmt .Fprintf (o .Out , "%s\n " , metadataVerifyMsg )
0 commit comments