{"record":{"id":"5f93dd2c8079fb4d","repo":"kubernetes/kops","slug":"error-getting-rest-mapping-for-v-w","errorCode":null,"errorMessage":"error getting rest mapping for %v: %w","messagePattern":"error getting rest mapping for (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/applylib/applyset/unstructuredclient.go","lineNumber":54,"sourceCode":"\t// restMapper is used to map object kind to resources, and to know if objects are cluster-scoped.\n\trestMapper meta.RESTMapper\n}\n\n// NewUnstructuredClient constructs an UnstructuredClient\nfunc NewUnstructuredClient(options Options) *UnstructuredClient {\n\treturn &UnstructuredClient{\n\t\tclient:     options.Client,\n\t\trestMapper: options.RESTMapper,\n\t}\n}\n\n// dynamicResource is a helper to get the resource for a gvk (with the namespace)\n// It returns an error if a namespace is provided for a cluster-scoped resource,\n// or no namespace is provided for a namespace-scoped resource.\nfunc (c *UnstructuredClient) dynamicResource(ctx context.Context, gvk schema.GroupVersionKind, ns string) (dynamic.ResourceInterface, error) {\n\trestMapping, err := c.restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting rest mapping for %v: %w\", gvk, err)\n\t}\n\tgvr := restMapping.Resource\n\n\tswitch restMapping.Scope.Name() {\n\tcase meta.RESTScopeNameNamespace:\n\t\tif ns == \"\" {\n\t\t\t// TODO: Differentiate between server-fixable vs client-fixable errors?\n\t\t\treturn nil, fmt.Errorf(\"namespace was not provided for namespace-scoped object %v\", gvk)\n\t\t}\n\t\treturn c.client.Resource(gvr).Namespace(ns), nil\n\n\tcase meta.RESTScopeNameRoot:\n\t\tif ns != \"\" {\n\t\t\t// TODO: Differentiate between server-fixable vs client-fixable errors?\n\t\t\treturn nil, fmt.Errorf(\"namespace %q was provided for cluster-scoped object %v\", ns, gvk)\n\t\t}\n\t\treturn c.client.Resource(gvr), nil\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/applylib/applyset/unstructuredclient.go#L36-L72","documentation":"UnstructuredClient.dynamicResource resolves a GroupVersionKind to a concrete resource via the RESTMapper before performing Patch/Update/Get. If the RESTMapper cannot find a mapping (resource not registered on the server, wrong GVK, or discovery failure), the error is wrapped as \"error getting rest mapping for %v\" including the GVK.","triggerScenarios":"Calling Patch/Update/Get with a GVK that has no registered REST mapping: the CRD doesn't exist yet, the apiVersion/kind in the manifest is misspelled, or the discovery cache is stale after a new CRD was installed.","commonSituations":"Applying a custom resource before its CRD in the same apply pass; typos like apps/v1 Deployment vs extensions/v1beta1; the API server's discovery endpoints unreachable so the mapper has no data; new CRD created moments ago and the cached mapper not invalidated.","solutions":["Confirm the resource exists: `kubectl api-resources | grep <kind>` and check the exact group/version.","Apply CRDs before the custom resources that use them (order the applyset, or run twice).","Fix the apiVersion/kind in the manifest to match a registered GVK.","Reset/refresh the discovery RESTMapper (recreate the client) after installing new CRDs to clear stale caches."],"exampleFix":"// before\napiVersion: apps/v1beta2\nkind: Deployment\n// after\napiVersion: apps/v1\nkind: Deployment","handlingStrategy":"validation","validationCode":"if _, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version); err != nil {\n    return fmt.Errorf(\"resource %s not present on cluster: %w\", gvk, err)\n}","typeGuard":null,"tryCatchPattern":"if err := client.Patch(ctx, gvk, nn, types.ApplyPatchType, data, opts); err != nil {\n    var discoveryErr *meta.NoKindMatchError\n    if errors.As(err, &discoveryErr) {\n        return applyCRDsFirst(ctx) // install CRDs, then retry\n    }\n    return err\n}","preventionTips":["Always apply CRDs before custom resources in the same pass.","Validate apiVersion/kind against `kubectl api-resources` output.","Refresh the discovery RESTMapper after installing new CRDs.","Check API server /healthz and discovery endpoints when mappings fail cluster-wide."],"tags":["kubernetes","restmapper","discovery","gvk"],"backgroundTag":"no-rest-mapping-for-gvk","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}