github.com/openshift/installer@v1.4.17/pkg/asset/rhcos/release.go (about)

     1  // Package rhcos contains assets for RHCOS.
     2  package rhcos
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  	"time"
     8  
     9  	"github.com/coreos/stream-metadata-go/arch"
    10  
    11  	"github.com/openshift/installer/pkg/asset"
    12  	"github.com/openshift/installer/pkg/asset/installconfig"
    13  	"github.com/openshift/installer/pkg/rhcos"
    14  	"github.com/openshift/installer/pkg/types"
    15  	"github.com/openshift/installer/pkg/types/azure"
    16  )
    17  
    18  // Release is a string which denotes the rhcos release, eg: 412.86.202208101040-0.
    19  // Currently we need this only for Azure to set the image version in the gallery.
    20  // In the future we could extend to other platforms as necessary.
    21  type Release string
    22  
    23  var _ asset.Asset = (*Release)(nil)
    24  
    25  // Name returns the human-friendly name of the asset.
    26  func (r *Release) Name() string {
    27  	return "Release"
    28  }
    29  
    30  // Dependencies returns dependencies used by the RHCOS asset.
    31  func (r *Release) Dependencies() []asset.Asset {
    32  	return []asset.Asset{
    33  		&installconfig.InstallConfig{},
    34  	}
    35  }
    36  
    37  // Generate the Release string.
    38  func (r *Release) Generate(ctx context.Context, p asset.Parents) error {
    39  	ic := &installconfig.InstallConfig{}
    40  	p.Get(ic)
    41  	config := ic.Config
    42  	release, err := release(ctx, config)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	*r = Release(release)
    47  	return nil
    48  }
    49  
    50  func release(ctx context.Context, config *types.InstallConfig) (string, error) {
    51  	ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
    52  	defer cancel()
    53  
    54  	archName := arch.RpmArch(string(config.ControlPlane.Architecture))
    55  
    56  	st, err := rhcos.FetchCoreOSBuild(ctx)
    57  	if err != nil {
    58  		return "", err
    59  	}
    60  	streamArch, err := st.GetArchitecture(archName)
    61  	if err != nil {
    62  		return "", err
    63  	}
    64  	switch config.Platform.Name() {
    65  	case azure.Name:
    66  		ext := streamArch.RHELCoreOSExtensions
    67  		if ext == nil {
    68  			return "", fmt.Errorf("%s: No azure build found", st.FormatPrefix(archName))
    69  		}
    70  		azd := ext.AzureDisk
    71  		if azd == nil {
    72  			return "", fmt.Errorf("%s: No azure build found", st.FormatPrefix(archName))
    73  		}
    74  		return azd.Release, nil
    75  	default:
    76  		return "", nil
    77  	}
    78  }
    79  
    80  // GetAzureReleaseVersion - generates a modified string for Azure image gallery images. Image gallery image versions cannot have
    81  // a "-" in the name and must be between 0-2147483647, so we have to truncate the hour and minutes of the date.
    82  func (r *Release) GetAzureReleaseVersion() string {
    83  	imageVersion := string(*r)
    84  	if imageVersion != "" {
    85  		imageVersion = imageVersion[:len(imageVersion)-6]
    86  	}
    87  	return imageVersion
    88  }