Linux Shell 学习笔记二

#!/bin/bash

# 当你遇到shell脚本异常时,加上此行set -euxo pipefail,会打印出执行过程并阻止脚本继续执行。
set -euo pipefail

# 使用说明:
# BACKUP= NS= ./migrate-ack.sh
# 其中BACKUP为velero backup的名称,可以通过 velero --kubecontext=jiankunking-ing-test backup get | grep old | grep Completed | head -n1 获取
# NS为要迁移的namespace列表,逗号隔开
# 需要安装velero/jq/kubectl等命令

ctx=jiankunking-test

echo "Usage: BACKUP= NS= $0"

# command -v 可以判断一个命令是否支持
if ! command -v velero >/dev/null; then
	echo "velero not installed" >&2
	exit 1
fi
if ! command -v jq >/dev/null; then
	echo "jq not installed" >&2
	exit 1
fi
if ! command -v kubectl >/dev/null; then
	echo "kubectl not installed" >&2
	exit 1
fi

echo "+ using backup $BACKUP"

# https://velero.io/
velero --kubecontext $ctx restore create --from-backup $BACKUP --include-namespaces $NS -w

# a,b,c=>a b c
for ns in `echo $NS | tr ',' ' '`; do
	echo "+ NS: $ns"
	k="kubectl --context $ctx -n $ns"

	# set svc ownerref
	svcs=`$k get svc --no-headers -owide 2>/dev/null | awk '{print $1}'`
	for svc in $svcs; do
		app=`$k get svc $svc -ojson | jq '.metadata.labels.app' -r`
		uid=`$k get deploy $app -ojson | jq -r .metadata.uid`

		$k patch svc $svc --patch '{
            "metadata": {
                "ownerReferences": [
                    {
                        "apiVersion": "apps/v1",
                        "blockOwnerDeletion": true,
                        "controller": true,
                        "kind": "Deployment",
                        "name": "'$app'",
                        "uid": "'$uid'"
                    }
                ]
			}
		}'
	done

	# replace ingress host: jiankunking-ing-internal -> jiankunking-ing-test-internal
	#	-gt:大于,greater than
	if [ `$k get ing -lingress-class!=internal-gw 2>/dev/null | wc -l` -gt 0 ]; then
		$k get ing -lingress-class!=internal-gw -ojson \
			| jq '.items[].spec.rules[].host |= sub("jiankunking-ing-internal"; "jiankunking-ing-test-internal")' \
			| $k apply -f -
	fi
done

svc jq

[jiankunking@hddxps8156 ~]# kubectl get svc gateway -n console-dev -ojson 
{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "annotations": {
            "k8s.haier.net/creator": "jiankunking"
        },
        "creationTimestamp": "2023-07-04T03:21:53Z",
        "labels": {
            "app": "gateway",
            "app.kubernetes.io/name": "gateway",
            "app.kubernetes.io/version": "v0"
        }
    },
    "spec": {},
    "status": {
        "loadBalancer": {}
    }
}
[jiankunking@hddxps8156 ~]# kubectl get svc gateway -n console-dev -ojson | jq '.metadata.labels.app' -r
gateway
[jiankunking@hddxps8156 ~]# kubectl get svc gateway -n console-dev -ojson | jq .metadata.labels.app -r
gateway
[jiankunking@hddxps8156 ~]# 

deploy jq

[jiankunking@hddxps8156 ~]# kubectl get deploy gateway -n console-dev -ojson
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "3",
            "k8s.haier.net/description": "",
            "k8s.haier.net/display-name": "gateway",
            "k8s.haier.net/updated-timestamp": "2023-07-04T02:58:06Z"
        },
        "creationTimestamp": "2023-07-04T02:51:18Z",
        "generation": 3,
        "labels": {
            "app": "gateway",
            "app.kubernetes.io/name": "gateway",
            "app.kubernetes.io/version": "v0",
            "k8s.haier.net/app-type": ""
        },
        "name": "gateway",
        "namespace": "console-dev",
        "resourceVersion": "689881460",
        "uid": "9fe8462b-d65a-4e9f-942c-4cffb912b0e5"
    },
    "spec": {},
    "status": {
       
    }
}
[jiankunking@hddxps8156 ~]# kubectl get deploy gateway -n console-dev -ojson | jq -r .metadata.uid
9fe8462b-d65a-4e9f-942c-4cffb912b0e5
[jiankunking@hddxps8156 ~]# 

作者:jiankunking 出处:http://blog.csdn.net/jiankunking

你可能感兴趣的:(Linux,Shell,linux,学习,笔记,shell)