Skip to content

Commit 9f06891

Browse files
redbeampraveenkumar
authored andcommitted
bundle validation: check for bundle preset mismatch during setup
1 parent 55a8836 commit 9f06891

4 files changed

Lines changed: 157 additions & 9 deletions

File tree

pkg/crc/machine/bundle/metadata.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ type ClusterInfo struct {
5252
OpenshiftPullSecret string `json:"openshiftPullSecret,omitempty"`
5353
}
5454

55+
type FilenameInfo struct {
56+
Preset crcPreset.Preset
57+
Driver string
58+
Version string
59+
Arch string
60+
CustomBundleSuffix string
61+
}
62+
5563
type Node struct {
5664
Kind []string `json:"kind"`
5765
Hostname string `json:"hostname"`
@@ -244,6 +252,35 @@ func GetBundleNameFromURI(bundleURI string) string {
244252
}
245253
}
246254

255+
// GetBundleInfoFromName Parses the bundle filename and returns a FilenameInfo struct
256+
func GetBundleInfoFromName(bundleName string) (*FilenameInfo, error) {
257+
var filenameInfo FilenameInfo
258+
259+
// crc_preset_driver_version_arch_customSuffix.crcbundle
260+
bundleNameRegex := regexp.MustCompile(`crc(?:(?:_)([[:alpha:]]+))?_([[:alpha:]]+)_([0-9.]+)_([[:alnum:]]+)(?:(?:_)([0-9]+))?\.crcbundle`)
261+
filenameParts := bundleNameRegex.FindStringSubmatch(bundleName)
262+
263+
if filenameParts == nil {
264+
return &filenameInfo, fmt.Errorf("bundle filename is in unrecognized format")
265+
}
266+
267+
if filenameParts[1] == "" {
268+
filenameInfo.Preset = crcPreset.OpenShift
269+
} else {
270+
parsedPreset, err := crcPreset.ParsePresetE(filenameParts[1])
271+
if err != nil {
272+
return &filenameInfo, err
273+
}
274+
filenameInfo.Preset = parsedPreset
275+
}
276+
filenameInfo.Driver = filenameParts[2]
277+
filenameInfo.Version = filenameParts[3]
278+
filenameInfo.Arch = filenameParts[4]
279+
filenameInfo.CustomBundleSuffix = filenameParts[5]
280+
281+
return &filenameInfo, nil
282+
}
283+
247284
func getBundleDownloadInfo(preset crcPreset.Preset) (*download.RemoteFile, error) {
248285
sha256sum, err := getDefaultBundleVerifiedHash(preset)
249286
if err != nil {

pkg/crc/machine/bundle/metadata_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,83 @@ func testDataURI(t *testing.T, sha256sum string) string {
211211
require.NoError(t, err)
212212
return fmt.Sprintf("file://%s", filepath.ToSlash(absPath))
213213
}
214+
215+
func TestGetBundleInfoFromNameValid(t *testing.T) {
216+
valid := [][]string{
217+
// crc_preset_driver_version_arch_customSuffix.crcbundle
218+
{"crc_libvirt_4.16.7_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", ""},
219+
{"crc_libvirt_4.16.7_amd64_232.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", "232"},
220+
{"crc_microshift_libvirt_4.16.7_amd64.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "amd64", ""},
221+
{"crc_microshift_libvirt_4.16.7_amd64_2345.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "amd64", "2345"},
222+
{"crc_okd_vfkit_4.16.7_amd64.crcbundle", preset.OKD.String(), "vfkit", "4.16.7", "amd64", ""},
223+
{"crc_okd_vfkit_4.16.7_amd64_2342465234654.crcbundle", preset.OKD.String(), "vfkit", "4.16.7", "amd64", "2342465234654"},
224+
{"crc_hyperv_4.18.0_arm64.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.0", "arm64", ""},
225+
226+
{"crc_hyperv_4.18_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", ""},
227+
{"crc_microshift_hyperv_4.18_x86.crcbundle", preset.Microshift.String(), "hyperv", "4.18", "x86", ""},
228+
{"crc_microshift_hyperv_4.18_x86_1233.crcbundle", preset.Microshift.String(), "hyperv", "4.18", "x86", "1233"},
229+
{"crc_hyperv_4.18_x86_4566.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", "4566"},
230+
{"crc_ABCdrv_4.18.0_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", "4566"},
231+
{"crc_ABCdrv_4.18.1.2_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.1.2", "x86", "4566"},
232+
{"crc_hyperv_4.18_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", ""},
233+
{"crc_ABCdrv_4.18.0_x86.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", ""},
234+
{"crc_ABCdrv_4.18.1.2_x86.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.1.2", "x86", ""},
235+
{"crc_hyperv_4.18_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "64bit", ""},
236+
{"crc_hyperv_4.1_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.1", "64bit", ""},
237+
238+
{"crc_openshift_libvirt_4.16.7_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", ""},
239+
{"crc_openshift_libvirt_4.16.7_amd64_1.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", "1"},
240+
{"crc_openshift_libvirt_00.00.00.00_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "00.00.00.00", "amd64", ""},
241+
{"crc_openshift_libvirt_00.00.00.00_amd64_100.crcbundle", preset.OpenShift.String(), "libvirt", "00.00.00.00", "amd64", "100"},
242+
{"crc_libvirt_4.16.7_intel.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "intel", ""},
243+
{"crc_libvirt_4.16.7_intel_23.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "intel", "23"},
244+
{"crc_libvirt_4.16.7_64.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "64", ""},
245+
{"crc_libvirt_4.16.7_64_132.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "64", "132"},
246+
{"crc_microshift_libvirt_4.16.7_64.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "64", ""},
247+
{"crc_microshift_libvirt_4.16.7_64_123.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "64", "123"},
248+
{"crc_libvirt_4_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4", "amd64", ""},
249+
{"crc_libvirt_4_amd64_0123.crcbundle", preset.OpenShift.String(), "libvirt", "4", "amd64", "0123"},
250+
{"crc_okd_libvirt_4_amd64.crcbundle", preset.OKD.String(), "libvirt", "4", "amd64", ""},
251+
{"crc_okd_libvirt_4_amd64_0123.crcbundle", preset.OKD.String(), "libvirt", "4", "amd64", "0123"},
252+
}
253+
254+
for _, parts := range valid {
255+
bundleInfo, err := GetBundleInfoFromName(parts[0])
256+
assert.NoError(t, err)
257+
assert.Equal(t, bundleInfo.Preset.String(), parts[1])
258+
assert.Equal(t, bundleInfo.Driver, parts[2])
259+
assert.Equal(t, bundleInfo.Version, parts[3])
260+
assert.Equal(t, bundleInfo.Arch, parts[4])
261+
assert.Equal(t, bundleInfo.CustomBundleSuffix, parts[5])
262+
}
263+
}
264+
265+
func TestGetBundleInfoFromNameInvalid(t *testing.T) {
266+
// missing version
267+
_, err := GetBundleInfoFromName("crc_libvirt_amd64.crcbundle")
268+
assert.Error(t, err)
269+
270+
// missing crc prefix
271+
_, err = GetBundleInfoFromName("libvirt_4.16.0_amd64.crcbundle")
272+
assert.Error(t, err)
273+
274+
// missing arch
275+
_, err = GetBundleInfoFromName("crc_microshift_libvirt_4.7.0.crcbundle")
276+
assert.Error(t, err)
277+
278+
// non numeric suffix
279+
_, err = GetBundleInfoFromName("crc_microshift_libvirt_4.7.0_x86_custom.crcbundle")
280+
assert.Error(t, err)
281+
282+
// missing driver
283+
_, err = GetBundleInfoFromName("crc_4.16.2_amd64_123.crcbundle")
284+
assert.Error(t, err)
285+
286+
// missing driver and version
287+
_, err = GetBundleInfoFromName("crc_amd64_123.crcbundle")
288+
assert.Error(t, err)
289+
290+
// unknown preset
291+
_, err = GetBundleInfoFromName("crc_nanoshift_libvirt_4.16.7_amd64_232.crcbundle")
292+
assert.Error(t, err)
293+
}

pkg/crc/machine/start.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
284284
return nil, errors.Wrap(err, "Error getting bundle metadata")
285285
}
286286

287-
if err := bundleMismatchWithPreset(startConfig.Preset, crcBundleMetadata); err != nil {
287+
if err := validation.BundleMismatchWithPresetMetadata(startConfig.Preset, crcBundleMetadata); err != nil {
288288
return nil, err
289289
}
290290

@@ -854,13 +854,6 @@ func updateKubeconfig(ctx context.Context, ocConfig oc.Config, sshRunner *crcssh
854854
return nil
855855
}
856856

857-
func bundleMismatchWithPreset(preset crcPreset.Preset, bundleMetadata *bundle.CrcBundleInfo) error {
858-
if preset != bundleMetadata.GetBundleType() {
859-
return errors.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleMetadata.GetBundleType())
860-
}
861-
return nil
862-
}
863-
864857
func startMicroshift(ctx context.Context, sshRunner *crcssh.Runner, ocConfig oc.Config, pullSec cluster.PullSecretLoader) error {
865858
logging.Infof("Starting Microshift service... [takes around 1min]")
866859
if err := ensurePullSecretPresentInVM(sshRunner, pullSec); err != nil {

pkg/crc/validation/validation.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,28 @@ func ValidateBundle(bundlePath string, preset crcpreset.Preset) error {
9595
if bundlePath == constants.GetDefaultBundlePath(preset) {
9696
return nil
9797
}
98-
return ValidateBundlePath(bundlePath, preset)
98+
99+
if err := ValidateBundlePath(bundlePath, preset); err != nil {
100+
return err
101+
}
102+
103+
bundleInfo, err := bundle.GetBundleInfoFromName(bundleName)
104+
if err != nil {
105+
return err
106+
}
107+
108+
if err := BundleMismatchWithPresetFilename(preset, bundleInfo); err != nil {
109+
logging.Fatal(err.Error())
110+
return err
111+
}
112+
return nil
113+
}
114+
115+
if err := BundleMismatchWithPresetMetadata(preset, bundleMetadata); err != nil {
116+
logging.Fatal(err.Error())
117+
return err
99118
}
119+
100120
bundleMismatchWarning(bundleMetadata.GetBundleName(), preset)
101121
/* 'bundle' is already unpacked in ~/.crc/cache */
102122
return nil
@@ -117,6 +137,24 @@ func bundleMismatchWarning(userProvidedBundle string, preset crcpreset.Preset) {
117137
}
118138
}
119139

140+
// BundleMismatchWithPresetFilename checks whether the bundle matches the configured preset based on the bundle filename
141+
// (bundle is not downloaded or uncompressed yet)
142+
func BundleMismatchWithPresetFilename(preset crcpreset.Preset, bundleFilenameInfo *bundle.FilenameInfo) error {
143+
if preset != bundleFilenameInfo.Preset {
144+
return fmt.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleFilenameInfo.Preset)
145+
}
146+
return nil
147+
}
148+
149+
// BundleMismatchWithPresetMetadata checks whether the bundle matches the configured preset based on the bundle metadata
150+
// (bundle is already downloaded and uncompressed)
151+
func BundleMismatchWithPresetMetadata(preset crcpreset.Preset, bundleMetadata *bundle.CrcBundleInfo) error {
152+
if preset != bundleMetadata.GetBundleType() {
153+
return fmt.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleMetadata.GetBundleType())
154+
}
155+
return nil
156+
}
157+
120158
// ValidateIPAddress checks if provided IP is valid
121159
func ValidateIPAddress(ipAddress string) error {
122160
ip := net.ParseIP(ipAddress).To4()

0 commit comments

Comments
 (0)