Skip to content

Commit

Permalink
bundle validation: check for bundle preset mismatch during setup
Browse files Browse the repository at this point in the history
  • Loading branch information
redbeam committed Sep 26, 2024
1 parent 71a3f1d commit 7c9961b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
35 changes: 35 additions & 0 deletions pkg/crc/machine/bundle/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ type ClusterInfo struct {
OpenshiftPullSecret string `json:"openshiftPullSecret,omitempty"`
}

type FilenameInfo struct {
Preset crcPreset.Preset
Driver string
Version string
Arch string
}

type Node struct {
Kind []string `json:"kind"`
Hostname string `json:"hostname"`
Expand Down Expand Up @@ -244,6 +251,34 @@ func GetBundleNameFromURI(bundleURI string) string {
}
}

// GetBundleInfoFromName Parses the bundle filename and returns a FilenameInfo struct
func GetBundleInfoFromName(bundleName string) (*FilenameInfo, error) {
var filenameInfo FilenameInfo
bundleName = GetBundleNameWithoutExtension(bundleName)
filenameParts := strings.Split(bundleName, "_")

switch len(filenameParts) {
case 4:
filenameInfo.Preset = crcPreset.OpenShift
filenameInfo.Driver = filenameParts[1]
filenameInfo.Version = filenameParts[2]
filenameInfo.Arch = filenameParts[3]
case 5:
parsedPreset, err := crcPreset.ParsePresetE(filenameParts[1])
if err != nil {
return &filenameInfo, err
}
filenameInfo.Preset = parsedPreset
filenameInfo.Driver = filenameParts[2]
filenameInfo.Version = filenameParts[3]
filenameInfo.Arch = filenameParts[4]
default:
return &filenameInfo, fmt.Errorf("bundle filename is in unrecognized format")
}

return &filenameInfo, nil
}

func getBundleDownloadInfo(preset crcPreset.Preset) (*download.RemoteFile, error) {
sha256sum, err := getDefaultBundleVerifiedHash(preset)
if err != nil {
Expand Down
9 changes: 1 addition & 8 deletions pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
return nil, errors.Wrap(err, "Error getting bundle metadata")
}

if err := bundleMismatchWithPreset(startConfig.Preset, crcBundleMetadata); err != nil {
if err := validation.BundleMismatchWithPresetMetadata(startConfig.Preset, crcBundleMetadata); err != nil {
return nil, err
}

Expand Down Expand Up @@ -854,13 +854,6 @@ func updateKubeconfig(ctx context.Context, ocConfig oc.Config, sshRunner *crcssh
return nil
}

func bundleMismatchWithPreset(preset crcPreset.Preset, bundleMetadata *bundle.CrcBundleInfo) error {
if preset != bundleMetadata.GetBundleType() {
return errors.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleMetadata.GetBundleType())
}
return nil
}

func startMicroshift(ctx context.Context, sshRunner *crcssh.Runner, ocConfig oc.Config, pullSec cluster.PullSecretLoader) error {
logging.Infof("Starting Microshift service... [takes around 1min]")
if err := ensurePullSecretPresentInVM(sshRunner, pullSec); err != nil {
Expand Down
40 changes: 39 additions & 1 deletion pkg/crc/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,28 @@ func ValidateBundle(bundlePath string, preset crcpreset.Preset) error {
if bundlePath == constants.GetDefaultBundlePath(preset) {
return nil
}
return ValidateBundlePath(bundlePath, preset)

if err := ValidateBundlePath(bundlePath, preset); err != nil {
return err
}

bundleInfo, err := bundle.GetBundleInfoFromName(bundle.GetBundleNameWithoutExtension(bundleName))
if err != nil {
return err
}

if err := BundleMismatchWithPresetFilename(preset, bundleInfo); err != nil {
logging.Fatal(err.Error())
return err
}
return nil
}

if err := BundleMismatchWithPresetMetadata(preset, bundleMetadata); err != nil {
logging.Fatal(err.Error())
return err
}

bundleMismatchWarning(bundleMetadata.GetBundleName(), preset)
/* 'bundle' is already unpacked in ~/.crc/cache */
return nil
Expand All @@ -117,6 +137,24 @@ func bundleMismatchWarning(userProvidedBundle string, preset crcpreset.Preset) {
}
}

// BundleMismatchWithPresetFilename checks whether the bundle matches the configured preset based on the bundle filename
// (bundle is not downloaded or uncompressed yet)
func BundleMismatchWithPresetFilename(preset crcpreset.Preset, bundleFilenameInfo *bundle.FilenameInfo) error {
if preset != bundleFilenameInfo.Preset {
return fmt.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleFilenameInfo.Preset)
}
return nil
}

// BundleMismatchWithPresetMetadata checks whether the bundle matches the configured preset based on the bundle metadata
// (bundle is already downloaded and uncompressed)
func BundleMismatchWithPresetMetadata(preset crcpreset.Preset, bundleMetadata *bundle.CrcBundleInfo) error {
if preset != bundleMetadata.GetBundleType() {
return fmt.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleMetadata.GetBundleType())
}
return nil
}

// ValidateIPAddress checks if provided IP is valid
func ValidateIPAddress(ipAddress string) error {
ip := net.ParseIP(ipAddress).To4()
Expand Down

0 comments on commit 7c9961b

Please sign in to comment.