Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bundle validation: check for bundle preset mismatch during setup #4343

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

@anjannath anjannath Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, apologies i should've mentioned this earlier, but forgot, there's a scenario where the filenameParts can be 6, we have a custom bundle generation command crc bundle generate that creates new bundle out of existing one and it names them as crc_microshift_vfkit_4.16.7_arm64_2345.crcbundle for a microshift bundle and crc_vfkit_4.16.7_arm64_2345.crcbundle for an openshift bundle which are valid names; see: https://github.com/crc-org/crc/blob/main/pkg/crc/machine/bundle/metadata.go#L227

so if i use a custom bundle currently this fails, for an openshift bundle as:

% crc config set bundle ./crc_vfkit_4.16.7_arm64_2345.crcbundle
WARN Using custom bundle crc_vfkit_4.16.7_arm64_2345.crcbundle 
Successfully configured bundle to ./crc_vfkit_4.16.7_arm64_2345.crcbundle

% crc setup --log-level debug
DEBU CRC version: 2.41.0+1921db
DEBU OpenShift version: 4.16.7
DEBU MicroShift version: 4.16.7
DEBU Running 'crc setup'
DEBU Got bundle path: ./crc_vfkit_4.16.7_arm64_2345.crcbundle
DEBU Failed to parse url: parse "./crc_vfkit_4.16.7_arm64_2345.crcbundle": invalid URI for request
WARN Using custom bundle crc_vfkit_4.16.7_arm64_2345.crcbundle
Cannot parse preset 'vfkit'

for a microshift bundle, as:

% crc config set bundle ./crc_microshift_vfkit_4.16.7_arm64_232.crcbundle
WARN Using custom bundle crc_microshift_vfkit_4.16.7_arm64_232.crcbundle
Successfully configured bundle to ./crc_microshift_vfkit_4.16.7_arm64_232.crcbundle

% crc setup --log-level debug
DEBU CRC version: 2.41.0+1921db
DEBU OpenShift version: 4.16.7
DEBU MicroShift version: 4.16.7
DEBU Running 'crc setup'
DEBU Got bundle path: ./crc_microshift_vfkit_4.16.7_arm64_232.crcbundle
DEBU Failed to parse url: parse "./crc_microshift_vfkit_4.16.7_arm64_232.crcbundle": invalid URI for request
WARN Using custom bundle crc_microshift_vfkit_4.16.7_arm64_232.crcbundle
bundle filename is in unrecognized format

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(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
Loading