|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 13 | +) |
| 14 | + |
| 15 | +// SearchResources searches for a query string in all resources across all namespaces. |
| 16 | +func (k *Kubernetes) SearchResources(ctx context.Context, query string, asTable bool) (runtime.Unstructured, error) { |
| 17 | + // Discovery client is used to discover different supported API groups, versions and resources. |
| 18 | + serverResources, err := k.manager.discoveryClient.ServerPreferredResources() |
| 19 | + if err != nil { |
| 20 | + return nil, fmt.Errorf("failed to get server resources: %w", err) |
| 21 | + } |
| 22 | + |
| 23 | + var matchingResources []unstructured.Unstructured |
| 24 | + for _, apiResourceList := range serverResources { |
| 25 | + for _, apiResource := range apiResourceList.APIResources { |
| 26 | + // Skip resources that do not support the "list" verb |
| 27 | + if !contains(apiResource.Verbs, "list") { |
| 28 | + continue |
| 29 | + } |
| 30 | + |
| 31 | + gvk := schema.GroupVersionKind{ |
| 32 | + Group: apiResourceList.GroupVersion, |
| 33 | + Version: apiResource.Version, |
| 34 | + Kind: apiResource.Kind, |
| 35 | + } |
| 36 | + if gvk.Group == "" { |
| 37 | + gvk.Group = "core" |
| 38 | + } |
| 39 | + |
| 40 | + if _, err := k.resourceFor(&gvk); err != nil { |
| 41 | + // Ignore errors for resources that cannot be mapped |
| 42 | + continue |
| 43 | + } |
| 44 | + var namespaces []string |
| 45 | + if apiResource.Namespaced { |
| 46 | + // Get all namespaces |
| 47 | + nsListObj, err := k.NamespacesList(ctx, ResourceListOptions{}) |
| 48 | + if err != nil { |
| 49 | + return nil, fmt.Errorf("failed to list namespaces: %w", err) |
| 50 | + } |
| 51 | + if unstructuredList, ok := nsListObj.(*unstructured.UnstructuredList); ok { |
| 52 | + for _, ns := range unstructuredList.Items { |
| 53 | + namespaces = append(namespaces, ns.GetName()) |
| 54 | + } |
| 55 | + } |
| 56 | + } else { |
| 57 | + // For cluster-scoped resources, use an empty namespace |
| 58 | + namespaces = append(namespaces, "") |
| 59 | + } |
| 60 | + |
| 61 | + for _, ns := range namespaces { |
| 62 | + list, err := k.ResourcesList(ctx, &gvk, ns, ResourceListOptions{}) |
| 63 | + if err != nil { |
| 64 | + // Ignore errors for resources that cannot be listed |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + if unstructuredList, ok := list.(*unstructured.UnstructuredList); ok { |
| 69 | + for _, item := range unstructuredList.Items { |
| 70 | + match, err := matchResource(item, query) |
| 71 | + if err != nil { |
| 72 | + // Ignore errors during matching |
| 73 | + continue |
| 74 | + } |
| 75 | + if match { |
| 76 | + matchingResources = append(matchingResources, item) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if asTable { |
| 85 | + return k.createTable(matchingResources) |
| 86 | + } |
| 87 | + |
| 88 | + return &unstructured.UnstructuredList{ |
| 89 | + Object: map[string]interface{}{ |
| 90 | + "apiVersion": "v1", |
| 91 | + "kind": "List", |
| 92 | + }, |
| 93 | + Items: matchingResources, |
| 94 | + }, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (k *Kubernetes) createTable(resources []unstructured.Unstructured) (runtime.Unstructured, error) { |
| 98 | + table := &metav1.Table{ |
| 99 | + TypeMeta: metav1.TypeMeta{ |
| 100 | + APIVersion: "meta.k8s.io/v1", |
| 101 | + Kind: "Table", |
| 102 | + }, |
| 103 | + ColumnDefinitions: []metav1.TableColumnDefinition{ |
| 104 | + {Name: "Namespace", Type: "string"}, |
| 105 | + {Name: "Kind", Type: "string"}, |
| 106 | + {Name: "Name", Type: "string"}, |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + for _, res := range resources { |
| 111 | + row := metav1.TableRow{ |
| 112 | + Cells: []interface{}{ |
| 113 | + res.GetNamespace(), |
| 114 | + res.GetKind(), |
| 115 | + res.GetName(), |
| 116 | + }, |
| 117 | + Object: runtime.RawExtension{Object: &res}, |
| 118 | + } |
| 119 | + table.Rows = append(table.Rows, row) |
| 120 | + } |
| 121 | + |
| 122 | + unstructuredObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(table) |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("failed to convert table to unstructured: %w", err) |
| 125 | + } |
| 126 | + return &unstructured.Unstructured{Object: unstructuredObject}, nil |
| 127 | +} |
| 128 | + |
| 129 | +func matchResource(resource unstructured.Unstructured, query string) (bool, error) { |
| 130 | + data, err := json.Marshal(resource.Object) |
| 131 | + if err != nil { |
| 132 | + return false, fmt.Errorf("failed to marshal resource: %w", err) |
| 133 | + } |
| 134 | + return strings.Contains(strings.ToLower(string(data)), strings.ToLower(query)), nil |
| 135 | +} |
| 136 | + |
| 137 | +func contains(slice []string, s string) bool { |
| 138 | + for _, item := range slice { |
| 139 | + if item == s { |
| 140 | + return true |
| 141 | + } |
| 142 | + } |
| 143 | + return false |
| 144 | +} |
0 commit comments