github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/pkg/utils/build/image.go (about) 1 package build 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "time" 8 9 . "github.com/onsi/ginkgo/v2" 10 11 "github.com/openshift/library-go/pkg/image/reference" 12 "github.com/openshift/oc/pkg/cli/image/extract" 13 "github.com/openshift/oc/pkg/cli/image/imagesource" 14 imageInfo "github.com/openshift/oc/pkg/cli/image/info" 15 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" 16 "k8s.io/cli-runtime/pkg/genericclioptions" 17 ) 18 19 func ExtractImage(image string) (string, error) { 20 dockerImageRef, err := reference.Parse(image) 21 if err != nil { 22 return "", fmt.Errorf("cannot parse docker pull spec (image) %s, error: %+v", image, err) 23 } 24 tmpDir, err := os.MkdirTemp(os.TempDir(), "sbom") 25 if err != nil { 26 return "", fmt.Errorf("error when creating a temp directory for extracting files: %+v", err) 27 } 28 GinkgoWriter.Printf("extracting contents of container image %s to dir: %s\n", image, tmpDir) 29 eMapping := extract.Mapping{ 30 ImageRef: imagesource.TypedImageReference{Type: "docker", Ref: dockerImageRef}, 31 To: tmpDir, 32 } 33 e := extract.NewExtractOptions(genericclioptions.IOStreams{Out: os.Stdout, ErrOut: os.Stderr}) 34 e.Mappings = []extract.Mapping{eMapping} 35 36 if err := e.Run(); err != nil { 37 return "", fmt.Errorf("error: %+v", err) 38 } 39 return tmpDir, nil 40 } 41 42 func ImageFromPipelineRun(pipelineRun *v1beta1.PipelineRun) (*imageInfo.Image, error) { 43 var outputImage string 44 for _, parameter := range pipelineRun.Spec.Params { 45 if parameter.Name == "output-image" { 46 outputImage = parameter.Value.StringVal 47 } 48 } 49 if outputImage == "" { 50 return nil, fmt.Errorf("output-image in PipelineRun not found") 51 } 52 53 dockerImageRef, err := reference.Parse(outputImage) 54 if err != nil { 55 return nil, fmt.Errorf("error parsing outputImage to dockerImageRef, %w", err) 56 } 57 58 imageRetriever := imageInfo.ImageRetriever{} 59 60 ctx, cancel := context.WithTimeout(context.Background(), time.Minute) 61 defer cancel() 62 image, err := imageRetriever.Image(ctx, imagesource.TypedImageReference{Type: "docker", Ref: dockerImageRef}) 63 if err != nil { 64 return nil, fmt.Errorf("error getting image from imageRetriver, %w", err) 65 } 66 return image, nil 67 }