github.com/openshift/installer@v1.4.17/pkg/asset/agent/image/releaseimage.go (about)

     1  package image
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/openshift/installer/pkg/version"
    10  )
    11  
    12  type releaseImage struct {
    13  	ReleaseVersion string   `json:"openshift_version"`
    14  	Arch           string   `json:"cpu_architecture"`
    15  	Archs          []string `json:"cpu_architectures"`
    16  	PullSpec       string   `json:"url"`
    17  	Tag            string   `json:"version"`
    18  }
    19  
    20  func isDigest(pullspec string) bool {
    21  	return regexp.MustCompile(`.*sha256:[a-fA-F0-9]{64}$`).MatchString(pullspec)
    22  }
    23  
    24  func releaseImageFromPullSpec(pullSpec string, arch string, archs []string, version string) (releaseImage, error) {
    25  	// When the pullspec it's a digest let's use the current version
    26  	// stored in the installer
    27  	if isDigest(pullSpec) {
    28  		return releaseImage{
    29  			ReleaseVersion: version,
    30  			Arch:           arch,
    31  			Archs:          archs,
    32  			PullSpec:       pullSpec,
    33  			Tag:            version,
    34  		}, nil
    35  	}
    36  
    37  	components := strings.Split(pullSpec, ":")
    38  	if len(components) < 2 {
    39  		return releaseImage{}, fmt.Errorf("invalid release image \"%s\"", pullSpec)
    40  	}
    41  	lastIndex := len(components) - 1
    42  	tag := strings.TrimSuffix(components[lastIndex], fmt.Sprintf("-%s", arch))
    43  
    44  	versionComponents := strings.Split(tag, ".")
    45  	if len(versionComponents) < 2 {
    46  		return releaseImage{}, fmt.Errorf("invalid release image version \"%s\"", tag)
    47  	}
    48  	relVersion := strings.Join(versionComponents[:2], ".")
    49  
    50  	return releaseImage{
    51  		ReleaseVersion: relVersion,
    52  		Arch:           arch,
    53  		Archs:          archs,
    54  		PullSpec:       pullSpec,
    55  		Tag:            tag,
    56  	}, nil
    57  }
    58  
    59  func releaseImageList(pullSpec, arch string, archs []string) (string, error) {
    60  	versionString, err := version.Version()
    61  	if err != nil {
    62  		return "", err
    63  	}
    64  
    65  	relImage, err := releaseImageFromPullSpec(pullSpec, arch, archs, versionString)
    66  	if err != nil {
    67  		return "", err
    68  	}
    69  
    70  	imageList := []interface{}{relImage}
    71  	text, err := json.Marshal(imageList)
    72  	return string(text), err
    73  }
    74  
    75  func releaseImageListWithVersion(pullSpec, arch string, archs []string, openshiftVersion string) (string, error) {
    76  	relImage, err := releaseImageFromPullSpec(pullSpec, arch, archs, openshiftVersion)
    77  	if err != nil {
    78  		return "", err
    79  	}
    80  
    81  	imageList := []interface{}{relImage}
    82  	text, err := json.Marshal(imageList)
    83  	return string(text), err
    84  }