$ cd $(mktemp -d)

$ (
	kubectl get pods --field-selector=status.phase=Pending -A -ojson | jq -c '.items[]';
	kubectl get deployments -ojson -A | jq -c '.items[]';
	kubectl get replicasets -ojson -A | jq -c '.items[]';
 	kubectl get daemonsets -ojson -A | jq -c '.items[]';
) > /tmp/cluster.jsonl

$ cat /tmp/cluster.jsonl \
  | jq -r '
		def parse_into_parts:
			. as $i
			|capture(
				"^((?<host>[a-zA-Z0-9-]+\\.[a-zA-Z0-9.-]+)/)?"	
				+ "(:(?<port>[0-9]+))?"
				+ "((?<path>[a-zA-Z0-9-._/]+)/)?"
				+ "(?<image>[a-zA-Z0-9-._]+)"
				+ "((:(?<tag>[a-z0-9_.-]+))|(@(?<digest>sha256:[a-z0-9]+)))?$"
			) // error("couldnt parse \($i)");

		def qualify_oci_image:
			if (.host==null) then .host="docker.io" end
			|if (.path==null and .host=="docker.io") then .path="library" end
			# |if (.tag==null and .digest==null) then .tag="latest" end
			;

		def glue_parts:
			[
				if (.host) then .host else "" end,
				if (.port) then ":\(.port)" else "" end,
				if (.host) then "/" else "" end,
				if (.path) then "\(.path)/" else "" end,
				.image,
				if (.digest) then "@\(.digest)" elif (.tag) then ":\(.tag)" else "" end
			]|join("")
			;

		def fix_oci_image:
			. as $i
			|parse_into_parts
			|qualify_oci_image
			|if (.path=="bitnami") then .path="bitnamilegacy" else . end
			|if (.host=="docker.io") then (.host="123456780123.dkr.ecr.us-east-1.amazonaws.com"|.path="docker-io/\(.path)") else . end
			|glue_parts;
		
		[
			..|objects|(.initContainers[]?,.containers[]?)
			|(.image|fix_oci_image) as $newImage
			|select(.image!=$newImage)
			|"\(.name)=\($newImage)"
		] as $p
		|select($p|length > 0)
		|"kubectl set image \(.kind) -n \(.metadata.namespace) \(.metadata.name) \($p|join(" "))"