|
| 1 | +package manifestclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 10 | + apirequest "k8s.io/apiserver/pkg/endpoints/request" |
| 11 | +) |
| 12 | + |
| 13 | +// must-gather has a few different ways to store resources |
| 14 | +// 1. cluster-scoped-resource/group/resource/<name>.yaml |
| 15 | +// 2. cluster-scoped-resource/group/resource.yaml |
| 16 | +// 3. namespaces/<namespace>/group/resource/<name>.yaml |
| 17 | +// 4. namespaces/<namespace>/group/resource.yaml |
| 18 | +// we have to choose which to prefer and we should always prefer the #2 if it's available. |
| 19 | +// Keep in mind that to produce a cluster-scoped list of namespaced resources, you can need to navigate many namespaces. |
| 20 | +func (mrt *manifestRoundTripper) list(requestInfo *apirequest.RequestInfo) ([]byte, error) { |
| 21 | + var retList *unstructured.UnstructuredList |
| 22 | + possibleListFiles, err := allPossibleListFileLocations(mrt.contentReader, requestInfo) |
| 23 | + if err != nil { |
| 24 | + return nil, fmt.Errorf("unable to determine list file locations: %w", err) |
| 25 | + } |
| 26 | + for _, listFile := range possibleListFiles { |
| 27 | + currList, err := readListFile(mrt.contentReader, listFile) |
| 28 | + switch { |
| 29 | + case errors.Is(err, fs.ErrNotExist): |
| 30 | + // do nothing, it's possible, not guaranteed |
| 31 | + continue |
| 32 | + case err != nil: |
| 33 | + return nil, fmt.Errorf("unable to determine read list file %v: %w", listFile, err) |
| 34 | + } |
| 35 | + |
| 36 | + if retList == nil { |
| 37 | + retList = currList |
| 38 | + continue |
| 39 | + } |
| 40 | + for i := range currList.Items { |
| 41 | + retList.Items = append(retList.Items, currList.Items[i]) |
| 42 | + } |
| 43 | + } |
| 44 | + if retList != nil { |
| 45 | + ret, err := serializeListObjToJSON(retList) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("failed to serialize: %v", err) |
| 48 | + } |
| 49 | + return []byte(ret), nil |
| 50 | + } |
| 51 | + |
| 52 | + retList = &unstructured.UnstructuredList{ |
| 53 | + Object: map[string]interface{}{}, |
| 54 | + Items: nil, |
| 55 | + } |
| 56 | + individualFiles, err := allIndividualFileLocations(mrt.contentReader, requestInfo) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("unable to determine individual file locations: %w", err) |
| 59 | + } |
| 60 | + for _, individualFile := range individualFiles { |
| 61 | + currInstance, err := readIndividualFile(mrt.contentReader, individualFile) |
| 62 | + switch { |
| 63 | + case errors.Is(err, fs.ErrNotExist): |
| 64 | + // do nothing, it's possible, not guaranteed |
| 65 | + continue |
| 66 | + case err != nil: |
| 67 | + return nil, fmt.Errorf("unable to determine read list file %v: %w", individualFile, err) |
| 68 | + } |
| 69 | + |
| 70 | + retList.Items = append(retList.Items, *currInstance) |
| 71 | + } |
| 72 | + if len(retList.Items) > 0 { |
| 73 | + retList.SetKind(retList.Items[0].GetKind() + "List") |
| 74 | + retList.SetAPIVersion(retList.Items[0].GetAPIVersion()) |
| 75 | + |
| 76 | + ret, err := serializeListObjToJSON(retList) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to serialize: %v", err) |
| 79 | + } |
| 80 | + return []byte(ret), nil |
| 81 | + } |
| 82 | + |
| 83 | + return nil, fmt.Errorf("unable to read any file so we have no Kind") |
| 84 | +} |
| 85 | + |
| 86 | +func allIndividualFileLocations(contentReader RawReader, requestInfo *apirequest.RequestInfo) ([]string, error) { |
| 87 | + resourceDirectoryParts := []string{} |
| 88 | + if len(requestInfo.APIGroup) > 0 { |
| 89 | + resourceDirectoryParts = append(resourceDirectoryParts, requestInfo.APIGroup) |
| 90 | + } else { |
| 91 | + resourceDirectoryParts = append(resourceDirectoryParts, "core") |
| 92 | + } |
| 93 | + resourceDirectoryParts = append(resourceDirectoryParts, requestInfo.Resource) |
| 94 | + |
| 95 | + resourceDirectoriesToCheckForIndividualFiles := []string{} |
| 96 | + if len(requestInfo.Namespace) > 0 { |
| 97 | + parts := append([]string{"namespaces", requestInfo.Namespace}, resourceDirectoryParts...) |
| 98 | + resourceDirectoriesToCheckForIndividualFiles = append(resourceDirectoriesToCheckForIndividualFiles, filepath.Join(parts...)) |
| 99 | + |
| 100 | + } else { |
| 101 | + clusterParts := append([]string{"cluster-scoped-resources"}, resourceDirectoryParts...) |
| 102 | + resourceDirectoriesToCheckForIndividualFiles = append(resourceDirectoriesToCheckForIndividualFiles, filepath.Join(clusterParts...)) |
| 103 | + |
| 104 | + namespaces, err := allNamespacesWithData(contentReader) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("unable to read namespaces") |
| 107 | + } |
| 108 | + for _, ns := range namespaces { |
| 109 | + nsParts := append([]string{"namespaces", ns}, resourceDirectoryParts...) |
| 110 | + resourceDirectoriesToCheckForIndividualFiles = append(resourceDirectoriesToCheckForIndividualFiles, filepath.Join(nsParts...)) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + allIndividualFilePaths := []string{} |
| 115 | + for _, resourceDirectory := range resourceDirectoriesToCheckForIndividualFiles { |
| 116 | + individualFiles, err := contentReader.ReadDir(resourceDirectory) |
| 117 | + switch { |
| 118 | + case errors.Is(err, fs.ErrNotExist): |
| 119 | + continue |
| 120 | + case err != nil: |
| 121 | + return nil, fmt.Errorf("unable to read resourceDir") |
| 122 | + } |
| 123 | + |
| 124 | + for _, curr := range individualFiles { |
| 125 | + allIndividualFilePaths = append(allIndividualFilePaths, filepath.Join(resourceDirectory, curr.Name())) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return allIndividualFilePaths, nil |
| 130 | +} |
| 131 | + |
| 132 | +func allPossibleListFileLocations(contentReader RawReader, requestInfo *apirequest.RequestInfo) ([]string, error) { |
| 133 | + resourceListFileParts := []string{} |
| 134 | + if len(requestInfo.APIGroup) > 0 { |
| 135 | + resourceListFileParts = append(resourceListFileParts, requestInfo.APIGroup) |
| 136 | + } else { |
| 137 | + resourceListFileParts = append(resourceListFileParts, "core") |
| 138 | + } |
| 139 | + resourceListFileParts = append(resourceListFileParts, fmt.Sprintf("%s.yaml", requestInfo.Resource)) |
| 140 | + |
| 141 | + allPossibleListFileLocations := []string{} |
| 142 | + if len(requestInfo.Namespace) > 0 { |
| 143 | + parts := append([]string{"namespaces", requestInfo.Namespace}, resourceListFileParts...) |
| 144 | + allPossibleListFileLocations = append(allPossibleListFileLocations, filepath.Join(parts...)) |
| 145 | + |
| 146 | + } else { |
| 147 | + clusterParts := append([]string{"cluster-scoped-resources"}, resourceListFileParts...) |
| 148 | + allPossibleListFileLocations = append(allPossibleListFileLocations, filepath.Join(clusterParts...)) |
| 149 | + |
| 150 | + namespaces, err := allNamespacesWithData(contentReader) |
| 151 | + if err != nil { |
| 152 | + return nil, fmt.Errorf("unable to read namespaces") |
| 153 | + } |
| 154 | + for _, ns := range namespaces { |
| 155 | + nsParts := append([]string{"namespaces", ns}, resourceListFileParts...) |
| 156 | + allPossibleListFileLocations = append(allPossibleListFileLocations, filepath.Join(nsParts...)) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return allPossibleListFileLocations, nil |
| 161 | +} |
| 162 | + |
| 163 | +func allNamespacesWithData(contentReader RawReader) ([]string, error) { |
| 164 | + nsDirs, err := contentReader.ReadDir("namespaces") |
| 165 | + if err != nil { |
| 166 | + return nil, fmt.Errorf("failed to read allNamespacesWithData: %w", err) |
| 167 | + } |
| 168 | + |
| 169 | + ret := []string{} |
| 170 | + for _, curr := range nsDirs { |
| 171 | + ret = append(ret, curr.Name()) |
| 172 | + } |
| 173 | + |
| 174 | + return ret, nil |
| 175 | +} |
0 commit comments