kubernetes之namespace資源導(dǎo)出02

從k8s導(dǎo)出資源后,需要刪除yaml里面的垃圾數(shù)據(jù),需要批量改namespace名稱,deploy中的鏡像地址,改ingress的域名等等信息
通過如下腳本實現(xiàn):

#!/bin/bash
# set -x

# 鏡像環(huán)境變量之項目環(huán)境變量標(biāo)簽
itemEnv=dev
# 項目域名
## 項目主域名
mainDoname="xiaojinguang.com"
## 項目前端域名二級域名頭
webDoname="side"
## 項目后端域名二級域名頭
apiDoname="api"
# 項目二級域名中的環(huán)境變量標(biāo)簽(導(dǎo)出時):比如:dev,test,正式為空,同上面變量組合為:apidev.xiaojinguang.com,apitest.xiaojinguang.com
oldName=""
# 項目二級域名中的環(huán)境變量標(biāo)簽(需變更后的)
repName="dev"
# namespace
## 變更后的命名空間名稱
NSnew=jintest
## change image
### 從k8s集群獲取deploy中容器image數(shù)據(jù)的命名空間名稱
NS=jindev
# docker 鏡像倉庫的項目名稱
PROJECK=xiao
# 指定獲取deploy中容器image數(shù)據(jù)時,連接k8s集群用的kubectl認(rèn)證文件
configFile=~/.kube/config

shareDel=(
  "creationTimestamp:"
  "generation:"
  "resourceVersion:"
  "selfLink:"
  "uid:"
)
deployDel=(
  "annotations:"
  "field.cattle.io\/publicEndpoints:"
  "deployment.kubernetes.io\/revision:"
  "cattle.io\/timestamp:"
  "field.cattle.io\/ports:"
  "creationTimestamp:"
  "environment:"
)
ingressDel=(
  "field.cattle.io\/publicEndpoints:"
)
servicesDel=(
  "clusterIP:"
)
pvcDel=(
  "pv.kubernetes.io\/bind-completed:"
  "pv.kubernetes.io\/bound-by-controller:"
)
secretsDel=(
  "annotations:"
  "field.cattle.io\/projectId:"
  "kubernetes.io\/service-account.name:"
  "istio.io\/service-account.name:"
)
namespacesDel=(
  "annotations:"
  "field.cattle.io\/projectId"
  "lifecycle.cattle.io\/create.namespace-auth"
  "cattle.io\/status"
)

function getDirList(){
  #script dir list
  dirList=$(/bin/ls -lF $scriptDir|grep "/$" |awk '{print $NF}' |cut -d/ -f1)
  # dirList=$(echo ingress)
  for i in $dirList; do
    # echo $(ls $i)
    if [ "$1" == "deleteStatus" ] || [ "$1" == "deleteYamlAges" ];then
      for ii in $(ls $i); do
        deleteStatus(){
          # delete item yaml status ages
          echo "deleteStatus sed $i/$ii start"
          sed -i "" -e '/^status:$/,$d' $i/$ii
        }
        deleteYamlAges(){
          tmpname="${i}Del"
          eval tmpList=\${$tmpname[@]}
          workList="${shareDel[@]} $tmpList"
          echo "workList=$workList"
          # delete item yaml tmp ages
          for iii in ${workList[@]}; do
            # if [ ${iii%#*}x == "x"  ];then
            #   continue
            # fi
            # if [[ $iii =~ "annotations" ]] && [ $i == "ingress" ];then
            #   continue
            # fi
            echo "deleteYamlAges $iii from $i/$ii sed"
            sed -i "" "/$iii/d" $i/$ii
          done
        }
        $1
      done
    elif [ $1 == "changeNamespace" ];then
      changeNamespace(){
        # delete namespce age
        echo "changeNamespace start $i/*.yaml sed"
        sed -i "" "s/namespace:.*/namespace: $NSnew/" $i/*.yaml
      }
      $1
    fi
  done
  if [ $1 == "changeNamespace" ];then
    sed -i "" "s/name:.*/name: $NSnew/" namespaces.yaml
  fi
}

changeItemEnv(){
  # replace item env value
  echo "changeItemEnv start sed"
  sed -i "" "/name: ITEMENV/{n;s/value:.*/value: $itemEnv/;}" deploy/*.yaml
}
changeDoname(){
  echo "changeDoname start sed"
  # replace item web doname
  sed -i "" "s#${webDoname}${oldName}.${mainDoname}#$webDoname$repName.$mainDoname#"  ingress/*.yaml
  # replace item api doname
  sed -i "" "s#${apiDoname}${oldName}.${mainDoname}#$apiDoname$repName.$mainDoname#"  ingress/*.yaml
}

resetimage(){
  resourceList=(
  deploy
  )
  for aa in ${resourceList[@]}; do
    aList=$(kubectl --kubeconfig $configFile -n $NS get $aa |grep -v NAME  |awk '{print $1}')
    if [ ! "${aList[*]}"x == "x" ];then
      #[ -d ./$aa ] || mkdir ./$aa
      for i in $aList;
      do
        itemImage=$(kubectl --kubeconfig $configFile -n $NS get deployments.apps $i -o wide |grep -v IMAGE |awk '{print $7}')
        echo "$aa $i image:$itemImage"
        # replace item image
        sed -i "" "s#image:.*$PROJECK.*#image: $itemImage#" $aa/$i.yaml
      done
    fi
  done
}

deleteOther(){
  oList=$(ls *.yaml)
  for bb in $oList; do
    for cc in ${shareDel[@]}; do
      echo "deleteOther $cc from $bb"
      sed -i "" "/$cc/d" $bb
    done
    if [ "$bb" == "namespaces.yaml" ];then
      for cc in ${namespacesDel[@]}; do
        echo "deleteOther $cc from $bb"
        sed -i "" "/$cc/d" $bb
      done
    fi
    # delete item yaml status ages
    echo "deleteStatus sed $bb start"
    sed -i "" -e '/^status:$/,$d' $bb
  done
}

# 替換deploy文件夾中yaml文件容器 image的值,通過參考一個k8s集群的namespace
resetimage
# 替換deploy 容器中環(huán)境變量名稱ITEMENV的值
changeItemEnv
# 替換ingress中域名
changeDoname
# 替換yaml文件中命名空間值
getDirList changeNamespace
# 刪除yaml資源中status資源數(shù)據(jù)
getDirList deleteStatus
# 刪除yaml垃圾數(shù)據(jù),可按不同的資源類型
getDirList deleteYamlAges
deleteOther
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容