Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # WARNING: USE AT YOUR OWN RISK.
  4.  
  5. set -eu
  6.  
  7. PRUNING_ATTRIBUTES="metadata.namespace metadata.selfLink metadata.generation metadata.annotations.[kubectl.kubernetes.io/last-applied-configuration]"
  8.  
  9. # Get the object
  10. get_object() {
  11. local dest="$1" ns="$2" kind="$3" name="$4"
  12. kubectl -n $ns get $kind $name -o yaml --export 2>/dev/null 1>$dest
  13. return $?
  14. }
  15.  
  16. # Prunes old attributes that might confuse things
  17. prune_attributes() {
  18. local src="$1"
  19. for a in $PRUNING_ATTRIBUTES
  20. do
  21. yq d --inplace "$src" "$a"
  22. done
  23. return 0
  24. }
  25.  
  26. apply_to_ns() {
  27. local dest_ns="$1" src="$2"
  28. kubectl -n $dest_ns apply -f $src 1>/dev/null
  29. return $?
  30. }
  31.  
  32. delete_from_ns() {
  33. local src_ns="$1" src_kind="$2" src_name="$3"
  34. kubectl -n $src_ns delete $src_kind $src_name 1>/dev/null
  35. return $?
  36. }
  37.  
  38. # Copy $src_ns/$src_kind/$src_name to $dest_ns
  39. move_to_namespace() {
  40. local src_ns="$1" src_kind="$2" src_name="$3" dest_ns="$4" temp_object=""
  41. temp_object=$(mktemp -t xxxxxx)
  42. touch $temp_object
  43. chmod 600 $temp_object
  44.  
  45. get_object $temp_object $src_ns $src_kind $src_name
  46. prune_attributes $temp_object
  47. delete_from_ns $src_ns $src_kind $src_name
  48. apply_to_ns $dest_ns $temp_object
  49. rm -f -- $temp_object
  50. }
  51. ###
  52.  
  53. src_ns=$1
  54. src_kind=$2
  55. src_name=$3
  56. dest_ns=$4
  57.  
  58. echo "Moving $src_ns/$src_kind/$src_name to $dest_ns/$src_kind/$src_name ..."
  59.  
  60. move_to_namespace $src_ns $src_kind $src_name $dest_ns
  61.  
  62. echo "Done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement