keep-added.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env bash
  2. set -eEuo pipefail
  3. DROP_FILE="$1"
  4. KEEP_FILE="$2"
  5. # WRITES TO STDOUT
  6. # DEBUGS TO STDERR
  7. : ${YQ:=yq}
  8. if [[ "$($YQ --version)" != "yq (https://github.com/mikefarah/yq/) version 4."* ]]; then
  9. echo "yq must be version 4.x"
  10. exit 1
  11. fi
  12. #
  13. # Create a file for each resource present in the drop set
  14. #
  15. drop_dir="$(mktemp -d)"
  16. pushd "${drop_dir}" &>/dev/stderr
  17. $YQ eval --split-exp '.kind + " " + .metadata.name + " "' "$DROP_FILE" # split into files by <kind> <name> .yaml
  18. # outputting the filenames with spaces after kind and name keeps the same sorting from before
  19. popd &>/dev/stderr
  20. #
  21. # Create a file for each resource present in the keep set
  22. #
  23. keep_dir="$(mktemp -d)"
  24. pushd "${keep_dir}" &>/dev/stderr
  25. $YQ eval --split-exp '.kind + " " + .metadata.name + " "' "$KEEP_FILE" # split into files by <kind> <name> .yaml
  26. # outputting the filenames with spaces after kind and name keeps the same sorting from before
  27. popd &>/dev/stderr
  28. #
  29. # In the keep set, remove every file that also exists in the drop set
  30. #
  31. pushd "${drop_dir}" &>/dev/stderr
  32. find . -type f -name '*.yml' -exec rm "${keep_dir}"/{} \;
  33. popd &>/dev/stderr
  34. #
  35. # Combine the kept files back into one yaml
  36. #
  37. RBAC_FILES=()
  38. while read -r line; do
  39. RBAC_FILES+=("$line")
  40. done < <(find "${keep_dir}"/. -type f -name '*.yml' | sort)
  41. # use keep-rbac-yaml.sh at the end to strip out only the RBAC, and sort and format it as we want
  42. $YQ eval-all '.' "${RBAC_FILES[@]}" | ./keep-rbac-yaml.sh
  43. rm -rf "${drop_dir}"
  44. rm -rf "${keep_dir}"