github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/utils/tekton/images.go (about)

     1  package tekton
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"strings"
     9  )
    10  
    11  const quayBaseUrl = "https://quay.io/api/v1"
    12  
    13  type ManifestResponse struct {
    14  	Layers []any `json:"layers"`
    15  }
    16  
    17  type QuayImageInfo struct {
    18  	ImageRef string
    19  	Layers   []any
    20  }
    21  
    22  type Tag struct {
    23  	Digest string `json:"manifest_digest"`
    24  }
    25  
    26  type TagResponse struct {
    27  	Tags []Tag `json:"tags"`
    28  }
    29  
    30  // getImageInfoFromQuay returns QuayImageInfo for a given image.
    31  func getImageInfoFromQuay(imageRepo, imageTag string) (*QuayImageInfo, error) {
    32  
    33  	res, err := http.Get(fmt.Sprintf("%s/repository/%s/tag/?specificTag=%s", quayBaseUrl, imageRepo, imageTag))
    34  	if err != nil {
    35  		return nil, fmt.Errorf("cannot get quay.io/%s:%s image from container registry: %+v", imageRepo, imageTag, err)
    36  	}
    37  	body, err := io.ReadAll(res.Body)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("cannot read body of a response from quay.io regarding quay.io/%s:%s image %+v", imageRepo, imageTag, err)
    40  	}
    41  
    42  	tagResponse := &TagResponse{}
    43  	if err = json.Unmarshal(body, tagResponse); err != nil {
    44  		return nil, fmt.Errorf("failed to unmarshal response from quay.io regarding quay.io/%s:%s image %+v", imageRepo, imageTag, err)
    45  	}
    46  
    47  	if len(tagResponse.Tags) < 1 {
    48  		return nil, fmt.Errorf("cannot get manifest digest from quay.io/%s:%s image. response body: %+v", imageRepo, imageTag, string(body))
    49  	}
    50  
    51  	quayImageInfo := &QuayImageInfo{}
    52  	quayImageInfo.ImageRef = fmt.Sprintf("quay.io/%s@%s", imageRepo, tagResponse.Tags[0].Digest)
    53  
    54  	if strings.Contains(imageTag, ".att") {
    55  		res, err = http.Get(fmt.Sprintf("%s/repository/%s/manifest/%s", quayBaseUrl, imageRepo, tagResponse.Tags[0].Digest))
    56  		if err != nil {
    57  			return nil, fmt.Errorf("cannot get quay.io/%s@%s image from container registry: %+v", imageRepo, quayImageInfo.ImageRef, err)
    58  		}
    59  		body, err = io.ReadAll(res.Body)
    60  		if err != nil {
    61  			return nil, fmt.Errorf("cannot read body of a response from quay.io regarding %s image: %+v", quayImageInfo.ImageRef, err)
    62  		}
    63  		manifestResponse := &ManifestResponse{}
    64  		if err := json.Unmarshal(body, manifestResponse); err != nil {
    65  			return nil, fmt.Errorf("failed to unmarshal response from quay.io regarding %s image: %+v", quayImageInfo.ImageRef, err)
    66  		}
    67  
    68  		if len(manifestResponse.Layers) < 1 {
    69  			return nil, fmt.Errorf("cannot get layers from %s image. response body: %+v", quayImageInfo.ImageRef, string(body))
    70  		}
    71  		quayImageInfo.Layers = manifestResponse.Layers
    72  	}
    73  
    74  	return quayImageInfo, nil
    75  }