I have a yaml file with an arbitrary amount of documents, and I'm trying to replace all missing namespaces for namespaceable resources with an arbitrary input one.
Getting the non-namespaceable resources is easy:
kubectl api-resources --namespaced=false --no-headers | awk '{print $NF}' > /tmp/bad_resources.yaml The problem is using this list in YQ (mike farah's).
This code works for hardcoded resources:
NAMESPACE="$NAMESPACE" yq ' select(.kind != "Namespace" and .kind != "CustomResourceDefinition") | .metadata.namespace = (.metadata.namespace // strenv(NAMESPACE)) ' "$INPUT" > "$OUTPUT" How can I replace this hardcoded list with the list I generated via kubectl?
I'm kind of going crazy with this, even LLMs utterly fail at this and keep mistaking yq versions and suggesting input arguments that don't exist.
Sample yaml:
--- kind: Namespace metadata: name: test --- kind: ConfigMap metadata: name: test1 namespace: asd --- kind: ConfigMap metadata: name: test2 In this example, it should be able to add the namespace to the Configmap test2, but not change test1, nor add it to Namespace, because Namespace is not a namespaceable resource. The output should be the same, except for the added namespace, so the last resource should have a new metadata.namespace field with the input namespace.
The kubectl list of resources looks like this:
Namespace Node PersistentVolume Given that I'm generating it with the command I posted above, I can manipulate this, so it could also be a yaml array.