|
| 1 | +package manifestclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + apirequest "k8s.io/apiserver/pkg/endpoints/request" |
| 12 | +) |
| 13 | + |
| 14 | +func (mrt *manifestRoundTripper) getGroupResourceDiscovery(requestInfo *apirequest.RequestInfo) ([]byte, error) { |
| 15 | + if len(requestInfo.Path) == 0 { |
| 16 | + return nil, fmt.Errorf("path required for group resource discovery") |
| 17 | + } |
| 18 | + |
| 19 | + apiResourceList := &metav1.APIResourceList{} |
| 20 | + |
| 21 | + group, version, err := splitGroupVersionFromRequestPath(requestInfo.Path) |
| 22 | + if err != nil { |
| 23 | + return nil, fmt.Errorf("unable to split group/version from path: %w", err) |
| 24 | + } |
| 25 | + |
| 26 | + apiResourceList.GroupVersion = fmt.Sprintf("%s/%s", group, version) |
| 27 | + if group == "core" { |
| 28 | + apiResourceList.GroupVersion = version |
| 29 | + } |
| 30 | + |
| 31 | + // Map of resource name to APIResource. |
| 32 | + apiResources := map[string]metav1.APIResource{} |
| 33 | + |
| 34 | + clusterGroupPath := filepath.Join("cluster-scoped-resources", group) |
| 35 | + clusterGroupDirEntries, err := mrt.contentReader.ReadDir(clusterGroupPath) |
| 36 | + if err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 37 | + return nil, fmt.Errorf("unable to read directory: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + apiResourcesForClusterScope, err := getAPIResourcesFromNamespaceDirEntries(clusterGroupDirEntries, mrt.contentReader, group, version, clusterGroupPath, false /* cluster-scoped */) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("unable to get resources from cluster-scoped directory: %w", err) |
| 43 | + } |
| 44 | + for resourceName, apiResource := range apiResourcesForClusterScope { |
| 45 | + apiResources[resourceName] = apiResource |
| 46 | + } |
| 47 | + |
| 48 | + namespaceDirEntries, err := mrt.contentReader.ReadDir("namespaces") |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("unable to read directory: %w", err) |
| 51 | + } |
| 52 | + for _, namespaceDirEntry := range namespaceDirEntries { |
| 53 | + if !namespaceDirEntry.IsDir() { |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + namespaceGroupPath := filepath.Join("namespaces", namespaceDirEntry.Name(), group) |
| 58 | + namespaceGroupDirEntries, err := mrt.contentReader.ReadDir(namespaceGroupPath) |
| 59 | + if err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 60 | + return nil, fmt.Errorf("unable to read directory: %w", err) |
| 61 | + } else if errors.Is(err, fs.ErrNotExist) { |
| 62 | + // No resources for this namespace. |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + apiResourcesForNamespace, err := getAPIResourcesFromNamespaceDirEntries(namespaceGroupDirEntries, mrt.contentReader, group, version, namespaceGroupPath, true /* namespaced */) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("unable to get resources from namespace directory: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + for resourceName, apiResource := range apiResourcesForNamespace { |
| 72 | + apiResources[resourceName] = apiResource |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for _, apiResource := range apiResources { |
| 77 | + apiResourceList.APIResources = append(apiResourceList.APIResources, apiResource) |
| 78 | + } |
| 79 | + |
| 80 | + ret, err := serializeAPIResourceListToJSON(apiResourceList) |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("failed to serialize group resource discovery: %v", err) |
| 83 | + } |
| 84 | + return []byte(ret), nil |
| 85 | +} |
| 86 | + |
| 87 | +func splitGroupVersionFromRequestPath(path string) (string, string, error) { |
| 88 | + if path == "/api/v1" { |
| 89 | + return "core", "v1", nil |
| 90 | + } |
| 91 | + |
| 92 | + parts := strings.Split(path, "/") |
| 93 | + if len(parts) != 4 { |
| 94 | + return "", "", fmt.Errorf("invalid path: %s", path) |
| 95 | + } |
| 96 | + |
| 97 | + return parts[2], parts[3], nil |
| 98 | +} |
| 99 | + |
| 100 | +func getResourceDirAPIServerListEntry(contentReader RawReader, groupPath, resourceName, group, version string, namespaced bool) (*metav1.APIResource, error) { |
| 101 | + resourceDirEntries, err := contentReader.ReadDir(filepath.Join(groupPath, resourceName)) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("unable to read directory: %w", err) |
| 104 | + } |
| 105 | + for _, fileEntry := range resourceDirEntries { |
| 106 | + if !strings.HasSuffix(fileEntry.Name(), ".yaml") { |
| 107 | + // There shouldn't be anything that hits this, but ignore it if there is. |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + individualObj, individualErr := readIndividualFile(contentReader, filepath.Join(groupPath, resourceName, fileEntry.Name())) |
| 112 | + if individualErr != nil { |
| 113 | + return nil, fmt.Errorf("unable to read file: %w", individualErr) |
| 114 | + } |
| 115 | + |
| 116 | + groupVersion := fmt.Sprintf("%s/%s", group, version) |
| 117 | + if group == "core" { |
| 118 | + group = "" |
| 119 | + groupVersion = version |
| 120 | + } |
| 121 | + |
| 122 | + if individualObj.GetAPIVersion() != groupVersion { |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + // No point checking further, all files should produce the same APIResource. |
| 127 | + return &metav1.APIResource{ |
| 128 | + Name: resourceName, |
| 129 | + Kind: individualObj.GetKind(), |
| 130 | + Group: group, |
| 131 | + Version: version, |
| 132 | + Namespaced: namespaced, |
| 133 | + Verbs: []string{"get", "list", "watch"}, |
| 134 | + }, nil |
| 135 | + } |
| 136 | + |
| 137 | + return nil, nil |
| 138 | +} |
| 139 | + |
| 140 | +func getAPIResourcesFromNamespaceDirEntries(dirEntries []fs.DirEntry, contentReader RawReader, group, version string, basePath string, namespaced bool) (map[string]metav1.APIResource, error) { |
| 141 | + apiResources := map[string]metav1.APIResource{} |
| 142 | + for _, dirEntry := range dirEntries { |
| 143 | + // Directories are named after the resource and contain individual resources. |
| 144 | + if dirEntry.IsDir() { |
| 145 | + apiResource, err := getResourceDirAPIServerListEntry(contentReader, basePath, dirEntry.Name(), group, version, namespaced) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("unable to get resource from directory: %w", err) |
| 148 | + } |
| 149 | + if apiResource != nil { |
| 150 | + apiResources[dirEntry.Name()] = *apiResource |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if !strings.HasSuffix(dirEntry.Name(), ".yaml") { |
| 155 | + // There shouldn't be anything that hits this, but ignore it if there is. |
| 156 | + continue |
| 157 | + } |
| 158 | + |
| 159 | + resourceName := strings.TrimSuffix(dirEntry.Name(), ".yaml") |
| 160 | + if _, ok := apiResources[resourceName]; ok { |
| 161 | + // We already have this resource. |
| 162 | + continue |
| 163 | + } |
| 164 | + |
| 165 | + // Files are named after the resource and contain a list of resources. |
| 166 | + listObj, err := readListFile(contentReader, filepath.Join(basePath, dirEntry.Name())) |
| 167 | + if err != nil { |
| 168 | + return nil, fmt.Errorf("unable to read list file: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + for _, obj := range listObj.Items { |
| 172 | + if obj.GetAPIVersion() != fmt.Sprintf("%s/%s", group, version) { |
| 173 | + continue |
| 174 | + } |
| 175 | + |
| 176 | + apiResources[resourceName] = metav1.APIResource{ |
| 177 | + Name: resourceName, |
| 178 | + Kind: obj.GetKind(), |
| 179 | + Group: group, |
| 180 | + Version: version, |
| 181 | + Namespaced: namespaced, |
| 182 | + Verbs: []string{"get", "list", "watch"}, |
| 183 | + } |
| 184 | + |
| 185 | + // Once we find a resource in the expected group/version, we can break. |
| 186 | + // Anything else would produce the same APIResource. |
| 187 | + break |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return apiResources, nil |
| 192 | +} |
0 commit comments