{"record":{"id":"2900dbe7081d2f3b","repo":"kubernetes/kops","slug":"error-listing-objects-in-namespace-s-w","errorCode":null,"errorMessage":"error listing objects in namespace %s: %w","messagePattern":"error listing objects in namespace (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"channels/pkg/channels/prune.go","lineNumber":106,"sourceCode":"\tvar listOptions v1.ListOptions\n\tlistOptions.LabelSelector = spec.LabelSelector\n\tlistOptions.FieldSelector = spec.FieldSelector\n\n\tbaseResource := p.Client.Resource(gvr)\n\tif len(spec.Namespaces) == 0 {\n\t\tobjects, err := baseResource.List(ctx, listOptions)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error listing objects: %w\", err)\n\t\t}\n\t\tif err := p.pruneObjects(ctx, gvr, objects, keepObjects); err != nil {\n\t\t\treturn err\n\t\t}\n\t} else {\n\t\tfor _, namespace := range spec.Namespaces {\n\t\t\tresource := baseResource.Namespace(namespace)\n\t\t\tactualObjects, err := resource.List(ctx, listOptions)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"error listing objects in namespace %s: %w\", namespace, err)\n\t\t\t}\n\t\t\tif err := p.pruneObjects(ctx, gvr, actualObjects, keepObjects); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (p *Pruner) pruneObjects(ctx context.Context, gvr schema.GroupVersionResource, actualObjects *unstructured.UnstructuredList, keepObjects []*kubemanifest.Object) error {\n\tkeepMap := make(map[string]*kubemanifest.Object)\n\tfor _, keepObject := range keepObjects {\n\t\tkey := keepObject.GetNamespace() + \"/\" + keepObject.GetName()\n\t\tkeepMap[key] = keepObject\n\t}\n\n\tfor _, actualObject := range actualObjects.Items {","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/channels/pkg/channels/prune.go#L88-L124","documentation":"When the PruneKindSpec specifies Namespaces, pruning lists per-namespace via the namespaced dynamic client. A per-namespace List failure is wrapped as `error listing objects in namespace <ns>`, and prune aborts at the first failing namespace.","triggerScenarios":"A named namespace does not exist (404), RBAC denies list in that namespace, the namespace is terminating, or selectors are invalid — during the per-namespace List loop.","commonSituations":"PruneSpec Namespaces list contains a namespace removed/renamed in the cluster; service account role only covers some namespaces; namespace stuck in Terminating so list returns errors.","solutions":["Verify each namespace exists: `kubectl get ns <name>`; correct or remove stale entries in the PruneSpec","Grant list permission on the resource in every listed namespace","Retry after a stuck Terminating namespace is fully deleted","Check the wrapped inner error to distinguish 404/403/400"],"exampleFix":"// before\nnamespaces:\n- kube-system\n- old-namespace   # deleted from cluster\n// after\nnamespaces:\n- kube-system","handlingStrategy":"validation","validationCode":"for _, ns := range spec.Namespaces {\n    if _, err := clientset.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{}); err != nil {\n        return fmt.Errorf(\"prune namespace %s unavailable: %w\", ns, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := pruner.Prune(ctx, manifest, spec); err != nil {\n    if strings.Contains(err.Error(), \"error listing objects in namespace\") {\n        // extract ns from the message and decide: skip missing ns or fix RBAC\n        return fmt.Errorf(\"prune aborted in namespace; reconcile namespaces: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep the PruneSpec Namespaces list in sync with actual cluster namespaces","Grant list permission per namespace for the pruned resource","Handle namespace lifecycle (creating/terminating) before pruning"],"tags":["list","namespace","dynamic-client","kubernetes"],"backgroundTag":"list-objects-namespace-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}