Skip to content

Commit 2cc4be5

Browse files
committed
feat(kubevirt): search kubevirt-os-images for DataSources
Previously, the vm_create tool only searched for DataSources in the OpenShift-specific 'openshift-virtualization-os-images' namespace. This change extends the search to also include the 'kubevirt-os-images' namespace, which is commonly used in upstream KubeVirt deployments. The logic for querying a namespace has been refactored into a helper function to accommodate searching multiple well-known locations for boot images. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
1 parent 52b12e7 commit 2cc4be5

File tree

1 file changed

+48
-30
lines changed
  • pkg/toolsets/kubevirt/vm/create

1 file changed

+48
-30
lines changed

pkg/toolsets/kubevirt/vm/create/tool.go

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -430,41 +430,23 @@ func searchDataSources(params api.ToolHandlerParams, query string) ([]DataSource
430430

431431
var results []DataSourceInfo
432432

433-
// Try to list DataSources from OpenShift virtualization OS images namespace first
434-
openshiftNamespace := "openshift-virtualization-os-images"
435-
openshiftList, err := dynamicClient.Resource(dataSourceGVR).Namespace(openshiftNamespace).List(params.Context, metav1.ListOptions{})
436-
if err == nil {
437-
// Parse OpenShift DataSources
438-
for _, item := range openshiftList.Items {
439-
name := item.GetName()
440-
namespace := item.GetNamespace()
441-
labels := item.GetLabels()
442-
443-
// Extract source information from the DataSource spec
444-
source := extractDataSourceInfo(&item)
445-
446-
// Extract default instancetype and preference from labels
447-
defaultInstancetype := ""
448-
defaultPreference := ""
449-
if labels != nil {
450-
defaultInstancetype = labels["instancetype.kubevirt.io/default-instancetype"]
451-
defaultPreference = labels["instancetype.kubevirt.io/default-preference"]
452-
}
433+
// Try to list DataSources from well-known namespaces first
434+
wellKnownNamespaces := []string{
435+
"openshift-virtualization-os-images",
436+
"kubevirt-os-images",
437+
}
453438

454-
results = append(results, DataSourceInfo{
455-
Name: name,
456-
Namespace: namespace,
457-
Source: source,
458-
DefaultInstancetype: defaultInstancetype,
459-
DefaultPreference: defaultPreference,
460-
})
439+
for _, ns := range wellKnownNamespaces {
440+
dsInfos, err := listDataSourcesFromNamespace(params, dynamicClient, dataSourceGVR, ns)
441+
if err == nil {
442+
results = append(results, dsInfos...)
461443
}
462444
}
463445

464446
// List DataSources from all namespaces
465447
list, err := dynamicClient.Resource(dataSourceGVR).List(params.Context, metav1.ListOptions{})
466448
if err != nil {
467-
// If we found OpenShift DataSources but couldn't list all namespaces, continue
449+
// If we found DataSources from well-known namespaces but couldn't list all, continue
468450
if len(results) > 0 {
469451
// Add common containerdisk images as well
470452
results = append(results, getDefaultContainerDisks()...)
@@ -480,9 +462,9 @@ func searchDataSources(params api.ToolHandlerParams, query string) ([]DataSource
480462
}, nil
481463
}
482464

483-
// Parse the results from all namespaces (this will include OpenShift namespace again, but we'll deduplicate)
465+
// Parse the results from all namespaces (this will include well-known namespaces again, but we'll deduplicate)
484466
seen := make(map[string]bool)
485-
// Mark OpenShift DataSources as already seen
467+
// Mark DataSources from well-known namespaces as already seen
486468
for _, ds := range results {
487469
key := ds.Namespace + "/" + ds.Name
488470
seen[key] = true
@@ -536,6 +518,42 @@ func searchDataSources(params api.ToolHandlerParams, query string) ([]DataSource
536518
return results, nil
537519
}
538520

521+
// listDataSourcesFromNamespace lists DataSources from a specific namespace
522+
func listDataSourcesFromNamespace(params api.ToolHandlerParams, dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, namespace string) ([]DataSourceInfo, error) {
523+
var results []DataSourceInfo
524+
list, err := dynamicClient.Resource(gvr).Namespace(namespace).List(params.Context, metav1.ListOptions{})
525+
if err != nil {
526+
return nil, err
527+
}
528+
529+
for _, item := range list.Items {
530+
name := item.GetName()
531+
ns := item.GetNamespace()
532+
labels := item.GetLabels()
533+
534+
// Extract source information from the DataSource spec
535+
source := extractDataSourceInfo(&item)
536+
537+
// Extract default instancetype and preference from labels
538+
defaultInstancetype := ""
539+
defaultPreference := ""
540+
if labels != nil {
541+
defaultInstancetype = labels["instancetype.kubevirt.io/default-instancetype"]
542+
defaultPreference = labels["instancetype.kubevirt.io/default-preference"]
543+
}
544+
545+
results = append(results, DataSourceInfo{
546+
Name: name,
547+
Namespace: ns,
548+
Source: source,
549+
DefaultInstancetype: defaultInstancetype,
550+
DefaultPreference: defaultPreference,
551+
})
552+
}
553+
554+
return results, nil
555+
}
556+
539557
// searchPreferences searches for VirtualMachineClusterPreference resources in the cluster
540558
func searchPreferences(params api.ToolHandlerParams) []PreferenceInfo {
541559
// Handle nil or invalid clients gracefully (e.g., in test environments)

0 commit comments

Comments
 (0)