Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # WARNING: USE AT YOUR OWN RISK.
- set -eu
- PRUNING_ATTRIBUTES="metadata.namespace metadata.selfLink metadata.generation metadata.annotations.[kubectl.kubernetes.io/last-applied-configuration]"
- # Get the object
- get_object() {
- local dest="$1" ns="$2" kind="$3" name="$4"
- kubectl -n $ns get $kind $name -o yaml --export 2>/dev/null 1>$dest
- return $?
- }
- # Prunes old attributes that might confuse things
- prune_attributes() {
- local src="$1"
- for a in $PRUNING_ATTRIBUTES
- do
- yq d --inplace "$src" "$a"
- done
- return 0
- }
- apply_to_ns() {
- local dest_ns="$1" src="$2"
- kubectl -n $dest_ns apply -f $src 1>/dev/null
- return $?
- }
- delete_from_ns() {
- local src_ns="$1" src_kind="$2" src_name="$3"
- kubectl -n $src_ns delete $src_kind $src_name 1>/dev/null
- return $?
- }
- # Copy $src_ns/$src_kind/$src_name to $dest_ns
- move_to_namespace() {
- local src_ns="$1" src_kind="$2" src_name="$3" dest_ns="$4" temp_object=""
- temp_object=$(mktemp -t xxxxxx)
- touch $temp_object
- chmod 600 $temp_object
- get_object $temp_object $src_ns $src_kind $src_name
- prune_attributes $temp_object
- delete_from_ns $src_ns $src_kind $src_name
- apply_to_ns $dest_ns $temp_object
- rm -f -- $temp_object
- }
- ###
- src_ns=$1
- src_kind=$2
- src_name=$3
- dest_ns=$4
- echo "Moving $src_ns/$src_kind/$src_name to $dest_ns/$src_kind/$src_name ..."
- move_to_namespace $src_ns $src_kind $src_name $dest_ns
- echo "Done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement